@Override public String toString() { return ByteArrayUtil2.loggable(subspace.pack()); } }
protected CompletableFuture<Optional<ResolverResult>> read(@Nonnull FDBRecordContext context, @Nonnull final String toRead) { return context.ensureActive().get(mappingSubspace.pack(toRead)) .thenApply(Optional::ofNullable) .thenApply(maybeValue -> maybeValue.map(this::deserializeValue)); }
protected CompletableFuture<Boolean> exists(@Nonnull FDBRecordContext context, @Nonnull final String toRead) { return context.ensureActive().get(mappingSubspace.pack(toRead)) .thenApply(Objects::nonNull); }
@Override public CompletableFuture<Void> deleteWhere(Transaction tr, @Nonnull Tuple prefix) { // NOTE: Range.startsWith(), Subspace.range() and so on cover keys *strictly* within the range, but we sometimes // store data at the prefix key itself. final byte[] key = state.indexSubspace.pack(prefix); tr.clear(key, ByteArrayUtil.strinc(key)); return AsyncUtil.DONE; }
protected CompletableFuture<Optional<String>> readReverse(@Nonnull FDBRecordContext context, @Nonnull final Long internedValue) { return context.ensureActive().get(reverseMappingSubspace.pack(internedValue)) .thenApply(Optional::ofNullable) .thenApply(maybeValue -> maybeValue.map(bytes -> Tuple.fromBytes(bytes).getString(0))); }
@Nonnull private CompletableFuture<ResolverStateProto.State> readResolverStateInTransaction(@Nonnull FDBRecordContext context) { // don't use a snapshot read, the lock state shouldn't change frequently but if it does we should // fail the transaction and should retry getting the value. return context.instrument(FDBStoreTimer.DetailEvents.RESOLVER_STATE_READ, context.ensureActive().get(getStateSubspace().pack()) .thenApply(LocatableResolver::deserializeResolverState)); }
@Override @Nonnull public void updateUniquenessViolations(@Nonnull Tuple valueKey, @Nonnull Tuple primaryKey, @Nullable Tuple existingKey, boolean remove) { byte[] uniquenessKeyBytes = state.store.indexUniquenessViolationsSubspace(state.index).pack(FDBRecordStoreBase.uniquenessViolationKey(valueKey, primaryKey)); if (remove) { state.transaction.clear(uniquenessKeyBytes); } else { state.transaction.set(uniquenessKeyBytes, (existingKey == null) ? new byte[0] : existingKey.pack()); } }
@Override public CompletableFuture<Void> deleteWhere(Transaction tr, @Nonnull Tuple prefix) { return super.deleteWhere(tr, prefix).thenApply(v -> { // NOTE: Range.startsWith(), Subspace.range() and so on cover keys *strictly* within the range, but we sometimes // store data at the prefix key itself. final Subspace rankSubspace = getSecondarySubspace(); final byte[] key = rankSubspace.pack(prefix); tr.clear(key, ByteArrayUtil.strinc(key)); return v; }); }
private CompletableFuture<ResolverResult> createMapping(@Nonnull FDBRecordContext context, @Nonnull final String toIntern, @Nullable final byte[] metadata) { final HighContentionAllocator hca = getHca(context); final byte[] mappingKey = mappingSubspace.pack(toIntern); return hca.allocate(toIntern) .thenApply(allocated -> { ResolverResult result = new ResolverResult(allocated, metadata); context.ensureActive().set(mappingKey, serializeValue(result)); return result; }); }
private CompletableFuture<Optional<ResolverResult>> readInternal(@Nonnull FDBRecordContext context, String key) { FDBReverseDirectoryCache reverseCache = context.getDatabase().getReverseDirectoryCache(); return context.ensureActive().get(getMappingSubspace().pack(key)) .thenCompose(bytes -> { if (bytes == null) { return CompletableFuture.completedFuture(Optional.empty()); } ResolverResult result = deserializeValue(bytes); return reverseCache.putIfNotExists(context, wrap(key), result.getValue()) .thenApply(ignore -> Optional.of(result)); }); }
private void saveVersionWithOldFormat(@Nonnull Tuple primaryKey, @Nonnull FDBRecordVersion version) { byte[] versionKey = getSubspace().pack(recordVersionKey(primaryKey)); if (version.isComplete()) { context.ensureActive().set(versionKey, version.toBytes()); } else { context.addToLocalVersionCache(primaryKey, version.getLocalVersion()); final byte[] valueBytes = version.writeTo(ByteBuffer.allocate(FDBRecordVersion.VERSION_LENGTH + Integer.BYTES).order(ByteOrder.BIG_ENDIAN)) .putInt(0) .array(); context.addVersionMutation(MutationType.SET_VERSIONSTAMPED_VALUE, versionKey, valueBytes); } }
/** * Manually save an index entry, for example when rebuilding in place with a different storage format. * Does not check uniqueness or maintain any secondary subspaces. * @param keyValue the entry to save */ protected void saveIndexEntryAsKeyValue(IndexEntry keyValue) { state.transaction.set(state.indexSubspace.pack(keyValue.getKey()), keyValue.getValue().pack()); }
private CompletableFuture<Long> countRange(ReadTransactionContext tc, int level, byte[] beginKey, byte[] endKey) { return tc.readAsync(tr -> AsyncUtil.mapIterable(tr.getRange(beginKey == null ? subspace.range(Tuple.from(level)).begin : subspace.pack(Tuple.from(level, beginKey)), endKey == null ? subspace.range(Tuple.from(level)).end : subspace.pack(Tuple.from(level, endKey))), keyValue -> decodeLong(keyValue.getValue())) .asList() .thenApply(longs -> longs.stream().reduce(0L, Long::sum))); }
public AsyncIterable<byte[]> getRange(ReadTransaction tr, byte[] beginKey, byte[] endKey) { checkKey(beginKey); return AsyncUtil.mapIterable(tr.getRange(subspace.pack(Tuple.from(0, beginKey)), subspace.pack(Tuple.from(0, endKey))), keyValue -> { Tuple t = subspace.unpack(keyValue.getKey()); return t.getBytes(1); }); }
protected void saveDirectory(TimeWindowLeaderboardDirectory directory) { final Subspace extraSubspace = getSecondarySubspace(); state.transaction.set(extraSubspace.pack(), directory.toProto().toByteArray()); }
private CompletableFuture<ResolverResult> createInternal(@Nonnull FDBRecordContext context, String key, @Nullable byte[] metadata) { FDBReverseDirectoryCache reverseCache = context.getDatabase().getReverseDirectoryCache(); HighContentionAllocator hca = getHca(context); return hca.allocate(key) .thenApply(allocated -> { ResolverResult result = new ResolverResult(allocated, metadata); context.ensureActive().set(getMappingSubspace().pack(key), serializeValue(result)); return result; }) .thenCompose(result -> reverseCache.putIfNotExists(context, wrap(key), result.getValue()).thenApply(ignore -> result)); }
private <M extends Message> void addRecordCount(@Nonnull RecordMetaData metaData, @Nonnull FDBStoredRecord<M> record, @Nonnull byte[] increment) { if (metaData.getRecordCountKey() == null) { return; } final Transaction tr = ensureContextActive(); Key.Evaluated subkey = metaData.getRecordCountKey().evaluateSingleton(record); final byte[] keyBytes = getSubspace().pack(Tuple.from(RECORD_COUNT_KEY).addAll(subkey.toTupleAppropriateList())); tr.mutate(MutationType.ADD, keyBytes, increment); }
private void addIndexStateReadConflict(@Nonnull String indexName) { if (!getRecordMetaData().hasIndex(indexName)) { throw new MetaDataException("Index " + indexName + " does not exist in meta-data."); } Transaction tr = ensureContextActive(); byte[] indexStateKey = getSubspace().pack(Tuple.from(INDEX_STATE_SPACE_KEY, indexName)); tr.addReadConflictKey(indexStateKey); }
private void clearIndexData(@Nonnull Index index) { fdb.database().run(tr -> { tr.clear(Range.startsWith(recordStore.indexSubspace(index).pack())); tr.clear(recordStore.indexSecondarySubspace(index).range()); tr.clear(recordStore.indexRangeSubspace(index).range()); return null; }); }