Search in sources :

Example 1 with BaseKVStoreClosure

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

the class RocksKVStoreTest method batchPutIfAbsentTest.

/**
 * Test method: {@link RocksRawKVStore#batchPutIfAbsent(KVStateOutputList)}
 */
@Test
public void batchPutIfAbsentTest() {
    final KVStateOutputList kvStates = KVStateOutputList.newInstance();
    final int batchWriteSize = RocksRawKVStore.MAX_BATCH_WRITE_SIZE + 1;
    for (int i = 1; i <= batchWriteSize; i++) {
        final byte[] key = makeKey("put_test" + i);
        final byte[] value = makeValue("put_test_value" + i);
        KVStoreClosure kvStoreClosure = new BaseKVStoreClosure() {

            @Override
            public void run(Status status) {
                assertEquals(status, Status.OK());
            }
        };
        kvStates.add(KVState.of(KVOperation.createPutIfAbsent(key, value), kvStoreClosure));
    }
    this.kvStore.batchPutIfAbsent(kvStates);
    kvStates.forEach(kvState -> assertNull(kvState.getDone().getData()));
    kvStates.clear();
    for (int i = 1; i <= batchWriteSize; i++) {
        final byte[] key = makeKey("put_test" + i);
        final byte[] value = makeValue("put_test_value" + i);
        KVStoreClosure kvStoreClosure = new BaseKVStoreClosure() {

            @Override
            public void run(Status status) {
                assertEquals(status, Status.OK());
            }
        };
        kvStates.add(KVState.of(KVOperation.createPutIfAbsent(key, value), kvStoreClosure));
    }
    this.kvStore.batchPutIfAbsent(kvStates);
    kvStates.forEach(kvState -> assertArrayEquals(kvState.getOp().getValue(), (byte[]) kvState.getDone().getData()));
}
Also used : Status(com.alipay.sofa.jraft.Status) KVStateOutputList(com.alipay.sofa.jraft.rhea.storage.KVStateOutputList) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) KVStoreClosure(com.alipay.sofa.jraft.rhea.storage.KVStoreClosure) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) Test(org.junit.Test)

Example 2 with BaseKVStoreClosure

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

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

the class RocksKVStoreTest method getAndPutTest.

/**
 * Test method: {@link RocksRawKVStore#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) KVStoreClosure(com.alipay.sofa.jraft.rhea.storage.KVStoreClosure) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) Test(org.junit.Test)

Example 4 with BaseKVStoreClosure

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

the class DefaultRegionKVService method handlePutRequest.

@Override
public void handlePutRequest(final PutRequest request, final RequestProcessClosure<BaseRequest, BaseResponse<?>> closure) {
    final PutResponse response = new PutResponse();
    response.setRegionId(getRegionId());
    response.setRegionEpoch(getRegionEpoch());
    try {
        KVParameterRequires.requireSameEpoch(request, getRegionEpoch());
        final byte[] key = KVParameterRequires.requireNonNull(request.getKey(), "put.key");
        final byte[] value = KVParameterRequires.requireNonNull(request.getValue(), "put.value");
        this.rawKVStore.put(key, value, new BaseKVStoreClosure() {

            @Override
            public void run(final Status status) {
                if (status.isOk()) {
                    response.setValue((Boolean) getData());
                } else {
                    setFailure(request, response, status, getError());
                }
                closure.sendResponse(response);
            }
        });
    } catch (final Throwable t) {
        LOG.error("Failed to handle: {}, {}.", request, StackTraceUtil.stackTrace(t));
        response.setError(Errors.forException(t));
        closure.sendResponse(response);
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure) GetAndPutResponse(com.alipay.sofa.jraft.rhea.cmd.store.GetAndPutResponse) CompareAndPutResponse(com.alipay.sofa.jraft.rhea.cmd.store.CompareAndPutResponse) BatchPutResponse(com.alipay.sofa.jraft.rhea.cmd.store.BatchPutResponse) PutResponse(com.alipay.sofa.jraft.rhea.cmd.store.PutResponse)

Example 5 with BaseKVStoreClosure

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

the class DefaultRegionKVService method handleBatchPutRequest.

@Override
public void handleBatchPutRequest(final BatchPutRequest request, final RequestProcessClosure<BaseRequest, BaseResponse<?>> closure) {
    final BatchPutResponse response = new BatchPutResponse();
    response.setRegionId(getRegionId());
    response.setRegionEpoch(getRegionEpoch());
    try {
        KVParameterRequires.requireSameEpoch(request, getRegionEpoch());
        final List<KVEntry> kvEntries = KVParameterRequires.requireNonEmpty(request.getKvEntries(), "put.kvEntries");
        this.rawKVStore.put(kvEntries, new BaseKVStoreClosure() {

            @Override
            public void run(final Status status) {
                if (status.isOk()) {
                    response.setValue((Boolean) getData());
                } else {
                    setFailure(request, response, status, getError());
                }
                closure.sendResponse(response);
            }
        });
    } catch (final Throwable t) {
        LOG.error("Failed to handle: {}, {}.", request, StackTraceUtil.stackTrace(t));
        response.setError(Errors.forException(t));
        closure.sendResponse(response);
    }
}
Also used : Status(com.alipay.sofa.jraft.Status) KVEntry(com.alipay.sofa.jraft.rhea.storage.KVEntry) BatchPutResponse(com.alipay.sofa.jraft.rhea.cmd.store.BatchPutResponse) BaseKVStoreClosure(com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure)

Aggregations

Status (com.alipay.sofa.jraft.Status)29 BaseKVStoreClosure (com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure)29 KVStoreClosure (com.alipay.sofa.jraft.rhea.storage.KVStoreClosure)8 Test (org.junit.Test)8 RawKVStore (com.alipay.sofa.jraft.rhea.storage.RawKVStore)4 Sequence (com.alipay.sofa.jraft.rhea.storage.Sequence)3 BatchDeleteResponse (com.alipay.sofa.jraft.rhea.cmd.store.BatchDeleteResponse)2 BatchPutResponse (com.alipay.sofa.jraft.rhea.cmd.store.BatchPutResponse)2 CompareAndPutResponse (com.alipay.sofa.jraft.rhea.cmd.store.CompareAndPutResponse)2 GetAndPutResponse (com.alipay.sofa.jraft.rhea.cmd.store.GetAndPutResponse)2 MultiGetResponse (com.alipay.sofa.jraft.rhea.cmd.store.MultiGetResponse)2 KVEntry (com.alipay.sofa.jraft.rhea.storage.KVEntry)2 KVStateOutputList (com.alipay.sofa.jraft.rhea.storage.KVStateOutputList)2 MemoryRawKVStore (com.alipay.sofa.jraft.rhea.storage.MemoryRawKVStore)2 RocksRawKVStore (com.alipay.sofa.jraft.rhea.storage.RocksRawKVStore)2 TestClosure (com.alipay.sofa.jraft.rhea.storage.TestClosure)2 DistributedLock (com.alipay.sofa.jraft.rhea.util.concurrent.DistributedLock)2 RegionEngine (com.alipay.sofa.jraft.rhea.RegionEngine)1 CASAllResponse (com.alipay.sofa.jraft.rhea.cmd.store.CASAllResponse)1 ContainsKeyResponse (com.alipay.sofa.jraft.rhea.cmd.store.ContainsKeyResponse)1