use of com.alipay.sofa.jraft.rhea.storage.TestClosure in project sofa-jraft by sofastack.
the class RocksKVStoreTest 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);
}
use of com.alipay.sofa.jraft.rhea.storage.TestClosure in project sofa-jraft by sofastack.
the class RocksKVStoreTest method sstFilesTest.
@Test
public void sstFilesTest() throws IOException {
for (int i = 0; i < 10000; i++) {
byte[] bytes = BytesUtil.writeUtf8(String.valueOf(i));
this.kvStore.put(bytes, bytes, null);
}
this.kvStore.getSequence(BytesUtil.writeUtf8("seq"), 100, null);
final File defaultSstFile = new File("default.sst");
final File seqSstFile = new File("seq.sst");
final File lockingFile = new File("locking.sst");
final File fencingFile = new File("fencing.sst");
if (defaultSstFile.exists()) {
FileUtils.forceDelete(defaultSstFile);
}
if (seqSstFile.exists()) {
FileUtils.forceDelete(seqSstFile);
}
if (lockingFile.exists()) {
FileUtils.forceDelete(lockingFile);
}
if (fencingFile.exists()) {
FileUtils.forceDelete(fencingFile);
}
final EnumMap<SstColumnFamily, File> sstFileTable = new EnumMap<>(SstColumnFamily.class);
sstFileTable.put(SstColumnFamily.DEFAULT, defaultSstFile);
sstFileTable.put(SstColumnFamily.SEQUENCE, seqSstFile);
sstFileTable.put(SstColumnFamily.LOCKING, lockingFile);
sstFileTable.put(SstColumnFamily.FENCING, fencingFile);
KVStoreAccessHelper.createSstFiles(this.kvStore, sstFileTable, null, null);
// remove keys
for (int i = 0; i < 10000; i++) {
byte[] bytes = BytesUtil.writeUtf8(String.valueOf(i));
this.kvStore.delete(bytes, null);
}
this.kvStore.resetSequence(BytesUtil.writeUtf8("seq"), null);
for (int i = 0; i < 10000; i++) {
byte[] bytes = BytesUtil.writeUtf8(String.valueOf(i));
TestClosure closure = new TestClosure();
this.kvStore.get(bytes, closure);
assertNull(closure.getData());
}
KVStoreAccessHelper.ingestSstFiles(this.kvStore, sstFileTable);
if (defaultSstFile.exists()) {
FileUtils.forceDelete(defaultSstFile);
}
if (seqSstFile.exists()) {
FileUtils.forceDelete(seqSstFile);
}
if (lockingFile.exists()) {
FileUtils.forceDelete(lockingFile);
}
if (fencingFile.exists()) {
FileUtils.forceDelete(fencingFile);
}
for (int i = 0; i < 10000; i++) {
byte[] bytes = BytesUtil.writeUtf8(String.valueOf(i));
TestClosure closure = new TestClosure();
this.kvStore.get(bytes, closure);
assertArrayEquals((byte[]) closure.getData(), bytes);
}
TestClosure closure = new TestClosure();
this.kvStore.getSequence(BytesUtil.writeUtf8("seq"), 100, closure);
assertEquals(((Sequence) closure.getData()).getStartValue(), 100);
}
use of com.alipay.sofa.jraft.rhea.storage.TestClosure in project sofa-jraft by sofastack.
the class MemoryKVStoreTest method getAndPutTest.
/**
* Test method: {@link MemoryRawKVStore#getAndPut(byte[], byte[], KVStoreClosure)}
*/
@Test
public void getAndPutTest() {
final byte[] key = makeKey("put_test");
TestClosure closure = new TestClosure();
this.kvStore.get(key, closure);
byte[] value = (byte[]) closure.getData();
assertNull(value);
value = makeValue("put_test_value");
KVStoreClosure kvStoreClosure = new BaseKVStoreClosure() {
@Override
public void run(Status status) {
assertEquals(status, Status.OK());
}
};
this.kvStore.getAndPut(key, value, kvStoreClosure);
assertNull(kvStoreClosure.getData());
byte[] newVal = makeValue("put_test_value_new");
this.kvStore.getAndPut(key, newVal, kvStoreClosure);
assertArrayEquals(value, (byte[]) kvStoreClosure.getData());
}
use of com.alipay.sofa.jraft.rhea.storage.TestClosure in project sofa-jraft by sofastack.
the class MemoryKVStoreTest method deleteListTest.
/**
* Test method: {@link MemoryRawKVStore#delete(List, KVStoreClosure)}
*/
@SuppressWarnings("unchecked")
@Test
public void deleteListTest() {
final List<KVEntry> entries = Lists.newArrayList();
final List<byte[]> keys = Lists.newArrayList();
for (int i = 0; i < 10; i++) {
final byte[] key = makeKey("batch_del_test" + i);
entries.add(new KVEntry(key, makeValue("batch_del_test_value")));
keys.add(key);
}
this.kvStore.put(entries, null);
this.kvStore.delete(keys, null);
TestClosure closure = new TestClosure();
this.kvStore.scan(makeKey("batch_del_test"), makeKey("batch_del_test" + 99), closure);
List<KVEntry> entries2 = (List<KVEntry>) closure.getData();
assertEquals(0, entries2.size());
for (int i = 0; i < keys.size(); i++) {
closure = new TestClosure();
this.kvStore.get(keys.get(i), closure);
byte[] value = (byte[]) closure.getData();
assertNull(value);
}
}
use of com.alipay.sofa.jraft.rhea.storage.TestClosure in project sofa-jraft by sofastack.
the class MemoryKVStoreTest method putIfAbsent.
/**
* Test method: {@link MemoryRawKVStore#putIfAbsent(byte[], byte[], KVStoreClosure)}
*/
@Test
public void putIfAbsent() {
byte[] key = makeKey("put_if_absent_test");
byte[] value = makeValue("put_if_absent_test_value");
TestClosure closure = new TestClosure();
this.kvStore.putIfAbsent(key, value, closure);
assertNull(closure.getData());
this.kvStore.putIfAbsent(key, value, closure);
assertArrayEquals(value, (byte[]) closure.getData());
}
Aggregations