Search in sources :

Example 1 with ScopedInterningLayer

use of com.apple.foundationdb.record.provider.foundationdb.layers.interning.ScopedInterningLayer 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);
    }
}
Also used : BeforeEach(org.junit.jupiter.api.BeforeEach) Tags(com.apple.test.Tags) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) Random(java.util.Random) CompletableFuture(java.util.concurrent.CompletableFuture) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) ScopedInterningLayer(com.apple.foundationdb.record.provider.foundationdb.layers.interning.ScopedInterningLayer) Collectors(java.util.stream.Collectors) FDBTestBase(com.apple.foundationdb.record.provider.foundationdb.FDBTestBase) Test(org.junit.jupiter.api.Test) Tuple(com.apple.foundationdb.tuple.Tuple) AfterEach(org.junit.jupiter.api.AfterEach) List(java.util.List) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) PreWriteCheck(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.PreWriteCheck) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) FDBDatabaseFactory(com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseFactory) Tag(org.junit.jupiter.api.Tag) CompletableFuture(java.util.concurrent.CompletableFuture) PreWriteCheck(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.PreWriteCheck) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ScopedInterningLayer(com.apple.foundationdb.record.provider.foundationdb.layers.interning.ScopedInterningLayer) Test(org.junit.jupiter.api.Test)

Example 2 with ScopedInterningLayer

use of com.apple.foundationdb.record.provider.foundationdb.layers.interning.ScopedInterningLayer in project fdb-record-layer by FoundationDB.

the class ResolverMappingDigestTest method testInterningLayerAndInterningLayer.

