Search in sources :

Example 16 with KVEntry

use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.

the class SnapshotBenchmark method put.

public void put() {
    final List<KVEntry> batch = Lists.newArrayListWithCapacity(100);
    for (int i = 0; i < KEY_COUNT * 10; i++) {
        final byte[] key = BytesUtil.writeUtf8("benchmark_" + i);
        batch.add(new KVEntry(key, VALUE_BYTES));
        if (batch.size() >= 100) {
            this.kvStore.put(batch, null);
            batch.clear();
        }
    }
}
Also used : KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry)

Example 17 with KVEntry

use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.

the class PutExample method put.

public static void put(final RheaKVStore rheaKVStore) {
    final byte[] value = writeUtf8("put_example_value");
    final CompletableFuture<Boolean> r1 = rheaKVStore.put("1", value);
    if (FutureHelper.get(r1)) {
        LOG.info("Async put 1 {} success.", readUtf8(rheaKVStore.bGet("1")));
    }
    final CompletableFuture<Boolean> r2 = rheaKVStore.put(writeUtf8("2"), value);
    if (FutureHelper.get(r2)) {
        LOG.info("Async put 2 {} success.", readUtf8(rheaKVStore.bGet("2")));
    }
    final boolean r3 = rheaKVStore.bPut("3", value);
    if (r3) {
        LOG.info("Sync put 3 {} success.", readUtf8(rheaKVStore.bGet("3")));
    }
    final boolean r4 = rheaKVStore.bPut(writeUtf8("4"), value);
    if (r4) {
        LOG.info("Sync put 4 {} success.", readUtf8(rheaKVStore.bGet("4")));
    }
    // put list
    final KVEntry kv1 = new KVEntry(writeUtf8("10"), value);
    final KVEntry kv2 = new KVEntry(writeUtf8("11"), value);
    final KVEntry kv3 = new KVEntry(writeUtf8("12"), value);
    final KVEntry kv4 = new KVEntry(writeUtf8("13"), value);
    final KVEntry kv5 = new KVEntry(writeUtf8("14"), value);
    List<KVEntry> entries = Lists.newArrayList(kv1, kv2, kv3);
    final CompletableFuture<Boolean> r5 = rheaKVStore.put(entries);
    if (FutureHelper.get(r5)) {
        for (final KVEntry entry : entries) {
            LOG.info("Async put list {} with value {} success.", readUtf8(entry.getKey()), readUtf8(entry.getValue()));
        }
    }
    entries = Lists.newArrayList(kv3, kv4, kv5);
    final boolean r6 = rheaKVStore.bPut(entries);
    if (r6) {
        for (final KVEntry entry : entries) {
            LOG.info("Sync put list {} with value {} success.", readUtf8(entry.getKey()), readUtf8(entry.getValue()));
        }
    }
}
Also used : KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry)

Example 18 with KVEntry

use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.

the class ScanExample 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(0);
    final byte[] lastKey = keys.get(keys.size() - 1);
    final String firstKeyString = readUtf8(firstKey);
    final String lastKeyString = readUtf8(lastKey);
    // async scan
    final CompletableFuture<List<KVEntry>> f1 = rheaKVStore.scan(firstKey, lastKey);
    final CompletableFuture<List<KVEntry>> f2 = rheaKVStore.scan(firstKey, lastKey, false);
    final CompletableFuture<List<KVEntry>> f3 = rheaKVStore.scan(firstKeyString, lastKeyString);
    final CompletableFuture<List<KVEntry>> f4 = rheaKVStore.scan(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 scan: key={}, value={}", readUtf8(kv.getKey()), readUtf8(kv.getValue()));
        }
    }
    // sync scan
    final List<KVEntry> l1 = rheaKVStore.bScan(firstKey, lastKey);
    final List<KVEntry> l2 = rheaKVStore.bScan(firstKey, lastKey, false);
    final List<KVEntry> l3 = rheaKVStore.bScan(firstKeyString, lastKeyString);
    final List<KVEntry> l4 = rheaKVStore.bScan(firstKeyString, lastKeyString, false);
    for (final List<KVEntry> l : new List[] { l1, l2, l3, l4 }) {
        for (final KVEntry kv : l) {
            LOG.info("sync scan: key={}, value={}", readUtf8(kv.getKey()), readUtf8(kv.getValue()));
        }
    }
}
Also used : CompletableFuture(java.util.concurrent.CompletableFuture) KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry) List(java.util.List)

Example 19 with KVEntry

use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.

the class MemoryKVStoreTest 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, 10);
}
Also used : KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry) Test(org.junit.Test)

Example 20 with KVEntry

use of com.alipay.sofa.jraft.rhea.storage.KVEntry in project sofa-jraft by sofastack.

the class MemoryKVStoreTest method deleteTest.

@SuppressWarnings("unchecked")
@Test
public void deleteTest() {
    final List<KVEntry> entries = Lists.newArrayList();
    for (int i = 0; i < 10; i++) {
        entries.add(new KVEntry(makeKey("del_test" + i), makeValue("del_test_value")));
    }
    this.kvStore.put(entries, null);
    this.kvStore.delete(makeKey("del_test5"), null);
    TestClosure closure = new TestClosure();
    this.kvStore.scan(makeKey("del_test"), makeKey("del_test" + 99), closure);
    List<KVEntry> entries2 = (List<KVEntry>) closure.getData();
    assertEquals(entries.size() - 1, entries2.size());
    closure = new TestClosure();
    this.kvStore.get(makeKey("del_test5"), closure);
    byte[] value = (byte[]) closure.getData();
    assertNull(value);
}
Also used : TestClosure(com.alipay.sofa.jraft.rhea.storage.TestClosure) KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry) List(java.util.List) Test(org.junit.Test)

Aggregations

KVEntry (com.alipay.sofa.jraft.rhea.storage.KVEntry)36 List (java.util.List)17 Test (org.junit.Test)14 Status (com.alipay.sofa.jraft.Status)8 Region (com.alipay.sofa.jraft.rhea.metadata.Region)8 KVStoreClosure (com.alipay.sofa.jraft.rhea.storage.KVStoreClosure)8 RawKVStore (com.alipay.sofa.jraft.rhea.storage.RawKVStore)8 CompletableFuture (java.util.concurrent.CompletableFuture)8 RouteTable (com.alipay.sofa.jraft.RouteTable)6 PeerId (com.alipay.sofa.jraft.entity.PeerId)6 DescriberManager (com.alipay.sofa.jraft.rhea.DescriberManager)6 FollowerStateListener (com.alipay.sofa.jraft.rhea.FollowerStateListener)6 JRaftHelper (com.alipay.sofa.jraft.rhea.JRaftHelper)6 LeaderStateListener (com.alipay.sofa.jraft.rhea.LeaderStateListener)6 RegionEngine (com.alipay.sofa.jraft.rhea.RegionEngine)6 StateListener (com.alipay.sofa.jraft.rhea.StateListener)6 StateListenerContainer (com.alipay.sofa.jraft.rhea.StateListenerContainer)6 StoreEngine (com.alipay.sofa.jraft.rhea.StoreEngine)6 FailoverClosure (com.alipay.sofa.jraft.rhea.client.failover.FailoverClosure)6 ListRetryCallable (com.alipay.sofa.jraft.rhea.client.failover.ListRetryCallable)6