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();
}
});
}
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();
}
});
}
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();
}
});
}
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();
}
});
}
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();
}
});
}
Aggregations