
com.couchbase.transactions.support.OptionsWrapperUtil Maven / Gradle / Ivy
Go to download
Show more of this group Show more artifacts with this name
Show all versions of couchbase-transactions Show documentation
Show all versions of couchbase-transactions Show documentation
Transaction Support for Couchbase on top of the Java SDK
The newest version!
/*
* Copyright 2021 Couchbase, Inc.
*
* 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 com.couchbase.transactions.support;
import com.couchbase.client.core.Core;
import com.couchbase.client.core.annotation.Stability;
import com.couchbase.client.core.logging.RedactableArgument;
import com.couchbase.client.core.msg.kv.DurabilityLevel;
import com.couchbase.client.java.json.JsonArray;
import com.couchbase.client.java.kv.InsertOptions;
import com.couchbase.client.java.kv.MutateInOptions;
import com.couchbase.client.java.kv.RemoveOptions;
import com.couchbase.transactions.TransactionDurabilityLevel;
import com.couchbase.transactions.config.MergedTransactionConfig;
import reactor.util.annotation.Nullable;
import java.time.Duration;
import java.util.HashMap;
import java.util.Map;
import java.util.Optional;
import static com.couchbase.client.java.kv.MutateInOptions.mutateInOptions;
import static com.couchbase.transactions.components.SerializationUtil.DEFAULT_JSON_SERIALIZER;
import static com.couchbase.transactions.components.SerializationUtil.DEFAULT_TRANSCODER;
@Stability.Internal
public class OptionsWrapperUtil {
private OptionsWrapperUtil() {
}
/**
* The timeout to use for non-mutating KV operations (or mutating operations that aren't important enough to
* justify durability.)
*/
public static Duration kvTimeoutNonMutating(MergedTransactionConfig config, Core core) {
return config.keyValueTimeout()
.orElse(core.context().environment().timeoutConfig().kvTimeout());
}
/**
* The timeout to use for important mutating KV operations.
*/
public static Duration kvTimeoutMutating(MergedTransactionConfig config, Core core) {
return config.keyValueTimeout()
.orElse(core.context().environment().timeoutConfig().kvDurableTimeout());
}
public static InsertOptions wrap(InsertOptions opts, SpanWrapper pspan, MergedTransactionConfig config, Core core) {
opts.timeout(kvTimeoutMutating(config, core))
.transcoder(DEFAULT_TRANSCODER)
.parentSpan(pspan.span());
if (config.transactionDurabilityLevel() != TransactionDurabilityLevel.NONE) {
opts.durability(convert(config.transactionDurabilityLevel()));
}
return opts;
}
public static InsertOptions wrap(InsertOptions opts, MergedTransactionConfig config, Optional override, Core core, SpanWrapper pspan) {
opts.timeout(kvTimeoutMutating(config, core))
.transcoder(DEFAULT_TRANSCODER)
.parentSpan(pspan.span());
if (override.isPresent()) {
opts.durability(override.get());
}
else if (config.transactionDurabilityLevel() != TransactionDurabilityLevel.NONE) {
opts.durability(convert(config.transactionDurabilityLevel()));
}
return opts;
}
public static RemoveOptions wrap(RemoveOptions opts, SpanWrapper pspan, MergedTransactionConfig config, Core core) {
opts.timeout(kvTimeoutMutating(config, core))
.parentSpan(pspan.span());
if (config.transactionDurabilityLevel() != TransactionDurabilityLevel.NONE) {
opts.durability(convert(config.transactionDurabilityLevel()));
}
return opts;
}
public static RemoveOptions wrap(RemoveOptions opts, MergedTransactionConfig config, Optional override, Core core, SpanWrapper pspan) {
opts.timeout(kvTimeoutMutating(config, core))
.parentSpan(pspan.span());
if (override.isPresent()) {
opts.durability(override.get());
}
else if (config.transactionDurabilityLevel() != TransactionDurabilityLevel.NONE) {
opts.durability(convert(config.transactionDurabilityLevel()));
}
return opts;
}
public static MutateInOptions wrap(MutateInOptions opts, SpanWrapper pspan, MergedTransactionConfig config, Core core) {
opts.timeout(kvTimeoutMutating(config, core))
.serializer(DEFAULT_JSON_SERIALIZER)
.parentSpan(pspan.span());
if (config.transactionDurabilityLevel() != TransactionDurabilityLevel.NONE) {
opts.durability(convert(config.transactionDurabilityLevel()));
}
return opts;
}
public static MutateInOptions wrap(MutateInOptions opts, MergedTransactionConfig config, Optional override, Core core, SpanWrapper pspan) {
opts.timeout(kvTimeoutMutating(config, core))
.serializer(DEFAULT_JSON_SERIALIZER)
.parentSpan(pspan.span());
if (override.isPresent()) {
opts.durability(override.get());
}
else if (config.transactionDurabilityLevel() != TransactionDurabilityLevel.NONE) {
opts.durability(convert(config.transactionDurabilityLevel()));
}
return opts;
}
public static DurabilityLevel convert(TransactionDurabilityLevel dl) {
switch (dl) {
case MAJORITY:
return DurabilityLevel.MAJORITY;
case PERSIST_TO_MAJORITY:
return DurabilityLevel.PERSIST_TO_MAJORITY;
case MAJORITY_AND_PERSIST_TO_ACTIVE:
return DurabilityLevel.MAJORITY_AND_PERSIST_TO_ACTIVE;
default:
return DurabilityLevel.NONE;
}
}
public static MutateInOptions wrap(SpanWrapper pspan, MergedTransactionConfig config, Core core) {
return wrap(mutateInOptions(), pspan, config, core);
}
public static Map createClientContext(String op) {
HashMap map = new HashMap<>();
map.put("txn.op", op);
return map;
}
public static Map createClientContext(@Nullable JsonArray params) {
Map map = createClientContext("query");
if (params != null) {
map.put("params", RedactableArgument.redactMeta(params.toString()));
}
return map;
}
}
© 2015 - 2025 Weber Informatics LLC | Privacy Policy