Skip to content

Commit 4fe9471

Browse files
Backport to branch(3) : Refactor operations and their builders (#2332)
Co-authored-by: Toshihiro Suzuki <[email protected]>
1 parent b689bba commit 4fe9471

28 files changed

+2056
-1413
lines changed

core/src/main/java/com/scalar/db/api/Delete.java

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
import com.scalar.db.io.Key;
99
import java.util.Optional;
1010
import javax.annotation.Nonnull;
11+
import javax.annotation.Nullable;
1112
import javax.annotation.concurrent.NotThreadSafe;
1213

1314
/**
@@ -18,6 +19,16 @@
1819
@NotThreadSafe
1920
public class Delete extends Mutation {
2021

22+
Delete(
23+
@Nullable String namespace,
24+
String tableName,
25+
Key partitionKey,
26+
@Nullable Key clusteringKey,
27+
@Nullable Consistency consistency,
28+
@Nullable MutationCondition condition) {
29+
super(namespace, tableName, partitionKey, clusteringKey, consistency, condition);
30+
}
31+
2132
/**
2233
* Constructs a {@code Delete} with the specified partition {@code Key}.
2334
*
@@ -69,7 +80,7 @@ public static Namespace newBuilder() {
6980

7081
/**
7182
* Build a {@code Delete} operation from an existing {@code Delete} object using a builder. The
72-
* builder will be parametrized by default with all the existing {@code Delete} attributes
83+
* builder will be parametrized by default with all the existing {@code Delete} parameters.
7384
*
7485
* @param delete an existing {@code Delete} operation
7586
* @return a {@code Delete} operation builder

core/src/main/java/com/scalar/db/api/DeleteBuilder.java

Lines changed: 2 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -92,16 +92,8 @@ public Buildable consistency(com.scalar.db.api.Consistency consistency) {
9292

9393
@Override
9494
public Delete build() {
95-
Delete delete = new Delete(partitionKey, clusteringKey);
96-
delete.forNamespace(namespaceName).forTable(tableName);
97-
if (condition != null) {
98-
delete.withCondition(condition);
99-
}
100-
if (consistency != null) {
101-
delete.withConsistency(consistency);
102-
}
103-
104-
return delete;
95+
return new Delete(
96+
namespaceName, tableName, partitionKey, clusteringKey, consistency, condition);
10597
}
10698
}
10799

core/src/main/java/com/scalar/db/api/Get.java

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@
33
import static com.google.common.base.Preconditions.checkNotNull;
44

55
import com.google.common.base.MoreObjects;
6+
import com.google.common.collect.ImmutableSet;
67
import com.scalar.db.api.GetBuilder.BuildableGetOrGetWithIndexFromExisting;
78
import com.scalar.db.api.GetBuilder.Namespace;
89
import com.scalar.db.io.Key;
910
import java.util.Collection;
11+
import java.util.List;
12+
import javax.annotation.Nullable;
1013
import javax.annotation.concurrent.NotThreadSafe;
1114

1215
/**
@@ -17,6 +20,18 @@
1720
@NotThreadSafe
1821
public class Get extends Selection {
1922

23+
Get(
24+
@Nullable String namespace,
25+
String tableName,
26+
Key partitionKey,
27+
@Nullable Key clusteringKey,
28+
@Nullable Consistency consistency,
29+
List<String> projections,
30+
ImmutableSet<Conjunction> conjunctions) {
31+
super(
32+
namespace, tableName, partitionKey, clusteringKey, consistency, projections, conjunctions);
33+
}
34+
2035
/**
2136
* Constructs a {@code Get} with the specified partition {@code Key}.
2237
*
@@ -68,7 +83,7 @@ public static Namespace newBuilder() {
6883

6984
/**
7085
* Build a {@code Get} operation from an existing {@code Get} object using a builder. The builder
71-
* will be parametrized by default with all the existing {@code Get} attributes
86+
* will be parametrized by default with all the existing {@code Get} parameters.
7287
*
7388
* @param get an existing {@code Get} operation
7489
* @return a {@code Get} operation builder
@@ -133,11 +148,6 @@ public Get withProjections(Collection<String> projections) {
133148
return (Get) super.withProjections(projections);
134149
}
135150

136-
@Override
137-
Get withConjunctions(Collection<Conjunction> conjunctions) {
138-
return (Get) super.withConjunctions(conjunctions);
139-
}
140-
141151
/**
142152
* Indicates whether some other object is "equal to" this object. The other object is considered
143153
* equal if:

core/src/main/java/com/scalar/db/api/GetBuilder.java

Lines changed: 36 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import static com.google.common.base.Preconditions.checkNotNull;
44

5+
import com.google.common.collect.ImmutableSet;
56
import com.scalar.db.api.OperationBuilder.And;
67
import com.scalar.db.api.OperationBuilder.Buildable;
78
import com.scalar.db.api.OperationBuilder.ClearClusteringKey;
@@ -135,15 +136,18 @@ public BuildableGet consistency(com.scalar.db.api.Consistency consistency) {
135136

136137
@Override
137138
public Get build() {
138-
Get get = new Get(partitionKey, clusteringKey);
139-
get.forNamespace(namespaceName).forTable(tableName);
140-
if (!projections.isEmpty()) {
141-
get.withProjections(projections);
142-
}
143-
if (consistency != null) {
144-
get.withConsistency(consistency);
145-
}
146-
return get;
139+
return build(ImmutableSet.of());
140+
}
141+
142+
private Get build(ImmutableSet<Conjunction> conjunctions) {
143+
return new Get(
144+
namespaceName,
145+
tableName,
146+
partitionKey,
147+
clusteringKey,
148+
consistency,
149+
projections,
150+
conjunctions);
147151
}
148152
}
149153

@@ -341,7 +345,7 @@ private BuildableGetWithWhere(BuildableGetWithOngoingWhere buildable) {
341345

342346
@Override
343347
public Get build() {
344-
return (Get) addConjunctionsTo(super.build(), where);
348+
return super.build(getConjunctions(where));
345349
}
346350
}
347351

@@ -420,15 +424,12 @@ public BuildableGetWithIndexOngoingWhereOr whereOr(Set<AndConditionSet> andCondi
420424
}
421425

422426
public Get build() {
423-
GetWithIndex getWithIndex = new GetWithIndex(indexKey);
424-
getWithIndex.forNamespace(namespaceName).forTable(tableName);
425-
if (!projections.isEmpty()) {
426-
getWithIndex.withProjections(projections);
427-
}
428-
if (consistency != null) {
429-
getWithIndex.withConsistency(consistency);
430-
}
431-
return getWithIndex;
427+
return build(ImmutableSet.of());
428+
}
429+
430+
private Get build(ImmutableSet<Conjunction> conjunctions) {
431+
return new GetWithIndex(
432+
namespaceName, tableName, indexKey, consistency, projections, conjunctions);
432433
}
433434
}
434435

@@ -583,7 +584,7 @@ public BuildableGetWithIndexWhere consistency(com.scalar.db.api.Consistency cons
583584
}
584585

585586
public Get build() {
586-
return (Get) addConjunctionsTo(buildableGetWithIndex.build(), where);
587+
return buildableGetWithIndex.build(getConjunctions(where));
587588
}
588589
}
589590

@@ -771,28 +772,24 @@ private void checkConditionsEmpty() {
771772

772773
@Override
773774
public Get build() {
774-
Get get;
775+
return build(
776+
conjunctions.stream().map(Conjunction::of).collect(ImmutableSet.toImmutableSet()));
777+
}
775778

779+
private Get build(ImmutableSet<Conjunction> conjunctions) {
776780
if (isGetWithIndex) {
777-
get = new GetWithIndex(indexKey);
781+
return new GetWithIndex(
782+
namespaceName, tableName, indexKey, consistency, projections, conjunctions);
778783
} else {
779-
get = new Get(partitionKey, clusteringKey);
780-
}
781-
782-
if (!conjunctions.isEmpty()) {
783-
get.withConjunctions(
784-
conjunctions.stream().map(Conjunction::of).collect(Collectors.toSet()));
784+
return new Get(
785+
namespaceName,
786+
tableName,
787+
partitionKey,
788+
clusteringKey,
789+
consistency,
790+
projections,
791+
conjunctions);
785792
}
786-
787-
get.forNamespace(namespaceName).forTable(tableName);
788-
if (!projections.isEmpty()) {
789-
get.withProjections(projections);
790-
}
791-
if (consistency != null) {
792-
get.withConsistency(consistency);
793-
}
794-
795-
return get;
796793
}
797794
}
798795

@@ -893,7 +890,7 @@ public BuildableGetFromExistingWithWhere clearNamespace() {
893890
}
894891

895892
public Get build() {
896-
return (Get) addConjunctionsTo(BuildableGetFromExisting.build(), where);
893+
return BuildableGetFromExisting.build(getConjunctions(where));
897894
}
898895
}
899896

core/src/main/java/com/scalar/db/api/GetWithIndex.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,27 @@
11
package com.scalar.db.api;
22

3+
import com.google.common.collect.ImmutableSet;
34
import com.scalar.db.io.Key;
45
import java.util.Collection;
6+
import java.util.List;
57
import java.util.Objects;
8+
import javax.annotation.Nullable;
69
import javax.annotation.concurrent.NotThreadSafe;
710

811
/** A command to retrieve an entry from the underlying storage by using an index. */
912
@NotThreadSafe
1013
public class GetWithIndex extends Get {
1114

15+
GetWithIndex(
16+
@Nullable String namespace,
17+
String tableName,
18+
Key indexKey,
19+
@Nullable Consistency consistency,
20+
List<String> projections,
21+
ImmutableSet<Conjunction> conjunctions) {
22+
super(namespace, tableName, indexKey, null, consistency, projections, conjunctions);
23+
}
24+
1225
/**
1326
* Constructs an {@code GetWithIndex} with the specified index {@code Key}.
1427
*

core/src/main/java/com/scalar/db/api/Insert.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,16 +17,16 @@
1717
@NotThreadSafe
1818
public class Insert extends Mutation {
1919

20-
private final Map<String, Column<?>> columns;
20+
private final ImmutableMap<String, Column<?>> columns;
2121

2222
Insert(
2323
@Nullable String namespace,
2424
String tableName,
2525
Key partitionKey,
2626
@Nullable Key clusteringKey,
27-
Map<String, Column<?>> columns) {
28-
super(namespace, tableName, partitionKey, clusteringKey, null);
29-
this.columns = ImmutableMap.copyOf(columns);
27+
ImmutableMap<String, Column<?>> columns) {
28+
super(namespace, tableName, partitionKey, clusteringKey, null, null);
29+
this.columns = columns;
3030
}
3131

3232
public Map<String, Column<?>> getColumns() {
@@ -120,7 +120,7 @@ public static InsertBuilder.Namespace newBuilder() {
120120

121121
/**
122122
* Build a {@code Insert} operation from an existing {@code Insert} object using a builder. The
123-
* builder will be parametrized by default with all the existing {@code Insert} object attributes.
123+
* builder will be parametrized by default with all the existing {@code Insert} parameters.
124124
*
125125
* @param insert an existing {@code Insert} operation
126126
* @return a {@code Insert} operation builder

core/src/main/java/com/scalar/db/api/InsertBuilder.java

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

33
import static com.google.common.base.Preconditions.checkNotNull;
44

5+
import com.google.common.collect.ImmutableMap;
56
import com.scalar.db.api.OperationBuilder.ClearClusteringKey;
67
import com.scalar.db.api.OperationBuilder.ClearNamespace;
78
import com.scalar.db.api.OperationBuilder.ClearValues;
@@ -186,7 +187,8 @@ public Buildable value(Column<?> column) {
186187

187188
@Override
188189
public Insert build() {
189-
return new Insert(namespaceName, tableName, partitionKey, clusteringKey, columns);
190+
return new Insert(
191+
namespaceName, tableName, partitionKey, clusteringKey, ImmutableMap.copyOf(columns));
190192
}
191193
}
192194

core/src/main/java/com/scalar/db/api/Mutation.java

Lines changed: 17 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,18 @@
1818
public abstract class Mutation extends Operation {
1919

2020
/** @deprecated As of release 3.13.0. Will be removed in release 5.0.0. */
21-
@Deprecated private Optional<MutationCondition> condition;
21+
@Deprecated @Nullable private MutationCondition condition;
22+
23+
Mutation(
24+
@Nullable String namespace,
25+
String tableName,
26+
Key partitionKey,
27+
@Nullable Key clusteringKey,
28+
@Nullable Consistency consistency,
29+
@Nullable MutationCondition condition) {
30+
super(namespace, tableName, partitionKey, clusteringKey, consistency);
31+
this.condition = condition;
32+
}
2233

2334
/**
2435
* @param partitionKey a partition key
@@ -28,7 +39,7 @@ public abstract class Mutation extends Operation {
2839
@Deprecated
2940
public Mutation(Key partitionKey, Key clusteringKey) {
3041
super(partitionKey, clusteringKey);
31-
condition = Optional.empty();
42+
condition = null;
3243
}
3344

3445
/**
@@ -48,7 +59,7 @@ public Mutation(Mutation mutation) {
4859
@Nullable Key clusteringKey,
4960
@Nullable MutationCondition condition) {
5061
super(namespace, tableName, partitionKey, clusteringKey);
51-
this.condition = Optional.ofNullable(condition);
62+
this.condition = condition;
5263
}
5364

5465
/**
@@ -60,7 +71,7 @@ public Mutation(Mutation mutation) {
6071
@Deprecated
6172
@Nonnull
6273
public Optional<MutationCondition> getCondition() {
63-
return condition;
74+
return Optional.ofNullable(condition);
6475
}
6576

6677
/**
@@ -72,7 +83,7 @@ public Optional<MutationCondition> getCondition() {
7283
*/
7384
@Deprecated
7485
public Mutation withCondition(MutationCondition condition) {
75-
this.condition = Optional.ofNullable(condition);
86+
this.condition = condition;
7687
return this;
7788
}
7889

@@ -101,7 +112,7 @@ public boolean equals(Object o) {
101112
return false;
102113
}
103114
Mutation other = (Mutation) o;
104-
return condition.equals(other.condition);
115+
return Objects.equals(condition, other.condition);
105116
}
106117

107118
@Override

0 commit comments

Comments
 (0)