use of com.apple.foundationdb.tuple.Tuple in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method testDirectoryLayerDirectoryUsingLongs.
@Test
public void testDirectoryLayerDirectoryUsingLongs() throws Exception {
KeySpace root = new KeySpace(new DirectoryLayerDirectory("cabinet", "cabinet").addSubdirectory(new DirectoryLayerDirectory("game")));
final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
final Tuple senetTuple;
final Tuple urTuple;
try (FDBRecordContext context = database.openContext()) {
senetTuple = root.path("cabinet").add("game", "senet").toTuple(context);
urTuple = root.path("cabinet").add("game", "royal_game_of_ur").toTuple(context);
context.commit();
}
try (FDBRecordContext context = database.openContext()) {
// Verify that I can create the tuple again using the directory layer values.
assertEquals(senetTuple, root.path("cabinet").add("game", senetTuple.getLong(1)).toTuple(context));
assertEquals(urTuple, root.path("cabinet").add("game", urTuple.getLong(1)).toTuple(context));
}
}
use of com.apple.foundationdb.tuple.Tuple in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method listRangeTestCases.
private List<Pair<ValueRange<Object>, List<Tuple>>> listRangeTestCases(List<Tuple> values) {
values.sort(null);
// Size >= 1. It is very likely to be 5 (but can be smaller) expect when the key type is NULL or BOOLEAN.
int size = values.size();
List<Pair<ValueRange<Object>, List<Tuple>>> testCases = new LinkedList<>();
testCases.add(Pair.of(null, new ArrayList<>(values)));
testCases.add(newTestCase(values.get(0), null, EndpointType.RANGE_INCLUSIVE, EndpointType.TREE_END, new ArrayList<>(values)));
testCases.add(newTestCase(values.get(0), null, EndpointType.RANGE_EXCLUSIVE, EndpointType.TREE_END, new ArrayList<>(values.subList(1, size))));
testCases.add(newTestCase(null, values.get(size - 1), EndpointType.TREE_START, EndpointType.RANGE_INCLUSIVE, new ArrayList<>(values)));
testCases.add(newTestCase(null, values.get(size - 1), EndpointType.TREE_START, EndpointType.RANGE_EXCLUSIVE, new ArrayList<>(values.subList(0, size - 1))));
testCases.add(newTestCase(null, null, EndpointType.TREE_START, EndpointType.TREE_END, new ArrayList<>(values)));
testCases.add(newTestCase(values.get(0), values.get(0), EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_INCLUSIVE, new ArrayList<>(values.subList(0, 1))));
// Only test this for LONG because it might be tricky to modify values for some other types.
if (KeyType.LONG.isMatch(values.get(0))) {
Tuple first = values.get(0);
Tuple justBeforeFirst = Tuple.from((Long) first.get(0) - 1);
Tuple justAfterFirst = Tuple.from((Long) first.get(0) + 1);
Tuple last = values.get(size - 1);
Tuple justBeforeLast = Tuple.from((Long) last.get(0) - 1);
Tuple justAfterLast = Tuple.from((Long) last.get(0) + 1);
// Endpoint type does not matters to the values who are not in the collection.
for (EndpointType endpointType : Arrays.asList(EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_EXCLUSIVE)) {
testCases.add(newTestCase(justBeforeFirst, null, endpointType, EndpointType.TREE_END, new ArrayList<>(values.subList(0, size))));
testCases.add(newTestCase(justAfterFirst, null, endpointType, EndpointType.TREE_END, new ArrayList<>(values.subList(1, size))));
testCases.add(newTestCase(null, justBeforeLast, EndpointType.TREE_START, endpointType, new ArrayList<>(values.subList(0, size - 1))));
testCases.add(newTestCase(null, justAfterLast, EndpointType.TREE_START, endpointType, new ArrayList<>(values.subList(0, size))));
}
}
if (size >= 2) {
Tuple first = values.get(0);
Tuple last = values.get(size - 1);
testCases.add(newTestCase(first, last, EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_INCLUSIVE, new ArrayList<>(values.subList(0, size))));
testCases.add(newTestCase(first, last, EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_EXCLUSIVE, new ArrayList<>(values.subList(0, size - 1))));
testCases.add(newTestCase(first, last, EndpointType.RANGE_EXCLUSIVE, EndpointType.RANGE_INCLUSIVE, new ArrayList<>(values.subList(1, size))));
testCases.add(newTestCase(first, last, EndpointType.RANGE_EXCLUSIVE, EndpointType.RANGE_EXCLUSIVE, new ArrayList<>(values.subList(1, size - 1))));
}
if (size >= 4) {
Tuple second = values.get(1);
Tuple secondLast = values.get(size - 2);
testCases.add(newTestCase(second, secondLast, EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_INCLUSIVE, new ArrayList<>(values.subList(1, size - 1))));
testCases.add(newTestCase(second, secondLast, EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_EXCLUSIVE, new ArrayList<>(values.subList(1, size - 2))));
testCases.add(newTestCase(second, secondLast, EndpointType.RANGE_EXCLUSIVE, EndpointType.RANGE_INCLUSIVE, new ArrayList<>(values.subList(2, size - 1))));
testCases.add(newTestCase(second, secondLast, EndpointType.RANGE_EXCLUSIVE, EndpointType.RANGE_EXCLUSIVE, new ArrayList<>(values.subList(2, size - 2))));
}
return testCases;
}
use of com.apple.foundationdb.tuple.Tuple 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());
}
}
use of com.apple.foundationdb.tuple.Tuple in project fdb-record-layer by FoundationDB.
the class ResolverCreateHooksTest method testPreWriteChecks.
@Test
void testPreWriteChecks() {
// reads the key, and chooses the resolver based on the value
final PreWriteCheck check = (context, providedResolver) -> {
CompletableFuture<LocatableResolver> expectedResolverFuture = root().add("should-use-A").toTupleAsync(context).thenCompose(keyTuple -> context.ensureActive().get(keyTuple.pack())).thenApply(value -> {
boolean useA = Tuple.fromBytes(value).getBoolean(0);
return new ScopedInterningLayer(database, resolverPath(context, useA ? "A" : "B"));
});
return expectedResolverFuture.thenApply(expectedResolver -> expectedResolver.equals(providedResolver));
};
final ResolverCreateHooks hooks = new ResolverCreateHooks(check, ResolverCreateHooks.DEFAULT_HOOK);
// use resolver A
database.run(context -> root().add("should-use-A").toTupleAsync(context).thenAccept(tuple -> context.ensureActive().set(tuple.pack(), Tuple.from(true).pack())));
try (FDBRecordContext context = database.openContext()) {
LocatableResolver resolverA = new ScopedInterningLayer(database, resolverPath(context, "A"));
LocatableResolver resolverB = new ScopedInterningLayer(database, resolverPath(context, "B"));
assertChecks(context, resolverA, hooks, true);
assertChecks(context, resolverB, hooks, false);
}
// use resolver B
database.run(context -> root().add("should-use-A").toTupleAsync(context).thenAccept(tuple -> context.ensureActive().set(tuple.pack(), Tuple.from(false).pack())));
// after migration
try (FDBRecordContext context = database.openContext()) {
LocatableResolver resolverA = new ScopedInterningLayer(database, resolverPath(context, "A"));
LocatableResolver resolverB = new ScopedInterningLayer(database, resolverPath(context, "B"));
assertChecks(context, resolverA, hooks, false);
assertChecks(context, resolverB, hooks, true);
}
}
use of com.apple.foundationdb.tuple.Tuple in project fdb-record-layer by FoundationDB.
the class FDBLuceneQueryTest method assertTermIndexedOrNot.
private void assertTermIndexedOrNot(String term, boolean indexedExpected, boolean shouldDeferFetch) throws Exception {
try (FDBRecordContext context = openContext()) {
openRecordStore(context);
final QueryComponent filter = new LuceneQueryComponent(term, Lists.newArrayList());
// Query for full records
RecordQuery query = RecordQuery.newBuilder().setRecordType(TextIndexTestUtils.SIMPLE_DOC).setFilter(filter).build();
setDeferFetchAfterUnionAndIntersection(shouldDeferFetch);
RecordQueryPlan plan = planner.plan(query);
RecordCursor<FDBQueriedRecord<Message>> fdbQueriedRecordRecordCursor = recordStore.executeQuery(plan);
RecordCursor<Tuple> map = fdbQueriedRecordRecordCursor.map(FDBQueriedRecord::getPrimaryKey);
List<Long> primaryKeys = map.map(t -> t.getLong(0)).asList().get();
if (indexedExpected) {
assertEquals(ImmutableSet.of(1L), ImmutableSet.copyOf(primaryKeys), "Expected term not indexed");
} else {
assertThat("Unexpected term indexed", primaryKeys.isEmpty());
}
}
}
Aggregations