Skip to content

Commit

Permalink
Move field_caps parsers to test codebase
Browse files Browse the repository at this point in the history
The parsing logic is test only at this point, lets move it to tests
accordingly to keep the prod codebase a little smaller.
Also fixed a missing `static`.
  • Loading branch information
original-brownbear committed Sep 20, 2024
1 parent 079b4c3 commit 69a018d
Show file tree
Hide file tree
Showing 11 changed files with 156 additions and 150 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,10 @@
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.core.Predicates;
import org.elasticsearch.index.mapper.TimeSeriesParams;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.InstantiatingObjectParser;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ParserConstructor;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -46,18 +43,17 @@
*/
public class FieldCapabilities implements Writeable, ToXContentObject {

private static final ParseField TYPE_FIELD = new ParseField("type");
private static final ParseField IS_METADATA_FIELD = new ParseField("metadata_field");
private static final ParseField SEARCHABLE_FIELD = new ParseField("searchable");
private static final ParseField AGGREGATABLE_FIELD = new ParseField("aggregatable");
private static final ParseField TIME_SERIES_DIMENSION_FIELD = new ParseField(TIME_SERIES_DIMENSION_PARAM);
private static final ParseField TIME_SERIES_METRIC_FIELD = new ParseField(TIME_SERIES_METRIC_PARAM);
private static final ParseField INDICES_FIELD = new ParseField("indices");
private static final ParseField NON_SEARCHABLE_INDICES_FIELD = new ParseField("non_searchable_indices");
private static final ParseField NON_AGGREGATABLE_INDICES_FIELD = new ParseField("non_aggregatable_indices");
private static final ParseField NON_DIMENSION_INDICES_FIELD = new ParseField("non_dimension_indices");
private static final ParseField METRIC_CONFLICTS_INDICES_FIELD = new ParseField("metric_conflicts_indices");
private static final ParseField META_FIELD = new ParseField("meta");
public static final ParseField TYPE_FIELD = new ParseField("type");
public static final ParseField IS_METADATA_FIELD = new ParseField("metadata_field");
public static final ParseField SEARCHABLE_FIELD = new ParseField("searchable");
public static final ParseField AGGREGATABLE_FIELD = new ParseField("aggregatable");
public static final ParseField TIME_SERIES_DIMENSION_FIELD = new ParseField(TIME_SERIES_DIMENSION_PARAM);
public static final ParseField TIME_SERIES_METRIC_FIELD = new ParseField(TIME_SERIES_METRIC_PARAM);
public static final ParseField INDICES_FIELD = new ParseField("indices");
public static final ParseField NON_SEARCHABLE_INDICES_FIELD = new ParseField("non_searchable_indices");
public static final ParseField NON_AGGREGATABLE_INDICES_FIELD = new ParseField("non_aggregatable_indices");
public static final ParseField NON_DIMENSION_INDICES_FIELD = new ParseField("non_dimension_indices");
public static final ParseField METRIC_CONFLICTS_INDICES_FIELD = new ParseField("metric_conflicts_indices");

private final String name;
private final String type;
Expand Down Expand Up @@ -312,37 +308,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
return builder;
}

public static FieldCapabilities fromXContent(String name, XContentParser parser) throws IOException {
return PARSER.parse(parser, name);
}

private static final InstantiatingObjectParser<FieldCapabilities, String> PARSER;

static {
InstantiatingObjectParser.Builder<FieldCapabilities, String> parser = InstantiatingObjectParser.builder(
"field_capabilities",
true,
FieldCapabilities.class
);
parser.declareString(ConstructingObjectParser.constructorArg(), TYPE_FIELD);
parser.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), IS_METADATA_FIELD);
parser.declareBoolean(ConstructingObjectParser.constructorArg(), SEARCHABLE_FIELD);
parser.declareBoolean(ConstructingObjectParser.constructorArg(), AGGREGATABLE_FIELD);
parser.declareBoolean(ConstructingObjectParser.optionalConstructorArg(), TIME_SERIES_DIMENSION_FIELD);
parser.declareString(ConstructingObjectParser.optionalConstructorArg(), TIME_SERIES_METRIC_FIELD);
parser.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), INDICES_FIELD);
parser.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), NON_SEARCHABLE_INDICES_FIELD);
parser.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), NON_AGGREGATABLE_INDICES_FIELD);
parser.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), NON_DIMENSION_INDICES_FIELD);
parser.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), METRIC_CONFLICTS_INDICES_FIELD);
parser.declareObject(
ConstructingObjectParser.optionalConstructorArg(),
(p, context) -> p.map(HashMap::new, v -> Set.copyOf(v.list())),
META_FIELD
);
PARSER = parser.build();
}

