Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Refactor] StreamInput, StreamOutput, Writeable to core library #7783

Closed
wants to merge 13 commits into from
  •  
  •  
  •  
1 change: 1 addition & 0 deletions .idea/inspectionProfiles/Project_Default.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Original file line number Diff line number Diff line change
Expand Up @@ -42,11 +42,11 @@
import org.opensearch.action.search.SearchRequest;
import org.opensearch.common.breaker.CircuitBreaker;
import org.opensearch.common.breaker.NoopCircuitBreaker;
import org.opensearch.common.io.stream.NamedWriteableRegistry;
import org.opensearch.core.common.io.stream.NamedWriteableRegistry;
import org.opensearch.common.lucene.search.TopDocsAndMaxScore;
import org.opensearch.common.settings.Settings;
import org.opensearch.index.Index;
import org.opensearch.index.shard.ShardId;
import org.opensearch.core.index.Index;
import org.opensearch.core.index.shard.ShardId;
import org.opensearch.indices.breaker.NoneCircuitBreakerService;
import org.opensearch.search.DocValueFormat;
import org.opensearch.search.SearchModule;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@

import org.apache.lucene.util.BytesRef;
import org.opensearch.common.io.stream.DelayableWriteable;
import org.opensearch.common.io.stream.NamedWriteableRegistry;
import org.opensearch.core.common.io.stream.NamedWriteableRegistry;
import org.opensearch.search.DocValueFormat;
import org.opensearch.search.aggregations.BucketOrder;
import org.opensearch.search.aggregations.InternalAggregation;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import org.opensearch.client.Requests;
import org.opensearch.client.node.NodeClient;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.index.shard.ShardId;
import org.opensearch.core.index.shard.ShardId;
import org.opensearch.rest.BaseRestHandler;
import org.opensearch.rest.BytesRestResponse;
import org.opensearch.rest.RestChannel;
Expand All @@ -56,7 +56,7 @@
import static java.util.Collections.unmodifiableList;
import static org.opensearch.rest.RestRequest.Method.POST;
import static org.opensearch.rest.RestRequest.Method.PUT;
import static org.opensearch.rest.RestStatus.OK;
import static org.opensearch.core.rest.RestStatus.OK;

public class RestNoopBulkAction extends BaseRestHandler {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@
import org.opensearch.action.support.HandledTransportAction;
import org.opensearch.action.update.UpdateResponse;
import org.opensearch.common.inject.Inject;
import org.opensearch.index.shard.ShardId;
import org.opensearch.core.index.shard.ShardId;
import org.opensearch.tasks.Task;
import org.opensearch.transport.TransportService;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
import org.opensearch.action.support.ActionFilters;
import org.opensearch.action.support.HandledTransportAction;
import org.opensearch.common.inject.Inject;
import org.opensearch.common.io.stream.Writeable;
import org.opensearch.core.common.io.stream.Writeable;
import org.opensearch.search.SearchHit;
import org.opensearch.search.SearchHits;
import org.opensearch.search.aggregations.InternalAggregations;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@
import org.opensearch.client.indices.GetComponentTemplatesRequest;
import org.opensearch.client.indices.GetComponentTemplatesResponse;
import org.opensearch.client.indices.PutComponentTemplateRequest;
import org.opensearch.rest.RestStatus;
import org.opensearch.core.rest.RestStatus;

import java.io.IOException;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,14 +32,14 @@

package org.opensearch.client;

import org.opensearch.OpenSearchException;
import org.opensearch.BaseOpenSearchException;
import org.opensearch.cluster.metadata.AliasMetadata;
import org.opensearch.common.xcontent.StatusToXContentObject;
import org.opensearch.core.xcontent.ToXContent;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.core.xcontent.XContentParser.Token;
import org.opensearch.rest.RestStatus;
import org.opensearch.core.rest.RestStatus;

import java.io.IOException;
import java.util.Collections;
Expand All @@ -48,7 +48,7 @@
import java.util.Map;
import java.util.Set;

import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;

/**
* Response obtained from the get aliases API.
Expand All @@ -63,7 +63,7 @@ public class GetAliasesResponse implements StatusToXContentObject {

private final RestStatus status;
private final String error;
private final OpenSearchException exception;
private final BaseOpenSearchException exception;

private final Map<String, Set<AliasMetadata>> aliases;

Expand All @@ -74,7 +74,7 @@ public class GetAliasesResponse implements StatusToXContentObject {
this.exception = null;
}

private GetAliasesResponse(RestStatus status, OpenSearchException exception) {
private GetAliasesResponse(RestStatus status, BaseOpenSearchException exception) {
this.status = status;
this.error = null;
this.aliases = Collections.emptyMap();
Expand All @@ -96,7 +96,7 @@ public String getError() {
/**
* Return the exception that may have been returned
*/
public OpenSearchException getException() {
public BaseOpenSearchException getException() {
return exception;
}

Expand Down Expand Up @@ -147,7 +147,7 @@ public static GetAliasesResponse fromXContent(XContentParser parser) throws IOEx
String currentFieldName;
Token token;
String error = null;
OpenSearchException exception = null;
BaseOpenSearchException exception = null;
RestStatus status = RestStatus.OK;

while (parser.nextToken() != Token.END_OBJECT) {
Expand All @@ -165,7 +165,7 @@ public static GetAliasesResponse fromXContent(XContentParser parser) throws IOEx
error = parser.text();
} else if (token == Token.START_OBJECT) {
parser.nextToken();
exception = OpenSearchException.innerFromXContent(parser, true);
exception = BaseOpenSearchException.innerFromXContent(parser, true);
} else if (token == Token.START_ARRAY) {
parser.skipChildren();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@
import org.opensearch.client.indices.SimulateIndexTemplateResponse;
import org.opensearch.client.indices.rollover.RolloverRequest;
import org.opensearch.client.indices.rollover.RolloverResponse;
import org.opensearch.rest.RestStatus;
import org.opensearch.core.rest.RestStatus;

import java.io.IOException;
import java.util.Collections;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@
import org.opensearch.common.Nullable;
import org.opensearch.common.Priority;
import org.opensearch.common.SuppressForbidden;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.util.BytesReferenceUtil;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.common.lucene.uid.Versions;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.util.CollectionUtils;
Expand Down Expand Up @@ -236,7 +237,7 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
}
metadata.endObject();

BytesRef metadataSource = BytesReference.bytes(metadata).toBytesRef();
BytesRef metadataSource = BytesReferenceUtil.bytes(metadata).toBytesRef();
content.write(metadataSource.bytes, metadataSource.offset, metadataSource.length);
content.write(separator);
}
Expand All @@ -262,7 +263,7 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
) {
try (XContentBuilder builder = XContentBuilder.builder(bulkContentType.xContent())) {
builder.copyCurrentStructure(parser);
source = BytesReference.bytes(builder).toBytesRef();
source = BytesReferenceUtil.bytes(builder).toBytesRef();
}
}
} else if (opType == DocWriteRequest.OpType.UPDATE) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@
import org.opensearch.index.reindex.UpdateByQueryRequest;
import org.opensearch.plugins.spi.NamedXContentProvider;
import org.opensearch.rest.BytesRestResponse;
import org.opensearch.rest.RestStatus;
import org.opensearch.core.rest.RestStatus;
import org.opensearch.script.mustache.MultiSearchTemplateRequest;
import org.opensearch.script.mustache.MultiSearchTemplateResponse;
import org.opensearch.script.mustache.SearchTemplateRequest;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@
import java.util.Collections;
import java.util.List;

import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;

/**
* A response to _remote/info API request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@

package org.opensearch.client.core;

import org.opensearch.action.support.DefaultShardOperationFailedException;
import org.opensearch.core.action.support.DefaultShardOperationFailedException;
import org.opensearch.core.ParseField;
import org.opensearch.core.xcontent.ConstructingObjectParser;
import org.opensearch.core.xcontent.XContentParser;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,14 +35,14 @@
import org.opensearch.action.search.ShardSearchFailure;
import org.opensearch.core.ParseField;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.rest.RestStatus;
import org.opensearch.core.rest.RestStatus;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;

/**
* A response to _count API request.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@

import org.opensearch.client.Validatable;
import org.opensearch.common.Nullable;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.util.BytesReferenceUtil;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.xcontent.ToXContentObject;
import org.opensearch.core.xcontent.XContentBuilder;

Expand Down Expand Up @@ -280,7 +281,7 @@ public XContentBuilder toXContent(XContentBuilder builder, Params params) throws
if (perFieldAnalyzer != null) builder.field("per_field_analyzer", perFieldAnalyzer);

if (docBuilder != null) {
BytesReference doc = BytesReference.bytes(docBuilder);
BytesReference doc = BytesReferenceUtil.bytes(docBuilder);
try (InputStream stream = doc.streamInput()) {
builder.rawField("doc", stream, docBuilder.contentType());
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,13 +32,13 @@
package org.opensearch.client.indices;

import org.opensearch.OpenSearchException;
import org.opensearch.action.support.DefaultShardOperationFailedException;
import org.opensearch.core.action.support.DefaultShardOperationFailedException;
import org.opensearch.action.support.master.ShardsAcknowledgedResponse;
import org.opensearch.common.Nullable;
import org.opensearch.core.ParseField;
import org.opensearch.core.xcontent.ConstructingObjectParser;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.common.xcontent.XContentParserUtils;
import org.opensearch.core.xcontent.XContentParserUtils;

import java.util.List;
import java.util.Objects;
Expand Down Expand Up @@ -107,8 +107,12 @@ public static class IndexResult {
static {
PARSER.declareBoolean(optionalConstructorArg(), new ParseField("closed"));
PARSER.declareObject(optionalConstructorArg(), (p, c) -> {
XContentParserUtils.ensureExpectedToken(XContentParser.Token.START_OBJECT, p.currentToken(), p);
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, p.nextToken(), p);
org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken(
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Seems like the full import is there already:

Suggested change
org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken(
XContentParserUtils.ensureExpectedToken(

XContentParser.Token.START_OBJECT,
p.currentToken(),
p
);
org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, p.nextToken(), p);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, p.nextToken(), p);
XContentParserUtils.ensureExpectedToken(XContentParser.Token.FIELD_NAME, p.nextToken(), p);

Exception e = OpenSearchException.failureFromXContent(p);
XContentParserUtils.ensureExpectedToken(XContentParser.Token.END_OBJECT, p.nextToken(), p);
return e;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,8 +39,9 @@
import org.opensearch.client.TimedRequest;
import org.opensearch.client.Validatable;
import org.opensearch.common.Strings;
import org.opensearch.common.bytes.BytesArray;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.util.BytesReferenceUtil;
import org.opensearch.core.common.bytes.BytesArray;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentHelper;
Expand Down Expand Up @@ -201,7 +202,7 @@ public CreateIndexRequest mapping(String source, MediaType mediaType) {
* @param source The mapping source
*/
public CreateIndexRequest mapping(XContentBuilder source) {
return mapping(BytesReference.bytes(source), source.contentType());
return mapping(BytesReferenceUtil.bytes(source), source.contentType());
}

