use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method testDirectoryLayerDirectoryWithMetadata.
@Test
public void testDirectoryLayerDirectoryWithMetadata() {
String testRoot = "test-root-" + random.nextInt();
ResolverCreateHooks hooks = new ResolverCreateHooks(DEFAULT_CHECK, DirWithMetadataWrapper::metadataHook);
KeySpace root = rootForMetadataTests(testRoot, hooks, getGenerator());
final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
try (FDBRecordContext context = database.openContext()) {
String dir1 = "test-string-" + random.nextInt();
String dir2 = "test-string-" + random.nextInt();
KeySpacePath path1 = root.path(testRoot).add("dir_with_metadata_name", dir1);
KeySpacePath path2 = root.path(testRoot).add("dir_with_metadata_name", dir2);
assertThat("path gets wrapped", path1, is(instanceOf(DirWithMetadataWrapper.class)));
assertThat("path gets wrapped", path2, is(instanceOf(DirWithMetadataWrapper.class)));
DirWithMetadataWrapper wrapped1 = (DirWithMetadataWrapper) path1;
DirWithMetadataWrapper wrapped2 = (DirWithMetadataWrapper) path2;
assertArrayEquals(wrapped1.metadata(context).join(), Tuple.from(dir1, dir1.length()).pack());
assertArrayEquals(wrapped2.metadata(context).join(), Tuple.from(dir2, dir2.length()).pack());
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method testInvalidListRange.
@Test
public void testInvalidListRange() throws Exception {
final String rootDir = "root_dir";
final String stringDir = "string_dir";
final String longConstDir = "long_const_dir";
KeySpaceDirectory dirA = new KeySpaceDirectory(rootDir, KeyType.LONG, random.nextLong()).addSubdirectory(new KeySpaceDirectory(stringDir, KeyType.STRING)).addSubdirectory(new KeySpaceDirectory(longConstDir, KeyType.LONG, 100));
KeySpace root = new KeySpace(dirA);
final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
try (FDBRecordContext context = database.openContext()) {
// Positive example.
root.path(rootDir).listSubdirectory(context, stringDir, new ValueRange<>("A", "B", EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_EXCLUSIVE), null, ScanProperties.FORWARD_SCAN);
// The range value should be in the same type.
assertThrows(RecordCoreArgumentException.class, () -> root.path(rootDir).listSubdirectory(context, stringDir, new ValueRange<>(100, 200, EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_EXCLUSIVE), null, ScanProperties.FORWARD_SCAN));
// PREFIX_STRING should not be used as a endpoint type.
assertThrows(RecordCoreArgumentException.class, () -> root.path(rootDir).listSubdirectory(context, stringDir, new ValueRange<>("A", "B", EndpointType.PREFIX_STRING, EndpointType.RANGE_EXCLUSIVE), null, ScanProperties.FORWARD_SCAN));
// Range should be null when the subdirectory has a value.
assertThrows(RecordCoreArgumentException.class, () -> root.path(rootDir).listSubdirectory(context, longConstDir, new ValueRange<>(100, 200, EndpointType.RANGE_INCLUSIVE, EndpointType.RANGE_EXCLUSIVE), null, ScanProperties.FORWARD_SCAN));
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method testDeleteAllDataAndHasData.
@Test
public void testDeleteAllDataAndHasData() throws Exception {
KeySpace root = new KeySpace(new KeySpaceDirectory("root", KeyType.LONG, Math.abs(random.nextLong())).addSubdirectory(new KeySpaceDirectory("dir1", KeyType.STRING, "a").addSubdirectory(new KeySpaceDirectory("dir1_1", KeyType.LONG))).addSubdirectory(new KeySpaceDirectory("dir2", KeyType.STRING, "b").addSubdirectory(new KeySpaceDirectory("dir2_1", KeyType.LONG))).addSubdirectory(new KeySpaceDirectory("dir3", KeyType.STRING, "c").addSubdirectory(new KeySpaceDirectory("dir3_1", KeyType.LONG))));
final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
try (FDBRecordContext context = database.openContext()) {
Transaction tr = context.ensureActive();
for (int i = 0; i < 5; i++) {
tr.set(root.path("root").add("dir1").add("dir1_1", i).toTuple(context).pack(), Tuple.from(i).pack());
tr.set(root.path("root").add("dir2").add("dir2_1", i).toTuple(context).pack(), Tuple.from(i).pack());
tr.set(root.path("root").add("dir3").add("dir3_1", i).toTuple(context).pack(), Tuple.from(i).pack());
}
context.commit();
}
try (FDBRecordContext context = database.openContext()) {
// All directories hava data?
for (int i = 1; i <= 3; i++) {
assertTrue(root.path("root").add("dir" + i).hasData(context), "dir" + i + " is empty!");
}
// Clear out dir2
root.path("root").add("dir2").deleteAllData(context);
context.commit();
}
try (FDBRecordContext context = database.openContext()) {
assertTrue(root.path("root").add("dir1").hasData(context), "dir1 is empty!");
assertFalse(root.path("root").add("dir2").hasData(context), "dir2 has data!");
assertTrue(root.path("root").add("dir3").hasData(context), "dir3 is empty!");
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method testMetadataFromLookupByKey.
@Test
public void testMetadataFromLookupByKey() {
String testRoot = "test-root-" + random.nextInt();
ResolverCreateHooks hooks = new ResolverCreateHooks(DEFAULT_CHECK, DirWithMetadataWrapper::metadataHook);
KeySpace root = rootForMetadataTests(testRoot, hooks, getGenerator());
final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
Tuple tuple;
String dir = "test-string-" + random.nextInt();
try (FDBRecordContext context = database.openContext()) {
KeySpacePath path1 = root.path(testRoot).add("dir_with_metadata_name", dir);
tuple = path1.toTuple(context);
context.ensureActive().set(tuple.pack(), Tuple.from(0).pack());
DirWithMetadataWrapper wrapped = (DirWithMetadataWrapper) path1;
assertArrayEquals(wrapped.metadata(context).join(), DirWithMetadataWrapper.metadataHook(dir));
context.commit();
}
try (FDBRecordContext context = database.openContext()) {
DirWithMetadataWrapper fromPath = (DirWithMetadataWrapper) root.resolveFromKey(context, tuple).toPath();
assertArrayEquals(fromPath.metadata(context).join(), DirWithMetadataWrapper.metadataHook(dir));
}
}
use of com.apple.foundationdb.record.provider.foundationdb.FDBDatabase in project fdb-record-layer by FoundationDB.
the class KeySpaceDirectoryTest method testFromTupleWithConstantValue.
@Test
public void testFromTupleWithConstantValue() throws Exception {
KeySpace root = new KeySpace(new KeySpaceDirectory("root", KeyType.LONG, 1L).addSubdirectory(new KeySpaceDirectory("dir1", KeyType.STRING, "a")).addSubdirectory(new KeySpaceDirectory("dir2", KeyType.STRING, "b")).addSubdirectory(new KeySpaceDirectory("dir3", KeyType.LONG)));
final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
try (FDBRecordContext context = database.openContext()) {
Tuple tuple = Tuple.from(1L, "a");
assertEquals(tuple, root.resolveFromKey(context, tuple).toTuple());
tuple = Tuple.from(1L, "b");
assertEquals(tuple, root.resolveFromKey(context, tuple).toTuple());
final Tuple badTuple1 = Tuple.from(1L, "c", "d");
assertThrows(RecordCoreArgumentException.class, () -> root.resolveFromKey(context, badTuple1).toTuple(), "key_tuple", badTuple1, "key_tuple_pos", 1);
}
}
Aggregations