Search in sources :

Example 6 with DeleteResult

use of org.apache.bookkeeper.api.kv.result.DeleteResult 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();
        }
    });
}
Also used : KeyValue(org.apache.bookkeeper.api.kv.result.KeyValue) KvApiException(org.apache.bookkeeper.api.kv.exceptions.KvApiException) DeleteResult(org.apache.bookkeeper.api.kv.result.DeleteResult)

Aggregations

DeleteResult (org.apache.bookkeeper.api.kv.result.DeleteResult)6 KeyValue (org.apache.bookkeeper.api.kv.result.KeyValue)6 KvApiException (org.apache.bookkeeper.api.kv.exceptions.KvApiException)4 CompareResult (org.apache.bookkeeper.api.kv.op.CompareResult)2 Code (org.apache.bookkeeper.api.kv.result.Code)2 RangeResult (org.apache.bookkeeper.api.kv.result.RangeResult)2 Result (org.apache.bookkeeper.api.kv.result.Result)2