use of org.apache.bookkeeper.api.kv.result.TxnResult in project bookkeeper by apache.
the class MVCCStoreImpl method processTxn.
synchronized TxnResult<K, V> processTxn(long revision, TxnOp<K, V> op) {
checkStoreOpen();
// 1. process the compares
boolean success = processCompares(op);
// 2. prepare the response list
List<Op<K, V>> operations;
List<Result<K, V>> results;
if (success) {
operations = op.successOps();
} else {
operations = op.failureOps();
}
if (operations == null) {
operations = Collections.emptyList();
}
results = Lists.newArrayListWithExpectedSize(operations.size());
// 3. process the operations
try (WriteBatch batch = new WriteBatch()) {
for (Op<K, V> o : operations) {
results.add(executeOp(revision, batch, o));
}
executeBatch(batch);
// 4. repare the result
TxnResultImpl<K, V> txnResult = resultFactory.newTxnResult(revision);
txnResult.isSuccess(success);
txnResult.results(results);
txnResult.code(Code.OK);
return txnResult;
} catch (StateStoreRuntimeException e) {
results.forEach(Result::close);
throw e;
}
}
use of org.apache.bookkeeper.api.kv.result.TxnResult in project bookkeeper by apache.
the class TableClientTest method testTableAPI.
@Test
public void testTableAPI() throws Exception {
// Create a namespace
NamespaceConfiguration nsConf = NamespaceConfiguration.newBuilder().setDefaultStreamConf(DEFAULT_STREAM_CONF).build();
NamespaceProperties nsProps = FutureUtils.result(adminClient.createNamespace(namespace, nsConf));
assertEquals(namespace, nsProps.getNamespaceName());
assertEquals(nsConf.getDefaultStreamConf(), nsProps.getDefaultStreamConf());
// Create a stream
String streamName = testName.getMethodName() + "_stream";
StreamConfiguration streamConf = StreamConfiguration.newBuilder(DEFAULT_STREAM_CONF).build();
StreamProperties streamProps = FutureUtils.result(adminClient.createStream(namespace, streamName, streamConf));
assertEquals(streamName, streamProps.getStreamName());
assertEquals(StreamConfiguration.newBuilder(streamConf).setBackendServiceUrl(defaultBackendUri.toString()).build(), streamProps.getStreamConf());
// Open the table
PTable<ByteBuf, ByteBuf> table = FutureUtils.result(storageClient.openPTable(streamName));
byte[] rKey = "routing-key".getBytes(UTF_8);
byte[] lKey = "testing-key".getBytes(UTF_8);
byte[] value = "testing-value".getBytes(UTF_8);
// put first key
ByteBuf rKeyBuf = Unpooled.wrappedBuffer(rKey);
ByteBuf lKeyBuf = Unpooled.wrappedBuffer(lKey);
ByteBuf valBuf = Unpooled.wrappedBuffer(value);
try (PutOption<ByteBuf> option = Options.putAndGet()) {
try (PutResult<ByteBuf, ByteBuf> putResult = FutureUtils.result(table.put(rKeyBuf, lKeyBuf, valBuf, option))) {
assertNull(putResult.prevKv());
}
}
// put second key
ByteBuf valBuf2 = Unpooled.wrappedBuffer("testing-value-2".getBytes(UTF_8));
try (PutOption<ByteBuf> option = Options.putAndGet()) {
try (PutResult<ByteBuf, ByteBuf> putResult = FutureUtils.result(table.put(rKeyBuf, lKeyBuf, valBuf2, option))) {
assertNotNull(putResult.prevKv());
KeyValue<ByteBuf, ByteBuf> prevKv = putResult.prevKv();
assertEquals("testing-key", new String(ByteBufUtil.getBytes(prevKv.key()), UTF_8));
assertEquals("testing-value", new String(ByteBufUtil.getBytes(prevKv.value()), UTF_8));
}
}
// get key
try (RangeOption<ByteBuf> option = optionFactory.newRangeOption().build()) {
try (RangeResult<ByteBuf, ByteBuf> getResult = FutureUtils.result(table.get(rKeyBuf, lKeyBuf, option))) {
assertEquals(1, getResult.count());
assertEquals(1, getResult.kvs().size());
KeyValue<ByteBuf, ByteBuf> kv = getResult.kvs().get(0);
assertEquals("testing-key", new String(ByteBufUtil.getBytes(kv.key()), UTF_8));
assertEquals("testing-value-2", new String(ByteBufUtil.getBytes(kv.value()), UTF_8));
}
}
// delete key
try (DeleteOption<ByteBuf> option = optionFactory.newDeleteOption().prevKv(true).build()) {
try (DeleteResult<ByteBuf, ByteBuf> deleteResult = FutureUtils.result(table.delete(rKeyBuf, lKeyBuf, option))) {
assertEquals(1, deleteResult.numDeleted());
assertEquals(1, deleteResult.prevKvs().size());
KeyValue<ByteBuf, ByteBuf> kv = deleteResult.prevKvs().get(0);
assertEquals("testing-key", new String(ByteBufUtil.getBytes(kv.key()), UTF_8));
assertEquals("testing-value-2", new String(ByteBufUtil.getBytes(kv.value()), UTF_8));
}
}
// write a range of key
int numKvs = 100;
rKeyBuf = Unpooled.wrappedBuffer("test-key".getBytes(UTF_8));
try (PutOption<ByteBuf> option = Options.blindPut()) {
for (int i = 0; i < numKvs; i++) {
lKeyBuf = getLKey(i);
valBuf = getValue(i);
FutureUtils.result(table.put(rKeyBuf, lKeyBuf, valBuf, option));
}
}
// get ranges
ByteBuf lStartKey = getLKey(20);
ByteBuf lEndKey = getLKey(50);
try (RangeOption<ByteBuf> option = optionFactory.newRangeOption().endKey(lEndKey).build()) {
try (RangeResult<ByteBuf, ByteBuf> rangeResult = FutureUtils.result(table.get(rKeyBuf, lStartKey, option))) {
assertEquals(31, rangeResult.kvs().size());
assertEquals(31, rangeResult.count());
int i = 20;
for (KeyValue<ByteBuf, ByteBuf> kvPair : rangeResult.kvs()) {
assertEquals(getLKey(i), kvPair.key());
assertEquals(getValue(i), kvPair.value());
++i;
}
assertEquals(51, i);
}
}
// delete range
try (DeleteOption<ByteBuf> option = optionFactory.newDeleteOption().prevKv(true).endKey(lEndKey).build()) {
try (DeleteResult<ByteBuf, ByteBuf> deleteRangeResult = FutureUtils.result(table.delete(rKeyBuf, lStartKey, option))) {
assertEquals(31, deleteRangeResult.numDeleted());
assertEquals(31, deleteRangeResult.prevKvs().size());
int i = 20;
for (KeyValue<ByteBuf, ByteBuf> kvPair : deleteRangeResult.prevKvs()) {
assertEquals(getLKey(i), kvPair.key());
assertEquals(getValue(i), kvPair.value());
++i;
}
assertEquals(51, i);
}
}
// test txn
byte[] lTxnKey = "txn-key".getBytes(UTF_8);
ByteBuf lTxnKeyBuf = Unpooled.wrappedBuffer(lTxnKey);
byte[] txnValue = "txn-value".getBytes(UTF_8);
ByteBuf txnValueBuf = Unpooled.wrappedBuffer(txnValue);
Txn<ByteBuf, ByteBuf> txn = table.txn(lTxnKeyBuf);
CompletableFuture<TxnResult<ByteBuf, ByteBuf>> commitFuture = txn.If(table.opFactory().compareValue(CompareResult.EQUAL, lTxnKeyBuf, Unpooled.wrappedBuffer(new byte[0]))).Then(table.opFactory().newPut(lTxnKeyBuf, txnValueBuf, table.opFactory().optionFactory().newPutOption().build())).commit();
try (TxnResult<ByteBuf, ByteBuf> txnResult = FutureUtils.result(commitFuture)) {
assertTrue(txnResult.isSuccess());
assertEquals(1, txnResult.results().size());
Result<ByteBuf, ByteBuf> opResult = txnResult.results().get(0);
assertEquals(OpType.PUT, opResult.type());
}
// get key
try (RangeOption<ByteBuf> option = optionFactory.newRangeOption().build()) {
try (RangeResult<ByteBuf, ByteBuf> getResult = FutureUtils.result(table.get(lTxnKeyBuf, lTxnKeyBuf, option))) {
assertEquals(1, getResult.count());
assertEquals(1, getResult.kvs().size());
KeyValue<ByteBuf, ByteBuf> kv = getResult.kvs().get(0);
assertEquals("txn-key", new String(ByteBufUtil.getBytes(kv.key()), UTF_8));
assertEquals("txn-value", new String(ByteBufUtil.getBytes(kv.value()), UTF_8));
}
}
txn = table.txn(lTxnKeyBuf);
// txn failure
commitFuture = txn.If(table.opFactory().compareValue(CompareResult.EQUAL, lTxnKeyBuf, Unpooled.wrappedBuffer(new byte[0]))).Then(table.opFactory().newPut(lTxnKeyBuf, valBuf, table.opFactory().optionFactory().newPutOption().build())).commit();
try (TxnResult<ByteBuf, ByteBuf> txnResult = FutureUtils.result(commitFuture)) {
assertFalse(txnResult.isSuccess());
assertEquals(0, txnResult.results().size());
}
}
Aggregations