use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.
the class RocksKVStoreTest method jumpOverTest.
@Test
public void jumpOverTest() {
final List<KVEntry> entries = Lists.newArrayList();
for (int i = 0; i < 10000; i++) {
entries.add(new KVEntry(makeKey("approximate_test" + i), makeValue("approximate_test_value")));
}
this.kvStore.put(entries, null);
final byte[] endKey = this.kvStore.jumpOver(makeKey("approximate_test0000"), 1000);
final long approximateKeys = this.kvStore.getApproximateKeysInRange(makeKey("approximate_test0000"), endKey);
assertEquals(1000, approximateKeys, 100);
}
use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.
the class RocksKVStoreTest method deleteListTest.
/**
* Test method: {@link RocksRawKVStore#delete(List, KVStoreClosure)}
*/
@SuppressWarnings("unchecked")
@Test
public void deleteListTest() {
final List<KVEntry> entries = Lists.newArrayList();
final List<byte[]> keys = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
final byte[] key = makeKey("batch_del_test" + i);
entries.add(new KVEntry(key, makeValue("batch_del_test_value")));
keys.add(key);
}
this.kvStore.put(entries, null);
this.kvStore.delete(keys, null);
TestClosure closure = new TestClosure();
this.kvStore.scan(makeKey("batch_del_test"), makeKey("batch_del_test" + 99), closure);
List<KVEntry> entries2 = (List<KVEntry>) closure.getData();
assertEquals(0, entries2.size());
for (int i = 0; i < keys.size(); i++) {
closure = new TestClosure();
this.kvStore.get(keys.get(i), closure);
byte[] value = (byte[]) closure.getData();
assertNull(value);
}
}
use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.
the class RocksKVStoreTest method getLocalIteratorTest.
/**
* Test method: {@link RocksRawKVStore#localIterator()}
*/
@Test
public void getLocalIteratorTest() {
final List<byte[]> keyList = Lists.newArrayList();
final List<byte[]> valueList = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
byte[] key = makeKey("iterator_test_key_" + i);
byte[] value = makeValue("iterator_test_value_" + i);
keyList.add(key);
valueList.add(value);
this.kvStore.put(key, value, null);
}
final List<KVEntry> entries = Lists.newArrayList();
try (final KVIterator it = this.kvStore.localIterator()) {
it.seekToFirst();
while (it.isValid()) {
entries.add(new KVEntry(it.key(), it.value()));
it.next();
}
} catch (final Exception e) {
e.printStackTrace();
}
assertEquals(entries.size(), keyList.size());
for (int i = 0; i < keyList.size(); i++) {
assertArrayEquals(keyList.get(i), entries.get(i).getKey());
assertArrayEquals(valueList.get(i), entries.get(i).getValue());
}
}
use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.
the class DefaultRheaKVStore method internalRegionScan.
private void internalRegionScan(final Region region, final byte[] subStartKey, final byte[] subEndKey, final boolean reverse, final boolean readOnlySafe, final boolean returnValue, final CompletableFuture<List<KVEntry>> future, final int retriesLeft, final Errors lastCause, final boolean requireLeader) {
final RegionEngine regionEngine = getRegionEngine(region.getId(), requireLeader);
// require leader on retry
final RetryRunner retryRunner = retryCause -> internalRegionScan(region, subStartKey, subEndKey, reverse, readOnlySafe, returnValue, future, retriesLeft - 1, retryCause, true);
final FailoverClosure<List<KVEntry>> closure = new FailoverClosureImpl<>(future, false, retriesLeft, retryRunner);
if (regionEngine != null) {
if (ensureOnValidEpoch(region, regionEngine, closure)) {
final RawKVStore rawKVStore = getRawKVStore(regionEngine);
if (reverse) {
if (this.kvDispatcher == null) {
rawKVStore.reverseScan(subStartKey, subEndKey, readOnlySafe, returnValue, closure);
} else {
this.kvDispatcher.execute(() -> rawKVStore.reverseScan(subStartKey, subEndKey, readOnlySafe, returnValue, closure));
}
} else {
if (this.kvDispatcher == null) {
rawKVStore.scan(subStartKey, subEndKey, readOnlySafe, returnValue, closure);
} else {
this.kvDispatcher.execute(() -> rawKVStore.scan(subStartKey, subEndKey, readOnlySafe, returnValue, closure));
}
}
}
} else {
final ScanRequest request = new ScanRequest();
request.setStartKey(subStartKey);
request.setEndKey(subEndKey);
request.setReadOnlySafe(readOnlySafe);
request.setReturnValue(returnValue);
request.setRegionId(region.getId());
request.setRegionEpoch(region.getRegionEpoch());
request.setReverse(reverse);
this.rheaKVRpcService.callAsyncWithRpc(request, closure, lastCause, requireLeader);
}
}
use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.
the class DefaultRheaKVStore method internalPut.
private FutureGroup<Boolean> internalPut(final List<KVEntry> entries, final int retriesLeft, final Throwable lastCause) {
final Map<Region, List<KVEntry>> regionMap = this.pdClient.findRegionsByKvEntries(entries, ApiExceptionHelper.isInvalidEpoch(lastCause));
final List<CompletableFuture<Boolean>> futures = Lists.newArrayListWithCapacity(regionMap.size());
final Errors lastError = lastCause == null ? null : Errors.forException(lastCause);
for (final Map.Entry<Region, List<KVEntry>> entry : regionMap.entrySet()) {
final Region region = entry.getKey();
final List<KVEntry> subEntries = entry.getValue();
final RetryCallable<Boolean> retryCallable = retryCause -> internalPut(subEntries, retriesLeft - 1, retryCause);
final BoolFailoverFuture future = new BoolFailoverFuture(retriesLeft, retryCallable);
internalRegionPut(region, subEntries, future, retriesLeft, lastError);
futures.add(future);
}
return new FutureGroup<>(futures);
}
Aggregations