Java Code Examples for org.apache.cassandra.utils.Pair#create()

The following examples show how to use org.apache.cassandra.utils.Pair#create() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: IndexSummaryManager.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a Pair of all compacting and non-compacting sstables.  Non-compacting sstables will be marked as
 * compacting.
 */
private Pair<List<SSTableReader>, Multimap<DataTracker, SSTableReader>> getCompactingAndNonCompactingSSTables()
{
    List<SSTableReader> allCompacting = new ArrayList<>();
    Multimap<DataTracker, SSTableReader> allNonCompacting = HashMultimap.create();
    for (Keyspace ks : Keyspace.all())
    {
        for (ColumnFamilyStore cfStore: ks.getColumnFamilyStores())
        {
            Set<SSTableReader> nonCompacting, allSSTables;
            do
            {
                allSSTables = cfStore.getDataTracker().getSSTables();
                nonCompacting = Sets.newHashSet(cfStore.getDataTracker().getUncompactingSSTables(allSSTables));
            }
            while (!(nonCompacting.isEmpty() || cfStore.getDataTracker().markCompacting(nonCompacting)));
            allNonCompacting.putAll(cfStore.getDataTracker(), nonCompacting);
            allCompacting.addAll(Sets.difference(allSSTables, nonCompacting));
        }
    }
    return Pair.create(allCompacting, allNonCompacting);
}
 
Example 2
Source File: ColumnFamilyRecordReader.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
protected Pair<ByteBuffer, SortedMap<ByteBuffer, Cell>> computeNext()
{
    maybeInit();
    if (rows == null)
        return endOfData();

    totalRead++;
    KeySlice ks = rows.get(i++);
    AbstractType<?> comp = isSuper ? CompositeType.getInstance(comparator, subComparator) : comparator;
    SortedMap<ByteBuffer, Cell> map = new TreeMap<ByteBuffer, Cell>(comp);
    for (ColumnOrSuperColumn cosc : ks.columns)
    {
        List<Cell> cells = unthriftify(cosc);
        for (Cell cell : cells)
            map.put(cell.name().toByteBuffer(), cell);
    }
    return Pair.create(ks.key, map);
}
 
Example 3
Source File: IndexSummaryTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private Pair<List<DecoratedKey>, IndexSummary> generateRandomIndex(int size, int interval)
{
    List<DecoratedKey> list = Lists.newArrayList();
    try (IndexSummaryBuilder builder = new IndexSummaryBuilder(list.size(), interval, BASE_SAMPLING_LEVEL))
    {
        for (int i = 0; i < size; i++)
        {
            UUID uuid = UUID.randomUUID();
            DecoratedKey key = DatabaseDescriptor.getPartitioner().decorateKey(ByteBufferUtil.bytes(uuid));
            list.add(key);
        }
        Collections.sort(list);
        for (int i = 0; i < size; i++)
            builder.maybeAddEntry(list.get(i), i);
        IndexSummary summary = builder.build(DatabaseDescriptor.getPartitioner());
        return Pair.create(list, summary);
    }
}
 
Example 4
Source File: CQLSSTableWriter.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private static <T extends CQLStatement> Pair<T, List<ColumnSpecification>> getStatement(String query, Class<T> klass, String type)
{
    try
    {
        ClientState state = ClientState.forInternalCalls();
        ParsedStatement.Prepared prepared = QueryProcessor.getStatement(query, state);
        CQLStatement stmt = prepared.statement;
        stmt.validate(state);

        if (!stmt.getClass().equals(klass))
            throw new IllegalArgumentException("Invalid query, must be a " + type + " statement");

        return Pair.create(klass.cast(stmt), prepared.boundNames);
    }
    catch (RequestValidationException e)
    {
        throw new IllegalArgumentException(e.getMessage(), e);
    }
}
 
Example 5
Source File: TriggerExecutor.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private Collection<Mutation> mergeMutations(Iterable<Mutation> mutations)
{
    Map<Pair<String, ByteBuffer>, Mutation> groupedMutations = new HashMap<>();

    for (Mutation mutation : mutations)
    {
        Pair<String, ByteBuffer> key = Pair.create(mutation.getKeyspaceName(), mutation.key());
        Mutation current = groupedMutations.get(key);
        if (current == null)
        {
            // copy in case the mutation's modifications map is backed by an immutable Collections#singletonMap().
            groupedMutations.put(key, mutation.copy());
        }
        else
        {
            current.addAll(mutation);
        }
    }

    return groupedMutations.values();
}
 
