use of com.apple.foundationdb.record.ScanProperties in project fdb-record-layer by FoundationDB.
the class KeyValueCursorTest method inclusiveRange.
@Test
public void inclusiveRange() {
fdb.run(context -> {
KeyValueCursor cursor = KeyValueCursor.Builder.withSubspace(subspace).setContext(context).setLow(Tuple.from(3, 3), EndpointType.RANGE_INCLUSIVE).setHigh(Tuple.from(4, 2), EndpointType.RANGE_INCLUSIVE).setContinuation(null).setScanProperties(ScanProperties.FORWARD_SCAN).build();
assertEquals(Arrays.asList(Tuple.from(3L, 3L), Tuple.from(3L, 4L), Tuple.from(4L, 0L), Tuple.from(4L, 1L), Tuple.from(4L, 2L)), cursor.map(KeyValue::getValue).map(Tuple::fromBytes).asList().join());
cursor = KeyValueCursor.Builder.withSubspace(subspace).setContext(context).setLow(Tuple.from(3, 3), EndpointType.RANGE_INCLUSIVE).setHigh(Tuple.from(4, 2), EndpointType.RANGE_INCLUSIVE).setContinuation(null).setScanProperties(new ScanProperties(ExecuteProperties.newBuilder().setReturnedRowLimit(2).build())).build();
assertEquals(Arrays.asList(Tuple.from(3L, 3L), Tuple.from(3L, 4L)), cursor.map(KeyValue::getValue).map(Tuple::fromBytes).asList().join());
cursor = KeyValueCursor.Builder.withSubspace(subspace).setContext(context).setLow(Tuple.from(3, 3), EndpointType.RANGE_INCLUSIVE).setHigh(Tuple.from(4, 2), EndpointType.RANGE_INCLUSIVE).setContinuation(cursor.getNext().getContinuation().toBytes()).setScanProperties(ScanProperties.FORWARD_SCAN).build();
assertEquals(Arrays.asList(Tuple.from(4L, 0L), Tuple.from(4L, 1L), Tuple.from(4L, 2L)), cursor.map(KeyValue::getValue).map(Tuple::fromBytes).asList().join());
return null;
});
}
use of com.apple.foundationdb.record.ScanProperties in project fdb-record-layer by FoundationDB.
the class KeyValueCursorTest method exclusiveRange.
@Test
public void exclusiveRange() {
fdb.run(context -> {
KeyValueCursor cursor = KeyValueCursor.Builder.withSubspace(subspace).setContext(context).setLow(Tuple.from(3, 3), EndpointType.RANGE_EXCLUSIVE).setHigh(Tuple.from(4, 2), EndpointType.RANGE_EXCLUSIVE).setContinuation(null).setScanProperties(ScanProperties.FORWARD_SCAN).build();
assertEquals(Arrays.asList(Tuple.from(3L, 4L), Tuple.from(4L, 0L), Tuple.from(4L, 1L)), cursor.map(KeyValue::getValue).map(Tuple::fromBytes).asList().join());
cursor = KeyValueCursor.Builder.withSubspace(subspace).setContext(context).setLow(Tuple.from(3, 3), EndpointType.RANGE_EXCLUSIVE).setHigh(Tuple.from(4, 2), EndpointType.RANGE_EXCLUSIVE).setContinuation(null).setScanProperties(new ScanProperties(ExecuteProperties.newBuilder().setReturnedRowLimit(2).build())).build();
assertEquals(Arrays.asList(Tuple.from(3L, 4L), Tuple.from(4L, 0L)), cursor.map(KeyValue::getValue).map(Tuple::fromBytes).asList().join());
cursor = KeyValueCursor.Builder.withSubspace(subspace).setContext(context).setLow(Tuple.from(3, 3), EndpointType.RANGE_EXCLUSIVE).setHigh(Tuple.from(4, 2), EndpointType.RANGE_EXCLUSIVE).setContinuation(cursor.getNext().getContinuation().toBytes()).setScanProperties(ScanProperties.FORWARD_SCAN).build();
assertEquals(Collections.singletonList(Tuple.from(4L, 1L)), cursor.map(KeyValue::getValue).map(Tuple::fromBytes).asList().join());
return null;
});
}
use of com.apple.foundationdb.record.ScanProperties in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method testListObeysTimeLimits.
@Test
public void testListObeysTimeLimits() {
KeySpace root = new KeySpace(new KeySpaceDirectory("root", KeyType.STRING, "root-" + random.nextInt(Integer.MAX_VALUE)).addSubdirectory(new KeySpaceDirectory("a", KeyType.LONG).addSubdirectory(new KeySpaceDirectory("b", KeyType.LONG).addSubdirectory(new KeySpaceDirectory("c", KeyType.LONG)))));
final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
try (FDBRecordContext context = database.openContext()) {
Transaction tr = context.ensureActive();
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 3; j++) {
for (int k = 0; k < 5; k++) {
tr.set(root.path("root").add("a", i).add("b", j).add("c", k).toTuple(context).pack(), Tuple.from(i + j).pack());
}
}
}
tr.commit().join();
}
try (FDBRecordContext context = database.openContext()) {
// Iteration will inject a 1ms pause in each "a" value we iterate over (there are 10 of them)
// so we want to make the time limit long enough to make *some* progress, but short enough to
// to make sure we cannot get them all.
ScanProperties props = new ScanProperties(ExecuteProperties.newBuilder().setFailOnScanLimitReached(false).setTimeLimit(5L).build());
// The inner and outer iterator are declared here instead of in-line with the call to flatMapPipelined
// because IntelliJ was having issues groking the call as a single call.
Function<byte[], RecordCursor<ResolvedKeySpacePath>> aIterator = outerContinuation -> root.path("root").listSubdirectoryAsync(context, "a", outerContinuation, props).map(value -> {
sleep(1L);
return value;
});
BiFunction<ResolvedKeySpacePath, byte[], RecordCursor<ResolvedKeySpacePath>> bIterator = (aPath, innerContinuation) -> aPath.toPath().add("b", 0).listSubdirectoryAsync(context, "c", innerContinuation, props);
RecordCursor<ResolvedKeySpacePath> cursor = RecordCursor.flatMapPipelined(aIterator, bIterator, null, 10);
long count = cursor.getCount().join();
assertEquals(RecordCursor.NoNextReason.TIME_LIMIT_REACHED, cursor.getNext().getNoNextReason());
// With a 1ms delay we should read no more than 5 "a" values (there are a total of 10)
// and each "c" value has 4 values. so we shouldn't have been able to read more than 40
// total values.
assertTrue(count <= 40, "Read too many values, query should have timed out");
}
}
use of com.apple.foundationdb.record.ScanProperties in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method doLimitedScan.
private void doLimitedScan(FDBDatabase database, KeySpace root, int returnedRowLimit, int scannedRecordLimit, RecordCursor.NoNextReason noNextReason) {
try (FDBRecordContext context = database.openContext()) {
ScanProperties props = new ScanProperties(ExecuteProperties.newBuilder().setFailOnScanLimitReached(false).setReturnedRowLimit(returnedRowLimit).setScannedRecordsLimit(scannedRecordLimit).build());
RecordCursor<ResolvedKeySpacePath> cursor = root.path("root").listSubdirectoryAsync(context, "a", null, props);
long count = cursor.getCount().join();
assertEquals(noNextReason, cursor.getNext().getNoNextReason());
assertEquals(Math.min(returnedRowLimit, scannedRecordLimit), count, "Wrong number of results");
}
}
use of com.apple.foundationdb.record.ScanProperties in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method testListReverse.
@Test
public void testListReverse() {
KeySpace root = new KeySpace(new KeySpaceDirectory("root", KeyType.STRING, "root-" + random.nextInt(Integer.MAX_VALUE)).addSubdirectory(new KeySpaceDirectory("a", KeyType.LONG).addSubdirectory(new KeySpaceDirectory("b", KeyType.LONG))));
final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
try (FDBRecordContext context = database.openContext()) {
Transaction tr = context.ensureActive();
for (int i = 0; i < 5; i++) {
for (int j = 0; j < 2; j++) {
tr.set(root.path("root").add("a", i).add("b", j).toTuple(context).pack(), Tuple.from(i + j).pack());
}
}
tr.commit().join();
}
final List<Tuple> results;
try (FDBRecordContext context = database.openContext()) {
ScanProperties props = new ScanProperties(ExecuteProperties.newBuilder().build(), true);
results = root.path("root").listSubdirectoryAsync(context, "a", null, props).asList().join().stream().map(path -> path.toTuple()).collect(Collectors.toList());
}
assertEquals(5, results.size());
for (int i = 0; i < 5; i++) {
assertEquals(i, ((Long) results.get(4 - i).getLong(1)).intValue());
}
}
Aggregations