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

com.couchbase.client.core.transaction.components.TransactionLinks Maven / Gradle / Ivy

There is a newer version: 3.7.2
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.core.transaction.components;

import com.couchbase.client.core.annotation.Stability;
import com.couchbase.client.core.io.CollectionIdentifier;
import com.couchbase.client.core.transaction.forwards.ForwardCompatibility;

import java.util.Objects;
import java.util.Optional;

/**
 * Stores the transaction links stored on each document in a transaction
 */
@Stability.Internal
public class TransactionLinks {
    private final Optional atrId;
    private final Optional atrBucketName;
    private final Optional atrScopeName;
    private final Optional atrCollectionName;
    // ID of the transaction that has staged content
    private final Optional stagedTransactionId;
    private final Optional stagedAttemptId;
    private final Optional stagedContent;
    private final Optional crc32OfStaging;

    // For {BACKUP_FIELDS}
    private final Optional casPreTxn;
    private final Optional revidPreTxn;
    private final Optional exptimePreTxn;
    private final Optional op;

    private final Optional forwardCompatibility;
    private final Optional stagedOperationId;

    /**
     * This is not part of the transactional metadata.  It's here for legacy reasons and could be refactoring into
     * TransactionGetResultInternal.
     */
    private final boolean isDeleted;

    public TransactionLinks(
            Optional stagedContent,
            Optional atrId,
            Optional atrBucketName,
            Optional atrScopeName,
            Optional atrCollectionName,
            Optional stagedTransactionId,
            Optional stagedAttemptId,
            Optional casPreTxn,
            Optional revidPreTxn,
            Optional exptimePreTxn,
            Optional op,
            boolean isDeleted,
            Optional crc32OfStaging,
            Optional forwardCompatibility,
            Optional stagedOperationId) {
        this.stagedContent = Objects.requireNonNull(stagedContent);
        this.atrId = Objects.requireNonNull(atrId);
        this.stagedTransactionId = Objects.requireNonNull(stagedTransactionId);
        this.stagedAttemptId = Objects.requireNonNull(stagedAttemptId);
        this.atrBucketName = Objects.requireNonNull(atrBucketName);
        this.atrScopeName = Objects.requireNonNull(atrScopeName);
        this.atrCollectionName = Objects.requireNonNull(atrCollectionName);
        this.casPreTxn = Objects.requireNonNull(casPreTxn);
        this.revidPreTxn = Objects.requireNonNull(revidPreTxn);
        this.exptimePreTxn = Objects.requireNonNull(exptimePreTxn);
        this.op = Objects.requireNonNull(op);
        this.isDeleted = isDeleted;
        this.crc32OfStaging = Objects.requireNonNull(crc32OfStaging);
        this.forwardCompatibility = Objects.requireNonNull(forwardCompatibility);
        this.stagedOperationId = Objects.requireNonNull(stagedOperationId);
    }

    /**
     * Note this doesn't guarantee an active transaction, as it may have expired and need rolling back.
     */
    public boolean isDocumentInTransaction() {
        return atrId.isPresent();
    }

    public boolean isDocumentBeingRemoved() {
        return op.isPresent() && op.get().equals(OperationTypes.REMOVE);
    }

    public boolean hasStagedWrite() {
        return stagedAttemptId.isPresent();
    }

    public Optional atrId() {
        return atrId;
    }

    public Optional stagedTransactionId() {
        return stagedTransactionId;
    }


    public Optional stagedAttemptId() {
        return stagedAttemptId;
    }

    @Stability.Internal
    public Optional stagedContent() {
        return stagedContent;
    }

    public Optional atrBucketName() {
        return atrBucketName;
    }

    public Optional atrScopeName() {
        return atrScopeName;
    }

    public Optional atrCollectionName() {
        return atrCollectionName;
    }

    public Optional casPreTxn() {
        return casPreTxn;
    }

    public Optional revidPreTxn() {
        return revidPreTxn;
    }

    public Optional exptimePreTxn() {
        return exptimePreTxn;
    }

    public Optional op() {
        return op;
    }

    public boolean isDeleted() {
        return isDeleted;
    }

    /**
     * The CRC32 from staging the document.
     * 

* It is only available if it has been explicitly fetched. E.g. it will not be present after the mutation (which * cannot return this field). */ public Optional crc32OfStaging() { return crc32OfStaging; } public Optional forwardCompatibility() { return forwardCompatibility; } public Optional stagedOperationId() { return stagedOperationId; } @Override public String toString() { final StringBuilder sb = new StringBuilder("TransactionLinks{"); sb.append("atr="); sb.append(atrBucketName.orElse("none")).append('.'); sb.append(atrScopeName.orElse("none")).append('.'); sb.append(atrCollectionName.orElse("none")).append('.'); sb.append(atrId.orElse("none")).append('.'); sb.append(",txnId=").append(stagedTransactionId.orElse("none")); sb.append(",attemptId=").append(stagedAttemptId.orElse("none")); sb.append(",crc32Staging=").append(crc32OfStaging.orElse("none")); sb.append(",isDeleted=").append(isDeleted); stagedContent.ifPresent(s -> sb.append(",content=").append(s.length()).append("chars")); sb.append(",op=").append(op.orElse("none")); sb.append(",fc=").append(forwardCompatibility.map(Object::toString).orElse("none")); sb.append(",opId=").append(stagedOperationId.orElse("none")); sb.append(",restore={"); sb.append(casPreTxn.orElse("none")); sb.append(','); sb.append(revidPreTxn.orElse("none")); sb.append(','); sb.append(exptimePreTxn.orElse(-1L)); sb.append("}}"); return sb.toString(); } public CollectionIdentifier collection() { return new CollectionIdentifier(atrBucketName.get(), atrScopeName, atrCollectionName); } }





© 2015 - 2024 Weber Informatics LLC | Privacy Policy