Skip to content

Commit

Permalink
Create DataObjectRequest superclass
Browse files Browse the repository at this point in the history
Signed-off-by: Daniel Widdis <[email protected]>
  • Loading branch information
dbwiddis committed Oct 29, 2024
1 parent ff38d30 commit caeea21
Show file tree
Hide file tree
Showing 6 changed files with 154 additions and 283 deletions.
120 changes: 120 additions & 0 deletions common/src/main/java/org/opensearch/sdk/DataObjectRequest.java
Original file line number Diff line number Diff line change
@@ -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.
* <p>
* 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<T extends Builder<T>> {
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -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;
}

/**
Expand All @@ -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<Builder> {

/**
* Builds the object
Expand Down
78 changes: 8 additions & 70 deletions common/src/main/java/org/opensearch/sdk/GetDataObjectRequest.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/**
Expand All @@ -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
Expand All @@ -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
Expand All @@ -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<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
Expand Down
Loading

0 comments on commit caeea21

Please sign in to comment.