/**
Expand All @@ -215,7 +216,7 @@ public CreateIndexRequest mapping(Map<String, ?> source) {
try {
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
builder.map(source);
return mapping(BytesReference.bytes(builder), builder.contentType());
return mapping(BytesReferenceUtil.bytes(builder), builder.contentType());
} catch (IOException e) {
throw new OpenSearchGenerationException("Failed to generate [" + source + "]", e);
}
Expand Down Expand Up @@ -265,7 +266,7 @@ public CreateIndexRequest aliases(Map<String, ?> source) {
try {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.map(source);
return aliases(BytesReference.bytes(builder), builder.contentType());
return aliases(BytesReferenceUtil.bytes(builder), builder.contentType());
} catch (IOException e) {
throw new OpenSearchGenerationException("Failed to generate [" + source + "]", e);
}
Expand All @@ -275,7 +276,7 @@ public CreateIndexRequest aliases(Map<String, ?> source) {
* Sets the aliases that will be associated with the index when it gets created
*/
public CreateIndexRequest aliases(XContentBuilder source) {
return aliases(BytesReference.bytes(source), source.contentType());
return aliases(BytesReferenceUtil.bytes(source), source.contentType());
}

/**
Expand Down Expand Up @@ -372,7 +373,7 @@ public CreateIndexRequest source(String source, MediaType mediaType) {
* Note that the mapping definition should *not* be nested under a type name.
*/
public CreateIndexRequest source(XContentBuilder source) {
return source(BytesReference.bytes(source), source.contentType());
return source(BytesReferenceUtil.bytes(source), source.contentType());
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,9 @@

package org.opensearch.client.indices;

import org.opensearch.common.util.BytesReferenceUtil;
import org.opensearch.core.ParseField;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.core.common.bytes.BytesReference;
import org.opensearch.core.xcontent.ConstructingObjectParser;
import org.opensearch.core.xcontent.ObjectParser;
import org.opensearch.core.xcontent.XContentBuilder;
Expand All @@ -49,7 +50,7 @@

import static org.opensearch.core.xcontent.ConstructingObjectParser.optionalConstructorArg;
import static org.opensearch.common.xcontent.XContentFactory.jsonBuilder;
import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;

/** Response object for {@link GetFieldMappingsRequest} API */
public class GetFieldMappingsResponse {
Expand Down Expand Up @@ -129,7 +130,7 @@ public static class FieldMappingMetadata {
PARSER.declareField(optionalConstructorArg(), (p, c) -> p.text(), FULL_NAME, ObjectParser.ValueType.STRING);
PARSER.declareField(optionalConstructorArg(), (p, c) -> {
final XContentBuilder jsonBuilder = jsonBuilder().copyCurrentStructure(p);
final BytesReference bytes = BytesReference.bytes(jsonBuilder);
final BytesReference bytes = BytesReferenceUtil.bytes(jsonBuilder);
return bytes;
}, MAPPING, ObjectParser.ValueType.OBJECT);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import java.util.List;
import java.util.Map;

import static org.opensearch.common.xcontent.XContentParserUtils.ensureExpectedToken;
import static org.opensearch.core.xcontent.XContentParserUtils.ensureExpectedToken;

/**
* A client side response for a get index action.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@
import org.opensearch.cluster.metadata.MappingMetadata;
import org.opensearch.core.ParseField;
import org.opensearch.core.xcontent.XContentParser;
import org.opensearch.common.xcontent.XContentParserUtils;
import org.opensearch.core.xcontent.XContentParserUtils;
import org.opensearch.index.mapper.MapperService;

import java.io.IOException;
Expand Down
Loading