Skip to content

Commit 4cf598e

Browse files
committed
[Refactor] StreamIO and OpenSearchException foundation to core library
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 <[email protected]>
1 parent c45073c commit 4cf598e

File tree

282 files changed

+2277
-1505
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

282 files changed

+2277
-1505
lines changed

client/rest-high-level/src/main/java/org/opensearch/client/GetAliasesResponse.java

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
package org.opensearch.client;
3434

35+
import org.opensearch.BaseOpenSearchException;
3536
import org.opensearch.OpenSearchException;
3637
import org.opensearch.cluster.metadata.AliasMetadata;
3738
import org.opensearch.common.xcontent.StatusToXContentObject;
@@ -63,7 +64,7 @@ public class GetAliasesResponse implements StatusToXContentObject {
6364

6465
private final RestStatus status;
6566
private final String error;
66-
private final OpenSearchException exception;
67+
private final BaseOpenSearchException exception;
6768

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

@@ -74,7 +75,7 @@ public class GetAliasesResponse implements StatusToXContentObject {
7475
this.exception = null;
7576
}
7677

77-
private GetAliasesResponse(RestStatus status, OpenSearchException exception) {
78+
private GetAliasesResponse(RestStatus status, BaseOpenSearchException exception) {
7879
this.status = status;
7980
this.error = null;
8081
this.aliases = Collections.emptyMap();
@@ -96,7 +97,7 @@ public String getError() {
9697
/**
9798
* Return the exception that may have been returned
9899
*/
99-
public OpenSearchException getException() {
100+
public BaseOpenSearchException getException() {
100101
return exception;
101102
}
102103

@@ -147,7 +148,7 @@ public static GetAliasesResponse fromXContent(XContentParser parser) throws IOEx
147148
String currentFieldName;
148149
Token token;
149150
String error = null;
150-
OpenSearchException exception = null;
151+
BaseOpenSearchException exception = null;
151152
RestStatus status = RestStatus.OK;
152153

153154
while (parser.nextToken() != Token.END_OBJECT) {

client/rest-high-level/src/main/java/org/opensearch/client/RequestConverters.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,7 @@
7676
import org.opensearch.common.bytes.BytesReference;
7777
import org.opensearch.common.lucene.uid.Versions;
7878
import org.opensearch.common.unit.TimeValue;
79+
import org.opensearch.common.util.BytesReferenceUtil;
7980
import org.opensearch.common.util.CollectionUtils;
8081
import org.opensearch.common.xcontent.XContentHelper;
8182
import org.opensearch.common.xcontent.XContentType;
@@ -236,7 +237,7 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
236237
}
237238
metadata.endObject();
238239

239-
BytesRef metadataSource = BytesReference.bytes(metadata).toBytesRef();
240+
BytesRef metadataSource = BytesReferenceUtil.bytes(metadata).toBytesRef();
240241
content.write(metadataSource.bytes, metadataSource.offset, metadataSource.length);
241242
content.write(separator);
242243
}
@@ -262,7 +263,7 @@ static Request bulk(BulkRequest bulkRequest) throws IOException {
262263
) {
263264
try (XContentBuilder builder = XContentBuilder.builder(bulkContentType.xContent())) {
264265
builder.copyCurrentStructure(parser);
265-
source = BytesReference.bytes(builder).toBytesRef();
266+
source = BytesReferenceUtil.bytes(builder).toBytesRef();
266267
}
267268
}
268269
} else if (opType == DocWriteRequest.OpType.UPDATE) {

client/rest-high-level/src/main/java/org/opensearch/client/core/TermVectorsRequest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@
3535
import org.opensearch.client.Validatable;
3636
import org.opensearch.common.Nullable;
3737
import org.opensearch.common.bytes.BytesReference;
38+
import org.opensearch.common.util.BytesReferenceUtil;
3839
import org.opensearch.core.xcontent.ToXContentObject;
3940
import org.opensearch.core.xcontent.XContentBuilder;
4041

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

282283
if (docBuilder != null) {
283-
BytesReference doc = BytesReference.bytes(docBuilder);
284+
BytesReference doc = BytesReferenceUtil.bytes(docBuilder);
284285
try (InputStream stream = doc.streamInput()) {
285286
builder.rawField("doc", stream, docBuilder.contentType());
286287
}

client/rest-high-level/src/main/java/org/opensearch/client/indices/CreateIndexRequest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.opensearch.common.bytes.BytesArray;
4343
import org.opensearch.common.bytes.BytesReference;
4444
import org.opensearch.common.settings.Settings;
45+
import org.opensearch.common.util.BytesReferenceUtil;
4546
import org.opensearch.common.xcontent.XContentFactory;
4647
import org.opensearch.common.xcontent.XContentHelper;
4748
import org.opensearch.common.xcontent.XContentType;
@@ -201,7 +202,7 @@ public CreateIndexRequest mapping(String source, MediaType mediaType) {
201202
* @param source The mapping source
202203
*/
203204
public CreateIndexRequest mapping(XContentBuilder source) {
204-
return mapping(BytesReference.bytes(source), source.contentType());
205+
return mapping(BytesReferenceUtil.bytes(source), source.contentType());
205206
}
206207

207208
/**
@@ -215,7 +216,7 @@ public CreateIndexRequest mapping(Map<String, ?> source) {
215216
try {
216217
XContentBuilder builder = XContentFactory.contentBuilder(XContentType.JSON);
217218
builder.map(source);
218-
return mapping(BytesReference.bytes(builder), builder.contentType());
219+
return mapping(BytesReferenceUtil.bytes(builder), builder.contentType());
219220
} catch (IOException e) {
220221
throw new OpenSearchGenerationException("Failed to generate [" + source + "]", e);
221222
}
@@ -265,7 +266,7 @@ public CreateIndexRequest aliases(Map<String, ?> source) {
265266
try {
266267
XContentBuilder builder = XContentFactory.jsonBuilder();
267268
builder.map(source);
268-
return aliases(BytesReference.bytes(builder), builder.contentType());
269+
return aliases(BytesReferenceUtil.bytes(builder), builder.contentType());
269270
} catch (IOException e) {
270271
throw new OpenSearchGenerationException("Failed to generate [" + source + "]", e);
271272
}
@@ -275,7 +276,7 @@ public CreateIndexRequest aliases(Map<String, ?> source) {
275276
* Sets the aliases that will be associated with the index when it gets created
276277
*/
277278
public CreateIndexRequest aliases(XContentBuilder source) {
278-
return aliases(BytesReference.bytes(source), source.contentType());
279+
return aliases(BytesReferenceUtil.bytes(source), source.contentType());
279280
}
280281

281282
/**
@@ -372,7 +373,7 @@ public CreateIndexRequest source(String source, MediaType mediaType) {
372373
* Note that the mapping definition should *not* be nested under a type name.
373374
*/
374375
public CreateIndexRequest source(XContentBuilder source) {
375-
return source(BytesReference.bytes(source), source.contentType());
376+
return source(BytesReferenceUtil.bytes(source), source.contentType());
376377
}
377378

378379
/**

client/rest-high-level/src/main/java/org/opensearch/client/indices/GetFieldMappingsResponse.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232

3333
package org.opensearch.client.indices;
3434

35+
import org.opensearch.common.util.BytesReferenceUtil;
3536
import org.opensearch.core.ParseField;
3637
import org.opensearch.common.bytes.BytesReference;
3738
import org.opensearch.core.xcontent.ConstructingObjectParser;
@@ -129,7 +130,7 @@ public static class FieldMappingMetadata {
129130
PARSER.declareField(optionalConstructorArg(), (p, c) -> p.text(), FULL_NAME, ObjectParser.ValueType.STRING);
130131
PARSER.declareField(optionalConstructorArg(), (p, c) -> {
131132
final XContentBuilder jsonBuilder = jsonBuilder().copyCurrentStructure(p);
132-
final BytesReference bytes = BytesReference.bytes(jsonBuilder);
133+
final BytesReference bytes = BytesReferenceUtil.bytes(jsonBuilder);
133134
return bytes;
134135
}, MAPPING, ObjectParser.ValueType.OBJECT);
135136
}

client/rest-high-level/src/main/java/org/opensearch/client/indices/PutIndexTemplateRequest.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@
4242
import org.opensearch.common.bytes.BytesArray;
4343
import org.opensearch.common.bytes.BytesReference;
4444
import org.opensearch.common.settings.Settings;
45+
import org.opensearch.common.util.BytesReferenceUtil;
4546
import org.opensearch.common.xcontent.XContentFactory;
4647
import org.opensearch.common.xcontent.XContentHelper;
4748
import org.opensearch.common.xcontent.XContentType;
@@ -241,7 +242,7 @@ public String cause() {
241242
* @param source The mapping source
242243
*/
243244
public PutIndexTemplateRequest mapping(XContentBuilder source) {
244-
internalMapping(XContentHelper.convertToMap(BytesReference.bytes(source), true, source.contentType()).v2());
245+
internalMapping(XContentHelper.convertToMap(BytesReferenceUtil.bytes(source), true, source.contentType()).v2());
245246
return this;
246247
}
247248

@@ -272,7 +273,7 @@ private PutIndexTemplateRequest internalMapping(Map<String, Object> source) {
272273
MediaType mediaType = builder.contentType();
273274
Objects.requireNonNull(mediaType);
274275
try {
275-
mappings = new BytesArray(XContentHelper.convertToJson(BytesReference.bytes(builder), false, false, mediaType));
276+
mappings = new BytesArray(XContentHelper.convertToJson(BytesReferenceUtil.bytes(builder), false, false, mediaType));
276277
return this;
277278
} catch (IOException e) {
278279
throw new UncheckedIOException("failed to convert source to json", e);
@@ -291,7 +292,7 @@ public BytesReference mappings() {
291292
*/
292293
public PutIndexTemplateRequest source(XContentBuilder templateBuilder) {
293294
try {
294-
return source(BytesReference.bytes(templateBuilder), templateBuilder.contentType());
295+
return source(BytesReferenceUtil.bytes(templateBuilder), templateBuilder.contentType());
295296
} catch (Exception e) {
296297
throw new IllegalArgumentException("Failed to build json for template request", e);
297298
}
@@ -421,7 +422,7 @@ public PutIndexTemplateRequest aliases(Map<String, ?> source) {
421422
try {
422423
XContentBuilder builder = XContentFactory.jsonBuilder();
423424
builder.map(source);
424-
return aliases(BytesReference.bytes(builder));
425+
return aliases(BytesReferenceUtil.bytes(builder));
425426
} catch (IOException e) {
426427
throw new OpenSearchGenerationException("Failed to generate [" + source + "]", e);
427428
}
@@ -431,7 +432,7 @@ public PutIndexTemplateRequest aliases(Map<String, ?> source) {
431432
* Sets the aliases that will be associated with the index when it gets created
432433
*/
433434
public PutIndexTemplateRequest aliases(XContentBuilder source) {
434-
return aliases(BytesReference.bytes(source));
435+
return aliases(BytesReferenceUtil.bytes(source));
435436
}
436437

437438
/**

client/rest-high-level/src/main/java/org/opensearch/client/indices/PutMappingRequest.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@
3838
import org.opensearch.client.TimedRequest;
3939
import org.opensearch.common.bytes.BytesArray;
4040
import org.opensearch.common.bytes.BytesReference;
41+
import org.opensearch.common.util.BytesReferenceUtil;
4142
import org.opensearch.common.xcontent.XContentFactory;
4243
import org.opensearch.common.xcontent.XContentType;
4344
import org.opensearch.core.xcontent.MediaType;
@@ -160,7 +161,7 @@ public PutMappingRequest source(String mappingSource, MediaType mediaType) {
160161
* Note that the definition should *not* be nested under a type name.
161162
*/
162163
public PutMappingRequest source(XContentBuilder builder) {
163-
this.source = BytesReference.bytes(builder);
164+
this.source = BytesReferenceUtil.bytes(builder);
164165
this.mediaType = builder.contentType();
165166
return this;
166167
}

client/rest-high-level/src/test/java/org/opensearch/client/CrudIT.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,7 @@
6262
import org.opensearch.common.settings.Settings;
6363
import org.opensearch.common.unit.ByteSizeUnit;
6464
import org.opensearch.common.unit.ByteSizeValue;
65+
import org.opensearch.common.util.BytesReferenceUtil;
6566
import org.opensearch.core.common.Strings;
6667
import org.opensearch.core.xcontent.XContentBuilder;
6768
import org.opensearch.common.xcontent.XContentFactory;
@@ -862,7 +863,7 @@ public void testBulk() throws IOException {
862863
bulkRequest.add(deleteRequest);
863864

864865
} else {
865-
BytesReference source = BytesReference.bytes(
866+
BytesReference source = BytesReferenceUtil.bytes(
866867
XContentBuilder.builder(xContentType.xContent()).startObject().field("id", i).endObject()
867868
);
868869
if (opType == DocWriteRequest.OpType.INDEX) {

client/rest-high-level/src/test/java/org/opensearch/client/IngestClientIT.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
import org.opensearch.action.ingest.SimulatePipelineRequest;
4343
import org.opensearch.action.ingest.SimulatePipelineResponse;
4444
import org.opensearch.action.support.master.AcknowledgedResponse;
45-
import org.opensearch.common.bytes.BytesReference;
45+
import org.opensearch.common.util.BytesReferenceUtil;
4646
import org.opensearch.core.xcontent.XContentBuilder;
4747
import org.opensearch.common.xcontent.XContentType;
4848
import org.opensearch.ingest.PipelineConfiguration;
@@ -58,7 +58,7 @@ public class IngestClientIT extends OpenSearchRestHighLevelClientTestCase {
5858
public void testPutPipeline() throws IOException {
5959
String id = "some_pipeline_id";
6060
XContentBuilder pipelineBuilder = buildRandomXContentPipeline();
61-
PutPipelineRequest request = new PutPipelineRequest(id, BytesReference.bytes(pipelineBuilder), pipelineBuilder.contentType());
61+
PutPipelineRequest request = new PutPipelineRequest(id, BytesReferenceUtil.bytes(pipelineBuilder), pipelineBuilder.contentType());
6262

6363
AcknowledgedResponse putPipelineResponse = execute(
6464
request,
@@ -72,7 +72,11 @@ public void testGetPipeline() throws IOException {
7272
String id = "some_pipeline_id";
7373
XContentBuilder pipelineBuilder = buildRandomXContentPipeline();
7474
{
75-
PutPipelineRequest request = new PutPipelineRequest(id, BytesReference.bytes(pipelineBuilder), pipelineBuilder.contentType());
75+
PutPipelineRequest request = new PutPipelineRequest(
76+
id,
77+
BytesReferenceUtil.bytes(pipelineBuilder),
78+
pipelineBuilder.contentType()
79+
);
7680
createPipeline(request);
7781
}
7882

@@ -87,7 +91,7 @@ public void testGetPipeline() throws IOException {
8791
assertEquals(response.pipelines().get(0).getId(), id);
8892
PipelineConfiguration expectedConfig = new PipelineConfiguration(
8993
id,
90-
BytesReference.bytes(pipelineBuilder),
94+
BytesReferenceUtil.bytes(pipelineBuilder),
9195
pipelineBuilder.contentType()
9296
);
9397
assertEquals(expectedConfig.getConfigAsMap(), response.pipelines().get(0).getConfigAsMap());
@@ -162,7 +166,7 @@ private void testSimulatePipeline(boolean isVerbose, boolean isFailure) throws I
162166
builder.endObject();
163167

164168
SimulatePipelineRequest request = new SimulatePipelineRequest(
165-
BytesReference.bytes(builder),
169+
BytesReferenceUtil.bytes(builder),
166170
XContentType.fromMediaType(builder.contentType())
167171
);
168172
request.setVerbose(isVerbose);

client/rest-high-level/src/test/java/org/opensearch/client/OpenSearchRestHighLevelClientTestCase.java

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -48,8 +48,8 @@
4848
import org.opensearch.client.indices.CreateIndexRequest;
4949
import org.opensearch.common.Booleans;
5050
import org.opensearch.common.CheckedRunnable;
51-
import org.opensearch.common.bytes.BytesReference;
5251
import org.opensearch.common.settings.Settings;
52+
import org.opensearch.common.util.BytesReferenceUtil;
5353
import org.opensearch.common.util.concurrent.ThreadContext;
5454
import org.opensearch.core.xcontent.XContentBuilder;
5555
import org.opensearch.common.xcontent.XContentHelper;
@@ -223,12 +223,12 @@ protected static void createFieldAddingPipleine(String id, String fieldName, Str
223223
.endArray()
224224
.endObject();
225225

226-
createPipeline(new PutPipelineRequest(id, BytesReference.bytes(pipeline), XContentType.JSON));
226+
createPipeline(new PutPipelineRequest(id, BytesReferenceUtil.bytes(pipeline), XContentType.JSON));
227227
}
228228

229229
protected static void createPipeline(String pipelineId) throws IOException {
230230
XContentBuilder builder = buildRandomXContentPipeline();
231-
createPipeline(new PutPipelineRequest(pipelineId, BytesReference.bytes(builder), builder.contentType()));
231+
createPipeline(new PutPipelineRequest(pipelineId, BytesReferenceUtil.bytes(builder), builder.contentType()));
232232
}
233233

234234
protected static void createPipeline(PutPipelineRequest putPipelineRequest) throws IOException {
@@ -266,7 +266,7 @@ protected void putConflictPipeline() throws IOException {
266266
.endObject();
267267
final PutPipelineRequest putPipelineRequest = new PutPipelineRequest(
268268
CONFLICT_PIPELINE_ID,
269-
BytesReference.bytes(pipelineBuilder),
269+
BytesReferenceUtil.bytes(pipelineBuilder),
270270
pipelineBuilder.contentType()
271271
);
272272
assertTrue(highLevelClient().ingest().putPipeline(putPipelineRequest, RequestOptions.DEFAULT).isAcknowledged());

0 commit comments

Comments
 (0)