Skip to content

Commit 0de9149

Browse files
committed
Apply suggestions
1 parent 533f3cd commit 0de9149

File tree

7 files changed

+351
-242
lines changed

7 files changed

+351
-242
lines changed

core/src/main/java/com/scalar/db/storage/objectstorage/MutateStatementHandler.java

Lines changed: 19 additions & 101 deletions
Original file line numberDiff line numberDiff line change
@@ -2,18 +2,12 @@
22

33
import com.fasterxml.jackson.core.type.TypeReference;
44
import com.scalar.db.api.Delete;
5-
import com.scalar.db.api.DeleteIf;
6-
import com.scalar.db.api.DeleteIfExists;
75
import com.scalar.db.api.Mutation;
86
import com.scalar.db.api.Put;
9-
import com.scalar.db.api.PutIf;
10-
import com.scalar.db.api.PutIfExists;
11-
import com.scalar.db.api.PutIfNotExists;
127
import com.scalar.db.api.TableMetadata;
138
import com.scalar.db.common.CoreError;
149
import com.scalar.db.common.TableMetadataManager;
1510
import com.scalar.db.exception.storage.ExecutionException;
16-
import com.scalar.db.exception.storage.NoMutationException;
1711
import com.scalar.db.exception.storage.RetriableExecutionException;
1812
import java.util.ArrayList;
1913
import java.util.Collections;
@@ -66,94 +60,19 @@ private void mutate(
6660
String namespaceName, String tableName, String partitionKey, List<Mutation> mutations)
6761
throws ExecutionException {
6862
Map<PartitionIdentifier, String> readVersionMap = new HashMap<>();
69-
Map<String, ObjectStorageRecord> partition =
63+
ObjectStoragePartition partition =
7064
getPartition(namespaceName, tableName, partitionKey, readVersionMap);
7165
for (Mutation mutation : mutations) {
7266
if (mutation instanceof Put) {
73-
putInternal(partition, (Put) mutation);
67+
partition.applyPut((Put) mutation, metadataManager.getTableMetadata(mutation));
7468
} else {
7569
assert mutation instanceof Delete;
76-
deleteInternal(partition, (Delete) mutation);
70+
partition.applyDelete((Delete) mutation, metadataManager.getTableMetadata(mutation));
7771
}
7872
}
7973
applyPartitionWrite(namespaceName, tableName, partitionKey, partition, readVersionMap);
8074
}
8175

82-
private void putInternal(Map<String, ObjectStorageRecord> partition, Put put)
83-
throws ExecutionException {
84-
TableMetadata tableMetadata = metadataManager.getTableMetadata(put);
85-
ObjectStorageMutation mutation = new ObjectStorageMutation(put, tableMetadata);
86-
if (!put.getCondition().isPresent()) {
87-
ObjectStorageRecord existingRecord = partition.get(mutation.getRecordId());
88-
if (existingRecord == null) {
89-
partition.put(mutation.getRecordId(), mutation.makeRecord());
90-
} else {
91-
partition.put(mutation.getRecordId(), mutation.makeRecord(existingRecord));
92-
}
93-
} else if (put.getCondition().get() instanceof PutIfNotExists) {
94-
if (partition.containsKey(mutation.getRecordId())) {
95-
throw new NoMutationException(
96-
CoreError.NO_MUTATION_APPLIED.buildMessage(), Collections.singletonList(put));
97-
}
98-
partition.put(mutation.getRecordId(), mutation.makeRecord());
99-
} else if (put.getCondition().get() instanceof PutIfExists) {
100-
ObjectStorageRecord existingRecord = partition.get(mutation.getRecordId());
101-
if (existingRecord == null) {
102-
throw new NoMutationException(
103-
CoreError.NO_MUTATION_APPLIED.buildMessage(), Collections.singletonList(put));
104-
}
105-
partition.put(mutation.getRecordId(), mutation.makeRecord(existingRecord));
106-
} else {
107-
assert put.getCondition().get() instanceof PutIf;
108-
ObjectStorageRecord existingRecord = partition.get(mutation.getRecordId());
109-
if (existingRecord == null) {
110-
throw new NoMutationException(
111-
CoreError.NO_MUTATION_APPLIED.buildMessage(), Collections.singletonList(put));
112-
}
113-
try {
114-
validateConditions(
115-
partition.get(mutation.getRecordId()),
116-
put.getCondition().get().getExpressions(),
117-
metadataManager.getTableMetadata(mutation.getOperation()));
118-
} catch (ExecutionException e) {
119-
throw new NoMutationException(
120-
CoreError.NO_MUTATION_APPLIED.buildMessage(), Collections.singletonList(put), e);
121-
}
122-
partition.put(mutation.getRecordId(), mutation.makeRecord(existingRecord));
123-
}
124-
}
125-
126-
private void deleteInternal(Map<String, ObjectStorageRecord> partition, Delete delete)
127-
throws ExecutionException {
128-
TableMetadata tableMetadata = metadataManager.getTableMetadata(delete);
129-
ObjectStorageMutation mutation = new ObjectStorageMutation(delete, tableMetadata);
130-
if (!delete.getCondition().isPresent()) {
131-
partition.remove(mutation.getRecordId());
132-
} else if (delete.getCondition().get() instanceof DeleteIfExists) {
133-
if (!partition.containsKey(mutation.getRecordId())) {
134-
throw new NoMutationException(
135-
CoreError.NO_MUTATION_APPLIED.buildMessage(), Collections.singletonList(delete));
136-
}
137-
partition.remove(mutation.getRecordId());
138-
} else {
139-
assert delete.getCondition().get() instanceof DeleteIf;
140-
if (!partition.containsKey(mutation.getRecordId())) {
141-
throw new NoMutationException(
142-
CoreError.NO_MUTATION_APPLIED.buildMessage(), Collections.singletonList(delete));
143-
}
144-
try {
145-
validateConditions(
146-
partition.get(mutation.getRecordId()),
147-
delete.getCondition().get().getExpressions(),
148-
metadataManager.getTableMetadata(mutation.getOperation()));
149-
} catch (ExecutionException e) {
150-
throw new NoMutationException(
151-
CoreError.NO_MUTATION_APPLIED.buildMessage(), Collections.singletonList(delete), e);
152-
}
153-
partition.remove(mutation.getRecordId());
154-
}
155-
}
156-
15776
/**
15877
* Applies the partition write.
15978
*
@@ -168,13 +87,11 @@ private void applyPartitionWrite(
16887
String namespaceName,
16988
String tableName,
17089
String partitionKey,
171-
Map<String, ObjectStorageRecord> partition,
90+
ObjectStoragePartition partition,
17291
Map<PartitionIdentifier, String> readVersionMap)
17392
throws ExecutionException {
174-
if (readVersionMap.containsKey(
175-
PartitionIdentifier.of(namespaceName, tableName, partitionKey))) {
176-
String readVersion =
177-
readVersionMap.get(PartitionIdentifier.of(namespaceName, tableName, partitionKey));
93+
if (readVersionMap.containsKey(partition.getPartitionIdentifier())) {
94+
String readVersion = readVersionMap.get(partition.getPartitionIdentifier());
17895
if (!partition.isEmpty()) {
17996
updatePartition(namespaceName, tableName, partitionKey, partition, readVersion);
18097
} else {
@@ -197,7 +114,7 @@ private void applyPartitionWrite(
197114
* @return the partition
198115
* @throws ExecutionException if a failure occurs during the operation
199116
*/
200-
private Map<String, ObjectStorageRecord> getPartition(
117+
private ObjectStoragePartition getPartition(
201118
String namespaceName,
202119
String tableName,
203120
String partitionKey,
@@ -207,13 +124,17 @@ private Map<String, ObjectStorageRecord> getPartition(
207124
try {
208125
Optional<ObjectStorageWrapperResponse> response = wrapper.get(objectKey);
209126
if (!response.isPresent()) {
210-
return new HashMap<>();
127+
return ObjectStoragePartition.newBuilder()
128+
.namespaceName(namespaceName)
129+
.tableName(tableName)
130+
.partitionKey(partitionKey)
131+
.build();
211132
}
212-
readVersionMap.put(
213-
PartitionIdentifier.of(namespaceName, tableName, partitionKey),
214-
response.get().getVersion());
215-
return Serializer.deserialize(
216-
response.get().getPayload(), new TypeReference<Map<String, ObjectStorageRecord>>() {});
133+
ObjectStoragePartition partition =
134+
Serializer.deserialize(
135+
response.get().getPayload(), new TypeReference<ObjectStoragePartition>() {});
136+
readVersionMap.put(partition.getPartitionIdentifier(), response.get().getVersion());
137+
return partition;
217138
} catch (ObjectStorageWrapperException e) {
218139
throw new ExecutionException(
219140
CoreError.OBJECT_STORAGE_ERROR_OCCURRED_IN_MUTATION.buildMessage(e.getMessage()), e);
@@ -231,10 +152,7 @@ private Map<String, ObjectStorageRecord> getPartition(
231152
* @throws ExecutionException if a failure occurs during the operation
232153
*/
233154
private void insertPartition(
234-
String namespaceName,
235-
String tableName,
236-
String partitionKey,
237-
Map<String, ObjectStorageRecord> partition)
155+
String namespaceName, String tableName, String partitionKey, ObjectStoragePartition partition)
238156
throws ExecutionException {
239157
try {
240158
wrapper.insert(
@@ -264,7 +182,7 @@ private void updatePartition(
264182
String namespaceName,
265183
String tableName,
266184
String partitionKey,
267-
Map<String, ObjectStorageRecord> partition,
185+
ObjectStoragePartition partition,
268186
String readVersion)
269187
throws ExecutionException {
270188
try {
Lines changed: 160 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,160 @@
1+
package com.scalar.db.storage.objectstorage;
2+
3+
import static com.scalar.db.storage.objectstorage.StatementHandler.validateConditions;
4+
5+
import com.fasterxml.jackson.annotation.JsonCreator;
6+
import com.fasterxml.jackson.annotation.JsonProperty;
7+
import com.scalar.db.api.Delete;
8+
import com.scalar.db.api.DeleteIf;
9+
import com.scalar.db.api.DeleteIfExists;
10+
import com.scalar.db.api.Put;
11+
import com.scalar.db.api.PutIf;
12+
import com.scalar.db.api.PutIfExists;
13+
import com.scalar.db.api.PutIfNotExists;
14+
import com.scalar.db.api.TableMetadata;
15+
import com.scalar.db.common.CoreError;
16+
import com.scalar.db.exception.storage.ExecutionException;
17+
import com.scalar.db.exception.storage.NoMutationException;
18+
import java.util.Collections;
19+
import java.util.HashMap;
20+
import java.util.Map;
21+
import java.util.Optional;
22+
import javax.annotation.Nullable;
23+
24+
public class ObjectStoragePartition {
25+
private final String namespaceName;
26+
private final String tableName;
27+
private final String partitionKey;
28+
private final Map<String, ObjectStorageRecord> records;
29+
30+
@JsonCreator
31+
public ObjectStoragePartition(
32+
@JsonProperty("namespaceName") @Nullable String namespaceName,
33+
@JsonProperty("tableName") @Nullable String tableName,
34+
@JsonProperty("partitionKey") @Nullable String partitionKey,
35+
@JsonProperty("records") @Nullable Map<String, ObjectStorageRecord> records) {
36+
this.namespaceName = namespaceName != null ? namespaceName : "";
37+
this.tableName = tableName != null ? tableName : "";
38+
this.partitionKey = partitionKey != null ? partitionKey : "";
39+
this.records = records != null ? records : new HashMap<>();
40+
}
41+
42+
public static ObjectStoragePartition.Builder newBuilder() {
43+
return new ObjectStoragePartition.Builder();
44+
}
45+
46+
public PartitionIdentifier getPartitionIdentifier() {
47+
return PartitionIdentifier.of(namespaceName, tableName, partitionKey);
48+
}
49+
50+
public Optional<ObjectStorageRecord> getRecord(String recordId) {
51+
return Optional.ofNullable(records.get(recordId));
52+
}
53+
54+
public Map<String, ObjectStorageRecord> getRecords() {
55+
return records;
56+
}
57+
58+
public boolean isEmpty() {
59+
return records.isEmpty();
60+
}
61+
62+
public void applyPut(Put put, TableMetadata tableMetadata) throws NoMutationException {
63+
ObjectStorageMutation mutation = new ObjectStorageMutation(put, tableMetadata);
64+
if (!put.getCondition().isPresent()) {
65+
if (!records.containsKey(mutation.getRecordId())) {
66+
records.put(mutation.getRecordId(), mutation.makeRecord());
67+
} else {
68+
records.compute(
69+
mutation.getRecordId(), (id, existingRecord) -> mutation.makeRecord(existingRecord));
70+
}
71+
} else if (put.getCondition().get() instanceof PutIfNotExists) {
72+
if (records.containsKey(mutation.getRecordId())) {
73+
throw new NoMutationException(
74+
CoreError.NO_MUTATION_APPLIED.buildMessage(), Collections.singletonList(put));
75+
}
76+
records.put(mutation.getRecordId(), mutation.makeRecord());
77+
} else if (put.getCondition().get() instanceof PutIfExists) {
78+
if (!records.containsKey(mutation.getRecordId())) {
79+
throw new NoMutationException(
80+
CoreError.NO_MUTATION_APPLIED.buildMessage(), Collections.singletonList(put));
81+
}
82+
records.compute(
83+
mutation.getRecordId(), (id, existingRecord) -> mutation.makeRecord(existingRecord));
84+
} else {
85+
assert put.getCondition().get() instanceof PutIf;
86+
if (!records.containsKey(mutation.getRecordId())) {
87+
throw new NoMutationException(
88+
CoreError.NO_MUTATION_APPLIED.buildMessage(), Collections.singletonList(put));
89+
}
90+
ObjectStorageRecord existingRecord = records.get(mutation.getRecordId());
91+
try {
92+
validateConditions(
93+
existingRecord, put.getCondition().get().getExpressions(), tableMetadata);
94+
} catch (ExecutionException e) {
95+
throw new NoMutationException(
96+
CoreError.NO_MUTATION_APPLIED.buildMessage(), Collections.singletonList(put), e);
97+
}
98+
records.put(mutation.getRecordId(), mutation.makeRecord(existingRecord));
99+
}
100+
}
101+
102+
public void applyDelete(Delete delete, TableMetadata tableMetadata) throws NoMutationException {
103+
ObjectStorageMutation mutation = new ObjectStorageMutation(delete, tableMetadata);
104+
if (!delete.getCondition().isPresent()) {
105+
records.remove(mutation.getRecordId());
106+
} else if (delete.getCondition().get() instanceof DeleteIfExists) {
107+
if (!records.containsKey(mutation.getRecordId())) {
108+
throw new NoMutationException(
109+
CoreError.NO_MUTATION_APPLIED.buildMessage(), Collections.singletonList(delete));
110+
}
111+
records.remove(mutation.getRecordId());
112+
} else {
113+
assert delete.getCondition().get() instanceof DeleteIf;
114+
if (!records.containsKey(mutation.getRecordId())) {
115+
throw new NoMutationException(
116+
CoreError.NO_MUTATION_APPLIED.buildMessage(), Collections.singletonList(delete));
117+
}
118+
ObjectStorageRecord existingRecord = records.get(mutation.getRecordId());
119+
try {
120+
validateConditions(
121+
existingRecord, delete.getCondition().get().getExpressions(), tableMetadata);
122+
} catch (ExecutionException e) {
123+
throw new NoMutationException(
124+
CoreError.NO_MUTATION_APPLIED.buildMessage(), Collections.singletonList(delete), e);
125+
}
126+
records.remove(mutation.getRecordId());
127+
}
128+
}
129+
130+
public static final class Builder {
131+
private String namespaceName;
132+
private String tableName;
133+
private String partitionKey;
134+
private Map<String, ObjectStorageRecord> records;
135+
136+
public Builder namespaceName(String namespaceName) {
137+
this.namespaceName = namespaceName;
138+
return this;
139+
}
140+
141+
public Builder tableName(String tableName) {
142+
this.tableName = tableName;
143+
return this;
144+
}
145+
146+
public Builder partitionKey(String partitionKey) {
147+
this.partitionKey = partitionKey;
148+
return this;
149+
}
150+
151+
public Builder records(Map<String, ObjectStorageRecord> records) {
152+
this.records = records;
153+
return this;
154+
}
155+
156+
public ObjectStoragePartition build() {
157+
return new ObjectStoragePartition(namespaceName, tableName, partitionKey, records);
158+
}
159+
}
160+
}

0 commit comments

Comments
 (0)