use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.
the class RawKVGetBenchmark method put.
public void put() {
final List<KVEntry> batch = Lists.newArrayListWithCapacity(100);
for (int i = 0; i < KEY_COUNT; i++) {
byte[] key = BytesUtil.writeUtf8("benchmark_" + i);
batch.add(new KVEntry(key, VALUE_BYTES));
if (batch.size() >= 100) {
this.kvStore.put(batch, null);
batch.clear();
}
}
}
use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.
the class RheaKVGetBenchmark method put.
public void put() {
final List<KVEntry> batch = Lists.newArrayListWithCapacity(100);
for (int i = 0; i < KEY_COUNT; i++) {
byte[] key = BytesUtil.writeUtf8("benchmark_" + i);
batch.add(new KVEntry(key, VALUE_BYTES));
if (batch.size() >= 10) {
this.kvStore.bPut(batch);
batch.clear();
}
}
}
use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.
the class ReverseScanExample method scan.
@SuppressWarnings("unchecked")
public static void scan(final RheaKVStore rheaKVStore) {
final List<byte[]> keys = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
final byte[] bytes = writeUtf8("scan_demo_" + i);
keys.add(bytes);
rheaKVStore.bPut(bytes, bytes);
}
final byte[] firstKey = keys.get(keys.size() - 1);
final byte[] lastKey = keys.get(0);
final String firstKeyString = readUtf8(firstKey);
final String lastKeyString = readUtf8(lastKey);
// async scan
final CompletableFuture<List<KVEntry>> f1 = rheaKVStore.reverseScan(firstKey, lastKey);
final CompletableFuture<List<KVEntry>> f2 = rheaKVStore.reverseScan(firstKey, lastKey, false);
final CompletableFuture<List<KVEntry>> f3 = rheaKVStore.reverseScan(firstKeyString, lastKeyString);
final CompletableFuture<List<KVEntry>> f4 = rheaKVStore.reverseScan(firstKeyString, lastKeyString, false);
CompletableFuture.allOf(f1, f2, f3, f4).join();
for (final CompletableFuture<List<KVEntry>> f : new CompletableFuture[] { f1, f2, f3, f4 }) {
for (final KVEntry kv : f.join()) {
LOG.info("Async reverseScan: key={}, value={}", readUtf8(kv.getKey()), readUtf8(kv.getValue()));
}
}
// sync scan
final List<KVEntry> l1 = rheaKVStore.bReverseScan(firstKey, lastKey);
final List<KVEntry> l2 = rheaKVStore.bReverseScan(firstKey, lastKey, false);
final List<KVEntry> l3 = rheaKVStore.bReverseScan(firstKeyString, lastKeyString);
final List<KVEntry> l4 = rheaKVStore.bReverseScan(firstKeyString, lastKeyString, false);
for (final List<KVEntry> l : new List[] { l1, l2, l3, l4 }) {
for (final KVEntry kv : l) {
LOG.info("sync reverseScan: key={}, value={}", readUtf8(kv.getKey()), readUtf8(kv.getValue()));
}
}
}
use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.
the class IteratorExample method iterator.
@SuppressWarnings("unchecked")
public static void iterator(final RheaKVStore rheaKVStore) {
final List<byte[]> keys = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
final byte[] bytes = writeUtf8("iterator_demo_" + i);
keys.add(bytes);
rheaKVStore.bPut(bytes, bytes);
}
final byte[] firstKey = keys.get(0);
final byte[] lastKey = keys.get(keys.size() - 1);
final String firstKeyString = readUtf8(firstKey);
final String lastKeyString = readUtf8(lastKey);
final RheaIterator<KVEntry> it1 = rheaKVStore.iterator(firstKey, lastKey, 5);
final RheaIterator<KVEntry> it2 = rheaKVStore.iterator(firstKey, lastKey, 6, false);
final RheaIterator<KVEntry> it3 = rheaKVStore.iterator(firstKeyString, lastKeyString, 5);
final RheaIterator<KVEntry> it4 = rheaKVStore.iterator(firstKeyString, lastKeyString, 6, false);
for (final RheaIterator<KVEntry> it : new RheaIterator[] { it1, it2, it3, it4 }) {
while (it.hasNext()) {
final KVEntry kv = it.next();
LOG.info("Sync iterator: key={}, value={}", readUtf8(kv.getKey()), readUtf8(kv.getValue()));
}
}
}
use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.
the class RegionRouteTable method findRegionsByKvEntries.
/**
* Returns the list of regions to which the keys belongs.
*/
public Map<Region, List<KVEntry>> findRegionsByKvEntries(final List<KVEntry> kvEntries) {
Requires.requireNonNull(kvEntries, "kvEntries");
final Map<Region, List<KVEntry>> regionMap = Maps.newHashMap();
final StampedLock stampedLock = this.stampedLock;
final long stamp = stampedLock.readLock();
try {
for (final KVEntry kvEntry : kvEntries) {
final Region region = findRegionByKeyWithoutLock(kvEntry.getKey());
regionMap.computeIfAbsent(region, k -> Lists.newArrayList()).add(kvEntry);
}
return regionMap;
} finally {
stampedLock.unlockRead(stamp);
}
}
Aggregations