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.PTableWriteView Maven / Gradle / Ivy
/*
* Licensed 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 PTableWriteView extends PTableBase {
CompletableFuture> put(K pKey, K lKey, V value, PutOption option);
CompletableFuture> delete(K pKey, K lKey, DeleteOption option);
CompletableFuture> increment(K pKey, K lKey, long amount, IncrementOption option);
Txn txn(K pKey);
default CompletableFuture increment(K pKey, K lKey, long amount) {
return increment(pKey, lKey, amount, Options.blindIncrement())
.thenApply(result -> {
try {
return (Void) null;
} finally {
result.close();
}
});
}
default CompletableFuture incrementAndGet(K pKey, K lKey, long amount) {
return increment(pKey, lKey, amount, Options.incrementAndGet())
.thenApply(result -> {
try {
return result.totalAmount();
} finally {
result.close();
}
});
}
default CompletableFuture getAndIncrement(K pKey, K lKey, long amount) {
return increment(pKey, lKey, amount, Options.incrementAndGet())
.thenApply(result -> {
try {
return result.totalAmount() - amount;
} finally {
result.close();
}
});
}
default CompletableFuture put(K pKey, K lKey, V value) {
if (value == null) {
return delete(pKey, lKey).thenApply(ignored -> null);
}
return put(pKey, lKey, 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 pKey, K lKey) {
return delete(pKey, lKey, 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>> deleteRange(K pKey, K lStartKey, K lEndKey) {
DeleteOption option = opFactory().optionFactory().newDeleteOption()
.prevKv(true)
.endKey(lEndKey)
.build();
return delete(pKey, lStartKey, option)
.thenApply(result -> {
try {
return result.getPrevKvsAndClear();
} finally {
result.close();
}
})
.whenComplete((v, cause) -> option.close());
}
default CompletableFuture delete(K pKey, K lKey, V value) {
Txn txn = txn(pKey);
txn
.If(
opFactory().compareValue(CompareResult.EQUAL, lKey, value))
.Then(
opFactory().newDelete(
lKey,
Options.deleteAndGet()));
return txn.commit()
.thenApply(result -> {
try {
return result.isSuccess() && !result.results().isEmpty();
} finally {
result.close();
}
});
}
default CompletableFuture> vDelete(K pKey, K lKey, long expectedVersion) {
Txn 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 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 " + lKey + " (version = " + expectedVersion + ")"));
}
} finally {
result.close();
}
});
}
default CompletableFuture> rDelete(K pKey, K lKey, long expectedRevision) {
Txn 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 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 " + lKey + " (mod_rev = " + expectedRevision + ")"));
}
} finally {
result.close();
}
});
}
}