Search in sources :

Example 1 with TestClosure

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);
}
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 2 with TestClosure

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);
}
Also used : TestClosure(com.alipay.sofa.jraft.rhea.storage.TestClosure) SstColumnFamily(com.alipay.sofa.jraft.rhea.storage.SstColumnFamily) KVStoreSnapshotFile(com.alipay.sofa.jraft.rhea.storage.KVStoreSnapshotFile) File(java.io.File) EnumMap(java.util.EnumMap) Test(org.junit.Test)

Example 3 with TestClosure

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());
}
Also used : TestClosure(com.alipay.sofa.jraft.rhea.storage.TestClosure) Status(com.alipay.sofa.jraft.Status) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) KVStoreClosure(com.alipay.sofa.jraft.rhea.storage.KVStoreClosure) Test(org.junit.Test)

Example 4 with TestClosure

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);
    }
}
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 5 with TestClosure

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());
}
Also used : TestClosure(com.alipay.sofa.jraft.rhea.storage.TestClosure) Test(org.junit.Test)

Aggregations

TestClosure (com.alipay.sofa.jraft.rhea.storage.TestClosure)13 Test (org.junit.Test)13 KVEntry (com.alipay.sofa.jraft.rhea.storage.KVEntry)6 List (java.util.List)6 BaseKVStoreClosure (com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure)4 KVStoreClosure (com.alipay.sofa.jraft.rhea.storage.KVStoreClosure)4 KVStateOutputList (com.alipay.sofa.jraft.rhea.storage.KVStateOutputList)3 Status (com.alipay.sofa.jraft.Status)2 RawKVStore (com.alipay.sofa.jraft.rhea.storage.RawKVStore)2 KVStoreSnapshotFile (com.alipay.sofa.jraft.rhea.storage.KVStoreSnapshotFile)1 MemoryRawKVStore (com.alipay.sofa.jraft.rhea.storage.MemoryRawKVStore)1 RocksRawKVStore (com.alipay.sofa.jraft.rhea.storage.RocksRawKVStore)1 SstColumnFamily (com.alipay.sofa.jraft.rhea.storage.SstColumnFamily)1 File (java.io.File)1 EnumMap (java.util.EnumMap)1