use of org.apache.bookkeeper.api.kv.result.KeyValue in project bookkeeper by apache.
the class MVCCAsyncStore method vDelete.
@Override
default CompletableFuture<KeyValue<K, V>> vDelete(K k, long expectedVersion) {
TxnOp<K, V> op = getOpFactory().newTxn().If(newCompareVersion(CompareResult.EQUAL, k, expectedVersion)).Then(getOpFactory().newDelete(k, Options.deleteAndGet())).build();
return txn(op).thenCompose(result -> {
try {
Code code = result.code();
if (Code.OK == code && !result.isSuccess()) {
code = Code.BAD_REVISION;
}
if (Code.OK == code) {
List<Result<K, V>> subResults = result.results();
DeleteResult<K, V> deleteResult = (DeleteResult<K, V>) subResults.get(0);
List<KeyValue<K, V>> prevKvs = deleteResult.getPrevKvsAndClear();
if (prevKvs.isEmpty()) {
return FutureUtils.value(null);
} else {
return FutureUtils.value(prevKvs.get(0));
}
} else {
return failWithCode(code, "Failed to vDelete key " + k + " (version=" + expectedVersion + ") to store " + name());
}
} finally {
result.close();
}
});
}
use of org.apache.bookkeeper.api.kv.result.KeyValue in project bookkeeper by apache.
the class PTableWriteView method vDelete.
default CompletableFuture<KeyValue<K, V>> vDelete(K pKey, K lKey, long expectedVersion) {
Txn<K, V> txn = txn(pKey);
txn.If(opFactory().compareVersion(CompareResult.EQUAL, lKey, expectedVersion)).Then(opFactory().newDelete(lKey, Options.deleteAndGet()));
return txn.commit().thenCompose(result -> {
try {
if (result.isSuccess()) {
DeleteResult<K, V> deleteResult = (DeleteResult<K, V>) result.results().get(0);
if (deleteResult.prevKvs().isEmpty()) {
return FutureUtils.value(null);
} else {
List<KeyValue<K, V>> prevKvs = deleteResult.getPrevKvsAndClear();
return FutureUtils.value(prevKvs.get(0));
}
} else {
return FutureUtils.exception(new KvApiException(Code.BAD_REVISION, "Failed to vDelete key " + lKey + " (version = " + expectedVersion + ")"));
}
} finally {
result.close();
}
});
}
use of org.apache.bookkeeper.api.kv.result.KeyValue in project bookkeeper by apache.
the class PTableWriteView method rDelete.
default CompletableFuture<KeyValue<K, V>> rDelete(K pKey, K lKey, long expectedRevision) {
Txn<K, V> txn = txn(pKey);
txn.If(opFactory().compareModRevision(CompareResult.EQUAL, lKey, expectedRevision)).Then(opFactory().newDelete(lKey, Options.deleteAndGet()));
return txn.commit().thenCompose(result -> {
try {
if (result.isSuccess()) {
DeleteResult<K, V> deleteResult = (DeleteResult<K, V>) result.results().get(0);
if (deleteResult.prevKvs().isEmpty()) {
return FutureUtils.value(null);
} else {
List<KeyValue<K, V>> prevKvs = deleteResult.getPrevKvsAndClear();
return FutureUtils.value(prevKvs.get(0));
}
} else {
return FutureUtils.exception(new KvApiException(Code.BAD_REVISION, "Failed to rDelete key " + lKey + " (mod_rev = " + expectedRevision + ")"));
}
} finally {
result.close();
}
});
}
use of org.apache.bookkeeper.api.kv.result.KeyValue in project bookkeeper by apache.
the class TableWriteView method vDelete.
default CompletableFuture<KeyValue<K, V>> vDelete(K key, long expectedVersion) {
Txn<K, V> txn = txn(key);
txn.If(opFactory().compareVersion(CompareResult.EQUAL, key, expectedVersion)).Then(opFactory().newDelete(key, Options.deleteAndGet()));
return txn.commit().thenCompose(result -> {
try {
if (result.isSuccess()) {
DeleteResult<K, V> deleteResult = (DeleteResult<K, V>) result.results().get(0);
if (deleteResult.prevKvs().isEmpty()) {
return FutureUtils.value(null);
} else {
List<KeyValue<K, V>> prevKvs = deleteResult.getPrevKvsAndClear();
return FutureUtils.value(prevKvs.get(0));
}
} else {
return FutureUtils.exception(new KvApiException(Code.BAD_REVISION, "Failed to vDelete key " + key + " (version = " + expectedVersion + ")"));
}
} finally {
result.close();
}
});
}
use of org.apache.bookkeeper.api.kv.result.KeyValue in project bookkeeper by apache.
the class TableClientSimpleTest method testTableSimpleAPI.
@Test
public void testTableSimpleAPI() throws Exception {
// Create a namespace
NamespaceConfiguration nsConf = NamespaceConfiguration.newBuilder().setDefaultStreamConf(DEFAULT_STREAM_CONF).build();
NamespaceProperties nsProps = 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 = 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 = result(storageClient.openPTable(streamName));
byte[] rKey = "routing-key".getBytes(UTF_8);
byte[] lKey = "testing-key".getBytes(UTF_8);
byte[] value1 = "testing-value-1".getBytes(UTF_8);
byte[] value2 = "testing-value-2".getBytes(UTF_8);
// put first key
ByteBuf rKeyBuf = Unpooled.wrappedBuffer(rKey);
ByteBuf lKeyBuf = Unpooled.wrappedBuffer(lKey);
ByteBuf valBuf1 = Unpooled.wrappedBuffer(value1);
ByteBuf valBuf2 = Unpooled.wrappedBuffer(value2);
// normal put
assertNull(result(table.put(rKeyBuf, lKeyBuf, valBuf1)));
// putIfAbsent failure
assertArrayEquals(value1, ByteBufUtil.getBytes(result(table.putIfAbsent(rKeyBuf, lKeyBuf, valBuf2))));
// delete failure
assertFalse(result(table.delete(rKeyBuf, lKeyBuf, valBuf2)));
// delete success
assertTrue(result(table.delete(rKeyBuf, lKeyBuf, valBuf1)));
// get
assertNull(result(table.get(rKeyBuf, lKeyBuf)));
// putIfAbsent success
assertNull(result(table.putIfAbsent(rKeyBuf, lKeyBuf, valBuf2)));
// get returns val2
assertArrayEquals(value2, ByteBufUtil.getBytes(result(table.get(rKeyBuf, lKeyBuf))));
// vPut failure
try {
result(table.vPut(rKeyBuf, lKeyBuf, valBuf1, 9999L));
fail("Should fail vPut if the version doesn't match");
} catch (KvApiException e) {
assertEquals(Code.BAD_REVISION, e.getCode());
}
// vPut success
assertEquals(1L, result(table.vPut(rKeyBuf, lKeyBuf, valBuf1, 0L)).longValue());
// vDelete failure
try {
result(table.vDelete(rKeyBuf, lKeyBuf, 9999L));
fail("Should fail vDelete if the version doesn't match");
} catch (KvApiException e) {
assertEquals(Code.BAD_REVISION, e.getCode());
}
// vDelete success
try (KeyValue<ByteBuf, ByteBuf> prevKv = result(table.vDelete(rKeyBuf, lKeyBuf, 1L))) {
assertNotNull(prevKv);
assertEquals(1L, prevKv.version());
assertArrayEquals(value1, ByteBufUtil.getBytes(prevKv.value()));
}
// write a range of key
int numKvs = 100;
rKeyBuf = Unpooled.wrappedBuffer("test-key".getBytes(UTF_8));
for (int i = 0; i < numKvs; i++) {
lKeyBuf = getLKey(i);
valBuf1 = getValue(i);
result(table.put(rKeyBuf, lKeyBuf, valBuf1));
}
// get ranges
ByteBuf lStartKey = getLKey(20);
ByteBuf lEndKey = getLKey(50);
List<KeyValue<ByteBuf, ByteBuf>> kvs = result(table.range(rKeyBuf, lStartKey, lEndKey));
assertEquals(31, kvs.size());
int i = 20;
for (KeyValue<ByteBuf, ByteBuf> kvPair : kvs) {
assertEquals(getLKey(i), kvPair.key());
assertEquals(getValue(i), kvPair.value());
++i;
kvPair.close();
}
assertEquals(51, i);
// delete range
kvs = result(table.deleteRange(rKeyBuf, lStartKey, lEndKey));
assertEquals(31, kvs.size());
i = 20;
for (KeyValue<ByteBuf, ByteBuf> kvPair : kvs) {
assertEquals(getLKey(i), kvPair.key());
assertEquals(getValue(i), kvPair.value());
++i;
kvPair.close();
}
assertEquals(51, i);
// get ranges again
kvs = result(table.range(rKeyBuf, lStartKey, lEndKey));
assertTrue(kvs.isEmpty());
byte[] lIncrKey = "test-incr-lkey".getBytes(UTF_8);
ByteBuf lIncrKeyBuf = Unpooled.wrappedBuffer(lIncrKey);
// test increment
for (int j = 0; j < 5; j++) {
result(table.increment(rKeyBuf, lIncrKeyBuf, 100L));
long number = result(table.getNumber(rKeyBuf, lIncrKeyBuf));
assertEquals(100L * (j + 1), number);
}
for (int j = 5; j < 10; j++) {
long number = result(table.incrementAndGet(rKeyBuf, lIncrKeyBuf, 100L));
assertEquals(100L * (j + 1), number);
}
}
Aggregations