/**
* The name of the field.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,9 @@
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.io.stream.Writeable;
import org.elasticsearch.common.xcontent.XContentParserUtils;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContentObject;
import org.elasticsearch.xcontent.XContentBuilder;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
import java.util.ArrayList;
Expand All @@ -28,8 +25,8 @@

public class FieldCapabilitiesFailure implements Writeable, ToXContentObject {

private static final ParseField INDICES_FIELD = new ParseField("indices");
private static final ParseField FAILURE_FIELD = new ParseField("failure");
public static final ParseField INDICES_FIELD = new ParseField("indices");
public static final ParseField FAILURE_FIELD = new ParseField("failure");
private final List<String> indices;
private final Exception exception;

Expand Down Expand Up @@ -58,30 +55,6 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
return builder;
}

@SuppressWarnings("unchecked")
private static final ConstructingObjectParser<FieldCapabilitiesFailure, Void> PARSER = new ConstructingObjectParser<>(
"field_capabilities_failure",
true,
a -> {
return new FieldCapabilitiesFailure(((List<String>) a[0]).toArray(String[]::new), (Exception) a[1]);
}
);

static {
PARSER.declareStringArray(ConstructingObjectParser.constructorArg(), INDICES_FIELD);
PARSER.declareObject(ConstructingObjectParser.constructorArg(), (p, c) -> {
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, p.currentToken(), p);
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, p.nextToken(), p);
Exception e = ElasticsearchException.failureFromXContent(p);
XContentParserUtils.ensureExpectedToken(XContentParser.Token.END_OBJECT, p.nextToken(), p);
return e;
}, FAILURE_FIELD);
}

public static FieldCapabilitiesFailure fromXContent(XContentParser parser) throws IOException {
return PARSER.parse(parser, null);
}

@Override
public void writeTo(StreamOutput out) throws IOException {
out.writeStringCollection(indices);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,31 +15,25 @@
import org.elasticsearch.common.io.stream.StreamInput;
import org.elasticsearch.common.io.stream.StreamOutput;
import org.elasticsearch.common.xcontent.ChunkedToXContentObject;
import org.elasticsearch.common.xcontent.XContentParserUtils;
import org.elasticsearch.core.Tuple;
import org.elasticsearch.xcontent.ConstructingObjectParser;
import org.elasticsearch.xcontent.ParseField;
import org.elasticsearch.xcontent.ToXContent;
import org.elasticsearch.xcontent.XContentParser;

import java.io.IOException;
import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.stream.Collectors;

/**
* Response for {@link FieldCapabilitiesRequest} requests.
*/
public class FieldCapabilitiesResponse extends ActionResponse implements ChunkedToXContentObject {
private static final ParseField INDICES_FIELD = new ParseField("indices");
private static final ParseField FIELDS_FIELD = new ParseField("fields");
public static final ParseField INDICES_FIELD = new ParseField("indices");
public static final ParseField FIELDS_FIELD = new ParseField("fields");
private static final ParseField FAILED_INDICES_FIELD = new ParseField("failed_indices");
private static final ParseField FAILURES_FIELD = new ParseField("failures");
public static final ParseField FAILURES_FIELD = new ParseField("failures");

private final String[] indices;
private final Map<String, Map<String, FieldCapabilities>> responseMap;
Expand Down Expand Up @@ -183,50 +177,6 @@ public Iterator<? extends ToXContent> toXContentChunked(ToXContent.Params params
);
}

public static FieldCapabilitiesResponse fromXContent(XContentParser parser) throws IOException {
return PARSER.parse(parser, null);
}

