Search in sources :

Example 21 with KVEntry

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

the class MemoryKVStoreTest method putListTest.

/**
 * Test method: {@link MemoryRawKVStore#put(List, KVStoreClosure)}
 */
@Test
public void putListTest() {
    final List<KVEntry> entries = Lists.newArrayList();
    for (int i = 0; i < 10; i++) {
        entries.add(new KVEntry(makeKey("batch_put_test_key" + i), makeValue("batch_put_test_value" + i)));
    }
    this.kvStore.put(entries, null);
    final List<KVEntry> entries2 = new SyncKVStore<List<KVEntry>>() {

        @Override
        public void execute(RawKVStore kvStore, KVStoreClosure closure) {
            kvStore.scan(makeKey("batch_put_test_key"), makeKey("batch_put_test_key" + 99), closure);
        }
    }.apply(this.kvStore);
    assertEquals(entries.size(), entries2.size());
    for (int i = 0; i < entries.size(); i++) {
        assertArrayEquals(entries.get(i).getKey(), entries2.get(i).getKey());
        assertArrayEquals(entries.get(i).getValue(), entries2.get(i).getValue());
    }
}
Also used : RawKVStore(com.alipay.sofa.jraft.rhea.storage.RawKVStore) MemoryRawKVStore(com.alipay.sofa.jraft.rhea.storage.MemoryRawKVStore) KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry) List(java.util.List) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) KVStoreClosure(com.alipay.sofa.jraft.rhea.storage.KVStoreClosure) Test(org.junit.Test)

Example 22 with KVEntry

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

the class MemoryKVStoreTest method deleteRangeTest.

@SuppressWarnings("unchecked")
@Test
public void deleteRangeTest() {
    final List<KVEntry> entries = Lists.newArrayList();
    for (int i = 0; i < 10; i++) {
        entries.add(new KVEntry(makeKey("del_range_test" + i), makeValue("del_range_test" + i)));
    }
    this.kvStore.put(entries, null);
    // delete [del_range_test5, del_range_test8)
    this.kvStore.deleteRange(makeKey("del_range_test5"), makeKey("del_range_test8"), null);
    TestClosure closure = new TestClosure();
    this.kvStore.scan(makeKey("del_range_test"), makeKey("del_range_test" + 99), closure);
    final List<KVEntry> entries2 = (List<KVEntry>) closure.getData();
    assertEquals(entries.size() - 3, entries2.size());
    closure = new TestClosure();
    this.kvStore.get(makeKey("del_range_test5"), closure);
    byte[] value = (byte[]) closure.getData();
    assertNull(value);
    closure = new TestClosure();
    this.kvStore.get(makeKey("del_range_test6"), closure);
    value = (byte[]) closure.getData();
    assertNull(value);
    closure = new TestClosure();
    this.kvStore.get(makeKey("del_range_test7"), closure);
    value = (byte[]) closure.getData();
    assertNull(value);
    closure = new TestClosure();
    this.kvStore.get(makeKey("del_range_test8"), closure);
    value = (byte[]) closure.getData();
    assertNotNull(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)

Example 23 with KVEntry

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

the class MemoryKVStoreTest method getLocalIteratorTest.

/**
 * Test method: {@link MemoryRawKVStore#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();
    KVIterator it = this.kvStore.localIterator();
    try {
        it.seekToFirst();
        while (it.isValid()) {
            entries.add(new KVEntry(it.key(), it.value()));
            it.next();
        }
    } finally {
        try {
            it.close();
        } catch (Exception ignored) {
        // ignored
        }
    }
    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());
    }
    entries.clear();
    // 
    it = this.kvStore.localIterator();
    try {
        it.seekToLast();
        while (it.isValid()) {
            entries.add(new KVEntry(it.key(), it.value()));
            it.prev();
        }
    } finally {
        try {
            it.close();
        } catch (Exception ignored) {
        // ignored
        }
    }
    for (int i = 0; i < keyList.size(); i++) {
        final int cmpIndex = keyList.size() - 1 - i;
        assertArrayEquals(keyList.get(i), entries.get(cmpIndex).getKey());
        assertArrayEquals(valueList.get(i), entries.get(cmpIndex).getValue());
    }
    entries.clear();
    // 
    it = this.kvStore.localIterator();
    try {
        it.seek(BytesUtil.writeUtf8("iterator_test_key_5"));
        while (it.isValid()) {
            entries.add(new KVEntry(it.key(), it.value()));
            it.next();
        }
    } finally {
        try {
            it.close();
        } catch (Exception ignored) {
        // ignored
        }
    }
    for (int i = 5; i < keyList.size(); i++) {
        assertArrayEquals(keyList.get(i), entries.get(i - 5).getKey());
        assertArrayEquals(valueList.get(i), entries.get(i - 5).getValue());
    }
    entries.clear();
    // 
    it = this.kvStore.localIterator();
    try {
        it.seekForPrev(BytesUtil.writeUtf8("iterator_test_key_5"));
        while (it.isValid()) {
            entries.add(new KVEntry(it.key(), it.value()));
            it.prev();
        }
    } finally {
        try {
            it.close();
        } catch (Exception ignored) {
        // ignored
        }
    }
    for (int i = 0; i < 5; i++) {
        assertArrayEquals(keyList.get(i), entries.get(4 - i).getKey());
        assertArrayEquals(valueList.get(i), entries.get(4 - i).getValue());
    }
}
Also used : KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry) KVIterator(com.alipay.sofa.jraft.rhea.storage.KVIterator) Test(org.junit.Test)

Example 24 with KVEntry

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

the class RocksKVStoreTest method deleteRangeTest.

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

Example 25 with KVEntry

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

the class AbstractRheaKVStoreTest method putListTest.

/**
 * Test method: {@link RheaKVStore#put(List)}
 */
private void putListTest(RheaKVStore store) {
    // regions: 1 -> [null, g), 2 -> [g, null)
    List<KVEntry> entries1 = Lists.newArrayList();
    for (int i = 0; i < 3; i++) {
        byte[] key = makeKey("batch_put_test_key" + i);
        checkRegion(store, key, 1);
        entries1.add(new KVEntry(key, makeValue("batch_put_test_value" + i)));
    }
    store.bPut(entries1);
    List<KVEntry> entries2 = Lists.newArrayList();
    for (int i = 0; i < 10; i++) {
        byte[] key = makeKey("g_batch_put_test_key" + i);
        checkRegion(store, key, 2);
        entries2.add(new KVEntry(key, makeValue("batch_put_test_value" + i)));
    }
    store.bPut(entries2);
    List<KVEntry> foundList = store.bScan(makeKey("batch_put_test_key"), makeKey("batch_put_test_key" + 99));
    assertEquals(entries1.size(), foundList.size());
    for (int i = 0; i < entries1.size(); i++) {
        assertArrayEquals(entries1.get(i).getKey(), foundList.get(i).getKey());
        assertArrayEquals(entries1.get(i).getValue(), foundList.get(i).getValue());
    }
    foundList = store.bScan(makeKey("g_batch_put_test_key"), makeKey("g_batch_put_test_key" + 99));
    assertEquals(entries2.size(), foundList.size());
    for (int i = 0; i < entries2.size(); i++) {
        assertArrayEquals(entries2.get(i).getKey(), foundList.get(i).getKey());
        assertArrayEquals(entries2.get(i).getValue(), foundList.get(i).getValue());
    }
}
Also used : KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry)

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