Search in sources :

Example 1 with KVIterator

use of com.alipay.sofa.jraft.rhea.storage.KVIterator 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 2 with KVIterator

use of com.alipay.sofa.jraft.rhea.storage.KVIterator 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());
    }
}
Also used : KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry) KVIterator(com.alipay.sofa.jraft.rhea.storage.KVIterator) IOException(java.io.IOException) Test(org.junit.Test)

Aggregations

KVEntry (com.alipay.sofa.jraft.rhea.storage.KVEntry)2 KVIterator (com.alipay.sofa.jraft.rhea.storage.KVIterator)2 Test (org.junit.Test)2 IOException (java.io.IOException)1