Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 5 additions & 78 deletions server/src/main/java/org/elasticsearch/index/get/GetResult.java
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@
import org.elasticsearch.search.lookup.Source;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
import java.util.Collections;
Expand All @@ -36,19 +35,18 @@
import java.util.Objects;

import static java.util.Collections.emptyMap;
import static org.elasticsearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_PRIMARY_TERM;
import static org.elasticsearch.index.seqno.SequenceNumbers.UNASSIGNED_SEQ_NO;

public class GetResult implements Writeable, Iterable<DocumentField>, ToXContentObject {

public static final String _INDEX = "_index";
public static final String _ID = "_id";
private static final String _VERSION = "_version";
private static final String _SEQ_NO = "_seq_no";
private static final String _PRIMARY_TERM = "_primary_term";
private static final String FOUND = "found";
private static final String FIELDS = "fields";
static final String _VERSION = "_version";
static final String _SEQ_NO = "_seq_no";
static final String _PRIMARY_TERM = "_primary_term";
static final String FOUND = "found";
static final String FIELDS = "fields";

private final String index;
private final String id;
Expand Down Expand Up @@ -286,77 +284,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
return builder;
}

public static GetResult fromXContentEmbedded(XContentParser parser) throws IOException {
XContentParser.Token token = parser.nextToken();
ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
return fromXContentEmbedded(parser, null, null);
}

public static GetResult fromXContentEmbedded(XContentParser parser, String index, String id) throws IOException {
XContentParser.Token token = parser.currentToken();
ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);

String currentFieldName = parser.currentName();
long version = -1;
long seqNo = UNASSIGNED_SEQ_NO;
long primaryTerm = UNASSIGNED_PRIMARY_TERM;
Boolean found = null;
BytesReference source = null;
Map<String, DocumentField> documentFields = new HashMap<>();
Map<String, DocumentField> metaFields = new HashMap<>();
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if (_INDEX.equals(currentFieldName)) {
index = parser.text();
} else if (_ID.equals(currentFieldName)) {
id = parser.text();
} else if (_VERSION.equals(currentFieldName)) {
version = parser.longValue();
} else if (_SEQ_NO.equals(currentFieldName)) {
seqNo = parser.longValue();
} else if (_PRIMARY_TERM.equals(currentFieldName)) {
primaryTerm = parser.longValue();
} else if (FOUND.equals(currentFieldName)) {
found = parser.booleanValue();
} else {
metaFields.put(currentFieldName, new DocumentField(currentFieldName, Collections.singletonList(parser.objectText())));
}
} else if (token == XContentParser.Token.START_OBJECT) {
if (SourceFieldMapper.NAME.equals(currentFieldName)) {
try (XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent())) {
// the original document gets slightly modified: whitespaces or pretty printing are not preserved,
// it all depends on the current builder settings
builder.copyCurrentStructure(parser);
source = BytesReference.bytes(builder);
}
} else if (FIELDS.equals(currentFieldName)) {
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
DocumentField getField = DocumentField.fromXContent(parser);
documentFields.put(getField.getName(), getField);
}
} else {
parser.skipChildren(); // skip potential inner objects for forward compatibility
}
} else if (token == XContentParser.Token.START_ARRAY) {
if (IgnoredFieldMapper.NAME.equals(currentFieldName) || IgnoredSourceFieldMapper.NAME.equals(currentFieldName)) {
metaFields.put(currentFieldName, new DocumentField(currentFieldName, parser.list()));
} else {
parser.skipChildren(); // skip potential inner arrays for forward compatibility
}
}
}
return new GetResult(index, id, seqNo, primaryTerm, version, found, source, documentFields, metaFields);
}

public static GetResult fromXContent(XContentParser parser) throws IOException {
XContentParser.Token token = parser.nextToken();
ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser);

