All Downloads are FREE. Search and download functionalities are using the official Maven repository.
Please wait. This can take some minutes ...
Many resources are needed to download a project. Please understand that we have to compensate our server costs. Thank you in advance.
Project price only 1 $
You can buy this project and download/modify it how often you want.
org.apache.bookkeeper.api.kv.TableWriteView Maven / Gradle / Ivy
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.apache.bookkeeper.api.kv;
import static io.netty.util.ReferenceCountUtil.retain;
import java.util.List;
import java.util.concurrent.CompletableFuture;
import org.apache.bookkeeper.api.kv.exceptions.KvApiException;
import org.apache.bookkeeper.api.kv.op.CompareResult;
import org.apache.bookkeeper.api.kv.options.DeleteOption;
import org.apache.bookkeeper.api.kv.options.IncrementOption;
import org.apache.bookkeeper.api.kv.options.Options;
import org.apache.bookkeeper.api.kv.options.PutOption;
import org.apache.bookkeeper.api.kv.result.Code;
import org.apache.bookkeeper.api.kv.result.DeleteResult;
import org.apache.bookkeeper.api.kv.result.IncrementResult;
import org.apache.bookkeeper.api.kv.result.KeyValue;
import org.apache.bookkeeper.api.kv.result.PutResult;
import org.apache.bookkeeper.api.kv.result.RangeResult;
import org.apache.bookkeeper.common.annotation.InterfaceAudience;
import org.apache.bookkeeper.common.annotation.InterfaceStability;
import org.apache.bookkeeper.common.concurrent.FutureUtils;
/**
* Write view of a given key space.
*/
@InterfaceAudience.Public
@InterfaceStability.Evolving
public interface TableWriteView extends PTableBase {
CompletableFuture> put(K key, V value, PutOption option);
CompletableFuture> delete(K key, DeleteOption option);
CompletableFuture> increment(K key, long amount, IncrementOption option);
Txn txn(K key);
default CompletableFuture increment(K key, long amount) {
return increment(key, amount, Options.blindIncrement())
.thenApply(result -> {
try {
return (Void) null;
} finally {
result.close();
}
});
}
default CompletableFuture incrementAndGet(K key, long amount) {
return increment(key, amount, Options.incrementAndGet())
.thenApply(result -> {
try {
return result.totalAmount();
} finally {
result.close();
}
});
}
default CompletableFuture getAndIncrement(K key, long amount) {
return increment(key, amount, Options.incrementAndGet())
.thenApply(result -> {
try {
return result.totalAmount() - amount;
} finally {
result.close();
}
});
}
default CompletableFuture put(K key, V value) {
if (value == null) {
return delete(key).thenApply(ignored -> null);
}
return put(key, value, Options.blindPut())
.thenApply(result -> {
try {
return (Void) null;
} finally {
result.close();
}
});
}
default CompletableFuture putIfAbsent(K pKey, K lKey, V value) {
Txn 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 rangeResult = (RangeResult) 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();
}
});
}
default CompletableFuture vPut(K pKey, K lKey, V value, long expectedVersion) {
Txn 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();
}
});
}
default CompletableFuture delete(K key) {
return delete(key, Options.deleteAndGet())
.thenApply(result -> {
try {
List> prevKvs = result.prevKvs();
if (prevKvs.isEmpty()) {
return null;
} else {
return retain(prevKvs.get(0).value());
}
} finally {
result.close();
}
});
}
default CompletableFuture delete(K key, V value) {
Txn txn = txn(key);
txn
.If(
opFactory().compareValue(CompareResult.EQUAL, key, value))
.Then(
opFactory().newDelete(
key,
Options.deleteAndGet()));
return txn.commit()
.thenApply(result -> {
try {
return result.isSuccess() && !result.results().isEmpty();
} finally {
result.close();
}
});
}
default CompletableFuture> vDelete(K key, long expectedVersion) {
Txn 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 deleteResult = (DeleteResult) result.results().get(0);
if (deleteResult.prevKvs().isEmpty()) {
return FutureUtils.value(null);
} else {
List> 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();
}
});
}
default CompletableFuture> rDelete(K key, long expectedRevision) {
Txn 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 deleteResult = (DeleteResult) result.results().get(0);
if (deleteResult.prevKvs().isEmpty()) {
return FutureUtils.value(null);
} else {
List> 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();
}
});
}
}