diff --git a/common/src/main/java/org/opensearch/sdk/DataObjectRequest.java b/common/src/main/java/org/opensearch/sdk/DataObjectRequest.java new file mode 100644 index 0000000000..0b17d61fa5 --- /dev/null +++ b/common/src/main/java/org/opensearch/sdk/DataObjectRequest.java @@ -0,0 +1,120 @@ +/* + * Copyright OpenSearch Contributors + * SPDX-License-Identifier: Apache-2.0 + * + * The OpenSearch Contributors require contributions made to + * this file be licensed under the Apache-2.0 license or a + * compatible open source license. + */ +package org.opensearch.sdk; + +public abstract class DataObjectRequest { + + private String index; + private final String id; + private final String tenantId; + + /** + * Instantiate this request with an index and id. + *

+ * For data storage implementations other than OpenSearch, an index may be referred to as a table and the id may be referred to as a primary key. + * @param index the index location to delete the object + * @param id the document id + * @param tenantId the tenant id + */ + protected DataObjectRequest(String index, String id, String tenantId) { + this.index = index; + this.id = id; + this.tenantId = tenantId; + } + + /** + * Returns the index + * @return the index + */ + public String index() { + return this.index; + } + + /** + * Sets the index + * @param index The new index to set + */ + public void index(String index) { + this.index = index; + } + + /** + * Returns the document id + * @return the id + */ + public String id() { + return this.id; + } + + /** + * Returns the tenant id + * @return the tenantId + */ + public String tenantId() { + return this.tenantId; + } + + /** + * Returns whether the subclass can be used in a {@link BulkDataObjectRequest} + * @return + */ + public abstract boolean isWriteRequest(); + + /** + * Superclass for common fields in subclass builders + */ + public static class Builder> { + protected String index = null; + protected String id = null; + protected String tenantId = null; + + /** + * Empty constructor to initialize + */ + protected Builder() {} + + /** + * Add an index to this builder + * @param index the index to put the object + * @return the updated builder + */ + public T index(String index) { + this.index = index; + return self(); + } + + /** + * Add an id to this builder + * @param id the document id + * @return the updated builder + */ + public T id(String id) { + this.id = id; + return self(); + } + + /** + * Add a tenant id to this builder + * @param tenantId the tenant id + * @return the updated builder + */ + public T tenantId(String tenantId) { + this.tenantId = tenantId; + return self(); + } + + /** + * Returns this builder as the parameterized type. + */ + @SuppressWarnings("unchecked") + protected T self() { + return (T) this; + } + } +} diff --git a/common/src/main/java/org/opensearch/sdk/DeleteDataObjectRequest.java b/common/src/main/java/org/opensearch/sdk/DeleteDataObjectRequest.java index 6dfed07293..4e4f6464f5 100644 --- a/common/src/main/java/org/opensearch/sdk/DeleteDataObjectRequest.java +++ b/common/src/main/java/org/opensearch/sdk/DeleteDataObjectRequest.java @@ -8,11 +8,7 @@ */ package org.opensearch.sdk; -public class DeleteDataObjectRequest { - - private final String index; - private final String id; - private final String tenantId; +public class DeleteDataObjectRequest extends DataObjectRequest { /** * Instantiate this request with an index and id. @@ -23,33 +19,12 @@ public class DeleteDataObjectRequest { * @param tenantId the tenant id */ public DeleteDataObjectRequest(String index, String id, String tenantId) { - this.index = index; - this.id = id; - this.tenantId = tenantId; + super(index, id, tenantId); } - /** - * Returns the index - * @return the index - */ - public String index() { - return this.index; - } - - /** - * Returns the document id - * @return the id - */ - public String id() { - return this.id; - } - - /** - * Returns the tenant id - * @return the tenantId - */ - public String tenantId() { - return this.tenantId; + @Override + public boolean isWriteRequest() { + return true; } /** @@ -63,45 +38,7 @@ public static Builder builder() { /** * Class for constructing a Builder for this Request Object */ - public static class Builder { - private String index = null; - private String id = null; - private String tenantId = null; - - /** - * Empty Constructor for the Builder object - */ - private Builder() {} - - /** - * Add an index to this builder - * @param index the index to put the object - * @return the updated builder - */ - public Builder index(String index) { - this.index = index; - return this; - } - - /** - * Add an id to this builder - * @param id the document id - * @return the updated builder - */ - public Builder id(String id) { - this.id = id; - return this; - } - - /** - * Add a tenant id to this builder - * @param tenantId the tenant id - * @return the updated builder - */ - public Builder tenantId(String tenantId) { - this.tenantId = tenantId; - return this; - } + public static class Builder extends DataObjectRequest.Builder { /** * Builds the object diff --git a/common/src/main/java/org/opensearch/sdk/GetDataObjectRequest.java b/common/src/main/java/org/opensearch/sdk/GetDataObjectRequest.java index 05d100d380..77165aa561 100644 --- a/common/src/main/java/org/opensearch/sdk/GetDataObjectRequest.java +++ b/common/src/main/java/org/opensearch/sdk/GetDataObjectRequest.java @@ -10,11 +10,8 @@ import org.opensearch.search.fetch.subphase.FetchSourceContext; -public class GetDataObjectRequest { +public class GetDataObjectRequest extends DataObjectRequest { - private final String index; - private final String id; - private final String tenantId; private final FetchSourceContext fetchSourceContext; /** @@ -27,36 +24,10 @@ public class GetDataObjectRequest { * @param fetchSourceContext the context to use when fetching _source */ public GetDataObjectRequest(String index, String id, String tenantId, FetchSourceContext fetchSourceContext) { - this.index = index; - this.id = id; - this.tenantId = tenantId; + super(index, id, tenantId); this.fetchSourceContext = fetchSourceContext; } - /** - * Returns the index - * @return the index - */ - public String index() { - return this.index; - } - - /** - * Returns the document id - * @return the id - */ - public String id() { - return this.id; - } - - /** - * Returns the tenant id - * @return the tenantId - */ - public String tenantId() { - return this.tenantId; - } - /** * Returns the context for fetching _source * @return the fetchSourceContext @@ -65,6 +36,11 @@ public FetchSourceContext fetchSourceContext() { return this.fetchSourceContext; } + @Override + public boolean isWriteRequest() { + return false; + } + /** * Instantiate a builder for this object * @return a builder instance @@ -76,47 +52,9 @@ public static Builder builder() { /** * Class for constructing a Builder for this Request Object */ - public static class Builder { - private String index = null; - private String id = null; - private String tenantId = null; + public static class Builder extends DataObjectRequest.Builder { private FetchSourceContext fetchSourceContext; - /** - * Empty Constructor for the Builder object - */ - private Builder() {} - - /** - * Add an index to this builder - * @param index the index to put the object - * @return the updated builder - */ - public Builder index(String index) { - this.index = index; - return this; - } - - /** - * Add an id to this builder - * @param id the document id - * @return the updated builder - */ - public Builder id(String id) { - this.id = id; - return this; - } - - /** - * Add a tenant id to this builder - * @param tenantId the tenant id - * @return the updated builder - */ - public Builder tenantId(String tenantId) { - this.tenantId = tenantId; - return this; - } - /** * Add a fetchSourceContext to this builder * @param fetchSourceContext the fetchSourceContext diff --git a/common/src/main/java/org/opensearch/sdk/PutDataObjectRequest.java b/common/src/main/java/org/opensearch/sdk/PutDataObjectRequest.java index ff06080abf..d77c86a514 100644 --- a/common/src/main/java/org/opensearch/sdk/PutDataObjectRequest.java +++ b/common/src/main/java/org/opensearch/sdk/PutDataObjectRequest.java @@ -9,16 +9,11 @@ package org.opensearch.sdk; import org.opensearch.core.xcontent.ToXContentObject; -import org.opensearch.core.xcontent.XContentBuilder; -import java.io.IOException; import java.util.Map; -public class PutDataObjectRequest { +public class PutDataObjectRequest extends DataObjectRequest { - private final String index; - private final String id; - private final String tenantId; private final boolean overwriteIfExists; private final ToXContentObject dataObject; @@ -30,37 +25,11 @@ public class PutDataObjectRequest { * @param dataObject the data object */ public PutDataObjectRequest(String index, String id, String tenantId, boolean overwriteIfExists, ToXContentObject dataObject) { - this.index = index; - this.id = id; - this.tenantId = tenantId; + super(index, id, tenantId); this.overwriteIfExists = overwriteIfExists; this.dataObject = dataObject; } - /** - * Returns the index - * @return the index - */ - public String index() { - return this.index; - } - - /** - * Returns the document id - * @return the id - */ - public String id() { - return this.id; - } - - /** - * Returns the tenant id - * @return the tenantId - */ - public String tenantId() { - return this.tenantId; - } - /** * Returns whether to overwrite an existing document (upsert) * @return true if this request should overwrite @@ -77,6 +46,11 @@ public ToXContentObject dataObject() { return this.dataObject; } + @Override + public boolean isWriteRequest() { + return true; + } + /** * Instantiate a builder for this object * @return a builder instance @@ -88,48 +62,10 @@ public static Builder builder() { /** * Class for constructing a Builder for this Request Object */ - public static class Builder { - private String index = null; - private String id = null; - private String tenantId = null; + public static class Builder extends DataObjectRequest.Builder { private boolean overwriteIfExists = true; private ToXContentObject dataObject = null; - /** - * Empty Constructor for the Builder object - */ - private Builder() {} - - /** - * Add an index to this builder - * @param index the index to put the object - * @return the updated builder - */ - public Builder index(String index) { - this.index = index; - return this; - } - - /** - * Add an id to this builder - * @param id the documet id - * @return the updated builder - */ - public Builder id(String id) { - this.id = id; - return this; - } - - /** - * Add a tenant id to this builder - * @param tenantId the tenant id - * @return the updated builder - */ - public Builder tenantId(String tenantId) { - this.tenantId = tenantId; - return this; - } - /** * Specify whether to overwrite an existing document/item (upsert). True by default. * @param overwriteIfExists whether to overwrite an existing document/item @@ -139,6 +75,7 @@ public Builder overwriteIfExists(boolean overwriteIfExists) { this.overwriteIfExists = overwriteIfExists; return this; } + /** * Add a data object to this builder * @param dataObject the data object diff --git a/common/src/main/java/org/opensearch/sdk/UpdateDataObjectRequest.java b/common/src/main/java/org/opensearch/sdk/UpdateDataObjectRequest.java index 65518b2755..b787fc50f8 100644 --- a/common/src/main/java/org/opensearch/sdk/UpdateDataObjectRequest.java +++ b/common/src/main/java/org/opensearch/sdk/UpdateDataObjectRequest.java @@ -15,11 +15,8 @@ import java.io.IOException; import java.util.Map; -public class UpdateDataObjectRequest { +public class UpdateDataObjectRequest extends DataObjectRequest { - private final String index; - private final String id; - private final String tenantId; private final Long ifSeqNo; private final Long ifPrimaryTerm; private final int retryOnConflict; @@ -46,39 +43,13 @@ public UpdateDataObjectRequest( int retryOnConflict, ToXContentObject dataObject ) { - this.index = index; - this.id = id; - this.tenantId = tenantId; + super(index, id, tenantId); this.ifSeqNo = ifSeqNo; this.ifPrimaryTerm = ifPrimaryTerm; this.retryOnConflict = retryOnConflict; this.dataObject = dataObject; } - /** - * Returns the index - * @return the index - */ - public String index() { - return this.index; - } - - /** - * Returns the document id - * @return the id - */ - public String id() { - return this.id; - } - - /** - * Returns the tenant id - * @return the tenantId - */ - public String tenantId() { - return this.tenantId; - } - /** * Returns the sequence number to match, or null if no match required * @return the ifSeqNo @@ -102,7 +73,7 @@ public Long ifPrimaryTerm() { public int retryOnConflict() { return retryOnConflict; } - + /** * Returns the data object * @return the data object @@ -111,6 +82,11 @@ public ToXContentObject dataObject() { return this.dataObject; } + @Override + public boolean isWriteRequest() { + return true; + } + /** * Instantiate a builder for this object * @return a builder instance @@ -122,50 +98,12 @@ public static Builder builder() { /** * Class for constructing a Builder for this Request Object */ - public static class Builder { - private String index = null; - private String id = null; - private String tenantId = null; + public static class Builder extends DataObjectRequest.Builder { private Long ifSeqNo = null; private Long ifPrimaryTerm = null; private int retryOnConflict = 0; private ToXContentObject dataObject = null; - /** - * Empty Constructor for the Builder object - */ - private Builder() {} - - /** - * Add an index to this builder - * @param index the index to put the object - * @return the updated builder - */ - public Builder index(String index) { - this.index = index; - return this; - } - - /** - * Add an id to this builder - * @param id the document id - * @return the updated builder - */ - public Builder id(String id) { - this.id = id; - return this; - } - - /** - * Add a tenant ID to this builder - * @param tenantId the tenant id - * @return the updated builder - */ - public Builder tenantId(String tenantId) { - this.tenantId = tenantId; - return this; - } - /** * Only perform this update request if the document's modification was assigned the given * sequence number. Must be used in combination with {@link #ifPrimaryTerm(long)} diff --git a/common/src/test/java/org/opensearch/sdk/PutDataObjectRequestTests.java b/common/src/test/java/org/opensearch/sdk/PutDataObjectRequestTests.java index 1377542203..5dec3bbc7a 100644 --- a/common/src/test/java/org/opensearch/sdk/PutDataObjectRequestTests.java +++ b/common/src/test/java/org/opensearch/sdk/PutDataObjectRequestTests.java @@ -14,6 +14,7 @@ import org.opensearch.common.xcontent.XContentHelper; import org.opensearch.common.xcontent.XContentType; import org.opensearch.core.common.bytes.BytesReference; +import org.opensearch.core.xcontent.MediaType; import org.opensearch.core.xcontent.ToXContent; import org.opensearch.core.xcontent.ToXContentObject; import org.opensearch.core.xcontent.XContentBuilder; @@ -79,7 +80,7 @@ public void testPutDataObjectRequestWithMap() throws IOException { contentBuilder.flush(); BytesReference bytes = BytesReference.bytes(contentBuilder); - Map resultingMap = XContentHelper.convertToMap(bytes, false, XContentType.JSON).v2(); + Map resultingMap = XContentHelper.convertToMap(bytes, false, (MediaType) XContentType.JSON).v2(); assertEquals(dataObjectMap, resultingMap); }