Search in sources :

Example 31 with Transaction

use of com.apple.foundationdb.Transaction in project fdb-record-layer by FoundationDB.

the class KeySpaceDirectoryTest method testListConstantValue.

@Test
public void testListConstantValue() throws Exception {
    // Create a root directory called "a" with subdirs of every type and a constant value
    Long rootValue = random.nextLong();
    KeySpaceDirectory dirA = new KeySpaceDirectory("a", KeyType.LONG, rootValue);
    for (KeyTypeValue kv : valueOfEveryType) {
        dirA.addSubdirectory(new KeySpaceDirectory(kv.keyType.toString(), kv.keyType, kv.generator.get()));
    }
    KeySpace root = new KeySpace(dirA);
    final FDBDatabase database = FDBDatabaseFactory.instance().getDatabase();
    try (FDBRecordContext context = database.openContext()) {
        Transaction tr = context.ensureActive();
        for (KeyTypeValue kv : valueOfEveryType) {
            KeySpaceDirectory dir = root.getDirectory("a").getSubdirectory(kv.keyType.name());
            for (int i = 0; i < 5; i++) {
                tr.set(Tuple.from(rootValue, dir.getValue(), i).pack(), Tuple.from(i).pack());
            }
        }
        context.commit();
    }
    try (FDBRecordContext context = database.openContext()) {
        for (KeyTypeValue kv : valueOfEveryType) {
            KeySpaceDirectory dir = root.getDirectory("a").getSubdirectory(kv.keyType.name());
            List<ResolvedKeySpacePath> paths = root.path("a").listSubdirectory(context, kv.keyType.toString());
            assertEquals(1, paths.size());
            if (dir.getKeyType() == KeyType.BYTES) {
                assertTrue(Arrays.equals((byte[]) dir.getValue(), paths.get(0).toTuple().getBytes(1)));
            } else {
                assertEquals(dir.getValue(), paths.get(0).toTuple().get(1));
            }
        }
    }
}
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 32 with Transaction

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

Example 33 with Transaction

use of com.apple.foundationdb.Transaction in project fdb-record-layer by FoundationDB.

the class FDBDatabaseTest method testCommitLatencyInjection.

@Test
public void testCommitLatencyInjection() throws Exception {
    testLatencyInjection(FDBLatencySource.COMMIT_ASYNC, 300L, context -> {
        final Transaction tr = context.ensureActive();
        tr.clear(new byte[] { (byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte) 0xef });
        context.commit();
    });
}
Also used : Transaction(com.apple.foundationdb.Transaction) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Example 34 with Transaction

use of com.apple.foundationdb.Transaction in project fdb-record-layer by FoundationDB.

the class FDBReverseDirectoryCacheTest method testPutIfNotExistsNotVisibleUntilCommit.

@Test
public void testPutIfNotExistsNotVisibleUntilCommit() throws Exception {
    final ScopedDirectoryLayer scope = globalScope;
    final String name = "dir_" + Math.abs(new Random().nextInt());
    final FDBReverseDirectoryCache rdc = fdb.getReverseDirectoryCache();
    final ScopedValue<String> scopedName = scope.wrap(name);
    // Create a new directory layer entry, put it in the cache in the same transaction
    try (FDBRecordContext context = openContext()) {
        Transaction transaction = context.ensureActive();
        DirectoryLayer directoryLayerToUse = new DirectoryLayer(context.join(scope.getNodeSubspace(context)), scope.getContentSubspace());
        final byte[] rawDirectoryEntry = directoryLayerToUse.createOrOpen(transaction, Collections.singletonList(name)).get().getKey();
        final Long id = Tuple.fromBytes(rawDirectoryEntry).getLong(0);
        rdc.putIfNotExists(context, scopedName, id).get();
        // This happens in its own transaction so should not yet see the uncommitted directory and reverse
        // directory entries that are being created.
        Optional<String> result = rdc.get(scope.wrap(id)).join();
        assertFalse(result.isPresent(), "Should not have gotten a result from RDC lookup");
        commit(context);
    }
}
Also used : DirectoryLayer(com.apple.foundationdb.directory.DirectoryLayer) ScopedDirectoryLayer(com.apple.foundationdb.record.provider.foundationdb.keyspace.ScopedDirectoryLayer) ScopedDirectoryLayer(com.apple.foundationdb.record.provider.foundationdb.keyspace.ScopedDirectoryLayer) Random(java.util.Random) Transaction(com.apple.foundationdb.Transaction) Test(org.junit.jupiter.api.Test)