@Test
public void testInterningLayerAndInterningLayer() throws Exception {
    LocatableResolver primary;
    LocatableResolver replica;
    try (FDBRecordContext context = database.openContext()) {
        primary = new ScopedInterningLayer(database, keySpace.path("test-path").add("to").add("primary").toResolvedPath(context));
        replica = new ScopedInterningLayer(database, keySpace.path("test-path").add("to").add("replica").toResolvedPath(context));
    }
    testComputeDigest(primary, replica, false);
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ScopedInterningLayer(com.apple.foundationdb.record.provider.foundationdb.layers.interning.ScopedInterningLayer) Test(org.junit.jupiter.api.Test)

Example 3 with ScopedInterningLayer

use of com.apple.foundationdb.record.provider.foundationdb.layers.interning.ScopedInterningLayer in project fdb-record-layer by FoundationDB.

the class ResolverMappingDigestTest method testInterningLayerAndInterningLayerWithMetadata.

@Test
public void testInterningLayerAndInterningLayerWithMetadata() throws Exception {
    LocatableResolver primary;
    LocatableResolver replica;
    try (FDBRecordContext context = database.openContext()) {
        primary = new ScopedInterningLayer(database, keySpace.path("test-path").add("to").add("primary").toResolvedPath(context));
        replica = new ScopedInterningLayer(database, keySpace.path("test-path").add("to").add("replica").toResolvedPath(context));
    }
    byte[] metadata = Tuple.from("some-metadata").pack();
    testComputeDigest(primary, replica, true);
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ScopedInterningLayer(com.apple.foundationdb.record.provider.foundationdb.layers.interning.ScopedInterningLayer) Test(org.junit.jupiter.api.Test)

Example 4 with ScopedInterningLayer

use of com.apple.foundationdb.record.provider.foundationdb.layers.interning.ScopedInterningLayer in project fdb-record-layer by FoundationDB.

the class ResolverMappingDigestTest method testInterningLayerAndExtendedLayerWithMetadata.

@Test
public void testInterningLayerAndExtendedLayerWithMetadata() throws Exception {
    LocatableResolver primary;
    LocatableResolver replica;
    try (FDBRecordContext context = database.openContext()) {
        primary = new ScopedInterningLayer(database, keySpace.path("test-path").add("to").add("primary").toResolvedPath(context));
        replica = new ExtendedDirectoryLayer(database, keySpace.path("test-path").add("to").add("replica").toResolvedPath(context));
    }
    testComputeDigest(primary, replica, true);
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ScopedInterningLayer(com.apple.foundationdb.record.provider.foundationdb.layers.interning.ScopedInterningLayer) Test(org.junit.jupiter.api.Test)

Example 5 with ScopedInterningLayer

use of com.apple.foundationdb.record.provider.foundationdb.layers.interning.ScopedInterningLayer in project fdb-record-layer by FoundationDB.

the class KeySpaceDirectoryTest method getGenerator.

private Function<FDBRecordContext, CompletableFuture<LocatableResolver>> getGenerator() {
    String tmpDirLayer = "tmp-dir-layer-" + random.nextLong();
    KeySpace dirLayerKeySpace = new KeySpace(new KeySpaceDirectory(tmpDirLayer, KeyType.STRING, tmpDirLayer));
    return context -> CompletableFuture.completedFuture(new ScopedInterningLayer(context.getDatabase(), dirLayerKeySpace.path(tmpDirLayer).toResolvedPath(context)));
}
Also used : Arrays(java.util.Arrays) IsInstanceOf.instanceOf(org.hamcrest.core.IsInstanceOf.instanceOf) BiFunction(java.util.function.BiFunction) Assertions.assertNotEquals(org.junit.jupiter.api.Assertions.assertNotEquals) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) Random(java.util.Random) Transaction(com.apple.foundationdb.Transaction) Tuple(com.apple.foundationdb.tuple.Tuple) KeyValueLogMessage(com.apple.foundationdb.record.logging.KeyValueLogMessage) Pair(org.apache.commons.lang3.tuple.Pair) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) Map(java.util.Map) Matchers.nullValue(org.hamcrest.Matchers.nullValue) Tag(org.junit.jupiter.api.Tag) FDBStoreTimer(com.apple.foundationdb.record.provider.foundationdb.FDBStoreTimer) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) UUID(java.util.UUID) Assertions.assertNotSame(org.junit.jupiter.api.Assertions.assertNotSame) RecordCoreArgumentException(com.apple.foundationdb.record.RecordCoreArgumentException) Collectors(java.util.stream.Collectors) Matchers.startsWith(org.hamcrest.Matchers.startsWith) Test(org.junit.jupiter.api.Test) List(java.util.List) TupleHelpers(com.apple.foundationdb.tuple.TupleHelpers) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) RandomStringUtils(org.apache.commons.lang3.RandomStringUtils) Matchers.is(org.hamcrest.Matchers.is) DEFAULT_CHECK(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.DEFAULT_CHECK) IntStream(java.util.stream.IntStream) Assertions.assertNull(org.junit.jupiter.api.Assertions.assertNull) HashMap(java.util.HashMap) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) Function(java.util.function.Function) Supplier(java.util.function.Supplier) FDBTestBase(com.apple.foundationdb.record.provider.foundationdb.FDBTestBase) ArrayList(java.util.ArrayList) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) KeyType(com.apple.foundationdb.record.provider.foundationdb.keyspace.KeySpaceDirectory.KeyType) TestHelpers.eventually(com.apple.foundationdb.record.TestHelpers.eventually) FDBRecordStore(com.apple.foundationdb.record.provider.foundationdb.FDBRecordStore) Lists(com.google.common.collect.Lists) ImmutableList(com.google.common.collect.ImmutableList) EndpointType(com.apple.foundationdb.record.EndpointType) ScanProperties(com.apple.foundationdb.record.ScanProperties) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) LinkedList(java.util.LinkedList) NoSuchElementException(java.util.NoSuchElementException) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Iterator(java.util.Iterator) Tags(com.apple.test.Tags) ScopedInterningLayer(com.apple.foundationdb.record.provider.foundationdb.layers.interning.ScopedInterningLayer) TimeUnit(java.util.concurrent.TimeUnit) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) TestHelpers.assertThrows(com.apple.foundationdb.record.TestHelpers.assertThrows) FDBDatabaseFactory(com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseFactory) RecordCursor(com.apple.foundationdb.record.RecordCursor) ValueRange(com.apple.foundationdb.record.ValueRange) ScopedInterningLayer(com.apple.foundationdb.record.provider.foundationdb.layers.interning.ScopedInterningLayer)

Aggregations

FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)11 ScopedInterningLayer (com.apple.foundationdb.record.provider.foundationdb.layers.interning.ScopedInterningLayer)11 Test (org.junit.jupiter.api.Test)8 BeforeEach (org.junit.jupiter.api.BeforeEach)4 FDBDatabase (com.apple.foundationdb.record.provider.foundationdb.FDBDatabase)3 FDBDatabaseFactory (com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseFactory)2 FDBTestBase (com.apple.foundationdb.record.provider.foundationdb.FDBTestBase)2 Tuple (com.apple.foundationdb.tuple.Tuple)2 Tags (com.apple.test.Tags)2 List (java.util.List)2 Random (java.util.Random)2 CompletableFuture (java.util.concurrent.CompletableFuture)2 Transaction (com.apple.foundationdb.Transaction)1 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)1 EndpointType (com.apple.foundationdb.record.EndpointType)1 ExecuteProperties (com.apple.foundationdb.record.ExecuteProperties)1 RecordCoreArgumentException (com.apple.foundationdb.record.RecordCoreArgumentException)1 RecordCursor (com.apple.foundationdb.record.RecordCursor)1 ScanProperties (com.apple.foundationdb.record.ScanProperties)1 TestHelpers.assertThrows (com.apple.foundationdb.record.TestHelpers.assertThrows)1