Search in sources :

Example 1 with LocatableResolver

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));
}
Also used : RecordCoreRetriableTransactionException(com.apple.foundationdb.record.RecordCoreRetriableTransactionException) LocatableResolver(com.apple.foundationdb.record.provider.foundationdb.keyspace.LocatableResolver)

Example 2 with LocatableResolver

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;
    });
}
Also used : LocatableResolver(com.apple.foundationdb.record.provider.foundationdb.keyspace.LocatableResolver) VisibleForTesting(com.google.common.annotations.VisibleForTesting)

Example 3 with LocatableResolver

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;
    }));
}
Also used : LogMessageKeys(com.apple.foundationdb.record.logging.LogMessageKeys) ScopedValue(com.apple.foundationdb.record.provider.foundationdb.keyspace.ScopedValue) LoggerFactory(org.slf4j.LoggerFactory) CompletableFuture(java.util.concurrent.CompletableFuture) AsyncUtil(com.apple.foundationdb.async.AsyncUtil) LocatableResolver(com.apple.foundationdb.record.provider.foundationdb.keyspace.LocatableResolver) Subspace(com.apple.foundationdb.subspace.Subspace) Transaction(com.apple.foundationdb.Transaction) ExecuteProperties(com.apple.foundationdb.record.ExecuteProperties) Tuple(com.apple.foundationdb.tuple.Tuple) KeyValueLogMessage(com.apple.foundationdb.record.logging.KeyValueLogMessage) RecordCoreRetriableTransactionException(com.apple.foundationdb.record.RecordCoreRetriableTransactionException) RecordCoreException(com.apple.foundationdb.record.RecordCoreException) ScanProperties(com.apple.foundationdb.record.ScanProperties) NoSuchElementException(java.util.NoSuchElementException) Nonnull(javax.annotation.Nonnull) Nullable(javax.annotation.Nullable) Charsets(com.google.common.base.Charsets) IsolationLevel(com.apple.foundationdb.record.IsolationLevel) Logger(org.slf4j.Logger) KeyValue(com.apple.foundationdb.KeyValue) DirectoryLayer(com.apple.foundationdb.directory.DirectoryLayer) TupleRange(com.apple.foundationdb.record.TupleRange) TimeUnit(java.util.concurrent.TimeUnit) AtomicLong(java.util.concurrent.atomic.AtomicLong) RecordCursor(com.apple.foundationdb.record.RecordCursor) Optional(java.util.Optional) API(com.apple.foundationdb.annotation.API) VisibleForTesting(com.google.common.annotations.VisibleForTesting) Collections(java.util.Collections) LocatableResolver(com.apple.foundationdb.record.provider.foundationdb.keyspace.LocatableResolver)

Aggregations

LocatableResolver (com.apple.foundationdb.record.provider.foundationdb.keyspace.LocatableResolver)3 RecordCoreRetriableTransactionException (com.apple.foundationdb.record.RecordCoreRetriableTransactionException)2 VisibleForTesting (com.google.common.annotations.VisibleForTesting)2 KeyValue (com.apple.foundationdb.KeyValue)1 Transaction (com.apple.foundationdb.Transaction)1 API (com.apple.foundationdb.annotation.API)1 AsyncUtil (com.apple.foundationdb.async.AsyncUtil)1 DirectoryLayer (com.apple.foundationdb.directory.DirectoryLayer)1 ExecuteProperties (com.apple.foundationdb.record.ExecuteProperties)1 IsolationLevel (com.apple.foundationdb.record.IsolationLevel)1 RecordCoreException (com.apple.foundationdb.record.RecordCoreException)1 RecordCursor (com.apple.foundationdb.record.RecordCursor)1 ScanProperties (com.apple.foundationdb.record.ScanProperties)1 TupleRange (com.apple.foundationdb.record.TupleRange)1 KeyValueLogMessage (com.apple.foundationdb.record.logging.KeyValueLogMessage)1 LogMessageKeys (com.apple.foundationdb.record.logging.LogMessageKeys)1 ScopedValue (com.apple.foundationdb.record.provider.foundationdb.keyspace.ScopedValue)1 Subspace (com.apple.foundationdb.subspace.Subspace)1 Tuple (com.apple.foundationdb.tuple.Tuple)1 Charsets (com.google.common.base.Charsets)1