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

com.palantir.atlasdb.keyvalue.impl.RowResults Maven / Gradle / Ivy

There is a newer version: 0.1152.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.keyvalue.impl;

import com.google.common.base.Function;
import com.google.common.base.Predicate;
import com.google.common.collect.ImmutableSortedMap;
import com.google.common.collect.Iterators;
import com.google.common.collect.Maps;
import com.google.common.primitives.UnsignedBytes;
import com.palantir.atlasdb.keyvalue.api.Cell;
import com.palantir.atlasdb.keyvalue.api.RowResult;
import com.palantir.common.collect.IterableView;
import com.palantir.logsafe.Preconditions;
import java.util.Arrays;
import java.util.Iterator;
import java.util.Map;
import java.util.NavigableMap;
import java.util.SortedMap;

public final class RowResults {
    private RowResults() {
        /* */
    }

    public static  IterableView> viewOfMap(Map> map) {
        return viewOfEntries(map.entrySet());
    }

    public static  IterableView> viewOfEntries(
            Iterable>> mapEntries) {
        return IterableView.of(mapEntries).transform(RowResults::createRowResult);
    }

    public static  Iterator> viewOfEntries(
            Iterator>> mapEntries) {
        return Iterators.transform(mapEntries, RowResults::createRowResult);
    }

    private static  RowResult createRowResult(Map.Entry> entry) {
        return RowResult.create(entry.getKey(), entry.getValue());
    }

    public static  IterableView>> entriesViewFromRows(
            Iterable> rows) {
        return IterableView.of(rows).transform(row -> Maps.immutableEntry(row.getRowName(), row.getColumns()));
    }

    public static  NavigableMap> viewOfSortedMap(
            NavigableMap> map) {
        return Maps.transformEntries(map, RowResult::create);
    }

    public static  Predicate> createIsEmptyPredicate() {
        return input -> input.getColumns().isEmpty();
    }

    public static  Function, RowResult> createFilterColumns(final Predicate keepColumn) {
        return row -> RowResult.create(row.getRowName(), Maps.filterKeys(row.getColumns(), keepColumn));
    }

    public static  Function, RowResult> transformValues(final Function transform) {
        return row -> RowResult.create(row.getRowName(), Maps.transformValues(row.getColumns(), transform));
    }

    public static  RowResult merge(RowResult base, RowResult overwrite) {
        Preconditions.checkArgument(Arrays.equals(base.getRowName(), overwrite.getRowName()));
        ImmutableSortedMap.Builder colBuilder =
                ImmutableSortedMap.orderedBy(UnsignedBytes.lexicographicalComparator());
        colBuilder.putAll(overwrite.getColumns());
        colBuilder.putAll(
                Maps.difference(base.getColumns(), overwrite.getColumns()).entriesOnlyOnLeft());
        return RowResult.create(base.getRowName(), colBuilder.buildOrThrow());
    }

    public static long getApproximateSizeOfRowResult(RowResult rr) {
        long size = rr.getRowName().length;

        for (Map.Entry entry : rr.getCells()) {
            size += Cells.getApproxSizeOfCell(entry.getKey()) + entry.getValue().length;
        }
        return size;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy