Skip to content

Commit b5fc762

Browse files
committed
Added support of raw ranges to ReadTableSettings
1 parent 872e4fc commit b5fc762

File tree

6 files changed

+81
-17
lines changed

6 files changed

+81
-17
lines changed

table/src/main/java/tech/ydb/table/impl/BaseSession.java

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1266,23 +1266,23 @@ public GrpcReadStream<ReadTablePart> executeReadTable(String tablePath, ReadTabl
12661266
.setBatchLimitBytes(settings.batchLimitBytes())
12671267
.setBatchLimitRows(settings.batchLimitRows());
12681268

1269-
Value<?> fromKey = settings.getFromKey();
1269+
ValueProtos.TypedValue fromKey = settings.getFromKeyRaw();
12701270
if (fromKey != null) {
12711271
YdbTable.KeyRange.Builder range = request.getKeyRangeBuilder();
12721272
if (settings.isFromInclusive()) {
1273-
range.setGreaterOrEqual(ProtoValue.toTypedValue(fromKey));
1273+
range.setGreaterOrEqual(fromKey);
12741274
} else {
1275-
range.setGreater(ProtoValue.toTypedValue(fromKey));
1275+
range.setGreater(fromKey);
12761276
}
12771277
}
12781278

1279-
Value<?> toKey = settings.getToKey();
1279+
ValueProtos.TypedValue toKey = settings.getToKeyRaw();
12801280
if (toKey != null) {
12811281
YdbTable.KeyRange.Builder range = request.getKeyRangeBuilder();
12821282
if (settings.isToInclusive()) {
1283-
range.setLessOrEqual(ProtoValue.toTypedValue(toKey));
1283+
range.setLessOrEqual(toKey);
12841284
} else {
1285-
range.setLess(ProtoValue.toTypedValue(toKey));
1285+
range.setLess(toKey);
12861286
}
12871287
}
12881288

table/src/main/java/tech/ydb/table/query/ReadTablePart.java

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,25 +28,29 @@ public long getTxId() {
2828
}
2929

3030
private final YdbTable.ReadTableResponse part;
31-
private final ResultSetReader resultSetReader;
32-
private final VirtualTimestamp timestamp;
31+
private ResultSetReader resultSetReader = null;
32+
private VirtualTimestamp timestamp = null;
3333

3434
public ReadTablePart(YdbTable.ReadTableResponse part) {
3535
this.part = part;
36-
this.resultSetReader = ProtoValueReaders.forResultSet(part.getResult().getResultSet());
37-
CommonProtos.VirtualTimestamp vt = part.getSnapshot();
38-
this.timestamp = new VirtualTimestamp(vt.getPlanStep(), vt.getTxId());
3936
}
4037

4138
public YdbTable.ReadTableResponse getReadTableResponse() {
4239
return part;
4340
}
4441

4542
public ResultSetReader getResultSetReader() {
43+
if (resultSetReader == null) {
44+
resultSetReader = ProtoValueReaders.forResultSet(part.getResult().getResultSet());
45+
}
4646
return resultSetReader;
4747
}
4848

4949
public VirtualTimestamp getVirtualTimestamp() {
50+
if (timestamp == null) {
51+
CommonProtos.VirtualTimestamp vt = part.getSnapshot();
52+
timestamp = new VirtualTimestamp(vt.getPlanStep(), vt.getTxId());
53+
}
5054
return timestamp;
5155
}
5256
}

table/src/main/java/tech/ydb/table/result/impl/ProtoValueReaders.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,17 @@ public static ResultSetReader forResultSets(Collection<ResultSetReader> resultSe
4040
return new ProtoResultSetReader(builder.build());
4141
}
4242

43+
@Deprecated
4344
public static ValueReader forType(ValueProtos.Type type) {
4445
return forTypeImpl(type);
4546
}
4647