Example 6
Source File: Descriptor.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Filename of the form "<ksname>-<cfname>-[tmp-][<version>-]<gen>-<component>"
 *
 * @param directory The directory of the SSTable files
 * @param name The name of the SSTable file
 * @param skipComponent true if the name param should not be parsed for a component tag
 *
 * @return A Descriptor for the SSTable, and the Component remainder.
 */
public static Pair<Descriptor,String> fromFilename(File directory, String name, boolean skipComponent)
{
    // tokenize the filename
    StringTokenizer st = new StringTokenizer(name, String.valueOf(separator));
    String nexttok;

    // all filenames must start with keyspace and column family
    String ksname = st.nextToken();
    String cfname = st.nextToken();

    // optional temporary marker
    nexttok = st.nextToken();
    Type type = Type.FINAL;
    if (nexttok.equals(Type.TEMP.marker))
    {
        type = Type.TEMP;
        nexttok = st.nextToken();
    }
    else if (nexttok.equals(Type.TEMPLINK.marker))
    {
        type = Type.TEMPLINK;
        nexttok = st.nextToken();
    }

    if (!Version.validate(nexttok))
        throw new UnsupportedOperationException("SSTable " + name + " is too old to open.  Upgrade to 2.0 first, and run upgradesstables");
    Version version = new Version(nexttok);

    nexttok = st.nextToken();
    int generation = Integer.parseInt(nexttok);

    // component suffix
    String component = null;
    if (!skipComponent)
        component = st.nextToken();
    directory = directory != null ? directory : new File(".");
    return Pair.create(new Descriptor(version, directory, ksname, cfname, generation, type), component);
}
 
Example 7
Source File: CompressedStreamWriter.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private List<Pair<Long, Long>> getTransferSections(CompressionMetadata.Chunk[] chunks)
{
    List<Pair<Long, Long>> transferSections = new ArrayList<>();
    Pair<Long, Long> lastSection = null;
    for (CompressionMetadata.Chunk chunk : chunks)
    {
        if (lastSection != null)
        {
            if (chunk.offset == lastSection.right)
            {
                // extend previous section to end of this chunk
                lastSection = Pair.create(lastSection.left, chunk.offset + chunk.length + 4); // 4 bytes for CRC
            }
            else
            {
                transferSections.add(lastSection);
                lastSection = Pair.create(chunk.offset, chunk.offset + chunk.length + 4);
            }
        }
        else
        {
            lastSection = Pair.create(chunk.offset, chunk.offset + chunk.length + 4);
        }
    }
    if (lastSection != null)
        transferSections.add(lastSection);
    return transferSections;
}
 
Example 8
Source File: IntegralSA.java    From sasi with Apache License 2.0 5 votes vote down vote up
@Override
protected Pair<ByteBuffer, TokenTreeBuilder> computeNext()
{
    if (!termIterator.hasNext())
        return endOfData();

    Term<ByteBuffer> term = termIterator.next();
    return Pair.create(term.getTerm(), term.getTokens().finish());
}
 
Example 9
Source File: TokenTreeBuilder.java    From sasi with Apache License 2.0 5 votes vote down vote up
protected Pair<Long, InteriorNode> splitBlock()
{
    final int splitPosition = TOKENS_PER_BLOCK - 2;
    InteriorNode sibling = new InteriorNode();
    sibling.parent = parent;
    next = sibling;

    Long middleValue = tokens.get(splitPosition);

    for (int i = splitPosition; i < TOKENS_PER_BLOCK; i++)
    {
        if (i != TOKENS_PER_BLOCK && i != splitPosition)
        {
            long token = tokens.get(i);
            sibling.updateTokenRange(token);
            sibling.tokens.add(token);
        }

        Node child = children.get(i + 1);
        child.parent = sibling;
        sibling.children.add(child);
        sibling.position++;
    }

    for (int i = TOKENS_PER_BLOCK; i >= splitPosition; i--)
    {
        if (i != TOKENS_PER_BLOCK)
            tokens.remove(i);

        if (i != splitPosition)
            children.remove(i);
    }

    nodeMinToken = smallestToken();
    nodeMaxToken = tokens.get(tokens.size() - 1);
    numBlocks++;

    return Pair.create(middleValue, sibling);
}
 
