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");
}
}
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));
}
}
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));
}
}
}
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;
});
}
Aggregations