return fromXContentEmbedded(parser);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeString(index);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
import org.elasticsearch.common.document.DocumentField;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.index.get.GetResult;
import org.elasticsearch.index.get.GetResultTests;
import org.elasticsearch.test.AbstractXContentSerializingTestCase;
import org.elasticsearch.test.RandomObjects;
import org.elasticsearch.xcontent.ConstructingObjectParser;
Expand Down Expand Up @@ -50,7 +51,7 @@ public class ExplainResponseTests extends AbstractXContentSerializingTestCase<Ex
PARSER.declareObject(ConstructingObjectParser.optionalConstructorArg(), explanationParser, ExplainResponse.EXPLANATION);
PARSER.declareObject(
ConstructingObjectParser.optionalConstructorArg(),
(p, c) -> GetResult.fromXContentEmbedded(p),
(p, c) -> GetResultTests.parseInstanceFromEmbedded(p),
ExplainResponse.GET
);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.core.Tuple;
import org.elasticsearch.index.get.GetResult;
import org.elasticsearch.index.get.GetResultTests;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xcontent.ToXContent;
import org.elasticsearch.xcontent.XContentParser;
Expand Down Expand Up @@ -188,7 +189,7 @@ private static GetResponse mutateGetResponse(GetResponse getResponse) {
}

