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

com.palantir.atlasdb.keyvalue.cassandra.ResultsExtractor Maven / Gradle / Ivy

The 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.keyvalue.cassandra;

import com.palantir.atlasdb.encoding.PtBytes;
import com.palantir.atlasdb.keyvalue.api.Cell;
import com.palantir.atlasdb.keyvalue.api.ColumnSelection;
import com.palantir.atlasdb.keyvalue.api.RangeRequests;
import com.palantir.atlasdb.keyvalue.api.RowResult;
import com.palantir.atlasdb.keyvalue.cassandra.CassandraKeyValueServices.ColumnAndTimestamp;
import com.palantir.atlasdb.keyvalue.impl.Cells;
import com.palantir.atlasdb.keyvalue.impl.RowResults;
import com.palantir.atlasdb.tracing.TraceStatistics;
import com.palantir.atlasdb.util.MetricsManager;
import com.palantir.util.paging.SimpleTokenBackedResultsPage;
import com.palantir.util.paging.TokenBackedBasicResultsPage;
import java.nio.ByteBuffer;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.NavigableMap;
import org.apache.cassandra.thrift.ColumnOrSuperColumn;

public abstract class ResultsExtractor {

    protected final MetricsManager metricsManager;

    public ResultsExtractor(MetricsManager metricsManager) {
        this.metricsManager = metricsManager;
    }

    @SuppressWarnings("VisibilityModifier")
    public final byte[] extractResults(
            Map> colsByKey, long startTs, ColumnSelection selection) {
        byte[] maxRow = null;
        for (Map.Entry> colEntry : colsByKey.entrySet()) {
            byte[] row = CassandraKeyValueServices.getBytesFromByteBuffer(colEntry.getKey());
            TraceStatistics.incBytesRead(row);

            maxRow = updatedMaxRow(maxRow, row);

            for (ColumnOrSuperColumn c : colEntry.getValue()) {
                ColumnAndTimestamp columnAndTimestamp = CassandraKeyValueServices.decomposeColumnName(c.getColumn());
                byte[] value = c.getColumn().getValue();

                // Read the value & the column name size; we're not currently trying to model all the overheads
                TraceStatistics.incBytesRead(columnAndTimestamp.columnName());
                TraceStatistics.incBytesRead(value);

                internalExtractResult(
                        startTs,
                        selection,
                        row,
                        columnAndTimestamp.columnName(),
                        value,
                        columnAndTimestamp.timestamp());
            }
        }
        return maxRow;
    }

    private byte[] updatedMaxRow(byte[] previousMaxRow, byte[] row) {
        if (previousMaxRow == null) {
            return row;
        } else {
            return PtBytes.BYTES_COMPARATOR.max(previousMaxRow, row);
        }
    }

    public TokenBackedBasicResultsPage, byte[]> getPageFromRangeResults(
            Map> colsByKey,
            long startTs,
            ColumnSelection selection,
            byte[] endExclusive) {
        byte[] lastRow = extractResults(colsByKey, startTs, selection);
        NavigableMap> resultsByRow = Cells.breakCellsUpByRow(asMap());
        return getRowResults(endExclusive, lastRow, resultsByRow);
    }

    public static  TokenBackedBasicResultsPage, byte[]> getRowResults(
            byte[] endExclusive, byte[] lastRow, NavigableMap> resultsByRow) {
        NavigableMap> ret = RowResults.viewOfSortedMap(resultsByRow);
        if (lastRow == null || RangeRequests.isLastRowName(lastRow)) {
            return new SimpleTokenBackedResultsPage<>(endExclusive, ret.values(), false);
        }
        byte[] nextStart = RangeRequests.nextLexicographicName(lastRow);
        if (Arrays.equals(nextStart, endExclusive)) {
            return new SimpleTokenBackedResultsPage<>(endExclusive, ret.values(), false);
        }
        return new SimpleTokenBackedResultsPage<>(nextStart, ret.values(), true);
    }

    public abstract void internalExtractResult(
            long startTs, ColumnSelection selection, byte[] row, byte[] col, byte[] val, long ts);

    public abstract Map asMap();
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy