All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.couchbase.client.java.transactions.ReactiveTransactionAttemptContext Maven / Gradle / Ivy

There is a newer version: 3.7.5
Show newest version
/*
 * Copyright 2022 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.client.java.transactions;

import com.couchbase.client.core.annotation.Stability;
import com.couchbase.client.core.api.query.CoreQueryContext;
import com.couchbase.client.core.api.query.CoreQueryOptions;
import com.couchbase.client.core.cnc.CbTracing;
import com.couchbase.client.core.cnc.RequestSpan;
import com.couchbase.client.core.cnc.TracingIdentifiers;
import com.couchbase.client.core.deps.com.fasterxml.jackson.databind.node.ObjectNode;
import com.couchbase.client.core.error.DocumentNotFoundException;
import com.couchbase.client.core.transaction.CoreTransactionAttemptContext;
import com.couchbase.client.core.transaction.log.CoreTransactionLogger;
import com.couchbase.client.core.transaction.support.SpanWrapper;
import com.couchbase.client.java.ReactiveCollection;
import com.couchbase.client.java.ReactiveScope;
import com.couchbase.client.java.codec.JsonSerializer;
import com.couchbase.client.java.codec.Transcoder;
import com.couchbase.client.java.transactions.config.TransactionGetOptions;
import com.couchbase.client.java.transactions.config.TransactionInsertOptions;
import com.couchbase.client.java.transactions.config.TransactionReplaceOptions;
import reactor.core.publisher.Mono;

import java.util.Objects;

import static com.couchbase.client.core.cnc.TracingIdentifiers.TRANSACTION_OP_INSERT;
import static com.couchbase.client.core.cnc.TracingIdentifiers.TRANSACTION_OP_REMOVE;
import static com.couchbase.client.core.cnc.TracingIdentifiers.TRANSACTION_OP_REPLACE;
import static com.couchbase.client.java.transactions.internal.ConverterUtil.makeCollectionIdentifier;
import static com.couchbase.client.java.transactions.internal.EncodingUtil.encode;

/**
 * Provides methods to allow an application's transaction logic to read, mutate, insert and delete documents, as well
 * as commit or rollback the transaction.
 */
public class ReactiveTransactionAttemptContext {
    private final CoreTransactionAttemptContext internal;
    private final JsonSerializer serializer;

    ReactiveTransactionAttemptContext(CoreTransactionAttemptContext internal, JsonSerializer serializer) {
        this.internal = Objects.requireNonNull(internal);
        this.serializer = Objects.requireNonNull(serializer);
    }

    @Stability.Internal
    CoreTransactionAttemptContext ctx() {
        return internal;
    }

