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