Skip to content

Commit 11dbc96

Browse files
Backport to branch(3) : Refactor KeyBytesEncoder to use ColumnVisitor (#2275)
Co-authored-by: Vincent Guilpain <[email protected]>
1 parent 30cc7ca commit 11dbc96

File tree

10 files changed

+103
-88
lines changed

10 files changed

+103
-88
lines changed

core/src/main/java/com/scalar/db/storage/dynamo/bytes/BigIntBytesEncoder.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,25 @@
33
import static com.scalar.db.storage.dynamo.bytes.BytesUtils.mask;
44

55
import com.scalar.db.api.Scan.Ordering.Order;
6-
import com.scalar.db.io.BigIntValue;
6+
import com.scalar.db.io.BigIntColumn;
77
import java.nio.ByteBuffer;
88
import javax.annotation.concurrent.ThreadSafe;
99

1010
@ThreadSafe
11-
public class BigIntBytesEncoder implements BytesEncoder<BigIntValue> {
11+
public class BigIntBytesEncoder implements BytesEncoder<BigIntColumn> {
1212

1313
BigIntBytesEncoder() {}
1414

1515
@Override
16-
public int encodedLength(BigIntValue value, Order order) {
16+
public int encodedLength(BigIntColumn column, Order order) {
1717
return 8;
1818
}
1919

2020
@Override
21-
public void encode(BigIntValue value, Order order, ByteBuffer dst) {
22-
long v = value.getAsLong();
21+
public void encode(BigIntColumn column, Order order, ByteBuffer dst) {
22+
assert !column.hasNullValue();
23+
24+
long v = column.getBigIntValue();
2325
dst.put(mask((byte) ((v >> 56) ^ 0x80), order)); // Flip a sign bit to make it binary comparable
2426
dst.put(mask((byte) (v >> 48), order));
2527
dst.put(mask((byte) (v >> 40), order));

core/src/main/java/com/scalar/db/storage/dynamo/bytes/BlobBytesEncoder.java

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,33 @@
44

55
import com.scalar.db.api.Scan.Ordering.Order;
66
import com.scalar.db.common.error.CoreError;
7-
import com.scalar.db.io.BlobValue;
7+
import com.scalar.db.io.BlobColumn;
88
import java.nio.ByteBuffer;
99
import javax.annotation.concurrent.ThreadSafe;
1010

1111
@ThreadSafe
12-
public class BlobBytesEncoder implements BytesEncoder<BlobValue> {
12+
public class BlobBytesEncoder implements BytesEncoder<BlobColumn> {
1313

1414
private static final byte TERM = (byte) 0x00;
1515
private static final byte MASKED_TERM = (byte) 0xff;
1616

1717
BlobBytesEncoder() {}
1818

1919
@Override
20-
public int encodedLength(BlobValue value, Order order) {
21-
assert value.getAsBytes().isPresent();
22-
return value.getAsBytes().get().length + (order == Order.ASC ? 0 : 1);
20+
public int encodedLength(BlobColumn column, Order order) {
21+
assert column.getBlobValueAsBytes() != null;
22+
23+
return column.getBlobValueAsBytes().length + (order == Order.ASC ? 0 : 1);
2324
}
2425

2526
@Override
26-
public void encode(BlobValue value, Order order, ByteBuffer dst) {
27-
assert value.getAsBytes().isPresent();
27+
public void encode(BlobColumn column, Order order, ByteBuffer dst) {
28+
assert column.getBlobValueAsBytes() != null;
29+
30+
byte[] value = column.getBlobValueAsBytes();
2831

2932
if (order == Order.DESC) {
30-
for (byte b : value.getAsBytes().get()) {
33+
for (byte b : value) {
3134
if (b == TERM) {
3235
throw new IllegalArgumentException(
3336
CoreError.DYNAMO_ENCODER_0X00_BYTES_NOT_ACCEPTED_IN_BLOB_VALUES_IN_DESC_ORDER
@@ -36,7 +39,7 @@ public void encode(BlobValue value, Order order, ByteBuffer dst) {
3639
}
3740
}
3841

39-
for (byte b : value.getAsBytes().get()) {
42+
for (byte b : value) {
4043
dst.put(mask(b, order));
4144
}
4245
if (order == Order.DESC) {

core/src/main/java/com/scalar/db/storage/dynamo/bytes/BooleanBytesEncoder.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@
33
import static com.scalar.db.storage.dynamo.bytes.BytesUtils.mask;
44

55
import com.scalar.db.api.Scan.Ordering.Order;
6-
import com.scalar.db.io.BooleanValue;
6+
import com.scalar.db.io.BooleanColumn;
77
import java.nio.ByteBuffer;
88
import javax.annotation.concurrent.ThreadSafe;
99

1010
@ThreadSafe
11-
public class BooleanBytesEncoder implements BytesEncoder<BooleanValue> {
11+
public class BooleanBytesEncoder implements BytesEncoder<BooleanColumn> {
1212
private static final byte FALSE = 0x00;
1313
private static final byte TRUE = 0x01;
1414

1515
BooleanBytesEncoder() {}
1616

1717
@Override
18-
public int encodedLength(BooleanValue value, Order order) {
18+
public int encodedLength(BooleanColumn column, Order order) {
1919
return 1;
2020
}
2121

2222
@Override
23-
public void encode(BooleanValue value, Order order, ByteBuffer dst) {
24-
boolean b = value.getAsBoolean();
23+
public void encode(BooleanColumn column, Order order, ByteBuffer dst) {
24+
assert !column.hasNullValue();
25+
26+
boolean b = column.getBooleanValue();
2527
dst.put(mask(b ? TRUE : FALSE, order));
2628
}
2729
}
Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,31 +1,31 @@
11
package com.scalar.db.storage.dynamo.bytes;
22

33
import com.scalar.db.api.Scan.Ordering.Order;
4-
import com.scalar.db.io.Value;
4+
import com.scalar.db.io.Column;
55
import java.nio.ByteBuffer;
66

77
/**
8-
* A bytes-encoder that encodes a value to bytes while preserving the sort order.
8+
* A bytes-encoder that encodes a column to bytes while preserving the sort order.
99
*
1010
* @param <T> the value type
1111
*/
12-
public interface BytesEncoder<T extends Value<?>> {
12+
public interface BytesEncoder<T extends Column<?>> {
1313

1414
/**
1515
* Calculates the encoded bytes length.
1616
*
17-
* @param value a value
17+
* @param column a column
1818
* @param order an order
1919
* @return the encoded bytes length
2020
*/
21-
int encodedLength(T value, Order order);
21+
int encodedLength(T column, Order order);
2222

2323
/**
24-
* Encodes the value to bytes
24+
* Encodes the column to bytes
2525
*
26-
* @param value a value
26+
* @param column a column
2727
* @param order an order
2828
* @param dst a ByteBuffer to write the encoded bytes
2929
*/
30-
void encode(T value, Order order, ByteBuffer dst);
30+
void encode(T column, Order order, ByteBuffer dst);
3131
}

core/src/main/java/com/scalar/db/storage/dynamo/bytes/DoubleBytesEncoder.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33
import static com.scalar.db.storage.dynamo.bytes.BytesUtils.mask;
44

55
import com.scalar.db.api.Scan.Ordering.Order;
6-
import com.scalar.db.io.DoubleValue;
6+
import com.scalar.db.io.DoubleColumn;
77
import java.nio.ByteBuffer;
88
import javax.annotation.concurrent.ThreadSafe;
99

1010
@ThreadSafe
11-
public class DoubleBytesEncoder implements BytesEncoder<DoubleValue> {
11+
public class DoubleBytesEncoder implements BytesEncoder<DoubleColumn> {
1212

1313
DoubleBytesEncoder() {}
1414

1515
@Override
16-
public int encodedLength(DoubleValue value, Order order) {
16+
public int encodedLength(DoubleColumn column, Order order) {
1717
return 8;
1818
}
1919

2020
@Override
21-
public void encode(DoubleValue value, Order order, ByteBuffer dst) {
21+
public void encode(DoubleColumn column, Order order, ByteBuffer dst) {
22+
assert !column.hasNullValue();
2223
/*
2324
* The IEE754 floating point format already preserves sort ordering for positive floating point
2425
* numbers when the raw bytes are compared in most significant byte order.
@@ -30,7 +31,7 @@ public void encode(DoubleValue value, Order order, ByteBuffer dst) {
3031
*/
3132

3233
// store the floating point bits into a 64-bit long
33-
long l = Double.doubleToLongBits(value.getAsDouble());
34+
long l = Double.doubleToLongBits(column.getDoubleValue());
3435

3536
// invert the sign bit and XOR's all other bits with the sign bit itself
3637
l ^= ((l >> (Long.SIZE - 1)) | Long.MIN_VALUE);

core/src/main/java/com/scalar/db/storage/dynamo/bytes/FloatBytesEncoder.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,22 +3,23 @@
33
import static com.scalar.db.storage.dynamo.bytes.BytesUtils.mask;
44

55
import com.scalar.db.api.Scan.Ordering.Order;
6-
import com.scalar.db.io.FloatValue;
6+
import com.scalar.db.io.FloatColumn;
77
import java.nio.ByteBuffer;
88
import javax.annotation.concurrent.ThreadSafe;
99

1010
@ThreadSafe
11-
public class FloatBytesEncoder implements BytesEncoder<FloatValue> {
11+
public class FloatBytesEncoder implements BytesEncoder<FloatColumn> {
1212

1313
FloatBytesEncoder() {}
1414

1515
@Override
16-
public int encodedLength(FloatValue value, Order order) {
16+
public int encodedLength(FloatColumn column, Order order) {
1717
return 4;
1818
}
1919

2020
@Override
21-
public void encode(FloatValue value, Order order, ByteBuffer dst) {
21+
public void encode(FloatColumn column, Order order, ByteBuffer dst) {
22+
assert !column.hasNullValue();
2223
/*
2324
* The IEE754 floating point format already preserves sort ordering for positive floating point
2425
* numbers when the raw bytes are compared in most significant byte order.
@@ -30,7 +31,7 @@ public void encode(FloatValue value, Order order, ByteBuffer dst) {
3031
*/
3132

3233
// store the floating point bits into a 32-bit int
33-
int i = Float.floatToIntBits(value.getAsFloat());
34+
int i = Float.floatToIntBits(column.getFloatValue());
3435

3536
// invert the sign bit and XOR's all other bits with the sign bit itself
3637
i ^= ((i >> (Integer.SIZE - 1)) | Integer.MIN_VALUE);

core/src/main/java/com/scalar/db/storage/dynamo/bytes/IntBytesEncoder.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -3,23 +3,25 @@
33
import static com.scalar.db.storage.dynamo.bytes.BytesUtils.mask;
44

55
import com.scalar.db.api.Scan.Ordering.Order;
6-
import com.scalar.db.io.IntValue;
6+
import com.scalar.db.io.IntColumn;
77
import java.nio.ByteBuffer;
88
import javax.annotation.concurrent.ThreadSafe;
99

1010
@ThreadSafe
11-
public class IntBytesEncoder implements BytesEncoder<IntValue> {
11+
public class IntBytesEncoder implements BytesEncoder<IntColumn> {
1212

1313
IntBytesEncoder() {}
1414

1515
@Override
16-
public int encodedLength(IntValue value, Order order) {
16+
public int encodedLength(IntColumn column, Order order) {
1717
return 4;
1818
}
1919

2020
@Override
21-
public void encode(IntValue value, Order order, ByteBuffer dst) {
22-
int v = value.getAsInt();
21+
public void encode(IntColumn column, Order order, ByteBuffer dst) {
22+
assert !column.hasNullValue();
23+
24+
int v = column.getIntValue();
2325
dst.put(mask((byte) ((v >> 24) ^ 0x80), order)); // Flip a sign bit to make it binary comparable
2426
dst.put(mask((byte) (v >> 16), order));
2527
dst.put(mask((byte) (v >> 8), order));

core/src/main/java/com/scalar/db/storage/dynamo/bytes/KeyBytesEncodedLengthCalculator.java

Lines changed: 19 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,77 +1,77 @@
11
package com.scalar.db.storage.dynamo.bytes;
22

33
import com.scalar.db.api.Scan.Ordering.Order;
4-
import com.scalar.db.io.BigIntValue;
5-
import com.scalar.db.io.BlobValue;
6-
import com.scalar.db.io.BooleanValue;
7-
import com.scalar.db.io.DoubleValue;
8-
import com.scalar.db.io.FloatValue;
9-
import com.scalar.db.io.IntValue;
4+
import com.scalar.db.io.BigIntColumn;
5+
import com.scalar.db.io.BlobColumn;
6+
import com.scalar.db.io.BooleanColumn;
7+
import com.scalar.db.io.Column;
8+
import com.scalar.db.io.ColumnVisitor;
9+
import com.scalar.db.io.DoubleColumn;
10+
import com.scalar.db.io.FloatColumn;
11+
import com.scalar.db.io.IntColumn;
1012
import com.scalar.db.io.Key;
11-
import com.scalar.db.io.TextValue;
12-
import com.scalar.db.io.Value;
13-
import com.scalar.db.io.ValueVisitor;
13+
import com.scalar.db.io.TextColumn;
1414
import edu.umd.cs.findbugs.annotations.SuppressFBWarnings;
1515
import java.util.Map;
1616
import javax.annotation.concurrent.NotThreadSafe;
1717

1818
@NotThreadSafe
19-
public class KeyBytesEncodedLengthCalculator implements ValueVisitor {
19+
public class KeyBytesEncodedLengthCalculator implements ColumnVisitor {
2020

2121
private int length;
2222
private Map<String, Order> keyOrders;
2323

2424
@SuppressFBWarnings("EI_EXPOSE_REP2")
2525
public int calculate(Key key, Map<String, Order> keyOrders) {
2626
this.keyOrders = keyOrders;
27-
for (Value<?> value : key) {
28-
value.accept(this);
27+
for (Column<?> column : key.getColumns()) {
28+
column.accept(this);
2929
}
3030
return length;
3131
}
3232

3333
@Override
34-
public void visit(BooleanValue value) {
34+
public void visit(BooleanColumn value) {
3535
length +=
3636
BytesEncoders.BOOLEAN.encodedLength(
3737
value, keyOrders.getOrDefault(value.getName(), Order.ASC));
3838
}
3939

4040
@Override
41-
public void visit(IntValue value) {
41+
public void visit(IntColumn value) {
4242
length +=
4343
BytesEncoders.INT.encodedLength(value, keyOrders.getOrDefault(value.getName(), Order.ASC));
4444
}
4545

4646
@Override
47-
public void visit(BigIntValue value) {
47+
public void visit(BigIntColumn value) {
4848
length +=
4949
BytesEncoders.BIGINT.encodedLength(
5050
value, keyOrders.getOrDefault(value.getName(), Order.ASC));
5151
}
5252

5353
@Override
54-
public void visit(FloatValue value) {
54+
public void visit(FloatColumn value) {
5555
length +=
5656
BytesEncoders.FLOAT.encodedLength(
5757
value, keyOrders.getOrDefault(value.getName(), Order.ASC));
5858
}
5959

6060
@Override
61-
public void visit(DoubleValue value) {
61+
public void visit(DoubleColumn value) {
6262
length +=
6363
BytesEncoders.DOUBLE.encodedLength(
6464
value, keyOrders.getOrDefault(value.getName(), Order.ASC));
6565
}
6666

6767
@Override
68-
public void visit(TextValue value) {
68+
public void visit(TextColumn value) {
6969
length +=
7070
BytesEncoders.TEXT.encodedLength(value, keyOrders.getOrDefault(value.getName(), Order.ASC));
7171
}
7272

7373
@Override
74-
public void visit(BlobValue value) {
74+
public void visit(BlobColumn value) {
7575
length +=
7676
BytesEncoders.BLOB.encodedLength(value, keyOrders.getOrDefault(value.getName(), Order.ASC));
7777
}

0 commit comments

Comments
 (0)