use of com.apple.foundationdb.Transaction in project fdb-record-layer by FoundationDB.
the class BunchedMapScanTest method testScanMulti.
private void testScanMulti(int limit, boolean reverse, List<List<Tuple>> keyLists, @Nonnull BiFunction<Transaction, byte[], BunchedMapMultiIterator<Tuple, Tuple, Long>> iteratorFunction) {
try (Transaction tr = db.createTransaction()) {
byte[] continuation = null;
List<BunchedMapScanEntry<Tuple, Tuple, Long>> entryList = new ArrayList<>();
BunchedMapScanEntry<Tuple, Tuple, Long> lastEntry = null;
do {
BunchedMapMultiIterator<Tuple, Tuple, Long> iterator = iteratorFunction.apply(tr, continuation);
int returned = 0;
while (iterator.hasNext()) {
BunchedMapScanEntry<Tuple, Tuple, Long> toAdd = iterator.peek();
assertEquals(toAdd, iterator.next());
if (lastEntry != null) {
if (toAdd.getSubspaceTag().equals(lastEntry.getSubspaceTag())) {
assertEquals(reverse ? 1 : -1, Integer.signum(lastEntry.getKey().compareTo(toAdd.getKey())));
} else {
assertEquals(reverse ? 1 : -1, Integer.signum(lastEntry.getSubspaceTag().compareTo(toAdd.getSubspaceTag())));
}
}
entryList.add(toAdd);
lastEntry = toAdd;
returned++;
}
continuation = iterator.getContinuation();
if (limit == ReadTransaction.ROW_LIMIT_UNLIMITED || returned < limit) {
assertNull(continuation);
} else {
assertNotNull(continuation);
}
} while (continuation != null);
if (reverse) {
entryList = Lists.reverse(entryList);
}
Long tag = null;
int pos = 0;
int totalRead = 0;
for (BunchedMapScanEntry<Tuple, Tuple, Long> entry : entryList) {
if (tag == null || !tag.equals(entry.getSubspaceTag())) {
if (tag != null) {
assertEquals(tag + 1, entry.getSubspaceTag().longValue());
}
tag = entry.getSubspaceTag();
pos = 0;
}
assertEquals(keyLists.get(tag.intValue()).get(pos), entry.getKey());
assertEquals(value, entry.getValue());
assertEquals(subSubspaces.get(tag.intValue()), entry.getSubspace());
pos++;
totalRead++;
}
int totalToSee = keyLists.stream().mapToInt(List::size).sum();
assertEquals(totalToSee, totalRead);
}
}
use of com.apple.foundationdb.Transaction in project fdb-record-layer by FoundationDB.
the class BunchedMapScanTest method continuationWithDeletes.
private void continuationWithDeletes(int limit, boolean reverse) {
try (Transaction tr = db.createTransaction()) {
byte[] continuation = null;
List<Tuple> readKeys = new ArrayList<>();
do {
List<Tuple> mostRecentReadKeys = new ArrayList<>();
int returned = 0;
BunchedMapIterator<Tuple, Tuple> bunchedMapIterator = map.scan(tr, subSubspaces.get(1), continuation, limit, reverse);
while (bunchedMapIterator.hasNext()) {
Tuple toAdd = bunchedMapIterator.peek().getKey();
assertEquals(toAdd, bunchedMapIterator.next().getKey());
mostRecentReadKeys.add(toAdd);
returned += 1;
}
assertFalse(bunchedMapIterator.hasNext());
assertThrows(NoSuchElementException.class, bunchedMapIterator::peek);
assertThrows(NoSuchElementException.class, bunchedMapIterator::next);
continuation = bunchedMapIterator.getContinuation();
if (returned != limit) {
assertNull(continuation);
} else {
assertNotNull(continuation);
}
// Remove all of the keys that were most recently read.
mostRecentReadKeys.forEach(k -> map.remove(tr, subSubspaces.get(1), k).join());
readKeys.addAll(mostRecentReadKeys);
} while (continuation != null);
if (reverse) {
readKeys = Lists.reverse(readKeys);
}
List<Tuple> expectedKeys = IntStream.range(0, keys.size()).filter(i -> i % subSubspaces.size() == 1).mapToObj(keys::get).collect(Collectors.toList());
assertEquals(expectedKeys, readKeys);
tr.cancel();
}
}
use of com.apple.foundationdb.Transaction in project fdb-record-layer by FoundationDB.
the class BunchedMapScanTest method getKeysContinuation.
@Test
public void getKeysContinuation() throws InterruptedException, ExecutionException {
clearAndPopulate();
try (Transaction tr = db.createTransaction()) {
getKeysContinuation(tr, false);
getKeysContinuation(tr, true);
}
}
use of com.apple.foundationdb.Transaction in project fdb-record-layer by FoundationDB.
the class BunchedMapScanTest method scanWithConflict.
@Test
public void scanWithConflict() throws InterruptedException, ExecutionException {
clearAndPopulate();
try (Transaction tr1 = db.createTransaction();
Transaction tr2 = db.createTransaction()) {
CompletableFuture.allOf(tr1.getReadVersion(), tr2.getReadVersion()).get();
BunchedMapIterator<Tuple, Tuple> iterator = map.scan(tr1, bmSubspace);
int count = MoreAsyncUtil.reduce(iterator, 0, (oldCount, item) -> oldCount + 1).get();
assertEquals(keys.size(), count);
tr1.addWriteConflictKey(Tuple.from(count).pack());
assertFalse(map.put(tr2, bmSubspace, Tuple.from(keys.get(keys.size() - 1).getLong(0) + 1), value).get().isPresent());
tr2.commit().get();
CompletionException e = assertThrows(CompletionException.class, () -> tr1.commit().join());
assertNotNull(e.getCause());
assertTrue(e.getCause() instanceof FDBException);
FDBException fdbE = (FDBException) e.getCause();
assertEquals(FDBError.NOT_COMMITTED.code(), fdbE.getCode());
}
byte[] continuation = null;
for (int i = 0; i < keys.size(); i++) {
}
try (Transaction tr1 = db.createTransaction();
Transaction tr2 = db.createTransaction()) {
CompletableFuture.allOf(tr1.getReadVersion(), tr2.getReadVersion()).get();
BunchedMapIterator<Tuple, Tuple> iterator = map.scan(tr1, bmSubspace);
}
}
use of com.apple.foundationdb.Transaction 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();
}
}
Aggregations