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

com.palantir.atlasdb.transaction.impl.ReadTransaction Maven / Gradle / Ivy

There is a newer version: 0.1193.0
Show newest version
/*
 * (c) Copyright 2018 Palantir Technologies Inc. All rights reserved.
 *
 * 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.palantir.atlasdb.transaction.impl;

import com.google.common.util.concurrent.ListenableFuture;
import com.palantir.atlasdb.keyvalue.api.BatchColumnRangeSelection;
import com.palantir.atlasdb.keyvalue.api.Cell;
import com.palantir.atlasdb.keyvalue.api.ColumnRangeSelection;
import com.palantir.atlasdb.keyvalue.api.ColumnSelection;
import com.palantir.atlasdb.keyvalue.api.RangeRequest;
import com.palantir.atlasdb.keyvalue.api.RowResult;
import com.palantir.atlasdb.keyvalue.api.TableReference;
import com.palantir.atlasdb.logging.LoggingArgs;
import com.palantir.atlasdb.table.description.SweeperStrategy;
import com.palantir.atlasdb.transaction.api.GetRangesQuery;
import com.palantir.atlasdb.transaction.api.annotations.ReviewedRestrictedApiUsage;
import com.palantir.atlasdb.transaction.api.exceptions.MoreCellsPresentThanExpectedException;
import com.palantir.common.base.BatchingVisitable;
import com.palantir.logsafe.Preconditions;
import com.palantir.logsafe.exceptions.SafeIllegalArgumentException;
import com.palantir.util.result.Result;
import java.util.Iterator;
import java.util.Map;
import java.util.NavigableMap;
import java.util.Optional;
import java.util.Set;
import java.util.function.BiFunction;
import java.util.stream.Stream;

public class ReadTransaction extends ForwardingCallbackAwareTransaction {
    private final CallbackAwareTransaction delegate;
    private final SweepStrategyManager sweepStrategies;

    public ReadTransaction(CallbackAwareTransaction delegate, SweepStrategyManager sweepStrategies) {
        this.delegate = delegate;
        this.sweepStrategies = sweepStrategies;
    }

    @Override
    public CallbackAwareTransaction delegate() {
        return delegate;
    }

    @Override
    public NavigableMap> getRows(
            TableReference tableRef, Iterable rows, ColumnSelection columnSelection) {
        checkTableName(tableRef);
        return delegate().getRows(tableRef, rows, columnSelection);
    }

    @Override
    public Map get(TableReference tableRef, Set cells) {
        checkTableName(tableRef);
        return delegate().get(tableRef, cells);
    }

    @ReviewedRestrictedApiUsage
    @Override
    public Result, MoreCellsPresentThanExpectedException> getWithExpectedNumberOfCells(
            TableReference tableRef, Set cells, long expectedNumberOfPresentCells) {
        checkTableName(tableRef);
        return delegate.getWithExpectedNumberOfCells(tableRef, cells, expectedNumberOfPresentCells);
    }

    @Override
    public BatchingVisitable> getRange(TableReference tableRef, RangeRequest rangeRequest) {
        checkTableName(tableRef);
        return delegate().getRange(tableRef, rangeRequest);
    }

    @Override
    public Iterable>> getRanges(
            TableReference tableRef, Iterable rangeRequests) {
        checkTableName(tableRef);
        return delegate().getRanges(tableRef, rangeRequests);
    }

    @Override
    public  Stream getRanges(
            final TableReference tableRef,
            Iterable rangeRequests,
            int concurrencyLevel,
            BiFunction>, T> visitableProcessor) {
        checkTableName(tableRef);
        return delegate().getRanges(tableRef, rangeRequests, concurrencyLevel, visitableProcessor);
    }

    @Override
    public  Stream getRanges(
            final TableReference tableRef,
            Iterable rangeRequests,
            BiFunction>, T> visitableProcessor) {
        checkTableName(tableRef);
        return delegate().getRanges(tableRef, rangeRequests, visitableProcessor);
    }

    @Override
    public  Stream getRanges(GetRangesQuery getRangesQuery) {
        return delegate().getRanges(getRangesQuery);
    }

    @Override
    public Stream>> getRangesLazy(
            final TableReference tableRef, Iterable rangeRequests) {
        checkTableName(tableRef);
        return delegate().getRangesLazy(tableRef, rangeRequests);
    }

    @Override
    public Map>> getRowsColumnRange(
            TableReference tableRef, Iterable rows, BatchColumnRangeSelection columnRangeSelection) {
        checkTableName(tableRef);
        return delegate().getRowsColumnRange(tableRef, rows, columnRangeSelection);
    }

    @Override
    public Map>> getRowsColumnRangeIterator(
            TableReference tableRef, Iterable rows, BatchColumnRangeSelection columnRangeSelection) {
        checkTableName(tableRef);
        return delegate().getRowsColumnRangeIterator(tableRef, rows, columnRangeSelection);
    }

    @Override
    public Iterator> getSortedColumns(
            TableReference tableRef, Iterable rows, BatchColumnRangeSelection batchColumnRangeSelection) {
        checkTableName(tableRef);
        return delegate().getSortedColumns(tableRef, rows, batchColumnRangeSelection);
    }

    @Override
    public Iterator> getRowsColumnRange(
            TableReference tableRef, Iterable rows, ColumnRangeSelection columnRangeSelection, int batchHint) {
        checkTableName(tableRef);
        return delegate().getRowsColumnRange(tableRef, rows, columnRangeSelection, batchHint);
    }

    private void checkTableName(TableReference tableRef) {
        Optional sweepStrategy = sweepStrategies.get(tableRef).getSweeperStrategy();
        Preconditions.checkState(
                sweepStrategy.isEmpty() || sweepStrategy.get().equals(SweeperStrategy.CONSERVATIVE),
                "This table cannot be read from a read-only transaction, because its sweep strategy is neither NOTHING"
                        + " nor CONSERVATIVE",
                LoggingArgs.tableRef(tableRef));
    }

    @Override
    public void put(TableReference tableRef, Map values) {
        throw new SafeIllegalArgumentException("This is a read only transaction.");
    }

    @Override
    public void delete(TableReference tableRef, Set keys) {
        throw new SafeIllegalArgumentException("This is a read only transaction.");
    }

    @Override
    public ListenableFuture> getAsync(TableReference tableRef, Set cells) {
        checkTableName(tableRef);
        return delegate().getAsync(tableRef, cells);
    }
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy