Skip to content

Commit

Permalink
[Refactor] StreamIO and OpenSearchException foundation to core library
Browse files Browse the repository at this point in the history
This commit refactors the Stream IO classes from the server to core
library to support cloud native and serverless extensions.

The following classes are refactored:

* StreamInput
* StreamOutput
* Writeable
* NamedWriteable
* NamedWriteableRegistry
* Index
* ShardId
* BytesReference
* ByteArray
* BigArray
* SecureString
* Text
* ParsingException
* RestStatus

The namespace is left unchanged but will be refactored in a follow up
commit to avoid split package in order to support jigsaw modularity.

Signed-off-by: Nicholas Walter Knize <nknize@apache.org>
  • Loading branch information
nknize committed Jun 13, 2023
1 parent c45073c commit 4cf598e
Show file tree
Hide file tree
Showing 282 changed files with 2,277 additions and 1,505 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@

package org.opensearch.client;

import org.opensearch.BaseOpenSearchException;
import org.opensearch.OpenSearchException;
import org.opensearch.cluster.metadata.AliasMetadata;
import org.opensearch.common.xcontent.StatusToXContentObject;
Expand Down Expand Up @@ -63,7 +64,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 +75,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 +97,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 +148,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 Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.lucene.uid.Versions;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.util.BytesReferenceUtil;
import org.opensearch.common.util.CollectionUtils;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentType;
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 @@ -35,6 +35,7 @@
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.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 @@ -42,6 +42,7 @@
import org.opensearch.common.bytes.BytesArray;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.BytesReferenceUtil;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentType;
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,6 +32,7 @@

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.xcontent.ConstructingObjectParser;
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 @@ -42,6 +42,7 @@
import org.opensearch.common.bytes.BytesArray;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.BytesReferenceUtil;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentType;
Expand Down Expand Up @@ -241,7 +242,7 @@ public String cause() {
* @param source The mapping source
*/
public PutIndexTemplateRequest mapping(XContentBuilder source) {
internalMapping(XContentHelper.convertToMap(BytesReference.bytes(source), true, source.contentType()).v2());
internalMapping(XContentHelper.convertToMap(BytesReferenceUtil.bytes(source), true, source.contentType()).v2());
return this;
}

Expand Down Expand Up @@ -272,7 +273,7 @@ private PutIndexTemplateRequest internalMapping(Map<String, Object> source) {
MediaType mediaType = builder.contentType();
Objects.requireNonNull(mediaType);
try {
mappings = new BytesArray(XContentHelper.convertToJson(BytesReference.bytes(builder), false, false, mediaType));
mappings = new BytesArray(XContentHelper.convertToJson(BytesReferenceUtil.bytes(builder), false, false, mediaType));
return this;
} catch (IOException e) {
throw new UncheckedIOException("failed to convert source to json", e);
Expand All @@ -291,7 +292,7 @@ public BytesReference mappings() {
*/
public PutIndexTemplateRequest source(XContentBuilder templateBuilder) {
try {
return source(BytesReference.bytes(templateBuilder), templateBuilder.contentType());
return source(BytesReferenceUtil.bytes(templateBuilder), templateBuilder.contentType());
} catch (Exception e) {
throw new IllegalArgumentException("Failed to build json for template request", e);
}
Expand Down Expand Up @@ -421,7 +422,7 @@ public PutIndexTemplateRequest aliases(Map<String, ?> source) {
try {
XContentBuilder builder = XContentFactory.jsonBuilder();
builder.map(source);
return aliases(BytesReference.bytes(builder));
return aliases(BytesReferenceUtil.bytes(builder));
} catch (IOException e) {
throw new OpenSearchGenerationException("Failed to generate [" + source + "]", e);
}
Expand All @@ -431,7 +432,7 @@ public PutIndexTemplateRequest aliases(Map<String, ?> source) {
* Sets the aliases that will be associated with the index when it gets created
*/
public PutIndexTemplateRequest aliases(XContentBuilder source) {
return aliases(BytesReference.bytes(source));
return aliases(BytesReferenceUtil.bytes(source));
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@
import org.opensearch.client.TimedRequest;
import org.opensearch.common.bytes.BytesArray;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.util.BytesReferenceUtil;
import org.opensearch.common.xcontent.XContentFactory;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.core.xcontent.MediaType;
Expand Down Expand Up @@ -160,7 +161,7 @@ public PutMappingRequest source(String mappingSource, MediaType mediaType) {
* Note that the definition should *not* be nested under a type name.
*/
public PutMappingRequest source(XContentBuilder builder) {
this.source = BytesReference.bytes(builder);
this.source = BytesReferenceUtil.bytes(builder);
this.mediaType = builder.contentType();
return this;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@
import org.opensearch.common.settings.Settings;
import org.opensearch.common.unit.ByteSizeUnit;
import org.opensearch.common.unit.ByteSizeValue;
import org.opensearch.common.util.BytesReferenceUtil;
import org.opensearch.core.common.Strings;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentFactory;
Expand Down Expand Up @@ -862,7 +863,7 @@ public void testBulk() throws IOException {
bulkRequest.add(deleteRequest);

} else {
BytesReference source = BytesReference.bytes(
BytesReference source = BytesReferenceUtil.bytes(
XContentBuilder.builder(xContentType.xContent()).startObject().field("id", i).endObject()
);
if (opType == DocWriteRequest.OpType.INDEX) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@
import org.opensearch.action.ingest.SimulatePipelineRequest;
import org.opensearch.action.ingest.SimulatePipelineResponse;
import org.opensearch.action.support.master.AcknowledgedResponse;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.util.BytesReferenceUtil;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.ingest.PipelineConfiguration;
Expand All @@ -58,7 +58,7 @@ public class IngestClientIT extends OpenSearchRestHighLevelClientTestCase {
public void testPutPipeline() throws IOException {
String id = "some_pipeline_id";
XContentBuilder pipelineBuilder = buildRandomXContentPipeline();
PutPipelineRequest request = new PutPipelineRequest(id, BytesReference.bytes(pipelineBuilder), pipelineBuilder.contentType());
PutPipelineRequest request = new PutPipelineRequest(id, BytesReferenceUtil.bytes(pipelineBuilder), pipelineBuilder.contentType());

AcknowledgedResponse putPipelineResponse = execute(
request,
Expand All @@ -72,7 +72,11 @@ public void testGetPipeline() throws IOException {
String id = "some_pipeline_id";
XContentBuilder pipelineBuilder = buildRandomXContentPipeline();
{
PutPipelineRequest request = new PutPipelineRequest(id, BytesReference.bytes(pipelineBuilder), pipelineBuilder.contentType());
PutPipelineRequest request = new PutPipelineRequest(
id,
BytesReferenceUtil.bytes(pipelineBuilder),
pipelineBuilder.contentType()
);
createPipeline(request);
}

Expand All @@ -87,7 +91,7 @@ public void testGetPipeline() throws IOException {
assertEquals(response.pipelines().get(0).getId(), id);
PipelineConfiguration expectedConfig = new PipelineConfiguration(
id,
BytesReference.bytes(pipelineBuilder),
BytesReferenceUtil.bytes(pipelineBuilder),
pipelineBuilder.contentType()
);
assertEquals(expectedConfig.getConfigAsMap(), response.pipelines().get(0).getConfigAsMap());
Expand Down Expand Up @@ -162,7 +166,7 @@ private void testSimulatePipeline(boolean isVerbose, boolean isFailure) throws I
builder.endObject();

SimulatePipelineRequest request = new SimulatePipelineRequest(
BytesReference.bytes(builder),
BytesReferenceUtil.bytes(builder),
XContentType.fromMediaType(builder.contentType())
);
request.setVerbose(isVerbose);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,8 +48,8 @@
import org.opensearch.client.indices.CreateIndexRequest;
import org.opensearch.common.Booleans;
import org.opensearch.common.CheckedRunnable;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.settings.Settings;
import org.opensearch.common.util.BytesReferenceUtil;
import org.opensearch.common.util.concurrent.ThreadContext;
import org.opensearch.core.xcontent.XContentBuilder;
import org.opensearch.common.xcontent.XContentHelper;
Expand Down Expand Up @@ -223,12 +223,12 @@ protected static void createFieldAddingPipleine(String id, String fieldName, Str
.endArray()
.endObject();

createPipeline(new PutPipelineRequest(id, BytesReference.bytes(pipeline), XContentType.JSON));
createPipeline(new PutPipelineRequest(id, BytesReferenceUtil.bytes(pipeline), XContentType.JSON));
}

protected static void createPipeline(String pipelineId) throws IOException {
XContentBuilder builder = buildRandomXContentPipeline();
createPipeline(new PutPipelineRequest(pipelineId, BytesReference.bytes(builder), builder.contentType()));
createPipeline(new PutPipelineRequest(pipelineId, BytesReferenceUtil.bytes(builder), builder.contentType()));
}

protected static void createPipeline(PutPipelineRequest putPipelineRequest) throws IOException {
Expand Down Expand Up @@ -266,7 +266,7 @@ protected void putConflictPipeline() throws IOException {
.endObject();
final PutPipelineRequest putPipelineRequest = new PutPipelineRequest(
CONFLICT_PIPELINE_ID,
BytesReference.bytes(pipelineBuilder),
BytesReferenceUtil.bytes(pipelineBuilder),
pipelineBuilder.contentType()
);
assertTrue(highLevelClient().ingest().putPipeline(putPipelineRequest, RequestOptions.DEFAULT).isAcknowledged());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@
import org.opensearch.common.logging.DeprecationLogger;
import org.opensearch.common.lucene.uid.Versions;
import org.opensearch.common.unit.TimeValue;
import org.opensearch.common.util.BytesReferenceUtil;
import org.opensearch.common.xcontent.XContentHelper;
import org.opensearch.common.xcontent.XContentType;
import org.opensearch.common.xcontent.json.JsonXContent;
Expand Down Expand Up @@ -424,7 +425,7 @@ public void testReindex() throws IOException {
"remote-host",
9200,
null,
BytesReference.bytes(matchAllQuery().toXContent(builder, ToXContent.EMPTY_PARAMS)),
BytesReferenceUtil.bytes(matchAllQuery().toXContent(builder, ToXContent.EMPTY_PARAMS)),
"user",
"pass",
emptyMap(),
Expand Down Expand Up @@ -1703,7 +1704,7 @@ public void testPutScript() throws Exception {
builder.startObject("script").field("lang", "painless").field("source", "Math.log(_score * 2) + params.multiplier").endObject();
builder.endObject();

putStoredScriptRequest.content(BytesReference.bytes(builder), xContentType);
putStoredScriptRequest.content(BytesReferenceUtil.bytes(builder), xContentType);
}

Map<String, String> expectedParams = new HashMap<>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@
import org.opensearch.client.core.MainRequest;
import org.opensearch.client.core.MainResponse;
import org.opensearch.common.CheckedFunction;
import org.opensearch.common.bytes.BytesReference;
import org.opensearch.common.collect.Tuple;
import org.opensearch.common.util.BytesReferenceUtil;
import org.opensearch.common.util.set.Sets;
import org.opensearch.core.xcontent.NamedXContentRegistry;
import org.opensearch.core.xcontent.ToXContent;
Expand Down Expand Up @@ -340,7 +340,7 @@ private static HttpEntity createBinaryEntity(XContentBuilder xContentBuilder, Co
builder.startObject();
builder.field("field", "value");
builder.endObject();
return new ByteArrayEntity(BytesReference.bytes(builder).toBytesRef().bytes, contentType);
return new ByteArrayEntity(BytesReferenceUtil.bytes(builder).toBytesRef().bytes, contentType);
}
}

Expand Down
Loading

0 comments on commit 4cf598e

Please sign in to comment.