StringInterningLayer { protected CompletableFuture<Void> updateMetadata(@Nonnull final FDBRecordContext context, @Nonnull final String key, @Nullable final byte[] metadata) { return read(context, key).thenApply(maybeRead -> maybeRead.map(read -> new ResolverResult(read.getValue(), metadata)) .orElseThrow(() -> new NoSuchElementException("updateMetadata must reference key that already exists"))) .thenAccept(newResult -> context.ensureActive().set(mappingSubspace.pack(key), serializeValue(newResult))); } StringInterningLayer(@Nonnull Subspace baseSubspace);  StringInterningLayer(@Nonnull Subspace baseSubspace, boolean isRootLevel); private  StringInterningLayer(@Nonnull Subspace mappingSubspace,
                                 @Nonnull Subspace reverseMappingSubspace,
                                 @Nonnull Subspace counterSubspace,
                                 boolean isRootLevel);   }
@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)); } }
@Test void testUpdateForMissingKey() { byte[] metadata = Tuple.from("some-metadata").pack(); try (FDBRecordContext context = database.openContext()) { StringInterningLayer interningLayer = new StringInterningLayer(testSubspace); interningLayer.updateMetadata(context, "a-missing-key", metadata).join(); fail("should throw NoSuchElementException"); } catch (CompletionException ex) { assertThat(ex.getCause(), is(instanceOf(NoSuchElementException.class))); assertThat(ex.getCause(), hasMessageContaining("updateMetadata must reference key that already exists")); } }