Java Code Examples for org.apache.cassandra.db.marshal.AbstractType#compare()

The following examples show how to use org.apache.cassandra.db.marshal.AbstractType#compare() . 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: CombinedTermIterator.java    From sasi with Apache License 2.0 6 votes vote down vote up
public CombinedTermIterator(Descriptor d, OnDiskIndex... parts)
{
    descriptor = d;
    union = OnDiskIndexIterator.union(parts);

    AbstractType<?> comparator = parts[0].getComparator(); // assumes all SAs have same comparator
    ByteBuffer minimum = parts[0].minTerm();
    ByteBuffer maximum = parts[0].maxTerm();

    for (int i = 1; i < parts.length; i++)
    {
        OnDiskIndex part = parts[i];
        if (part == null)
            continue;

        minimum = comparator.compare(minimum, part.minTerm()) > 0 ? part.minTerm() : minimum;
        maximum = comparator.compare(maximum, part.maxTerm()) < 0 ? part.maxTerm() : maximum;
    }

    min = minimum;
    max = maximum;
}
 
Example 2
Source File: TriggerExecutor.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private void validateForSinglePartition(AbstractType<?> keyValidator,
                                        UUID cfId,
                                        ByteBuffer key,
                                        Collection<Mutation> tmutations)
throws InvalidRequestException
{
    for (Mutation mutation : tmutations)
    {
        if (keyValidator.compare(mutation.key(), key) != 0)
            throw new InvalidRequestException("Partition key of additional mutation does not match primary update key");

        for (ColumnFamily cf : mutation.getColumnFamilies())
        {
            if (! cf.id().equals(cfId))
                throw new InvalidRequestException("Column family of additional mutation does not match primary update cf");
        }
    }
    validate(tmutations);
}
 
Example 3
Source File: MapSerializer.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
/**
 * Given a serialized map, gets the value associated with a given key.
 * @param serializedMap a serialized map
 * @param serializedKey a serialized key
 * @param keyType the key type for the map
 * @return the value associated with the key if one exists, null otherwise
 */
public ByteBuffer getSerializedValue(ByteBuffer serializedMap, ByteBuffer serializedKey, AbstractType keyType)
{
    try
    {
        ByteBuffer input = serializedMap.duplicate();
        int n = readCollectionSize(input, Server.VERSION_3);
        for (int i = 0; i < n; i++)
        {
            ByteBuffer kbb = readValue(input, Server.VERSION_3);
            ByteBuffer vbb = readValue(input, Server.VERSION_3);
            int comparison = keyType.compare(kbb, serializedKey);
            if (comparison == 0)
                return vbb;
            else if (comparison > 0)
                // since the map is in sorted order, we know we've gone too far and the element doesn't exist
                return null;
        }
        return null;
    }
    catch (BufferUnderflowException e)
    {
        throw new MarshalException("Not enough bytes to read a map");
    }
}
 
Example 4
Source File: Sets.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public boolean equals(SetType st, Value v)
{
    if (elements.size() != v.elements.size())
        return false;

    Iterator<ByteBuffer> thisIter = elements.iterator();
    Iterator<ByteBuffer> thatIter = v.elements.iterator();
    AbstractType elementsType = st.getElementsType();
    while (thisIter.hasNext())
        if (elementsType.compare(thisIter.next(), thatIter.next()) != 0)
            return false;

    return true;
}
 
Example 5
Source File: ColumnSlice.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public boolean intersects(List<ByteBuffer> minCellNames, List<ByteBuffer> maxCellNames, CellNameType comparator, boolean reversed)
{
    Composite sStart = reversed ? finish : start;
    Composite sEnd = reversed ? start : finish;

    if (compare(sStart, maxCellNames, comparator, true) > 0 || compare(sEnd, minCellNames, comparator, false) < 0)
        return false;

    // We could safely return true here, but there's a minor optimization: if the first component is restricted
    // to a single value, we can check that the second component falls within the min/max for that component
    // (and repeat for all components).
    for (int i = 0; i < minCellNames.size() && i < maxCellNames.size(); i++)
    {
        AbstractType<?> t = comparator.subtype(i);
        ByteBuffer s = i < sStart.size() ? sStart.get(i) : ByteBufferUtil.EMPTY_BYTE_BUFFER;
        ByteBuffer f = i < sEnd.size() ? sEnd.get(i) : ByteBufferUtil.EMPTY_BYTE_BUFFER;

        // we already know the first component falls within its min/max range (otherwise we wouldn't get here)
        if (i > 0 && (i < sEnd.size() && t.compare(f, minCellNames.get(i)) < 0 ||
                      i < sStart.size() && t.compare(s, maxCellNames.get(i)) > 0))
            return false;

        // if this component isn't equal in the start and finish, we don't need to check any more
        if (i >= sStart.size() || i >= sEnd.size() || t.compare(s, f) != 0)
            break;
    }

    return true;
}
 
Example 6
Source File: ColumnNameHelper.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * return the min column
 *
 * note that comparator should not be of CompositeType!
 *
 * @param b1 lhs
 * @param b2 rhs
 * @param comparator the comparator to use
 * @return the smallest column according to comparator
 */
private static ByteBuffer min(ByteBuffer b1, ByteBuffer b2, AbstractType<?> comparator)
{
    if (b1 == null)
        return b2;
    if (b2 == null)
        return b1;

    if (comparator.compare(b1, b2) >= 0)
        return b2;
    return b1;
}
 
Example 7
Source File: ColumnNameHelper.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * return the max column
 *
 * note that comparator should not be of CompositeType!
 *
 * @param b1 lhs
 * @param b2 rhs
 * @param comparator the comparator to use
 * @return the biggest column according to comparator
 */
private static ByteBuffer max(ByteBuffer b1, ByteBuffer b2, AbstractType<?> comparator)
{
    if (b1 == null)
        return b2;
    if (b2 == null)
        return b1;

    if (comparator.compare(b1, b2) >= 0)
        return b1;
    return b2;
}
 
Example 8
Source File: SortField.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Returns a Java {@link Comparator} for {@link Columns} with the same logic as this {@link SortField}.
 *
 * @return A Java {@link Comparator} for {@link Columns} with the same logic as this {@link SortField}.
 */
public Comparator<Columns> comparator() {
    return new Comparator<Columns>() {
        public int compare(Columns o1, Columns o2) {

            if (o1 == null) {
                return o2 == null ? 0 : 1;
            }
            if (o2 == null) {
                return -1;
            }

            Column column1 = o1.getColumn(field);
            Column column2 = o2.getColumn(field);

            if (column1 == null) {
                return column2 == null ? 0 : 1;
            }
            if (column2 == null) {
                return -1;
            }

            AbstractType<?> type = column1.getType();
            ByteBuffer value1 = column1.getDecomposedValue();
            ByteBuffer value2 = column2.getDecomposedValue();
            return reverse ? type.compare(value2, value1) : type.compare(value1, value2);
        }
    };
}
 
Example 9
Source File: MappedBuffer.java    From sasi with Apache License 2.0 4 votes vote down vote up
public int comparePageTo(long offset, int length, AbstractType<?> comparator, ByteBuffer other)
{
    return comparator.compare(getPageRegion(offset, length), other);
}
 
Example 10
Source File: ByteTerm.java    From sasi with Apache License 2.0 4 votes vote down vote up
@Override
public int compareTo(AbstractType<?> comparator, Term other)
{
    return comparator.compare(value, (ByteBuffer) other.value);
}
 
Example 11
Source File: KeysIndex.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public boolean indexes(CellName name)
{
    // This consider the full clusteringKey directly
    AbstractType<?> comparator = baseCfs.metadata.getColumnDefinitionComparator(columnDef);
    return comparator.compare(columnDef.name.bytes, name.toByteBuffer()) == 0;
}