Search in sources :

Example 6 with ResolverResult

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

the class StringInterningLayerTest method testTransactional.

@Test
void testTransactional() {
    final String toInternWithCommit;
    final ResolverResult committedValue;
    try (FDBRecordContext context = database.openContext()) {
        StringInterningLayer interningLayer = new StringInterningLayer(testSubspace);
        toInternWithCommit = "with-commit";
        committedValue = interningLayer.intern(context, toInternWithCommit).join();
        context.commit();
    }
    final String toInternNoCommit;
    final ResolverResult uncommittedValue;
    try (FDBRecordContext context = database.openContext()) {
        StringInterningLayer interningLayer = new StringInterningLayer(testSubspace);
        toInternNoCommit = "no-commit";
        uncommittedValue = interningLayer.intern(context, toInternNoCommit).join();
    }
    try (FDBRecordContext context = database.openContext()) {
        StringInterningLayer interningLayer = new StringInterningLayer(testSubspace);
        assertTrue(interningLayer.exists(context, toInternWithCommit).join(), "committed value exists");
        assertIsPresentWithValue("read value", interningLayer.read(context, toInternWithCommit).join(), committedValue);
        assertIsPresentWithValue("reverse lookup", interningLayer.readReverse(context, committedValue.getValue()).join(), toInternWithCommit);
        assertFalse(interningLayer.exists(context, toInternNoCommit).join(), "uncommitted value doesn't exist");
        assertFalse(interningLayer.read(context, toInternNoCommit).join().isPresent(), "read value is empty");
        assertFalse(interningLayer.readReverse(context, uncommittedValue.getValue()).join().isPresent(), "read reverse is empty");
    }
}
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 7 with ResolverResult

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

the class StringInterningLayerTest method testReverseLookup.

@Test
void testReverseLookup() {
    try (FDBRecordContext context = database.openContext()) {
        StringInterningLayer interningLayer = new StringInterningLayer(testSubspace);
        ResolverResult internedValue = interningLayer.intern(context, "a-string").join();
        Optional<String> maybeReverseLookup = interningLayer.readReverse(context, internedValue.getValue()).join();
        assertThat("we can lookup the string by the interned value", maybeReverseLookup.get(), is("a-string"));
        Optional<String> maybeNotThere = interningLayer.readReverse(context, internedValue.getValue() + 1).join();
        assertThat("lookups for missing values return nothing", maybeNotThere.isPresent(), is(false));
    }
}
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 8 with ResolverResult

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

the class StringInterningLayerTest method testCreate.

@Test
void testCreate() {
    try (FDBRecordContext context = database.openContext()) {
        StringInterningLayer interningLayer = new StringInterningLayer(testSubspace);
        String toIntern = "some-string";
        byte[] metadata = Tuple.from("some-metadata").pack();
        ResolverResult interned = interningLayer.create(context, toIntern, metadata).join();
        assertArrayEquals(interned.getMetadata(), metadata, "we see the metadata with the interned value");
        Optional<ResolverResult> maybeRead = interningLayer.read(context, toIntern).join();
        assertIsPresentWithValue("we read the value", maybeRead, interned);
        Optional<String> maybeReverseRead = interningLayer.readReverse(context, interned.getValue()).join();
        assertIsPresentWithValue("the reverse lookup works", maybeReverseRead, toIntern);
        try {
            interningLayer.create(context, toIntern, null).join();
            fail("should throw exception");
        } catch (CompletionException ex) {
            assertThat(ex.getCause(), is(instanceOf(RecordCoreException.class)));
            assertThat(ex.getCause().getMessage(), containsString("value already exists in interning layer"));
            RecordCoreException cause = (RecordCoreException) ex.getCause();
            assertThat("exception log info has the key", cause.getLogInfo(), hasEntry("value", toIntern));
        }
    }
}
Also used : RecordCoreException(com.apple.foundationdb.record.RecordCoreException) FDBRecordContext(com.apple.foundationdb.record.provider.foundationdb.FDBRecordContext) CompletionException(java.util.concurrent.CompletionException) ResolverResult(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverResult) Matchers.containsString(org.hamcrest.Matchers.containsString) Test(org.junit.jupiter.api.Test)

Example 9 with ResolverResult

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

the class StringInterningLayer method createMapping.

private CompletableFuture<ResolverResult> createMapping(@Nonnull FDBRecordContext context, @Nonnull final String toIntern, @Nullable final byte[] metadata) {
    final HighContentionAllocator hca = getHca(context);
    final byte[] mappingKey = mappingSubspace.pack(toIntern);
    return hca.allocate(toIntern).thenApply(allocated -> {
        ResolverResult result = new ResolverResult(allocated, metadata);
        context.ensureActive().set(mappingKey, serializeValue(result));
        return result;
    });
}
Also used : ResolverResult(com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverResult)

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