use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.
the class DefaultMetadataStore method batchUpdateRegionStats.
@Override
public CompletableFuture<Boolean> batchUpdateRegionStats(final long clusterId, final List<Pair<Region, RegionStats>> regionStatsList) {
final List<KVEntry> entries = Lists.newArrayListWithCapacity(regionStatsList.size());
for (final Pair<Region, RegionStats> p : regionStatsList) {
final String key = MetadataKeyHelper.getRegionStatsKey(clusterId, p.getKey().getId());
final byte[] bytes = this.serializer.writeObject(p);
entries.add(new KVEntry(BytesUtil.writeUtf8(key), bytes));
}
return this.rheaKVStore.put(entries);
}
use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.
the class AbstractRheaKVStoreTest method deleteListTest.
/**
* Test method: {@link RheaKVStore#delete(List)}
*/
private void deleteListTest(RheaKVStore store) {
List<KVEntry> entries1 = Lists.newArrayList();
List<byte[]> keys1 = Lists.newArrayList();
for (int i = 0; i < 3; i++) {
byte[] key = makeKey("batch_del_test_key" + i);
checkRegion(store, key, 1);
entries1.add(new KVEntry(key, makeValue("batch_del_test_value" + i)));
keys1.add(key);
}
store.bPut(entries1);
store.bDelete(keys1);
List<KVEntry> entries2 = Lists.newArrayList();
List<byte[]> keys2 = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
byte[] key = makeKey("g_batch_del_test_key" + i);
checkRegion(store, key, 2);
entries2.add(new KVEntry(key, makeValue("batch_del_test_value" + i)));
keys2.add(key);
}
store.bPut(entries2);
store.bDelete(keys2);
List<KVEntry> foundList = store.bScan(makeKey("batch_del_test_key"), makeKey("batch_del_test_key" + 99));
assertEquals(0, foundList.size());
for (int i = 0; i < keys1.size(); i++) {
byte[] value = store.bGet(keys1.get(i));
assertNull(value);
}
foundList = store.bScan(makeKey("g_batch_del_test_key"), makeKey("g_batch_put_test_key" + 99));
assertEquals(0, foundList.size());
for (int i = 0; i < keys2.size(); i++) {
byte[] value = store.bGet(keys2.get(i));
assertNull(value);
}
}
use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.
the class AbstractRheaKVStoreTest method deleteRangeTest.
/**
* Test method: {@link RheaKVStore#deleteRange(byte[], byte[])}
*/
private void deleteRangeTest(RheaKVStore store) {
// regions: 1 -> [null, g), 2 -> [g, null)
List<KVEntry> entries = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
byte[] key = makeKey("del_range_test" + i);
checkRegion(store, key, 1);
byte[] value = makeValue("del_range_test_value" + i);
entries.add(new KVEntry(key, value));
}
for (int i = 0; i < 10; i++) {
byte[] key = makeKey("t_del_range_test" + i);
checkRegion(store, key, 2);
byte[] value = makeValue("del_range_test_value" + i);
entries.add(new KVEntry(key, value));
}
store.bPut(entries);
// delete [del_range_test5, t_del_range_test8)
// cover three region: 1 -> [null, g), 2 -> [g, t), 3 -> [t, null)
store.bDeleteRange(makeKey("del_range_test5"), makeKey("t_del_range_test8"));
List<KVEntry> entries2 = store.bScan(makeKey("del_range_test"), makeKey("t_del_range_test" + 99));
assertEquals(entries.size() - 13, entries2.size());
byte[] value = store.bGet(makeKey("del_range_test5"));
assertNull(value);
value = store.bGet(makeKey("del_range_test6"));
assertNull(value);
value = store.bGet(makeKey("del_range_test7"));
assertNull(value);
value = store.bGet(makeKey("t_del_range_test8"));
assertNotNull(value);
}
use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.
the class AbstractRheaKVStoreTest method deleteTest.
/**
* Test method: {@link RheaKVStore#delete(byte[])}
*/
private void deleteTest(RheaKVStore store) {
// regions: 1 -> [null, g), 2 -> [g, null)
List<KVEntry> entries = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
byte[] key = makeKey("del_test" + i);
checkRegion(store, key, 1);
byte[] value = makeValue("del_test_value" + i);
entries.add(new KVEntry(key, value));
}
store.bPut(entries);
store.bDelete(makeKey("del_test5"));
List<KVEntry> entries2 = store.bScan(makeKey("del_test"), makeKey("del_test" + 99));
assertEquals(entries.size() - 1, entries2.size());
byte[] value = store.bGet(makeKey("del_test5"));
assertNull(value);
}
use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.
the class DefaultRheaKVStore method internalScan.
private FutureGroup<List<KVEntry>> internalScan(final byte[] startKey, final byte[] endKey, final boolean readOnlySafe, final boolean returnValue, final int retriesLeft, final Throwable lastCause) {
Requires.requireNonNull(startKey, "startKey");
final List<Region> regionList = this.pdClient.findRegionsByKeyRange(startKey, endKey, ApiExceptionHelper.isInvalidEpoch(lastCause));
final List<CompletableFuture<List<KVEntry>>> futures = Lists.newArrayListWithCapacity(regionList.size());
final Errors lastError = lastCause == null ? null : Errors.forException(lastCause);
for (final Region region : regionList) {
final byte[] regionStartKey = region.getStartKey();
final byte[] regionEndKey = region.getEndKey();
final byte[] subStartKey = regionStartKey == null ? startKey : BytesUtil.max(regionStartKey, startKey);
final byte[] subEndKey = regionEndKey == null ? endKey : (endKey == null ? regionEndKey : BytesUtil.min(regionEndKey, endKey));
final ListRetryCallable<KVEntry> retryCallable = retryCause -> internalScan(subStartKey, subEndKey, readOnlySafe, returnValue, retriesLeft - 1, retryCause);
final ListFailoverFuture<KVEntry> future = new ListFailoverFuture<>(retriesLeft, retryCallable);
internalRegionScan(region, subStartKey, subEndKey, false, readOnlySafe, returnValue, future, retriesLeft, lastError, this.onlyLeaderRead);
futures.add(future);
}
return new FutureGroup<>(futures);
}
Aggregations