-
Notifications
You must be signed in to change notification settings - Fork 37
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
Make Cosmos DTO objects immutable #1013
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,52 +2,58 @@ | |
|
||
import com.google.common.base.MoreObjects; | ||
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings; | ||
import java.util.Collections; | ||
import java.util.LinkedHashSet; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import java.util.Set; | ||
import javax.annotation.concurrent.NotThreadSafe; | ||
import javax.annotation.Nullable; | ||
import javax.annotation.concurrent.Immutable; | ||
|
||
/** | ||
* A metadata class for a table of ScalarDB to know the type of each column | ||
* | ||
* @author Yuji Ito | ||
*/ | ||
@SuppressFBWarnings({"EI_EXPOSE_REP", "EI_EXPOSE_REP2"}) | ||
@NotThreadSafe | ||
@Immutable | ||
public class CosmosTableMetadata { | ||
private String id; | ||
private LinkedHashSet<String> partitionKeyNames; | ||
private LinkedHashSet<String> clusteringKeyNames; | ||
private Map<String, String> clusteringOrders; | ||
private Set<String> secondaryIndexNames; | ||
private Map<String, String> columns; | ||
|
||
public CosmosTableMetadata() {} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public void setPartitionKeyNames(List<String> partitionKeyNames) { | ||
this.partitionKeyNames = new LinkedHashSet<>(partitionKeyNames); | ||
} | ||
|
||
public void setClusteringKeyNames(List<String> clusteringKeyNames) { | ||
this.clusteringKeyNames = new LinkedHashSet<>(clusteringKeyNames); | ||
} | ||
|
||
public void setClusteringOrders(Map<String, String> clusteringOrders) { | ||
this.clusteringOrders = clusteringOrders; | ||
private final String id; | ||
private final LinkedHashSet<String> partitionKeyNames; | ||
private final LinkedHashSet<String> clusteringKeyNames; | ||
private final Map<String, String> clusteringOrders; | ||
private final Set<String> secondaryIndexNames; | ||
private final Map<String, String> columns; | ||
|
||
public CosmosTableMetadata() { | ||
this(null, null, null, null, null, null); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This constructor is used only in unit tests ( There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Jackson requires a default constructor so we need it not only for testing purpose. I will add a comment. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Where are we using Jackson databind for CosmosTableMetadata? I want to reproduce it and look into it as I'm a bit familiar with Jackson databind. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ooops. Sorry, I missed the background.
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. JFYI: The following might be an option to work with Jackson databind internally used in Cosmos SDK, although it would make things a bit complicated.
public CosmosTableMetadata(
@JsonProperty("id")
@Nullable String id,
@JsonProperty("partitionKeyNames")
@Nullable LinkedHashSet<String> partitionKeyNames,
@JsonProperty("clusteringKeyNames")
@Nullable LinkedHashSet<String> clusteringKeyNames,
@JsonProperty("clusteringOrders")
@Nullable Map<String, String> clusteringOrders,
@JsonProperty("secondaryIndexNames")
@Nullable Set<String> secondaryIndexNames,
@JsonProperty("columns")
@Nullable Map<String, String> columns) {
this.id = id != null ? id : "";
this.partitionKeyNames = partitionKeyNames != null ? partitionKeyNames : new LinkedHashSet<>();
...
} I'm wondering why Cosmos SDK doesn't use https://github.com/FasterXML/jackson-modules-java8/tree/master/parameter-names or equivalent on their side... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Thanks for sharing it. |
||
} | ||
|
||
public void setSecondaryIndexNames(Set<String> secondaryIndexNames) { | ||
this.secondaryIndexNames = secondaryIndexNames; | ||
public CosmosTableMetadata( | ||
@Nullable String id, | ||
@Nullable LinkedHashSet<String> partitionKeyNames, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This can be declared as an interface There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah no, we can't do that because the insertion order is important for this. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I see. Understood. (Another option might be simply using |
||
@Nullable LinkedHashSet<String> clusteringKeyNames, | ||
@Nullable Map<String, String> clusteringOrders, | ||
@Nullable Set<String> secondaryIndexNames, | ||
@Nullable Map<String, String> columns) { | ||
this.id = id != null ? id : ""; | ||
this.partitionKeyNames = partitionKeyNames != null ? partitionKeyNames : new LinkedHashSet<>(); | ||
this.clusteringKeyNames = | ||
clusteringKeyNames != null ? clusteringKeyNames : new LinkedHashSet<>(); | ||
this.clusteringOrders = clusteringOrders != null ? clusteringOrders : Collections.emptyMap(); | ||
this.secondaryIndexNames = | ||
secondaryIndexNames != null ? secondaryIndexNames : Collections.emptySet(); | ||
this.columns = columns != null ? columns : Collections.emptyMap(); | ||
} | ||
|
||
public void setColumns(Map<String, String> columns) { | ||
this.columns = columns; | ||
private CosmosTableMetadata(Builder builder) { | ||
this( | ||
builder.id, | ||
builder.partitionKeyNames, | ||
builder.clusteringKeyNames, | ||
builder.clusteringOrders, | ||
builder.secondaryIndexNames, | ||
builder.columns); | ||
} | ||
|
||
public String getId() { | ||
|
@@ -108,4 +114,54 @@ public String toString() { | |
.add("columns", columns) | ||
.toString(); | ||
} | ||
|
||
public static Builder newBuilder() { | ||
return new Builder(); | ||
} | ||
|
||
public static final class Builder { | ||
|
||
private String id; | ||
private LinkedHashSet<String> partitionKeyNames; | ||
komamitsu marked this conversation as resolved.
Show resolved
Hide resolved
|
||
private LinkedHashSet<String> clusteringKeyNames; | ||
private Map<String, String> clusteringOrders; | ||
private Set<String> secondaryIndexNames; | ||
private Map<String, String> columns; | ||
|
||
private Builder() {} | ||
|
||
public Builder id(String val) { | ||
id = val; | ||
return this; | ||
} | ||
|
||
public Builder partitionKeyNames(LinkedHashSet<String> val) { | ||
partitionKeyNames = val; | ||
return this; | ||
} | ||
|
||
public Builder clusteringKeyNames(LinkedHashSet<String> val) { | ||
clusteringKeyNames = val; | ||
return this; | ||
} | ||
|
||
public Builder clusteringOrders(Map<String, String> val) { | ||
clusteringOrders = val; | ||
return this; | ||
} | ||
|
||
public Builder secondaryIndexNames(Set<String> val) { | ||
secondaryIndexNames = val; | ||
return this; | ||
} | ||
|
||
public Builder columns(Map<String, String> val) { | ||
columns = val; | ||
return this; | ||
} | ||
|
||
public CosmosTableMetadata build() { | ||
return new CosmosTableMetadata(this); | ||
} | ||
} | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,42 +4,39 @@ | |
import java.util.Collections; | ||
import java.util.Map; | ||
import java.util.Objects; | ||
import javax.annotation.concurrent.NotThreadSafe; | ||
import javax.annotation.Nullable; | ||
import javax.annotation.concurrent.Immutable; | ||
|
||
/** | ||
* A record class that Cosmos DB uses for storing a document based on ScalarDB data model. | ||
* | ||
* @author Yuji Ito | ||
*/ | ||
@SuppressFBWarnings({"EI_EXPOSE_REP", "EI_EXPOSE_REP2"}) | ||
@NotThreadSafe | ||
@Immutable | ||
public class Record { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Likewise |
||
private String id = ""; | ||
private String concatenatedPartitionKey = ""; | ||
private Map<String, Object> partitionKey = Collections.emptyMap(); | ||
private Map<String, Object> clusteringKey = Collections.emptyMap(); | ||
private Map<String, Object> values = Collections.emptyMap(); | ||
|
||
public Record() {} | ||
|
||
public void setId(String id) { | ||
this.id = id; | ||
} | ||
|
||
public void setConcatenatedPartitionKey(String concatenatedPartitionKey) { | ||
this.concatenatedPartitionKey = concatenatedPartitionKey; | ||
} | ||
|
||
public void setPartitionKey(Map<String, Object> partitionKey) { | ||
this.partitionKey = partitionKey; | ||
} | ||
|
||
public void setClusteringKey(Map<String, Object> clusteringKey) { | ||
this.clusteringKey = clusteringKey; | ||
private final String id; | ||
private final String concatenatedPartitionKey; | ||
private final Map<String, Object> partitionKey; | ||
private final Map<String, Object> clusteringKey; | ||
private final Map<String, Object> values; | ||
|
||
public Record() { | ||
this(null, null, null, null, null); | ||
} | ||
|
||
public void setValues(Map<String, Object> values) { | ||
this.values = values; | ||
public Record( | ||
@Nullable String id, | ||
@Nullable String concatenatedPartitionKey, | ||
@Nullable Map<String, Object> partitionKey, | ||
@Nullable Map<String, Object> clusteringKey, | ||
@Nullable Map<String, Object> values) { | ||
this.id = id != null ? id : ""; | ||
this.concatenatedPartitionKey = | ||
concatenatedPartitionKey != null ? concatenatedPartitionKey : ""; | ||
this.partitionKey = partitionKey != null ? partitionKey : Collections.emptyMap(); | ||
this.clusteringKey = clusteringKey != null ? clusteringKey : Collections.emptyMap(); | ||
this.values = values != null ? values : Collections.emptyMap(); | ||
} | ||
|
||
public String getId() { | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Remove setters and make attributes final here