use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method testListPreservesWrapper.
@Test
public void testListPreservesWrapper() {
KeySpace keySpace = new KeySpace(new KeySpaceDirectory("a", KeyType.STRING, PathA::new).addSubdirectory(new KeySpaceDirectory("b", KeyType.STRING, PathB::new)));
final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
try (FDBRecordContext context = database.openContext()) {
Transaction tr = context.ensureActive();
PathA root = (PathA) keySpace.path("a", "foo");
tr.set(root.add("b", "one").toTuple(context).pack(), TupleHelpers.EMPTY.pack());
tr.set(root.add("b", "two").toTuple(context).pack(), TupleHelpers.EMPTY.pack());
tr.set(root.add("b", "three").toTuple(context).pack(), TupleHelpers.EMPTY.pack());
tr.commit();
}
try (FDBRecordContext context = database.openContext()) {
List<ResolvedKeySpacePath> paths = keySpace.path("a", "foo").listSubdirectory(context, "b");
for (ResolvedKeySpacePath path : paths) {
assertThat("Path should be PathB", path, instanceOf(PathB.class));
assertThat("parent should be PathA", path.getParent(), instanceOf(PathA.class));
}
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method testInvalidPath.
@Test
public void testInvalidPath() throws Exception {
KeySpace root = new KeySpace(new KeySpaceDirectory("root", KeyType.STRING, "production").addSubdirectory(new KeySpaceDirectory("a", KeyType.STRING).addSubdirectory(new KeySpaceDirectory("b", KeyType.STRING))));
final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
try (FDBRecordContext context = database.openContext()) {
// Building a tuple in the correct order works
Tuple tuple = root.path("root").add("a", "foo").add("b", "bar").toTuple(context);
assertEquals(Tuple.from("production", "foo", "bar"), tuple);
// Walking in the wrong order fails
assertThrows(NoSuchDirectoryException.class, () -> root.path("foo").add("a", "bar").toTuple(context));
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method testDirectoryLayerDirectoryValidation.
@Test
public void testDirectoryLayerDirectoryValidation() throws Exception {
KeySpace root = new KeySpace(new DirectoryLayerDirectory("school", "school").addSubdirectory(new DirectoryLayerDirectory("school_name").addSubdirectory(new DirectoryLayerDirectory("teachers", "teachers")).addSubdirectory(new DirectoryLayerDirectory("students", "students"))));
final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
final Tuple teachersTuple;
final Tuple studentsTuple;
try (FDBRecordContext context = database.openContext()) {
teachersTuple = root.path("school").add("school_name", "Football Tech").add("teachers").toTuple(context);
studentsTuple = root.path("school").add("school_name", "Football Tech").add("students").toTuple(context);
context.commit();
}
try (FDBRecordContext context = database.openContext()) {
// Use the wrong directory layer value for the students and teachers
assertThrows(RecordCoreArgumentException.class, () -> root.path("school").add("school_name", "Football Tech").add("teachers", studentsTuple.getLong(1)).toTuple(context));
assertThrows(RecordCoreArgumentException.class, () -> root.path("school").add("school_name", "Football Tech").add("students", teachersTuple.getLong(1)).toTuple(context));
// Use a value that does not exist in the directory layer as the school name.
assertThrows(NoSuchElementException.class, () -> root.path("school").add("school_name", -746464638L).add("teachers").toTuple(context));
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method testListAcrossTransactions.
@Test
public void testListAcrossTransactions() throws Exception {
KeySpace root = new KeySpace(new KeySpaceDirectory("a", KeyType.LONG, random.nextLong()).addSubdirectory(new KeySpaceDirectory("b", KeyType.STRING)));
final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
final List<String> directoryEntries = IntStream.range(0, 10).boxed().map(i -> "val_" + i).collect(Collectors.toList());
final KeySpacePath rootPath = root.path("a");
try (final FDBRecordContext context = database.openContext()) {
final Transaction tr = context.ensureActive();
directoryEntries.forEach(name -> tr.set(rootPath.add("b", name).toTuple(context).pack(), TupleHelpers.EMPTY.pack()));
context.commit();
}
byte[] continuation = null;
int idx = 0;
do {
try (final FDBRecordContext context = database.openContext()) {
final RecordCursor<ResolvedKeySpacePath> cursor = rootPath.listSubdirectoryAsync(context, "b", continuation, new ScanProperties(ExecuteProperties.newBuilder().setReturnedRowLimit(2).build()));
List<ResolvedKeySpacePath> subdirs = context.asyncToSync(FDBStoreTimer.Waits.WAIT_KEYSPACE_LIST, cursor.asList());
if (!subdirs.isEmpty()) {
assertEquals(2, subdirs.size(), "Wrong number of path entries returned");
assertEquals("val_" + idx, subdirs.get(0).getResolvedValue());
assertEquals("val_" + (idx + 1), subdirs.get(1).getResolvedValue());
idx += 2;
continuation = cursor.getNext().getContinuation().toBytes();
System.out.println(continuation == null ? "null" : Tuple.fromBytes(continuation));
} else {
continuation = cursor.getNext().getContinuation().toBytes();
assertNull(continuation);
}
}
} while (continuation != null);
assertEquals(directoryEntries.size(), idx);
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method testAllTypesAnyValues.
@Test
public void testAllTypesAnyValues() throws Exception {
KeySpaceDirectory rootDir = new KeySpaceDirectory("root", KeyType.LONG, 1L);
for (KeyTypeValue kv : valueOfEveryType) {
rootDir.addSubdirectory(new KeySpaceDirectory(kv.keyType.toString(), kv.keyType));
}
KeySpace root = new KeySpace(rootDir);
final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
try (FDBRecordContext context = database.openContext()) {
for (KeyTypeValue kv : valueOfEveryType) {
// Test that we can set a good type and get the right tuple back
final Object value = kv.generator.get();
assertEquals(Tuple.from(1L, value), root.path("root").add(kv.keyType.toString(), value).toTuple(context));
final Object badValue = pickDifferentType(kv.keyType).generator.get();
assertThrows(RecordCoreArgumentException.class, () -> root.path("root").add(kv.keyType.toString(), badValue).toTuple(context));
}
}
}
Aggregations