Example 10
Source File: IndexSummaryManager.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@VisibleForTesting
static Pair<List<SSTableReader>, List<ResampleEntry>> distributeRemainingSpace(List<ResampleEntry> toDownsample, long remainingSpace)
{
    // sort by the amount of space regained by doing the downsample operation; we want to try to avoid operations
    // that will make little difference.
    Collections.sort(toDownsample, new Comparator<ResampleEntry>()
    {
        public int compare(ResampleEntry o1, ResampleEntry o2)
        {
            return Double.compare(o1.sstable.getIndexSummaryOffHeapSize() - o1.newSpaceUsed,
                                  o2.sstable.getIndexSummaryOffHeapSize() - o2.newSpaceUsed);
        }
    });

    int noDownsampleCutoff = 0;
    List<SSTableReader> willNotDownsample = new ArrayList<>();
    while (remainingSpace > 0 && noDownsampleCutoff < toDownsample.size())
    {
        ResampleEntry entry = toDownsample.get(noDownsampleCutoff);

        long extraSpaceRequired = entry.sstable.getIndexSummaryOffHeapSize() - entry.newSpaceUsed;
        // see if we have enough leftover space to keep the current sampling level
        if (extraSpaceRequired <= remainingSpace)
        {
            logger.trace("Using leftover space to keep {} at the current sampling level ({})",
                         entry.sstable, entry.sstable.getIndexSummarySamplingLevel());
            willNotDownsample.add(entry.sstable);
            remainingSpace -= extraSpaceRequired;
        }
        else
        {
            break;
        }

        noDownsampleCutoff++;
    }
    return Pair.create(willNotDownsample, toDownsample.subList(noDownsampleCutoff, toDownsample.size()));
}
 
Example 11
Source File: MapSerializer.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public static synchronized <K, V> MapSerializer<K, V> getInstance(TypeSerializer<K> keys, TypeSerializer<V> values)
{
    Pair<TypeSerializer<?>, TypeSerializer<?>> p = Pair.<TypeSerializer<?>, TypeSerializer<?>>create(keys, values);
    MapSerializer<K, V> t = instances.get(p);
    if (t == null)
    {
        t = new MapSerializer<K, V>(keys, values);
        instances.put(p, t);
    }
    return t;
}
 
Example 12
Source File: Component.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Filename of the form "<ksname>/<cfname>-[tmp-][<version>-]<gen>-<component>",
 * @return A Descriptor for the SSTable, and a Component for this particular file.
 * TODO move descriptor into Component field
 */
public static Pair<Descriptor,Component> fromFilename(File directory, String name)
{
    Pair<Descriptor,String> path = Descriptor.fromFilename(directory, name);

    // parse the component suffix
    Type type = Type.fromRepresentation(path.right);
    // build (or retrieve singleton for) the component object
    Component component;
    switch(type)
    {
        case DATA:              component = Component.DATA;                         break;
        case PRIMARY_INDEX:     component = Component.PRIMARY_INDEX;                break;
        case FILTER:            component = Component.FILTER;                       break;
        case COMPRESSION_INFO:  component = Component.COMPRESSION_INFO;             break;
        case STATS:             component = Component.STATS;                        break;
        case DIGEST:            component = Component.DIGEST;                       break;
        case CRC:               component = Component.CRC;                          break;
        case SUMMARY:           component = Component.SUMMARY;                      break;
        case TOC:               component = Component.TOC;                          break;
        case CUSTOM:            component = new Component(Type.CUSTOM, path.right); break;
        default:
             throw new IllegalStateException();
    }

    return Pair.create(path.left, component);
}
 
Example 13
Source File: MapType.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public static synchronized <K, V> MapType<K, V> getInstance(AbstractType<K> keys, AbstractType<V> values, boolean isMultiCell)
{
    Map<Pair<AbstractType<?>, AbstractType<?>>, MapType> internMap = isMultiCell ? instances : frozenInstances;
    Pair<AbstractType<?>, AbstractType<?>> p = Pair.<AbstractType<?>, AbstractType<?>>create(keys, values);
    MapType<K, V> t = internMap.get(p);
    if (t == null)
    {
        t = new MapType<>(keys, values, isMultiCell);
        internMap.put(p, t);
    }
    return t;
}
 
Example 14
Source File: Schema.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Load individual ColumnFamily Definition to the schema
 * (to make ColumnFamily lookup faster)
 *
 * @param cfm The ColumnFamily definition to load
 */
