use of com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure in project sofa-jraft by sofastack.
the class RocksKVStoreTest method compareAndPutTest.
/**
* Test method: {@link RocksRawKVStore#compareAndPut(byte[], byte[], byte[], KVStoreClosure)}
*/
@Test
public void compareAndPutTest() {
final byte[] key = makeKey("put_test");
final byte[] value = makeValue("put_test_value");
this.kvStore.put(key, value, null);
final byte[] update = makeValue("put_test_update");
KVStoreClosure kvStoreClosure = new BaseKVStoreClosure() {
@Override
public void run(Status status) {
assertEquals(status, Status.OK());
}
};
this.kvStore.compareAndPut(key, value, update, kvStoreClosure);
assertEquals(kvStoreClosure.getData(), Boolean.TRUE);
byte[] newValue = new SyncKVStore<byte[]>() {
@Override
public void execute(RawKVStore kvStore, KVStoreClosure closure) {
kvStore.get(key, closure);
}
}.apply(this.kvStore);
assertArrayEquals(update, newValue);
this.kvStore.compareAndPut(key, value, update, kvStoreClosure);
assertEquals(kvStoreClosure.getData(), Boolean.FALSE);
}
use of com.alipay.sofa.jraft.rhea.storage.BaseKVStoreClosure in project sofa-jraft by sofastack.
the class DefaultRegionKVService method handleDeleteRangeRequest.
@Override
public void handleDeleteRangeRequest(final DeleteRangeRequest request, final RequestProcessClosure<BaseRequest, BaseResponse<?>> closure) {
final DeleteRangeResponse response = new DeleteRangeResponse();
response.setRegionId(getRegionId());
response.setRegionEpoch(getRegionEpoch());
try {
KVParameterRequires.requireSameEpoch(request, getRegionEpoch());
final byte[] startKey = KVParameterRequires.requireNonNull(request.getStartKey(), "deleteRange.startKey");
final byte[] endKey = KVParameterRequires.requireNonNull(request.getEndKey(), "deleteRange.endKey");
this.rawKVStore.deleteRange(startKey, endKey, 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 handleNodeExecuteRequest.
@Override
public void handleNodeExecuteRequest(final NodeExecuteRequest request, final RequestProcessClosure<BaseRequest, BaseResponse<?>> closure) {
final NodeExecuteResponse response = new NodeExecuteResponse();
response.setRegionId(getRegionId());
response.setRegionEpoch(getRegionEpoch());
try {
KVParameterRequires.requireSameEpoch(request, getRegionEpoch());
final NodeExecutor executor = KVParameterRequires.requireNonNull(request.getNodeExecutor(), "node.executor");
this.rawKVStore.execute(executor, true, 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 handleCompareAndPutAll.
@Override
public void handleCompareAndPutAll(final CASAllRequest request, final RequestProcessClosure<BaseRequest, BaseResponse<?>> closure) {
final CASAllResponse response = new CASAllResponse();
response.setRegionId(getRegionId());
response.setRegionEpoch(getRegionEpoch());
try {
KVParameterRequires.requireSameEpoch(request, getRegionEpoch());
final List<CASEntry> casEntries = KVParameterRequires.requireNonEmpty(request.getCasEntries(), "casAll.casEntries");
this.rawKVStore.compareAndPutAll(casEntries, 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 handleGetSequence.
@Override
public void handleGetSequence(final GetSequenceRequest request, final RequestProcessClosure<BaseRequest, BaseResponse<?>> closure) {
final GetSequenceResponse response = new GetSequenceResponse();
response.setRegionId(getRegionId());
response.setRegionEpoch(getRegionEpoch());
try {
KVParameterRequires.requireSameEpoch(request, getRegionEpoch());
final byte[] seqKey = KVParameterRequires.requireNonNull(request.getSeqKey(), "sequence.seqKey");
final int step = KVParameterRequires.requireNonNegative(request.getStep(), "sequence.step");
this.rawKVStore.getSequence(seqKey, step, new BaseKVStoreClosure() {
@Override
public void run(final Status status) {
if (status.isOk()) {
response.setValue((Sequence) 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