    /**
     * Gets a document with the specified id and from the specified Couchbase collection.
     * 

* If the document does not exist it will throw a {@link DocumentNotFoundException}. * * @param collection the Couchbase collection the document exists on * @param id the document's ID * @return a TransactionGetResult containing the document */ public Mono get(ReactiveCollection collection, String id) { return get(collection, id, TransactionGetOptions.DEFAULT); } /** * Gets a document with the specified id and from the specified Couchbase collection. *

* If the document does not exist it will throw a {@link DocumentNotFoundException}. * * @param collection the Couchbase collection the document exists on * @param id the document's ID * @param options options controlling the operation * @return a TransactionGetResult containing the document */ public Mono get(ReactiveCollection collection, String id, TransactionGetOptions options) { TransactionGetOptions.Built built = options.build(); return internal.get(makeCollectionIdentifier(collection.async()), id) .map(result -> new TransactionGetResult(result, serializer(), built.transcoder())); } /** * Inserts a new document into the specified Couchbase collection. * * @param collection the Couchbase collection in which to insert the doc * @param id the document's unique ID * @param content the content to insert * @return the doc, updated with its new CAS value and ID, and converted to a TransactionGetResult */ public Mono insert(ReactiveCollection collection, String id, Object content) { return insert(collection, id, content, TransactionInsertOptions.DEFAULT); } /** * Inserts a new document into the specified Couchbase collection. * * @param collection the Couchbase collection in which to insert the doc * @param id the document's unique ID * @param content the content to insert * @param options options controlling the operation * @return the doc, updated with its new CAS value and ID, and converted to a TransactionGetResult */ public Mono insert(ReactiveCollection collection, String id, Object content, TransactionInsertOptions options) { TransactionInsertOptions.Built built = options.build(); RequestSpan span = CbTracing.newSpan(internal.core().context(), TRANSACTION_OP_INSERT, internal.span()); span.lowCardinalityAttribute(TracingIdentifiers.ATTR_OPERATION, TRANSACTION_OP_INSERT); Transcoder.EncodedValue encoded = encode(content, span, serializer, built.transcoder(), internal.core().context()); return internal.insert(makeCollectionIdentifier(collection.async()), id, encoded.encoded(), encoded.flags(), new SpanWrapper(span)) .map(result -> new TransactionGetResult(result, serializer(), built.transcoder())) .doOnError(err -> span.status(RequestSpan.StatusCode.ERROR)) .doOnTerminate(() -> span.end()); } private JsonSerializer serializer() { return serializer; } /** * Mutates the specified doc with new content. * * @param doc the doc to be mutated * @param content the content to replace the doc with * @return the doc, updated with its new CAS value. For performance a copy is not created and the original doc * object is modified. */ public Mono replace(TransactionGetResult doc, Object content) { return replace(doc, content, TransactionReplaceOptions.DEFAULT); } /** * Mutates the specified doc with new content. * * @param doc the doc to be mutated * @param content the content to replace the doc with * @param options options controlling the operation * @return the doc, updated with its new CAS value. For performance a copy is not created and the original doc * object is modified. */ public Mono replace(TransactionGetResult doc, Object content, TransactionReplaceOptions options) { TransactionReplaceOptions.Built built = options.build(); RequestSpan span = CbTracing.newSpan(internal.core().context(), TRANSACTION_OP_REPLACE, internal.span()); span.lowCardinalityAttribute(TracingIdentifiers.ATTR_OPERATION, TRANSACTION_OP_REPLACE); Transcoder.EncodedValue encoded = encode(content, span, serializer, built.transcoder(), internal.core().context()); return internal.replace(doc.internal(), encoded.encoded(), encoded.flags(), new SpanWrapper(span)) .map(result -> new TransactionGetResult(result, serializer(), built.transcoder())) .doOnError(err -> span.status(RequestSpan.StatusCode.ERROR)) .doOnTerminate(() -> span.end()); } /** * Removes the specified doc. *

* @param doc - the doc to be removed */ public Mono remove(TransactionGetResult doc) { RequestSpan span = CbTracing.newSpan(internal.core().context(), TRANSACTION_OP_REMOVE, internal.span()); span.lowCardinalityAttribute(TracingIdentifiers.ATTR_OPERATION, TRANSACTION_OP_REMOVE); return internal.remove(doc.internal(), new SpanWrapper(span)) .doOnError(err -> span.status(RequestSpan.StatusCode.ERROR)) .doOnTerminate(() -> span.end()); } @SuppressWarnings("unused") @Stability.Internal CoreTransactionLogger logger() { return internal.logger(); } /** * Calls query() with default options. */ public Mono query(final String statement) { return query(statement, null); } /** * Runs a N1QL query and returns the result. *

* All rows are buffered in-memory. *

* Raises CouchbaseException or an error derived from it on failure. The application can choose to catch and ignore this error, and the * transaction attempt is allowed to continue. This differs from Key-Value operations, whose failure will * cause the attempt to fail. */ public Mono query(final String statement, final TransactionQueryOptions options) { return query(null, statement, options); } /** * Runs a N1QL query and returns the result, with default parameters. *

* All rows are buffered in-memory. *

* This overload performs a 'scope-level query': that is, one in which a collection may be referenced by name in the * query statement, without needing to specify the full bucket.scope.collection syntax. *

* Raises CouchbaseException or an error derived from it on failure. The application can choose to catch and ignore this error, and the * transaction attempt is allowed to continue. This differs from Key-Value operations, whose failure will * cause the attempt to fail. */ public Mono query(final ReactiveScope scope, final String statement) { return query(scope, statement, null); } /** * Runs a N1QL query and returns the result. *

* All rows are buffered in-memory. *

* This overload performs a 'scope-level query': that is, one in which a collection may be referenced by name in the * query statement, without needing to specify the full bucket.scope.collection syntax. *

* Raises CouchbaseException or an error derived from it on failure. The application can choose to catch and ignore this error, and the * transaction attempt is allowed to continue. This differs from Key-Value operations, whose failure will * cause the attempt to fail. */ public Mono query(final ReactiveScope scope, final String statement, final TransactionQueryOptions options) { CoreQueryOptions opts = options != null ? options.builder().build() : null; return internal.queryBlocking(statement, scope == null ? null : CoreQueryContext.of(scope.bucketName(), scope.name()), opts, false) .map(response -> new TransactionQueryResult(response, serializer())); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy