Skip to content

Commit d3d6571

Browse files
Backport to branch(3) : Refactor ConcatenationVisitor to use ColumnVisitor (#2269)
Co-authored-by: Vincent Guilpain <[email protected]>
1 parent 745df17 commit d3d6571

File tree

3 files changed

+93
-83
lines changed

3 files changed

+93
-83
lines changed
Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
package com.scalar.db.storage.cosmos;
22

3-
import com.scalar.db.io.BigIntValue;
4-
import com.scalar.db.io.BlobValue;
5-
import com.scalar.db.io.BooleanValue;
6-
import com.scalar.db.io.DoubleValue;
7-
import com.scalar.db.io.FloatValue;
8-
import com.scalar.db.io.IntValue;
9-
import com.scalar.db.io.TextValue;
10-
import com.scalar.db.io.ValueVisitor;
3+
import com.scalar.db.io.BigIntColumn;
4+
import com.scalar.db.io.BlobColumn;
5+
import com.scalar.db.io.BooleanColumn;
6+
import com.scalar.db.io.ColumnVisitor;
7+
import com.scalar.db.io.DoubleColumn;
8+
import com.scalar.db.io.FloatColumn;
9+
import com.scalar.db.io.IntColumn;
10+
import com.scalar.db.io.TextColumn;
1111
import java.util.ArrayList;
1212
import java.util.Base64;
1313
import java.util.List;
@@ -19,88 +19,94 @@
1919
* @author Yuji Ito
2020
*/
2121
@NotThreadSafe
22-
public class ConcatenationVisitor implements ValueVisitor {
23-
private final List<String> values;
22+
public class ConcatenationVisitor implements ColumnVisitor {
23+
private final List<String> columns;
2424

2525
public ConcatenationVisitor() {
26-
values = new ArrayList<>();
26+
columns = new ArrayList<>();
2727
}
2828

2929
public String build() {
30-
// TODO What if the string or blob value includes `:`?
31-
return String.join(":", values);
30+
// TODO What if the string or blob column includes `:`?
31+
return String.join(":", columns);
3232
}
3333

3434
/**
35-
* Sets the specified {@code BooleanValue} to the key string
35+
* Sets the specified {@code BooleanColumn} to the key string
3636
*
37-
* @param value a {@code BooleanValue} to be set
37+
* @param column a {@code BooleanColumn} to be set
3838
*/
3939
@Override
40-
public void visit(BooleanValue value) {
41-
values.add(String.valueOf(value.get()));
40+
public void visit(BooleanColumn column) {
41+
assert !column.hasNullValue();
42+
columns.add(String.valueOf(column.getBooleanValue()));
4243
}
4344

4445
/**
45-
* Sets the specified {@code IntValue} to the key string
46+
* Sets the specified {@code IntColumn} to the key string
4647
*
47-
* @param value a {@code IntValue} to be set
48+
* @param column a {@code IntColumn} to be set
4849
*/
4950
@Override
50-
public void visit(IntValue value) {
51-
values.add(String.valueOf(value.get()));
51+
public void visit(IntColumn column) {
52+
assert !column.hasNullValue();
53+
columns.add(String.valueOf(column.getIntValue()));
5254
}
5355

5456
/**
55-
* Sets the specified {@code BigIntValue} to the key string
57+
* Sets the specified {@code BigIntColumn} to the key string
5658
*
57-
* @param value a {@code BigIntValue} to be set
59+
* @param column a {@code BigIntColumn} to be set
5860
*/
5961
@Override
60-
public void visit(BigIntValue value) {
61-
values.add(String.valueOf(value.get()));
62+
public void visit(BigIntColumn column) {
63+
assert !column.hasNullValue();
64+
columns.add(String.valueOf(column.getBigIntValue()));
6265
}
6366

6467
/**
65-
* Sets the specified {@code FloatValue} to the key string
68+
* Sets the specified {@code FloatColumn} to the key string
6669
*
67-
* @param value a {@code FloatValue} to be set
70+
* @param column a {@code FloatColumn} to be set
6871
*/
6972
@Override
70-
public void visit(FloatValue value) {
71-
values.add(String.valueOf(value.get()));
73+
public void visit(FloatColumn column) {
74+
assert !column.hasNullValue();
75+
columns.add(String.valueOf(column.getFloatValue()));
7276
}
7377

7478
/**
75-
* Sets the specified {@code DoubleValue} to the key string
79+
* Sets the specified {@code DoubleColumn} to the key string
7680
*
77-
* @param value a {@code DoubleValue} to be set
81+
* @param column a {@code DoubleColumn} to be set
7882
*/
7983
@Override
80-
public void visit(DoubleValue value) {
81-
values.add(String.valueOf(value.get()));
84+
public void visit(DoubleColumn column) {
85+
assert !column.hasNullValue();
86+
columns.add(String.valueOf(column.getDoubleValue()));
8287
}
8388

8489
/**
85-
* Sets the specified {@code TextValue} to the key string
90+
* Sets the specified {@code TextColumn} to the key string
8691
*
87-
* @param value a {@code TextValue} to be set
92+
* @param column a {@code TextColumn} to be set
8893
*/
8994
@Override
90-
public void visit(TextValue value) {
91-
value.get().ifPresent(values::add);
95+
public void visit(TextColumn column) {
96+
assert !column.hasNullValue();
97+
column.getValue().ifPresent(columns::add);
9298
}
9399

94100
/**
95-
* Sets the specified {@code BlobValue} to the key string
101+
* Sets the specified {@code BlobColumn} to the key string
96102
*
97-
* @param value a {@code BlobValue} to be set
103+
* @param column a {@code BlobColumn} to be set
98104
*/
99105
@Override
100-
public void visit(BlobValue value) {
106+
public void visit(BlobColumn column) {
107+
assert !column.hasNullValue();
101108
// Use Base64 encoding
102-
value
103-
.get()
104-
.ifPresent(b -> values.add(Base64.getUrlEncoder().withoutPadding().encodeToString(b)));
109+
columns.add(
110+
Base64.getUrlEncoder().withoutPadding().encodeToString(column.getBlobValueAsBytes()));
105111
}
106112
}

core/src/main/java/com/scalar/db/storage/cosmos/CosmosOperation.java

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import com.google.common.collect.Streams;
66
import com.scalar.db.api.Operation;
77
import com.scalar.db.api.TableMetadata;
8+
import com.scalar.db.io.Column;
89
import com.scalar.db.io.Value;
910
import java.util.HashMap;
1011
import java.util.Map;
@@ -47,8 +48,8 @@ public Operation getOperation() {
4748

4849
@Nonnull
4950
public String getConcatenatedPartitionKey() {
50-
Map<String, Value<?>> keyMap = new HashMap<>();
51-
operation.getPartitionKey().get().forEach(v -> keyMap.put(v.getName(), v));
51+
Map<String, Column<?>> keyMap = new HashMap<>();
52+
operation.getPartitionKey().getColumns().forEach(c -> keyMap.put(c.getName(), c));
5253

5354
ConcatenationVisitor visitor = new ConcatenationVisitor();
5455
metadata.getPartitionKeyNames().forEach(name -> keyMap.get(name).accept(visitor));
@@ -63,9 +64,11 @@ public PartitionKey getCosmosPartitionKey() {
6364

6465
@Nonnull
6566
public String getId() {
66-
Map<String, Value<?>> keyMap = new HashMap<>();
67-
operation.getPartitionKey().get().forEach(v -> keyMap.put(v.getName(), v));
68-
operation.getClusteringKey().ifPresent(k -> k.get().forEach(v -> keyMap.put(v.getName(), v)));
67+
Map<String, Column<?>> keyMap = new HashMap<>();
68+
operation.getPartitionKey().getColumns().forEach(c -> keyMap.put(c.getName(), c));
69+
operation
70+
.getClusteringKey()
71+
.ifPresent(k -> k.getColumns().forEach(c -> keyMap.put(c.getName(), c)));
6972

7073
ConcatenationVisitor visitor = new ConcatenationVisitor();
7174
Streams.concat(

core/src/test/java/com/scalar/db/storage/cosmos/ConcatenationVisitorTest.java

Lines changed: 36 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -2,34 +2,35 @@
22

33
import static org.assertj.core.api.Assertions.assertThat;
44

5+
import com.scalar.db.io.BigIntColumn;
56
import com.scalar.db.io.BigIntValue;
6-
import com.scalar.db.io.BlobValue;
7-
import com.scalar.db.io.BooleanValue;
8-
import com.scalar.db.io.DoubleValue;
9-
import com.scalar.db.io.FloatValue;
10-
import com.scalar.db.io.IntValue;
11-
import com.scalar.db.io.TextValue;
7+
import com.scalar.db.io.BlobColumn;
8+
import com.scalar.db.io.BooleanColumn;
9+
import com.scalar.db.io.DoubleColumn;
10+
import com.scalar.db.io.FloatColumn;
11+
import com.scalar.db.io.IntColumn;
12+
import com.scalar.db.io.TextColumn;
1213
import java.nio.charset.StandardCharsets;
1314
import java.util.Base64;
1415
import org.junit.jupiter.api.BeforeEach;
1516
import org.junit.jupiter.api.Test;
1617

1718
public class ConcatenationVisitorTest {
1819
private static final boolean ANY_BOOLEAN = false;
19-
private static final BooleanValue ANY_BOOLEAN_VALUE =
20-
new BooleanValue("any_boolean", ANY_BOOLEAN);
20+
private static final BooleanColumn ANY_BOOLEAN_COLUMN =
21+
BooleanColumn.of("any_boolean", ANY_BOOLEAN);
2122
private static final int ANY_INT = Integer.MIN_VALUE;
22-
private static final IntValue ANY_INT_VALUE = new IntValue("any_int", ANY_INT);
23+
private static final IntColumn ANY_INT_COLUMN = IntColumn.of("any_int", ANY_INT);
2324
private static final long ANY_BIGINT = BigIntValue.MAX_VALUE;
24-
private static final BigIntValue ANY_BIGINT_VALUE = new BigIntValue("any_bigint", ANY_BIGINT);
25+
private static final BigIntColumn ANY_BIGINT_COLUMN = BigIntColumn.of("any_bigint", ANY_BIGINT);
2526
private static final float ANY_FLOAT = Float.MIN_NORMAL;
26-
private static final FloatValue ANY_FLOAT_VALUE = new FloatValue("any_float", ANY_FLOAT);
27+
private static final FloatColumn ANY_FLOAT_COLUMN = FloatColumn.of("any_float", ANY_FLOAT);
2728
private static final double ANY_DOUBLE = Double.MIN_NORMAL;
28-
private static final DoubleValue ANY_DOUBLE_VALUE = new DoubleValue("any_double", ANY_DOUBLE);
29+
private static final DoubleColumn ANY_DOUBLE_COLUMN = DoubleColumn.of("any_double", ANY_DOUBLE);
2930
private static final String ANY_TEXT = "test";
30-
private static final TextValue ANY_TEXT_VALUE = new TextValue("any_text", ANY_TEXT);
31+
private static final TextColumn ANY_TEXT_COLUMN = TextColumn.of("any_text", ANY_TEXT);
3132
private static final byte[] ANY_BLOB = "scalar".getBytes(StandardCharsets.UTF_8);
32-
private static final BlobValue ANY_BLOB_VALUE = new BlobValue("any_blob", ANY_BLOB);
33+
private static final BlobColumn ANY_BLOB_COLUMN = BlobColumn.of("any_blob", ANY_BLOB);
3334
private ConcatenationVisitor visitor;
3435

3536
@BeforeEach
@@ -40,13 +41,13 @@ public void setUp() {
4041
@Test
4142
public void build_AllTypesGiven_ShouldBuildString() {
4243
// Act
43-
visitor.visit(ANY_BOOLEAN_VALUE);
44-
visitor.visit(ANY_INT_VALUE);
45-
visitor.visit(ANY_BIGINT_VALUE);
46-
visitor.visit(ANY_FLOAT_VALUE);
47-
visitor.visit(ANY_DOUBLE_VALUE);
48-
visitor.visit(ANY_TEXT_VALUE);
49-
visitor.visit(ANY_BLOB_VALUE);
44+
visitor.visit(ANY_BOOLEAN_COLUMN);
45+
visitor.visit(ANY_INT_COLUMN);
46+
visitor.visit(ANY_BIGINT_COLUMN);
47+
visitor.visit(ANY_FLOAT_COLUMN);
48+
visitor.visit(ANY_DOUBLE_COLUMN);
49+
visitor.visit(ANY_TEXT_COLUMN);
50+
visitor.visit(ANY_BLOB_COLUMN);
5051
String actual = visitor.build();
5152

5253
// Assert
@@ -63,63 +64,63 @@ public void build_AllTypesGiven_ShouldBuildString() {
6364
}
6465

6566
@Test
66-
public void visit_BooleanValueAcceptCalled_ShouldBuildBooleanAsString() {
67+
public void visit_BooleanColumnAcceptCalled_ShouldBuildBooleanAsString() {
6768
// Act
68-
ANY_BOOLEAN_VALUE.accept(visitor);
69+
ANY_BOOLEAN_COLUMN.accept(visitor);
6970

7071
// Assert
7172
assertThat(visitor.build()).isEqualTo(String.valueOf(ANY_BOOLEAN));
7273
}
7374

7475
@Test
75-
public void visit_IntValueAcceptCalled_ShouldBuildIntAsString() {
76+
public void visit_IntColumnAcceptCalled_ShouldBuildIntAsString() {
7677
// Act
77-
ANY_INT_VALUE.accept(visitor);
78+
ANY_INT_COLUMN.accept(visitor);
7879

7980
// Assert
8081
assertThat(visitor.build()).isEqualTo(String.valueOf(ANY_INT));
8182
}
8283

8384
@Test
84-
public void visit_BigIntValueAcceptCalled_ShouldBuildBigIntAsString() {
85+
public void visit_BigIntColumnAcceptCalled_ShouldBuildBigIntAsString() {
8586
// Act
86-
ANY_BIGINT_VALUE.accept(visitor);
87+
ANY_BIGINT_COLUMN.accept(visitor);
8788

8889
// Assert
8990
assertThat(visitor.build()).isEqualTo(String.valueOf(ANY_BIGINT));
9091
}
9192

9293
@Test
93-
public void visit_FloatValueAcceptCalled_ShouldBuildFloatAsString() {
94+
public void visit_FloatColumnAcceptCalled_ShouldBuildFloatAsString() {
9495
// Act
95-
ANY_FLOAT_VALUE.accept(visitor);
96+
ANY_FLOAT_COLUMN.accept(visitor);
9697

9798
// Assert
9899
assertThat(visitor.build()).isEqualTo(String.valueOf(ANY_FLOAT));
99100
}
100101

101102
@Test
102-
public void visit_DoubleValueAcceptCalled_ShouldBuildDoubleAsString() {
103+
public void visit_DoubleColumnAcceptCalled_ShouldBuildDoubleAsString() {
103104
// Act
104-
ANY_DOUBLE_VALUE.accept(visitor);
105+
ANY_DOUBLE_COLUMN.accept(visitor);
105106

106107
// Assert
107108
assertThat(visitor.build()).isEqualTo(String.valueOf(ANY_DOUBLE));
108109
}
109110

110111
@Test
111-
public void visit_TextValueAcceptCalled_ShouldBuildText() {
112+
public void visit_TextColumnAcceptCalled_ShouldBuildText() {
112113
// Act
113-
ANY_TEXT_VALUE.accept(visitor);
114+
ANY_TEXT_COLUMN.accept(visitor);
114115

115116
// Assert
116117
assertThat(visitor.build()).isEqualTo(ANY_TEXT);
117118
}
118119

119120
@Test
120-
public void visit_BlobValueAcceptCalled_ShouldBuildBlobAsString() {
121+
public void visit_BlobColumnAcceptCalled_ShouldBuildBlobAsString() {
121122
// Act
122-
ANY_BLOB_VALUE.accept(visitor);
123+
ANY_BLOB_COLUMN.accept(visitor);
123124

124125
// Assert
125126
assertThat(visitor.build())

0 commit comments

Comments
 (0)