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

org.apache.cassandra.db.lifecycle.Helpers Maven / Gradle / Ivy

Go to download

The Apache Cassandra Project develops a highly scalable second-generation distributed database, bringing together Dynamo's fully distributed design and Bigtable's ColumnFamily-based data model.

There is a newer version: 5.0.2
Show newest version
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you 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 org.apache.cassandra.db.lifecycle;

import java.util.*;

import com.google.common.base.Predicate;
import com.google.common.collect.*;

import org.apache.cassandra.io.sstable.SSTable;
import org.apache.cassandra.io.sstable.format.SSTableReader;
import org.apache.cassandra.utils.Throwables;

import static com.google.common.base.Predicates.*;
import static com.google.common.collect.Iterables.any;
import static com.google.common.collect.Iterables.concat;
import static com.google.common.collect.Iterables.filter;
import static com.google.common.collect.Iterables.getFirst;
import static org.apache.cassandra.utils.Throwables.merge;

class Helpers
{
    /**
     * update the contents of a set with the provided sets, ensuring that the items to remove are
     * really present, and that the items to add are not (unless we're also removing them)
     * @return a new set with the contents of the provided one modified
     */
    static  Set replace(Set original, Set remove, Iterable add)
    {
        return ImmutableSet.copyOf(replace(identityMap(original), remove, add).keySet());
    }

    /**
     * update the contents of an "identity map" with the provided sets, ensuring that the items to remove are
     * really present, and that the items to add are not (unless we're also removing them)
     * @return a new identity map with the contents of the provided one modified
     */
    static  Map replace(Map original, Set remove, Iterable add)
    {
        // ensure the ones being removed are the exact same ones present
        for (T reader : remove)
            assert original.get(reader) == reader;

        // ensure we don't already contain any we're adding, that we aren't also removing
        assert !any(add, and(not(in(remove)), in(original.keySet()))) : String.format("original:%s remove:%s add:%s", original.keySet(), remove, add);

        Map result =
            identityMap(concat(add, filter(original.keySet(), not(in(remove)))));

        assert result.size() == original.size() - remove.size() + Iterables.size(add) :
        String.format("Expecting new size of %d, got %d while replacing %s by %s in %s",
                      original.size() - remove.size() + Iterables.size(add), result.size(), remove, add, original.keySet());
        return result;
    }

    /**
     * A convenience method for encapsulating this action over multiple SSTableReader with exception-safety
     * @return accumulate if not null (with any thrown exception attached), or any thrown exception otherwise
     */
    static void setupOnline(Iterable readers)
    {
        for (SSTableReader reader : readers)
            reader.setupOnline();
    }

    /**
     * A convenience method for encapsulating this action over multiple SSTableReader with exception-safety
     * @return accumulate if not null (with any thrown exception attached), or any thrown exception otherwise
     */
    static Throwable setReplaced(Iterable readers, Throwable accumulate)
    {
        for (SSTableReader reader : readers)
        {
            try
            {
                reader.setReplaced();
            }
            catch (Throwable t)
            {
                accumulate = merge(accumulate, t);
            }
        }
        return accumulate;
    }

    /**
     * assert that none of these readers have been replaced
     */
    static void checkNotReplaced(Iterable readers)
    {
        for (SSTableReader reader : readers)
            assert !reader.isReplaced();
    }

    static Throwable markObsolete(List obsoletions, Throwable accumulate)
    {
        if (obsoletions == null || obsoletions.isEmpty())
            return accumulate;

        for (LogTransaction.Obsoletion obsoletion : obsoletions)
        {
            try
            {
                obsoletion.reader.markObsolete(obsoletion.tidier);
            }
            catch (Throwable t)
            {
                accumulate = merge(accumulate, t);
            }
        }
        return accumulate;
    }

    static Throwable prepareForObsoletion(Iterable readers, LogTransaction txnLogs, List obsoletions, Throwable accumulate)
    {
        Map logRecords = txnLogs.makeRemoveRecords(readers);
        for (SSTableReader reader : readers)
        {
            try
            {
                obsoletions.add(new LogTransaction.Obsoletion(reader, txnLogs.obsoleted(reader, logRecords.get(reader))));
            }
            catch (Throwable t)
            {
                accumulate = Throwables.merge(accumulate, t);
            }
        }
        return accumulate;
    }

    static Throwable abortObsoletion(List obsoletions, Throwable accumulate)
    {
        if (obsoletions == null || obsoletions.isEmpty())
            return accumulate;

        for (LogTransaction.Obsoletion obsoletion : obsoletions)
        {
            try
            {
                obsoletion.tidier.abort();
            }
            catch (Throwable t)
            {
                accumulate = merge(accumulate, t);
            }
        }
        return accumulate;
    }

    /**
     * @return the identity function, as a Map, with domain of the provided values
     */
    static  Map identityMap(Iterable values)
    {
        ImmutableMap.Builder builder = ImmutableMap.builder();
        for (T t : values)
            builder.put(t, t);
        return builder.build();
    }

    /**
     * @return an Iterable of the union if the sets, with duplicates being represented by their first encountered instance
     * (as defined by the order of set provision)
     */
    static  Iterable concatUniq(Set... sets)
    {
        List> notIn = new ArrayList<>(sets.length);
        for (Set set : sets)
            notIn.add(not(in(set)));
        List> results = new ArrayList<>(sets.length);
        for (int i = 0 ; i < sets.length ; i++)
            results.add(filter(sets[i], and(notIn.subList(0, i))));
        return concat(results);
    }

    /**
     * @return a Predicate yielding true for an item present in NONE of the provided sets
     */
    static  Predicate notIn(Set... sets)
    {
        return not(orIn(sets));
    }

    /**
     * @return a Predicate yielding true for an item present in ANY of the provided sets
     */
    static  Predicate orIn(Collection... sets)
    {
        Predicate[] orIn = new Predicate[sets.length];
        for (int i = 0 ; i < orIn.length ; i++)
            orIn[i] = in(sets[i]);
        return or(orIn);
    }

    /**
     * filter out (i.e. remove) matching elements
     * @return filter, filtered to only those elements that *are not* present in *any* of the provided sets (are present in none)
     */
    static  Iterable filterOut(Iterable filter, Set... inNone)
    {
        return filter(filter, notIn(inNone));
    }

    /**
     * filter in (i.e. retain)
     *
     * @return filter, filtered to only those elements that *are* present in *any* of the provided sets
     */
    static  Iterable filterIn(Iterable filter, Set... inAny)
    {
        return filter(filter, orIn(inAny));
    }

    static Set emptySet()
    {
        return Collections.emptySet();
    }

    static  T select(T t, Collection col)
    {
        if (col instanceof Set && !col.contains(t))
            return null;
        return getFirst(filter(col, equalTo(t)), null);
    }

    static  T selectFirst(T t, Collection ... sets)
    {
        for (Collection set : sets)
        {
            T select = select(t, set);
            if (select != null)
                return select;
        }
        return null;
    }

    static  Predicate idIn(Set set)
    {
        return idIn(identityMap(set));
    }

    static  Predicate idIn(final Map identityMap)
    {
        return new Predicate()
        {
            public boolean apply(T t)
            {
                return identityMap.get(t) == t;
            }
        };
    }

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy