use of com.apple.foundationdb.record.provider.foundationdb.keyspace.LocatableResolver in project fdb-record-layer by FoundationDB.
the class FDBReverseDirectoryCache method putIfNotExists.
/**
* Add a new entry to the reverse directory cache if it does not already exist. If an entry already exists
* for the <code>pathString</code> provided, the value for the key in the RDC will be compared against the
* <code>pathValue</code> provided and an <code>IllegalStateException</code> will be thrown if they differ.
* Note that no effort is made to determine if the <code>pathValue</code> actually exists in the directory
* layer itself!
*
* @param context the transaction context in which to do the operation
* @param scopedPathString the name of the entry within the scoped of an FDB directory layer
* @param pathValue the value of the entry that from that FDB directory layer
* @return a future that performs the action
* @throws IllegalStateException if the <code>pathkey</code> provided already exists in the reverse directory
* layer and the <code>pathValue</code> provided does not match that value
*/
public CompletableFuture<Void> putIfNotExists(@Nonnull FDBRecordContext context, @Nonnull ScopedValue<String> scopedPathString, @Nonnull Long pathValue) {
LocatableResolver scope = scopedPathString.getScope();
String pathString = scopedPathString.getData();
String cachedString = context.getDatabase().getReverseDirectoryInMemoryCache().getIfPresent(scope.wrap(pathValue));
if (cachedString != null) {
if (!cachedString.equals(pathString)) {
// newer read version
throw new RecordCoreRetriableTransactionException("Provided value for path key does not match existing value in reverse directory layer in-memory cache").addLogInfo(LogMessageKeys.RESOLVER, scope).addLogInfo(LogMessageKeys.RESOLVER_PATH, pathString).addLogInfo(LogMessageKeys.RESOLVER_KEY, pathValue).addLogInfo(LogMessageKeys.CACHED_KEY, cachedString);
}
if (LOGGER.isDebugEnabled()) {
LOGGER.debug("In-memory cache contains '" + pathString + "' -> '" + pathValue + "' mapping. No need to put");
}
return AsyncUtil.DONE;
}
return getReverseCacheSubspace(scope).thenCompose(subspace -> putToSubspace(context, subspace, scopedPathString, pathValue));
}
use of com.apple.foundationdb.record.provider.foundationdb.keyspace.LocatableResolver in project fdb-record-layer by FoundationDB.
the class FDBReverseDirectoryCache method putOrReplaceForTesting.
/**
* Write an entry to the reverse directory, <b>STRICTLY FOR TESTING</b>. This method writes an entry to the
* reverse directory, overwriting any pre-existing entry that may have been there in the process. There are
* (deliberately) no checks to validate that the entry being written is consistent with the forward mapping.
* Note that this does not manipulate any in-memory caches that may have referred to the value.
*
* @param context transaction context used to remove the mapping
* @param scopedPathString the key being written
* @param value the value being written
* @return a future that, when completed, will have written the entry
*/
@VisibleForTesting
public CompletableFuture<Void> putOrReplaceForTesting(@Nonnull FDBRecordContext context, @Nonnull ScopedValue<String> scopedPathString, @Nonnull Long value) {
final LocatableResolver scope = scopedPathString.getScope();
final String pathString = scopedPathString.getData();
return getReverseCacheSubspace(scope).thenApply(reverseCacheSubspace -> {
context.ensureActive().set(reverseCacheSubspace.pack(value), Tuple.from(pathString).pack());
return null;
});
}
use of com.apple.foundationdb.record.provider.foundationdb.keyspace.LocatableResolver in project fdb-record-layer by FoundationDB.
the class FDBReverseDirectoryCache method put.
/**
* Explicitly add a new entry to the reverse directory cache.
*
* @param context the transactional context
* @param pathKey the name of an entry in the FDB directory, the value of which is retrieved from the directory
* and explicitly inserted into the reverse directory cache
* @return a future that performs the action
* @throws NoSuchElementException will be thrown by this future if the <code>name</code> provided does not exist in the directory layer.
*/
public CompletableFuture<Void> put(@Nonnull FDBRecordContext context, @Nonnull ScopedValue<String> pathKey) {
final LocatableResolver scope = pathKey.getScope();
final String key = pathKey.getData();
return getReverseCacheSubspace(scope).thenCompose(reverseCacheSubspace -> scope.mustResolve(context, key).thenApply(value -> {
if (LOGGER.isDebugEnabled()) {
LOGGER.debug(KeyValueLogMessage.of("Adding value to reverse directory cache", LogMessageKeys.KEY, pathKey, LogMessageKeys.VALUE, value));
}
context.ensureActive().set(reverseCacheSubspace.pack(value), Tuple.from(pathKey.getData()).pack());
return null;
}));
}
Aggregations