private static GetResponse parseInstance(XContentParser parser) throws IOException {
GetResult getResult = GetResult.fromXContent(parser);
GetResult getResult = GetResultTests.parseInstance(parser);

// At this stage we ensure that we parsed enough information to return
// a valid GetResponse instance. If it's not the case, we throw an
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
import org.elasticsearch.common.bytes.BytesReference;
import org.elasticsearch.common.logging.DeprecationLogger;
import org.elasticsearch.index.get.GetResult;
import org.elasticsearch.index.get.GetResultTests;
import org.elasticsearch.rest.action.document.RestMultiGetAction;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.xcontent.ParseField;
Expand Down Expand Up @@ -129,7 +130,7 @@ private static MultiGetItemResponse parseItem(XContentParser parser) throws IOEx
if (MultiGetResponse.INDEX.match(currentFieldName, parser.getDeprecationHandler()) == false
&& MultiGetResponse.ID.match(currentFieldName, parser.getDeprecationHandler()) == false
&& ERROR.match(currentFieldName, parser.getDeprecationHandler()) == false) {
getResult = GetResult.fromXContentEmbedded(parser, index, id);
getResult = GetResultTests.parseInstanceFromEmbedded(parser, index, id);
}
break;
case VALUE_STRING:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ public static void parseXContentFields(XContentParser parser, UpdateResponse.Bui

if (UpdateResponse.GET.equals(currentFieldName)) {
if (token == XContentParser.Token.START_OBJECT) {
context.setGetResult(GetResult.fromXContentEmbedded(parser));
context.setGetResult(GetResultTests.parseInstanceFromEmbedded(parser));
}
} else {
BulkItemResponseTests.parseInnerToXContent(parser, context);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,16 @@
import org.elasticsearch.common.xcontent.XContentHelper;
import org.elasticsearch.core.Tuple;
import org.elasticsearch.index.mapper.IdFieldMapper;
import org.elasticsearch.index.mapper.IgnoredFieldMapper;
import org.elasticsearch.index.mapper.IgnoredSourceFieldMapper;
import org.elasticsearch.index.mapper.IndexFieldMapper;
import org.elasticsearch.index.mapper.SeqNoFieldMapper;
import org.elasticsearch.index.mapper.SourceFieldMapper;
import org.elasticsearch.index.mapper.VersionFieldMapper;
import org.elasticsearch.test.ESTestCase;
import org.elasticsearch.test.RandomObjects;
import org.elasticsearch.xcontent.ToXContent;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;
import org.elasticsearch.xcontent.XContentType;

Expand All @@ -49,6 +52,77 @@

public class GetResultTests extends ESTestCase {

public static GetResult parseInstance(XContentParser parser) throws IOException {
XContentParser.Token token = parser.nextToken();
ensureExpectedToken(XContentParser.Token.START_OBJECT, token, parser);

return parseInstanceFromEmbedded(parser);
}

public static GetResult parseInstanceFromEmbedded(XContentParser parser) throws IOException {
XContentParser.Token token = parser.nextToken();
ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
return parseInstanceFromEmbedded(parser, null, null);
}

public static GetResult parseInstanceFromEmbedded(XContentParser parser, String index, String id) throws IOException {
XContentParser.Token token = parser.currentToken();
ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);

String currentFieldName = parser.currentName();
long version = -1;
long seqNo = UNASSIGNED_SEQ_NO;
long primaryTerm = UNASSIGNED_PRIMARY_TERM;
Boolean found = null;
BytesReference source = null;
Map<String, DocumentField> documentFields = new HashMap<>();
Map<String, DocumentField> metaFields = new HashMap<>();
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
if (token == XContentParser.Token.FIELD_NAME) {
currentFieldName = parser.currentName();
} else if (token.isValue()) {
if (GetResult._INDEX.equals(currentFieldName)) {
index = parser.text();
} else if (GetResult._ID.equals(currentFieldName)) {
id = parser.text();
} else if (GetResult._VERSION.equals(currentFieldName)) {
version = parser.longValue();
} else if (GetResult._SEQ_NO.equals(currentFieldName)) {
seqNo = parser.longValue();
} else if (GetResult._PRIMARY_TERM.equals(currentFieldName)) {
primaryTerm = parser.longValue();
} else if (GetResult.FOUND.equals(currentFieldName)) {
found = parser.booleanValue();
} else {
metaFields.put(currentFieldName, new DocumentField(currentFieldName, singletonList(parser.objectText())));
}
} else if (token == XContentParser.Token.START_OBJECT) {
if (SourceFieldMapper.NAME.equals(currentFieldName)) {
try (XContentBuilder builder = XContentBuilder.builder(parser.contentType().xContent())) {
// the original document gets slightly modified: whitespaces or pretty printing are not preserved,
// it all depends on the current builder settings
builder.copyCurrentStructure(parser);
source = BytesReference.bytes(builder);
}
} else if (GetResult.FIELDS.equals(currentFieldName)) {
while (parser.nextToken() != XContentParser.Token.END_OBJECT) {
DocumentField getField = DocumentField.fromXContent(parser);
documentFields.put(getField.getName(), getField);
}
} else {
parser.skipChildren(); // skip potential inner objects for forward compatibility
}
} else if (token == XContentParser.Token.START_ARRAY) {
if (IgnoredFieldMapper.NAME.equals(currentFieldName) || IgnoredSourceFieldMapper.NAME.equals(currentFieldName)) {
metaFields.put(currentFieldName, new DocumentField(currentFieldName, parser.list()));
} else {
parser.skipChildren(); // skip potential inner arrays for forward compatibility
}
}
}
return new GetResult(index, id, seqNo, primaryTerm, version, found, source, documentFields, metaFields);
}

public void testToAndFromXContent() throws Exception {
XContentType xContentType = randomFrom(XContentType.values());
Tuple<GetResult, GetResult> tuple = randomGetResult(xContentType);
Expand All @@ -59,7 +133,7 @@ public void testToAndFromXContent() throws Exception {
// test that we can parse what we print out
GetResult parsedGetResult;
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
parsedGetResult = GetResult.fromXContent(parser);
parsedGetResult = parseInstance(parser);
assertNull(parser.nextToken());
}
assertEquals(expectedGetResult, parsedGetResult);
Expand Down Expand Up @@ -136,7 +210,7 @@ public void testToAndFromXContentEmbedded() throws Exception {
GetResult parsedEmbeddedGetResult;
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
parsedEmbeddedGetResult = GetResult.fromXContentEmbedded(parser);
parsedEmbeddedGetResult = parseInstanceFromEmbedded(parser);
assertNull(parser.nextToken());
}
assertEquals(expectedGetResult, parsedEmbeddedGetResult);
Expand Down