@SuppressWarnings("unchecked")
private static final ConstructingObjectParser<FieldCapabilitiesResponse, Void> PARSER = new ConstructingObjectParser<>(
"field_capabilities_response",
true,
a -> {
Map<String, Map<String, FieldCapabilities>> responseMap = ((List<Tuple<String, Map<String, FieldCapabilities>>>) a[0]).stream()
.collect(Collectors.toMap(Tuple::v1, Tuple::v2));
List<String> indices = a[1] == null ? Collections.emptyList() : (List<String>) a[1];
List<FieldCapabilitiesFailure> failures = a[2] == null ? Collections.emptyList() : (List<FieldCapabilitiesFailure>) a[2];
return new FieldCapabilitiesResponse(indices.toArray(String[]::new), responseMap, failures);
}
);

static {
PARSER.declareNamedObjects(ConstructingObjectParser.constructorArg(), (p, c, n) -> {
Map<String, FieldCapabilities> typeToCapabilities = parseTypeToCapabilities(p, n);
return new Tuple<>(n, typeToCapabilities);
}, FIELDS_FIELD);
PARSER.declareStringArray(ConstructingObjectParser.optionalConstructorArg(), INDICES_FIELD);
PARSER.declareObjectArray(
ConstructingObjectParser.optionalConstructorArg(),
(p, c) -> FieldCapabilitiesFailure.fromXContent(p),
FAILURES_FIELD
);
}

private static Map<String, FieldCapabilities> parseTypeToCapabilities(XContentParser parser, String name) throws IOException {
Map<String, FieldCapabilities> typeToCapabilities = new HashMap<>();

XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, parser.nextToken(), parser);
XContentParser.Token token;
while ((token = parser.nextToken()) != XContentParser.Token.END_OBJECT) {
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, token, parser);
String type = parser.currentName();
FieldCapabilities capabilities = FieldCapabilities.fromXContent(name, parser);
typeToCapabilities.put(type, capabilities);
}
return typeToCapabilities;
}

@Override
public boolean equals(Object o) {
if (this == o) return true;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@ private static void checkIndexBlocks(ClusterState clusterState, String[] concret
}
}

private void mergeIndexResponses(
private static void mergeIndexResponses(
FieldCapabilitiesRequest request,
CancellableTask task,
Map<String, FieldCapabilitiesIndexResponse> indexResponses,
Expand Down Expand Up @@ -564,7 +564,7 @@ boolean isEmpty() {

private class NodeTransportHandler implements TransportRequestHandler<FieldCapabilitiesNodeRequest> {
@Override
public void messageReceived(FieldCapabilitiesNodeRequest request, TransportChannel channel, Task task) throws Exception {
public void messageReceived(FieldCapabilitiesNodeRequest request, TransportChannel channel, Task task) {
assert task instanceof CancellableTask;
final ActionListener<FieldCapabilitiesNodeResponse> listener = new ChannelActionListener<>(channel);
ActionListener.completeWith(listener, () -> {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ public void testFailureParsing() throws IOException {
);
FieldCapabilitiesResponse parsedResponse;
try (XContentParser parser = createParser(xContentType.xContent(), originalBytes)) {
parsedResponse = FieldCapabilitiesResponse.fromXContent(parser);
parsedResponse = FieldCapsUtils.parseFieldCapsResponse(parser);
assertNull(parser.nextToken());
}
assertNotSame(parsedResponse, randomResponse);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ public class FieldCapabilitiesTests extends AbstractXContentSerializingTestCase<

@Override
protected FieldCapabilities doParseInstance(XContentParser parser) throws IOException {
return FieldCapabilities.fromXContent(FIELD_NAME, parser);
return FieldCapsUtils.parseFieldCaps(FIELD_NAME, parser);
}

@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ public class MergedFieldCapabilitiesResponseTests extends AbstractChunkedSeriali

@Override
protected FieldCapabilitiesResponse doParseInstance(XContentParser parser) throws IOException {
return FieldCapabilitiesResponse.fromXContent(parser);
return FieldCapsUtils.parseFieldCapsResponse(parser);
}

@Override
Expand Down
Loading

0 comments on commit 69a018d

Please sign in to comment.