use of com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.MetadataHook in project fdb-record-layer by FoundationDB.
the class ResolverMappingReplicatorTest method seedDirectoryLayer.
protected Map<String, ResolverResult> seedDirectoryLayer(FDBRecordContext context, LocatableResolver scope, int entries) {
Map<String, ResolverResult> mappings = new HashMap<>();
for (int i = 0; i < entries; i++) {
String key = "entry-" + random.nextLong();
ResolverResult result;
if (seedWithMetadata) {
// include metadata in ~1/2 the entries
MetadataHook hook = random.nextBoolean() ? name -> Tuple.from("metadata-for-key-" + name, random.nextLong()).pack() : ignore -> null;
result = scope.resolveWithMetadata(context.getTimer(), key, new ResolverCreateHooks(ResolverCreateHooks.DEFAULT_CHECK, hook)).join();
} else {
result = scope.resolveWithMetadata(context.getTimer(), key, ResolverCreateHooks.getDefault()).join();
}
mappings.put(key, result);
}
context.commit();
return mappings;
}
use of com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.MetadataHook in project fdb-record-layer by FoundationDB.
the class ScopedDirectoryLayerTest method testResolveWithMetadata.
@Test
@Override
public void testResolveWithMetadata() {
ResolverCreateHooks noMetadata = ResolverCreateHooks.getDefault();
String key1 = "key1";
assertThat(noMetadata.getMetadataHook().apply(key1), is(nullValue()));
// works as long as the metadatahook returns null
globalScope.resolveWithMetadata(key1, noMetadata).join();
String key2 = "key2";
MetadataHook hook = name -> Tuple.from(name).pack();
ResolverCreateHooks withMetadata = new ResolverCreateHooks(ResolverCreateHooks.DEFAULT_CHECK, hook);
assertThat(withMetadata.getMetadataHook().apply(key2), is(not(nullValue())));
assertThrows(CompletionException.class, () -> globalScope.resolveWithMetadata(key2, withMetadata).join());
}
use of com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.MetadataHook in project fdb-record-layer by FoundationDB.
the class ExtendedDirectoryLayerTest method testScopedDirectoryLayerResolvesWithoutMetadata.
@Test
public void testScopedDirectoryLayerResolvesWithoutMetadata() {
MetadataHook hook = name -> Tuple.from("metadata-for-" + name).pack();
ResolverCreateHooks createHooks = new ResolverCreateHooks(ResolverCreateHooks.DEFAULT_CHECK, hook);
ResolverResult result = globalScope.resolveWithMetadata("some-key", createHooks).join();
assertArrayEquals(Tuple.from("metadata-for-some-key").pack(), result.getMetadata(), "metadata was added");
ResolverResult resultFromScoped = ScopedDirectoryLayer.global(database).resolveWithMetadata("some-key", /* no hooks */
ResolverCreateHooks.getDefault()).join();
assertEquals(resultFromScoped.getValue(), result.getValue());
assertThat(resultFromScoped.getMetadata(), is(nullValue()));
try (FDBRecordContext context = database.openContext()) {
assertArrayEquals(globalScope.mustResolveWithMetadata(context, "some-key").join().getMetadata(), Tuple.from("metadata-for-some-key").pack(), "we can still read the metadata with " + globalScope.getClass().getName());
}
}
use of com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.MetadataHook in project fdb-record-layer by FoundationDB.
the class ResolverMappingDigestTest method testComputeDigest.
private void testComputeDigest(LocatableResolver primary, LocatableResolver replica, boolean allowMetadata) throws Exception {
SortedMap<String, ResolverResult> mappings = new TreeMap<>();
ResolverResult result;
for (int i = 0; i < 10; i++) {
String key = "some-key-" + i;
// if we allow metadata, set the metadata ~1/2 the time
boolean metadataForThisKey = allowMetadata && random.nextBoolean();
byte[] metadata = metadataForThisKey ? Tuple.from("some metadata for key: " + key).pack() : null;
MetadataHook hook = ignore -> metadata;
result = primary.resolveWithMetadata(key, new ResolverCreateHooks(DEFAULT_CHECK, hook)).join();
mappings.put(key, result);
}
ResolverMappingReplicator replicator = new ResolverMappingReplicator(primary);
replicator.copyTo(replica);
final byte[] expectedDigest = expectedDigest(mappings);
final byte[] wrongKeyDigest = wrongKeyDigest(mappings);
final byte[] wrongValueDigest = wrongValueDigest(mappings);
final byte[] wrongMetadataDigest = wrongMetadata(mappings);
final byte[] extraEntryDigest = extraEntry(mappings);
ResolverMappingDigest primaryResolverMappingDigest = new ResolverMappingDigest(primary);
ResolverMappingDigest replicaResolverMappingDigest = new ResolverMappingDigest(replica);
assertThat("digests match only if key, value and metadata are the same (independent of scope)", primaryResolverMappingDigest.computeDigest().join(), allOf(is(expectedDigest), is(replicaResolverMappingDigest.computeDigest().join()), is(not(wrongKeyDigest)), is(not(wrongValueDigest)), is(not(wrongMetadataDigest)), is(not(extraEntryDigest))));
}
use of com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.MetadataHook in project fdb-record-layer by FoundationDB.
the class LocatableResolverTest method testResolveWithMetadata.
@Test
public void testResolveWithMetadata() {
byte[] metadata = Tuple.from("some-metadata").pack();
MetadataHook hook = ignore -> metadata;
final ResolverResult result;
final ResolverCreateHooks hooks = new ResolverCreateHooks(DEFAULT_CHECK, hook);
result = globalScope.resolveWithMetadata("a-key", hooks).join();
assertArrayEquals(metadata, result.getMetadata());
// check that the result with metadata is persisted to the database
ResolverResult expected = new ResolverResult(result.getValue(), metadata);
try (FDBRecordContext context = database.openContext()) {
ResolverResult resultFromDB = globalScope.mustResolveWithMetadata(context, "a-key").join();
assertEquals(expected.getValue(), resultFromDB.getValue());
assertArrayEquals(expected.getMetadata(), resultFromDB.getMetadata());
}
assertEquals(expected, globalScope.resolveWithMetadata("a-key", hooks).join());
byte[] newMetadata = Tuple.from("some-different-metadata").pack();
MetadataHook newHook = ignore -> newMetadata;
final ResolverCreateHooks newHooks = new ResolverCreateHooks(DEFAULT_CHECK, newHook);
// make sure we don't just read the cached value
database.clearCaches();
assertArrayEquals(metadata, globalScope.resolveWithMetadata("a-key", newHooks).join().getMetadata(), "hook is only run on create, does not update metadata");
}
Aggregations