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