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());
}
}
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);
}
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());
}
}
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);
}
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());
}
}
Aggregations