Search in sources :

Example 31 with FDBDatabase

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());
    }
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Test(org.junit.jupiter.api.Test)

Example 32 with FDBDatabase

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));
    }
}
Also used : ValueRange(com.apple.foundationdb.record.ValueRange) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Test(org.junit.jupiter.api.Test)

Example 33 with FDBDatabase

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!");
    }
}
Also used : Transaction(com.apple.foundationdb.Transaction) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Test(org.junit.jupiter.api.Test)

Example 34 with FDBDatabase

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));
    }
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Tuple(com.apple.foundationdb.tuple.Tuple) Test(org.junit.jupiter.api.Test)

Example 35 with FDBDatabase

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);
    }
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Tuple(com.apple.foundationdb.tuple.Tuple) Test(org.junit.jupiter.api.Test)

Aggregations

FDBDatabase (com.apple.foundationdb.record.provider.foundationdb.FDBDatabase)42 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)39 Test (org.junit.jupiter.api.Test)38 Tuple (com.apple.foundationdb.tuple.Tuple)19 FDBStoreTimer (com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer)14 Transaction (com.apple.foundationdb.Transaction)12 FDBDatabaseFactory (com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseFactory)11 ScanProperties (com.apple.foundationdb.record.ScanProperties)9 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)9 ArrayList (java.util.ArrayList)8 HashMap (java.util.HashMap)8 List (java.util.List)8 CompletableFuture (java.util.concurrent.CompletableFuture)8 Assertions.assertEquals (org.junit.jupiter.api.Assertions.assertEquals)8 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)7 ExecuteProperties (com.apple.foundationdb.record.ExecuteProperties)7 RecordCoreArgumentException (com.apple.foundationdb.record.RecordCoreArgumentException)7 RecordCursor (com.apple.foundationdb.record.RecordCursor)7 FDBTestBase (com.apple.foundationdb.record.provider.foundationdb.FDBTestBase)7 Tags (com.apple.test.Tags)7