Example 35 with Transaction

use of com.apple.foundationdb.Transaction in project fdb-record-layer by FoundationDB.

the class FDBReverseDirectoryCacheTest method testPutIfNotExistsWrongValue.

@Test
public void testPutIfNotExistsWrongValue() throws Exception {
    final Random random = new Random();
    final String name = "dir_" + Math.abs(random.nextInt());
    final Long id;
    ScopedValue<String> scopedName = globalScope.wrap(name);
    try (FDBRecordContext context = openContext()) {
        Transaction tr = context.ensureActive();
        // need to use the FDB DirectoryLayer to bypass LocatableResolver which populates the reverse directory cache automatically
        id = Tuple.fromBytes(DirectoryLayer.getDefault().createOrOpen(tr, PathUtil.from(name)).get().pack()).getLong(0);
        commit(context);
    }
    FDBReverseDirectoryCache rdc = fdb.getReverseDirectoryCache();
    // Store the correct value
    try (FDBRecordContext context = openContext()) {
        rdc.putIfNotExists(context, scopedName, id).get();
        // The put should be considered a hard miss because it had to write to the cache
        assertEquals(0L, rdc.getPersistentCacheHitCount());
        assertEquals(1L, rdc.getPersistentCacheMissCount());
        assertEquals(0L, context.getTimer().getCount(FDBStoreTimer.Counts.REVERSE_DIR_PERSISTENT_CACHE_HIT_COUNT));
        assertEquals(1L, context.getTimer().getCount(FDBStoreTimer.Counts.REVERSE_DIR_PERSISTENT_CACHE_MISS_COUNT));
        commit(context);
    }
    // Try again with a different value
    try (FDBRecordContext context = openContext()) {
        rdc.putIfNotExists(context, globalScope.wrap(name + "_x"), id).get();
        commit(context);
        fail("Should have thrown an exception due to wrong value");
    } catch (ExecutionException e) {
        // This will throw if we didn't get the exception we wanted
        RecordCoreException yay = (RecordCoreException) e.getCause();
    }
}
Also used : RecordCoreException(com.apple.foundationdb.record.RecordCoreException) Random(java.util.Random) Transaction(com.apple.foundationdb.Transaction) ExecutionException(java.util.concurrent.ExecutionException) Test(org.junit.jupiter.api.Test)

Aggregations

Transaction (com.apple.foundationdb.Transaction)84 ReadTransaction (com.apple.foundationdb.ReadTransaction)34 Tuple (com.apple.foundationdb.tuple.Tuple)34 Test (org.junit.jupiter.api.Test)33 Nonnull (javax.annotation.Nonnull)28 ArrayList (java.util.ArrayList)26 List (java.util.List)26 CompletableFuture (java.util.concurrent.CompletableFuture)26 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)22 Subspace (com.apple.foundationdb.subspace.Subspace)21 Collectors (java.util.stream.Collectors)19 Nullable (javax.annotation.Nullable)19 KeyValue (com.apple.foundationdb.KeyValue)18 Map (java.util.Map)18 KeyValueLogMessage (com.apple.foundationdb.record.logging.KeyValueLogMessage)17 Range (com.apple.foundationdb.Range)16 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)16 AtomicReference (java.util.concurrent.atomic.AtomicReference)16 Collections (java.util.Collections)15 RecordCursor (com.apple.foundationdb.record.RecordCursor)14