public void load(CFMetaData cfm)
{
    Pair<String, String> key = Pair.create(cfm.ksName, cfm.cfName);

    if (cfIdMap.containsKey(key))
        throw new RuntimeException(String.format("Attempting to load already loaded column family %s.%s", cfm.ksName, cfm.cfName));

    logger.debug("Adding {} to cfIdMap", cfm);
    cfIdMap.put(key, cfm.cfId);
}
 
Example 15
Source File: OptionCodec.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public Pair<T, Object> decodeOne(ByteBuf body, int version)
{
    T opt = fromId(body.readUnsignedShort());
    Object value = opt.readValue(body, version);
    return Pair.create(opt, value);
}
 
Example 16
Source File: Component.java    From hadoop-sstable with Apache License 2.0 4 votes vote down vote up
/**
 * Filename of the form "<ksname>/<cfname>-[tmp-][<version>-]<gen>-<component>",
 *
 * @return A Descriptor for the SSTable, and a Component for this particular file.
 *         TODO move descriptor into Component field
 */
public static Pair<Descriptor, Component> fromFilename(Path directory, String name, FileSystem fs) {
    Pair<Descriptor, String> path = Descriptor.fromFilename(directory, name);

    // parse the component suffix
    Type type = Type.fromRepresentation(path.right);
    // build (or retrieve singleton for) the component object
    Component component;
    switch (type) {
        case DATA:
            component = Component.DATA;
            break;
        case PRIMARY_INDEX:
            component = Component.PRIMARY_INDEX;
            break;
        case FILTER:
            component = Component.FILTER;
            break;
        case COMPACTED_MARKER:
            component = Component.COMPACTED_MARKER;
            break;
        case COMPRESSION_INFO:
            component = Component.COMPRESSION_INFO;
            break;
        case STATS:
            component = Component.STATS;
            break;
        case DIGEST:
            component = Component.DIGEST;
            break;
        case SUMMARY:
            component = Component.SUMMARY;
            break;
        case TOC:
            component = Component.TOC;
            break;
        case CUSTOM:
            component = new Component(Type.CUSTOM, path.right);
            break;
        default:
            throw new IllegalStateException();
    }

    return Pair.create(path.left, component);
}
 
Example 17
Source File: TypeParser.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public Pair<Pair<String, ByteBuffer>, List<Pair<ByteBuffer, AbstractType>>> getUserTypeParameters() throws SyntaxException, ConfigurationException
{

    if (isEOS() || str.charAt(idx) != '(')
        throw new IllegalStateException();

    ++idx; // skipping '('

    skipBlankAndComma();
    String keyspace = readNextIdentifier();
    skipBlankAndComma();
    ByteBuffer typeName = fromHex(readNextIdentifier());
    List<Pair<ByteBuffer, AbstractType>> defs = new ArrayList<>();

    while (skipBlankAndComma())
    {
        if (str.charAt(idx) == ')')
        {
            ++idx;
            return Pair.create(Pair.create(keyspace, typeName), defs);
        }

        ByteBuffer name = fromHex(readNextIdentifier());
        skipBlank();
        if (str.charAt(idx) != ':')
            throwSyntaxError("expecting ':' token");
        ++idx;
        skipBlank();
        try
        {
            AbstractType type = parse();
            defs.add(Pair.create(name, type));
        }
        catch (SyntaxException e)
        {
            SyntaxException ex = new SyntaxException(String.format("Exception while parsing '%s' around char %d", str, idx));
            ex.initCause(e);
            throw ex;
        }
    }
    throw new SyntaxException(String.format("Syntax error parsing '%s' at char %d: unexpected end of string", str, idx));
}
 
Example 18
Source File: RangeSliceResponseResolver.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
protected Pair<Row,InetAddress> computeNext()
{
    return iter.hasNext() ? Pair.create(iter.next(), source) : endOfData();
}
 
Example 19
Source File: SuffixSA.java    From sasi with Apache License 2.0 4 votes vote down vote up
private Pair<ByteBuffer, TokenTreeBuilder> finishSuffix()
{
    return Pair.create(lastProcessedSuffix, container.finish());
}
 
Example 20
Source File: SuffixSA.java    From sasi with Apache License 2.0 4 votes vote down vote up
private Pair<ByteBuffer, TokenTreeBuilder> suffixAt(int position)
{
    long index = suffixes[position];
    Term term = terms.get((int) (index >>> 32));
    return Pair.create(term.getSuffix(((int) index) - term.getPosition()), term.getTokens());
}