Search in sources :

Example 1 with ResolverResult

use of com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverResult in project fdb-record-layer by FoundationDB.

the class StringInterningLayerTest method testReadValue.

@Test
void testReadValue() {
    try (FDBRecordContext context = database.openContext()) {
        StringInterningLayer interningLayer = new StringInterningLayer(testSubspace);
        ResolverResult internedValue = interningLayer.intern(context, "string-a").join();
        Optional<ResolverResult> maybereadValueA = interningLayer.read(context, "string-a").join();
        Optional<ResolverResult> maybereadValueB = interningLayer.read(context, "string-b").join();
        assertThat("we read strings that are there", maybereadValueA.get(), is(internedValue));
        assertThat("we get empty result for values not interned", maybereadValueB, is(Optional.empty()));
    }
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ResolverResult(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverResult) Test(org.junit.jupiter.api.Test)

Example 2 with ResolverResult

use of com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverResult in project fdb-record-layer by FoundationDB.

the class StringInterningLayerTest method testInterning.

@Test
void testInterning() {
    final ResolverResult internedValue;
    try (FDBRecordContext context = database.openContext()) {
        StringInterningLayer interningLayer = new StringInterningLayer(testSubspace);
        internedValue = interningLayer.intern(context, "a-string").join();
        for (int i = 0; i < 5; i++) {
            ResolverResult currentCall = interningLayer.intern(context, "a-string").join();
            assertThat("we see the same interned value for subsequent calls in the transaction", currentCall, is(internedValue));
        }
        context.commit();
    }
    try (FDBRecordContext context = database.openContext()) {
        StringInterningLayer interningLayer = new StringInterningLayer(testSubspace);
        for (int i = 0; i < 5; i++) {
            ResolverResult currentCall = interningLayer.intern(context, "a-string").join();
            assertThat("we see the same interned value for subsequent transactions", currentCall, is(internedValue));
        }
    }
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ResolverResult(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverResult) Test(org.junit.jupiter.api.Test)

Example 3 with ResolverResult

use of com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverResult in project fdb-record-layer by FoundationDB.

the class StringInterningLayerTest method testConcurrentAddSameValue.

@Test
void testConcurrentAddSameValue() {
    List<CompletableFuture<Pair<String, ResolverResult>>> futures = new ArrayList<>();
    for (int i = 0; i < 50; i++) {
        futures.add(database.runAsync(context -> {
            StringInterningLayer interningLayer = new StringInterningLayer(testSubspace);
            return interningLayer.intern(context, "same-string");
        }).thenApply(value -> Pair.of("same-string", value)));
    }
    final Set<Pair<String, ResolverResult>> allocationSet = new HashSet<>(AsyncUtil.getAll(futures).join());
    assertThat("only one value is allocated", allocationSet, hasSize(1));
    ResolverResult allocated = allocationSet.stream().findFirst().get().getRight();
    try (FDBRecordContext context = database.openContext()) {
        StringInterningLayer interningLayer = new StringInterningLayer(testSubspace);
        assertIsPresentWithValue("forward mapping is consistent", interningLayer.read(context, "same-string").join(), allocated);
        assertIsPresentWithValue("reverse mapping is consistent", interningLayer.readReverse(context, allocated.getValue()).join(), "same-string");
    }
}
Also used : IntStream(java.util.stream.IntStream) Assertions.fail(org.junit.jupiter.api.Assertions.fail) BeforeEach(org.junit.jupiter.api.BeforeEach) IsInstanceOf.instanceOf(org.hamcrest.core.IsInstanceOf.instanceOf) ResolverResult(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverResult) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) FDBTestBase(com.apple.foundationdb.record.provider.foundationdb.FDBTestBase) Subspace(com.apple.foundationdb.subspace.Subspace) ArrayList(java.util.ArrayList) HashSet(java.util.HashSet) ExceptionMessageMatcher.hasMessageContaining(com.apple.foundationdb.record.TestHelpers.ExceptionMessageMatcher.hasMessageContaining) Tuple(com.apple.foundationdb.tuple.Tuple) Range(com.apple.foundationdb.Range) Pair(org.apache.commons.lang3.tuple.Pair) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) Map(java.util.Map) Is.is(org.hamcrest.core.Is.is) Matchers.hasSize(org.hamcrest.Matchers.hasSize) Tag(org.junit.jupiter.api.Tag) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) NoSuchElementException(java.util.NoSuchElementException) Matchers.hasEntry(org.hamcrest.Matchers.hasEntry) Tags(com.apple.test.Tags) Set(java.util.Set) CompletionException(java.util.concurrent.CompletionException) FDBDatabase(com.apple.foundationdb.record.provider.foundationdb.FDBDatabase) Collectors(java.util.stream.Collectors) Test(org.junit.jupiter.api.Test) Assertions.assertArrayEquals(org.junit.jupiter.api.Assertions.assertArrayEquals) List(java.util.List) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) FDBDatabaseFactory(com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseFactory) Optional(java.util.Optional) Matchers.containsString(org.hamcrest.Matchers.containsString) CompletableFuture(java.util.concurrent.CompletableFuture) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ArrayList(java.util.ArrayList) ResolverResult(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverResult) Matchers.containsString(org.hamcrest.Matchers.containsString) Pair(org.apache.commons.lang3.tuple.Pair) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 4 with ResolverResult

use of com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverResult in project fdb-record-layer by FoundationDB.

the class StringInterningLayerTest method testUpdateMetadata.

@Test
void testUpdateMetadata() {
    byte[] oldMetadata = Tuple.from("old-metadata").pack();
    byte[] newMetadata = Tuple.from("new-metadata").pack();
    StringInterningLayer interningLayer;
    ResolverResult initial;
    ResolverResult updated;
    final String key = "update-metadata";
    try (FDBRecordContext context = database.openContext()) {
        interningLayer = new StringInterningLayer(testSubspace);
        initial = interningLayer.create(context, key, oldMetadata).join();
        interningLayer.updateMetadata(context, key, newMetadata).join();
        updated = interningLayer.read(context, key).join().orElseThrow(() -> new AssertionError("should be present"));
        assertThat("we see the new metadata", updated, is(new ResolverResult(initial.getValue(), newMetadata)));
        context.commit();
    }
    try (FDBRecordContext context = database.openContext()) {
        ResolverResult newTransactionRead = interningLayer.read(context, key).join().orElseThrow(() -> new AssertionError("should be present"));
        assertThat("we see the committed metadata in a new transaction", newTransactionRead, is(updated));
    }
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ResolverResult(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverResult) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.jupiter.api.Test)

Example 5 with ResolverResult

use of com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverResult in project fdb-record-layer by FoundationDB.

the class StringInterningLayerTest method testFilterInvalidAllocationValues.

@Test
@Tag(Tags.WipesFDB)
void testFilterInvalidAllocationValues() {
    Range everything = new Range(new byte[] { (byte) 0x00 }, new byte[] { (byte) 0xFF });
    database.run(context -> {
        context.ensureActive().clear(everything);
        return null;
    });
    try (FDBRecordContext context = database.openContext()) {
        StringInterningLayer interningLayer = new StringInterningLayer(testSubspace, true);
        // so write something for all of those keys
        for (int i = 0; i < 64; i++) {
            byte[] key = Tuple.from(i, "string-" + i).pack();
            byte[] value = new byte[0];
            context.ensureActive().set(key, value);
        }
        try {
            interningLayer.intern(context, "a-string").join();
            fail("intern should throw exception");
        } catch (Exception e) {
            assertThat("a", e.getCause().getMessage(), is("database already has keys in allocation range"));
        }
        Optional<ResolverResult> maybeRead = interningLayer.read(context, "a-string").join();
        assertThat("we don't read anything", maybeRead, is(Optional.empty()));
    }
}
Also used : FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) ResolverResult(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverResult) Range(com.apple.foundationdb.Range) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) NoSuchElementException(java.util.NoSuchElementException) CompletionException(java.util.concurrent.CompletionException) Test(org.junit.jupiter.api.Test) Tag(org.junit.jupiter.api.Tag)

Aggregations

ResolverResult (com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverResult)9 FDBRecordContext (com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext)8 Test (org.junit.jupiter.api.Test)8 Matchers.containsString (org.hamcrest.Matchers.containsString)5 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)3 CompletionException (java.util.concurrent.CompletionException)3 Range (com.apple.foundationdb.Range)2 NoSuchElementException (java.util.NoSuchElementException)2 Tag (org.junit.jupiter.api.Tag)2 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)1 ExceptionMessageMatcher.hasMessageContaining (com.apple.foundationdb.record.TestHelpers.ExceptionMessageMatcher.hasMessageContaining)1 FDBDatabase (com.apple.foundationdb.record.provider.foundationdb.FDBDatabase)1 FDBDatabaseFactory (com.apple.foundationdb.record.provider.foundationdb.FDBDatabaseFactory)1 FDBTestBase (com.apple.foundationdb.record.provider.foundationdb.FDBTestBase)1 Subspace (com.apple.foundationdb.subspace.Subspace)1 Tuple (com.apple.foundationdb.tuple.Tuple)1 Tags (com.apple.test.Tags)1 ArrayList (java.util.ArrayList)1 HashSet (java.util.HashSet)1 List (java.util.List)1