Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 43 additions & 0 deletions src/main/java/org/tikv/raw/RawKVClient.java
Original file line number Diff line number Diff line change
Expand Up @@ -345,6 +345,49 @@ public Long getKeyTTL(ByteString key) {
}
}

/**
* Create a new `batch scan` request with `keyOnly` option Once resolved this request will result
* in a set of scanners over the given keys.
*
* <p>WARNING: This method is experimental. The `each_limit` parameter does not work as expected.
* It does not limit the number of results returned of each range, instead it limits the number of
* results in each region of each range. As a result, you may get more than each_limit key-value
* pairs for each range. But you should not miss any entries.
*
* @param ranges a list of ranges
* @return a set of scanners for keys over the given keys.
*/
public List<List<ByteString>> batchScanKeys(
List<Pair<ByteString, ByteString>> ranges, int eachLimit) {
return batchScan(
ranges
.stream()
.map(
range ->
ScanOption.newBuilder()
.setStartKey(range.first)
.setEndKey(range.second)
.setLimit(eachLimit)
.setKeyOnly(true)
.build())
.collect(Collectors.toList()))
.stream()
.map(kvs -> kvs.stream().map(kv -> kv.getKey()).collect(Collectors.toList()))
.collect(Collectors.toList());
}

/**
* Create a new `batch scan` request. Once resolved this request will result in a set of scanners
* over the given keys.
*
* <p>WARNING: This method is experimental. The `each_limit` parameter does not work as expected.
* It does not limit the number of results returned of each range, instead it limits the number of
* results in each region of each range. As a result, you may get more than each_limit key-value
* pairs for each range. But you should not miss any entries.
*
* @param ranges a list of `ScanOption` for each range
* @return a set of scanners over the given keys.
*/
public List<List<KvPair>> batchScan(List<ScanOption> ranges) {
String label = "client_raw_batch_scan";
Histogram.Timer requestTimer = RAW_REQUEST_LATENCY.labels(label).startTimer();
Expand Down
18 changes: 18 additions & 0 deletions src/test/java/org/tikv/raw/RawKVClientTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import org.tikv.common.exception.TiKVException;
import org.tikv.common.key.Key;
import org.tikv.common.util.FastByteComparisons;
import org.tikv.common.util.Pair;
import org.tikv.common.util.ScanOption;
import org.tikv.kvproto.Kvrpcpb;

Expand Down Expand Up @@ -644,6 +645,11 @@ private void rawBatchScanTest(int scanCases, boolean benchmark) {
scanOptions.add(scanOption);
}
checkBatchScan(scanOptions);
checkBatchScanKeys(
scanOptions
.stream()
.map(scanOption -> Pair.create(scanOption.getStartKey(), scanOption.getEndKey()))
.collect(Collectors.toList()));
}
}

Expand Down Expand Up @@ -801,6 +807,7 @@ private void checkScan(
}

private void checkBatchScan(List<ScanOption> scanOptions) {
logger.info("checking batch scan");
List<List<Kvrpcpb.KvPair>> result = client.batchScan(scanOptions);
int i = 0;
for (ScanOption scanOption : scanOptions) {
Expand All @@ -820,6 +827,17 @@ private void checkBatchScan(List<ScanOption> scanOptions) {
}
}

private void checkBatchScanKeys(List<Pair<ByteString, ByteString>> ranges) {
logger.info("checking batch scan keys");
List<List<ByteString>> result = client.batchScanKeys(ranges, limit);
for (int i = 0; i < ranges.size(); i++) {
Pair<ByteString, ByteString> range = ranges.get(i);
List<ByteString> partialResult =
new ArrayList<>(data.subMap(range.first, range.second).keySet());
assert result.get(i).equals(partialResult);
}
}

private void checkDelete(ByteString key) {
client.delete(key);
checkEmpty(key);
Expand Down