48+
public static ValueReader forTypedValue(ValueProtos.TypedValue tv) {
49+
AbstractValueReader vr = forTypeImpl(tv.getType());
50+
vr.setProtoValue(tv.getValue());
51+
return vr;
52+
}
53+
4754
static AbstractValueReader forTypeImpl(ValueProtos.Type type) {
4855
switch (type.getTypeCase()) {
4956
case TYPE_ID:

table/src/main/java/tech/ydb/table/settings/ReadTableSettings.java

Lines changed: 34 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,12 @@
1111

1212
import tech.ydb.core.grpc.GrpcFlowControl;
1313
import tech.ydb.core.settings.BaseRequestSettings;
14+
import tech.ydb.proto.ValueProtos;
15+
import tech.ydb.proto.table.YdbTable;
16+
import tech.ydb.table.result.impl.ProtoValueReaders;
1417
import tech.ydb.table.values.PrimitiveValue;
1518
import tech.ydb.table.values.TupleValue;
19+
import tech.ydb.table.values.proto.ProtoValue;
1620

1721
import static com.google.common.base.Preconditions.checkArgument;
1822

@@ -24,9 +28,9 @@
2428
public class ReadTableSettings extends BaseRequestSettings {
2529

2630
private final boolean ordered;
27-
private final TupleValue fromKey;
31+
private final ValueProtos.TypedValue fromKey;
2832
private final boolean fromInclusive;
29-
private final TupleValue toKey;
33+
private final ValueProtos.TypedValue toKey;
3034
private final boolean toInclusive;
3135
private final int rowLimit;
3236
private final ImmutableList<String> columns;
@@ -58,6 +62,11 @@ public boolean isOrdered() {
5862

5963
@Nullable
6064
public TupleValue getFromKey() {
65+
return fromKey == null ? null : ProtoValueReaders.forTypedValue(fromKey).getValue().asTuple();
66+
}
67+
68+
@Nullable
69+
public ValueProtos.TypedValue getFromKeyRaw() {
6170
return fromKey;
6271
}
6372

@@ -67,6 +76,11 @@ public boolean isFromInclusive() {
6776

6877
@Nullable
6978
public TupleValue getToKey() {
79+
return toKey == null ? null : ProtoValueReaders.forTypedValue(toKey).getValue().asTuple();
80+
}
81+
82+
@Nullable
83+
public ValueProtos.TypedValue getToKeyRaw() {
7084
return toKey;
7185
}
7286

@@ -94,14 +108,18 @@ public GrpcFlowControl getGrpcFlowControl() {
94108
return flowControl;
95109
}
96110

111+
public void applyToRequest(YdbTable.ReadTableRequest.Builder request) {
112+
113+
}
114+
97115
/**
98116
* BUILDER
99117
*/
100118
public static final class Builder extends BaseBuilder<Builder> {
101119
private boolean ordered = false;
102-
private TupleValue fromKey = null;
120+
private ValueProtos.TypedValue fromKey = null;
103121
private boolean fromInclusive = false;
104-
private TupleValue toKey = null;
122+
private ValueProtos.TypedValue toKey = null;
105123
private boolean toInclusive = false;
106124
private int rowLimit = 0;
107125
private List<String> columns = Collections.emptyList();
@@ -115,12 +133,24 @@ public Builder orderedRead(boolean ordered) {
115133
}
116134

117135
public Builder fromKey(TupleValue value, boolean inclusive) {
136+
this.fromKey = ProtoValue.toTypedValue(value);
137+
this.fromInclusive = inclusive;
138+
return this;
139+
}
140+
141+
public Builder fromKey(ValueProtos.TypedValue value, boolean inclusive) {
118142
this.fromKey = value;
119143
this.fromInclusive = inclusive;
120144
return this;
121145
}
122146

123147
public Builder toKey(TupleValue value, boolean inclusive) {
148+
this.toKey = ProtoValue.toTypedValue(value);
149+
this.toInclusive = inclusive;
150+
return this;
151+
}
152+
153+
public Builder toKey(ValueProtos.TypedValue value, boolean inclusive) {
124154
this.toKey = value;
125155
this.toInclusive = inclusive;
126156
return this;

table/src/main/java/tech/ydb/table/values/Value.java

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,10 @@ default StructValue asStruct() {
4444
return (StructValue) this;
4545
}
4646

47+
default TupleValue asTuple() {
48+
return (TupleValue) this;
49+
}
50+
4751
default VariantValue asVariant() {
4852
return (VariantValue) this;
4953
}

table/src/test/java/tech/ydb/table/integration/ReadTableTest.java

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,8 @@
3838
import tech.ydb.table.values.PrimitiveValue;
3939
import tech.ydb.table.values.StructType;
4040
import tech.ydb.table.values.StructValue;
41+
import tech.ydb.table.values.TupleValue;
42+
import tech.ydb.table.values.proto.ProtoValue;
4143
import tech.ydb.test.junit4.GrpcTransportRule;
4244

4345
/**
@@ -143,6 +145,13 @@ public void limitedReadTableTest() {
143145
AtomicLong rewsRead = new AtomicLong(0);
144146

145147
ReadTableSettings rts = ReadTableSettings.newBuilder().column("id").batchLimitRows(100).build();
148+
Assert.assertNull(rts.getFromKey());
149+
Assert.assertNull(rts.getToKey());
150+
Assert.assertNull(rts.getFromKeyRaw());
151+
Assert.assertNull(rts.getToKeyRaw());
152+
Assert.assertEquals(0, rts.batchLimitBytes());
153+
Assert.assertEquals(100, rts.batchLimitRows());
154+
146155
retryCtx.supplyStatus(session -> {
147156
rewsRead.set(0);
148157
return session.executeReadTable(tablePath, rts).start(part -> {
@@ -159,10 +168,20 @@ public void partialReadTableTest() {
159168
String tablePath = tablePath(TEST_TABLE);
160169
AtomicLong rowsRead = new AtomicLong(0);
161170

171+
PrimitiveValue from = PrimitiveValue.newInt64(1);
172+
PrimitiveValue to = PrimitiveValue.newInt64(TEST_TABLE_SIZE);
162173
ReadTableSettings rts = ReadTableSettings.newBuilder().column("id")
163-
.fromKeyExclusive(PrimitiveValue.newInt64(1))
164-
.toKeyExclusive(PrimitiveValue.newInt64(TEST_TABLE_SIZE))
174+
.fromKeyExclusive(from) // always coverted to optional type
175+
.toKeyExclusive(to)
165176
.build();
177+
178+
Assert.assertEquals(TupleValue.of(from.makeOptional()), rts.getFromKey());
179+
Assert.assertEquals(TupleValue.of(to.makeOptional()), rts.getToKey());
180+
Assert.assertEquals(ProtoValue.toTypedValue(TupleValue.of(from.makeOptional())), rts.getFromKeyRaw());
181+
Assert.assertEquals(ProtoValue.toTypedValue(TupleValue.of(to.makeOptional())), rts.getToKeyRaw());
182+
Assert.assertEquals(0, rts.batchLimitBytes());
183+
Assert.assertEquals(0, rts.batchLimitRows());
184+
166185
retryCtx.supplyStatus(session -> {
167186
rowsRead.set(0);
168187
return session.executeReadTable(tablePath, rts).start(part -> {

0 commit comments

Comments
 (0)