use of com.apple.foundationdb.record.provider.foundationdb.keyspace.ResolverCreateHooks.DEFAULT_CHECK 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.DEFAULT_CHECK 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