Search in sources :

Example 1 with KeySelector

use of com.apple.foundationdb.KeySelector in project fdb-record-layer by FoundationDB.

the class BunchedMapTest method inconsistentScan.

private static List<KeyValue> inconsistentScan(@Nonnull Database db, @Nonnull Subspace subspace) {
    // Note that tr is mutated in the block, hence not using try-with-resources
    Transaction tr = db.createTransaction();
    try {
        KeySelector begin = KeySelector.firstGreaterOrEqual(subspace.range().begin);
        KeySelector end = KeySelector.firstGreaterOrEqual(subspace.range().end);
        KeyValue lastSeen = null;
        AsyncIterator<KeyValue> rangeIterator = tr.getRange(begin, end).iterator();
        List<KeyValue> rangeKVs = new ArrayList<>();
        boolean done = false;
        while (!done) {
            // Might loop if there are timeouts encountered within loop.
            try {
                while (rangeIterator.hasNext()) {
                    KeyValue next = rangeIterator.next();
                    rangeKVs.add(next);
                    lastSeen = next;
                }
                done = true;
            } catch (RuntimeException e) {
                FDBException fdbE = unwrapException(e);
                if (fdbE == null || fdbE.getCode() != FDBError.TRANSACTION_TOO_OLD.code()) {
                    throw e;
                } else {
                    // Timed out. Restart transaction and keep going.
                    tr.close();
                    tr = db.createTransaction();
                    if (lastSeen != null) {
                        // Update begin if we have any results.
                        begin = KeySelector.firstGreaterThan(lastSeen.getKey());
                        lastSeen = null;
                    }
                    rangeIterator = tr.getRange(begin, end).iterator();
                }
            }
        }
        return rangeKVs;
    } finally {
        tr.close();
    }
}
Also used : KeyValue(com.apple.foundationdb.KeyValue) Transaction(com.apple.foundationdb.Transaction) ArrayList(java.util.ArrayList) FDBException(com.apple.foundationdb.FDBException) KeySelector(com.apple.foundationdb.KeySelector)

Example 2 with KeySelector

use of com.apple.foundationdb.KeySelector in project fdb-record-layer by FoundationDB.

the class VersionFromTimestamp method versionFromTimestamp.

private static CompletableFuture<Long> versionFromTimestamp(@Nonnull ReadTransaction tr, @Nonnull Instant timestamp, boolean start) {
    final byte[] dateKey = ByteArrayUtil.join(SystemKeyspace.TIMEKEEPER_KEY_PREFIX, Tuple.from(timestamp.getEpochSecond()).pack());
    final KeySelector startKey;
    final KeySelector endKey;
    if (start) {
        startKey = KeySelector.firstGreaterThan(SystemKeyspace.TIMEKEEPER_KEY_PREFIX);
        endKey = KeySelector.firstGreaterThan(dateKey);
    } else {
        startKey = KeySelector.firstGreaterOrEqual(dateKey);
        endKey = KeySelector.firstGreaterOrEqual(ByteArrayUtil.strinc(SystemKeyspace.TIMEKEEPER_KEY_PREFIX));
    }
    final AsyncIterator<KeyValue> range = tr.getRange(startKey, endKey, 1, start).iterator();
    return range.onHasNext().thenApply(hasNext -> {
        if (hasNext) {
            return Tuple.fromBytes(range.next().getValue()).getLong(0);
        } else if (start) {
            return 0L;
        } else {
            return Long.MAX_VALUE;
        }
    });
}
Also used : KeyValue(com.apple.foundationdb.KeyValue) KeySelector(com.apple.foundationdb.KeySelector)

Aggregations

KeySelector (com.apple.foundationdb.KeySelector)2 KeyValue (com.apple.foundationdb.KeyValue)2 FDBException (com.apple.foundationdb.FDBException)1 Transaction (com.apple.foundationdb.Transaction)1 ArrayList (java.util.ArrayList)1