Search in sources :

Example 1 with KvApiException

use of org.apache.bookkeeper.api.kv.exceptions.KvApiException 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 KvApiException

use of org.apache.bookkeeper.api.kv.exceptions.KvApiException in project bookkeeper by apache.

the class PTableWriteView method vPut.

default CompletableFuture<Long> vPut(K pKey, K lKey, V value, long expectedVersion) {
    Txn<K, V> txn = txn(pKey);
    txn.If(opFactory().compareVersion(CompareResult.EQUAL, lKey, expectedVersion)).Then(opFactory().newPut(lKey, value, opFactory().optionFactory().newPutOption().build()));
    return txn.commit().thenCompose(result -> {
        try {
            if (result.isSuccess()) {
                return FutureUtils.value(expectedVersion + 1);
            } else {
                return FutureUtils.exception(new KvApiException(Code.BAD_REVISION, "Failed to vPut(" + lKey + ", " + value + ")@version=" + expectedVersion));
            }
        } finally {
            result.close();
        }
    });
}
Also used : KvApiException(org.apache.bookkeeper.api.kv.exceptions.KvApiException)

Example 3 with KvApiException

use of org.apache.bookkeeper.api.kv.exceptions.KvApiException in project bookkeeper by apache.

the class TableWriteView method rDelete.

default CompletableFuture<KeyValue<K, V>> rDelete(K key, long expectedRevision) {
    Txn<K, V> txn = txn(key);
    txn.If(opFactory().compareModRevision(CompareResult.EQUAL, key, expectedRevision)).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 rDelete key " + key + " (mod_rev = " + expectedRevision + ")"));
            }
        } 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)

Example 4 with KvApiException

use of org.apache.bookkeeper.api.kv.exceptions.KvApiException 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();
        }
    });
}
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)

Example 5 with KvApiException

use of org.apache.bookkeeper.api.kv.exceptions.KvApiException 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();
        }
    });
}
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

KvApiException (org.apache.bookkeeper.api.kv.exceptions.KvApiException)9 KeyValue (org.apache.bookkeeper.api.kv.result.KeyValue)5 DeleteResult (org.apache.bookkeeper.api.kv.result.DeleteResult)4 RangeResult (org.apache.bookkeeper.api.kv.result.RangeResult)2 ByteBuf (io.netty.buffer.ByteBuf)1 NamespaceConfiguration (org.apache.bookkeeper.stream.proto.NamespaceConfiguration)1 NamespaceProperties (org.apache.bookkeeper.stream.proto.NamespaceProperties)1 StreamConfiguration (org.apache.bookkeeper.stream.proto.StreamConfiguration)1 StreamProperties (org.apache.bookkeeper.stream.proto.StreamProperties)1 Endpoint (org.apache.bookkeeper.stream.proto.common.Endpoint)1 Test (org.junit.Test)1