Search in sources :

Example 1 with RangeResult

use of org.apache.bookkeeper.api.kv.result.RangeResult in project bookkeeper by apache.

the class PTableWriteView method putIfAbsent.

default CompletableFuture<V> putIfAbsent(K pKey, K lKey, V value) {
    Txn<K, V> txn = txn(pKey);
    txn.If(opFactory().compareValue(CompareResult.EQUAL, lKey, null)).Then(opFactory().newPut(lKey, value, opFactory().optionFactory().newPutOption().build())).Else(newGet(lKey));
    return txn.commit().thenCompose(result -> {
        try {
            if (result.isSuccess()) {
                return FutureUtils.value(null);
            } else {
                RangeResult<K, V> rangeResult = (RangeResult<K, V>) result.results().get(0);
                if (rangeResult.kvs().isEmpty()) {
                    return FutureUtils.exception(new KvApiException(Code.UNEXPECTED, "Key " + lKey + " not found when putIfAbsent failed"));
                } else {
                    return FutureUtils.value(retain(rangeResult.kvs().get(0).value()));
                }
            }
        } finally {
            result.close();
        }
    });
}
Also used : RangeResult(org.apache.bookkeeper.api.kv.result.RangeResult) KvApiException(org.apache.bookkeeper.api.kv.exceptions.KvApiException)

Example 2 with RangeResult

use of org.apache.bookkeeper.api.kv.result.RangeResult in project bookkeeper by apache.

the class TableWriteView method putIfAbsent.

default CompletableFuture<V> putIfAbsent(K pKey, K lKey, V value) {
    Txn<K, V> txn = txn(pKey);
    txn.If(opFactory().compareValue(CompareResult.EQUAL, lKey, null)).Then(opFactory().newPut(lKey, value, opFactory().optionFactory().newPutOption().build())).Else(newGet(lKey));
    return txn.commit().thenCompose(result -> {
        try {
            if (result.isSuccess()) {
                return FutureUtils.value(null);
            } else {
                RangeResult<K, V> rangeResult = (RangeResult<K, V>) result.results().get(0);
                if (rangeResult.kvs().isEmpty()) {
                    return FutureUtils.exception(new KvApiException(Code.UNEXPECTED, "Key " + lKey + " not found when putIfAbsent failed"));
                } else {
                    return FutureUtils.value(retain(rangeResult.kvs().get(0).value()));
                }
            }
        } finally {
            result.close();
        }
    });
}
Also used : RangeResult(org.apache.bookkeeper.api.kv.result.RangeResult) KvApiException(org.apache.bookkeeper.api.kv.exceptions.KvApiException)

Aggregations

KvApiException (org.apache.bookkeeper.api.kv.exceptions.KvApiException)2 RangeResult (org.apache.bookkeeper.api.kv.result.RangeResult)2