Example usage for org.apache.cassandra.db.marshal AbstractType compare

List of usage examples for org.apache.cassandra.db.marshal AbstractType compare

Introduction

In this page you can find the example usage for org.apache.cassandra.db.marshal AbstractType compare.

Prototype

public final int compare(ByteBuffer left, ByteBuffer right) 

Source Link

Usage

From source file:com.datastax.brisk.BriskServer.java

License:Apache License

private Integer seekToSubColumn(CFMetaData metadata, FileDataInput file, ByteBuffer sblockId,
        List<IndexHelper.IndexInfo> indexList) throws IOException {
    file.readInt(); // column count

    /* get the various column ranges we have to read */
    AbstractType comparator = metadata.comparator;

    int index = IndexHelper.indexFor(sblockId, indexList, comparator, false);
    if (index == indexList.size())
        return null;

    IndexHelper.IndexInfo indexInfo = indexList.get(index);
    if (comparator.compare(sblockId, indexInfo.firstName) < 0)
        return null;

    FileMark mark = file.mark();/*w  ww. j a v a 2 s . com*/

    FileUtils.skipBytesFully(file, indexInfo.offset);

    while (file.bytesPastMark(mark) < indexInfo.offset + indexInfo.width) {
        Integer dataLength = isSubBlockFound(metadata, file, sblockId);

        if (dataLength == null)
            return null;

        if (dataLength < 0)
            continue;

        return dataLength;
    }

    return null;
}

From source file:com.mirantis.magnetodb.cassandra.db.index.MagnetoDBLocalSecondaryIndex.java

License:Apache License

@Override
public boolean indexes(CellName name) {
    AbstractType<?> comp = baseCfs.metadata.getColumnDefinitionComparator(columnDef);
    return name.size() > columnDef.position()
            && comp.compare(name.get(columnDef.position()), columnDef.name.bytes) == 0;
}

From source file:com.protectwise.cassandra.retrospect.deletion.RuleBasedDeletionConvictor.java

License:Apache License

/**
 * Returns true if value is found inside one of the ranges defined.
 *
 * @param ranges/*from  www .j  a  v a2  s.c o m*/
 * @param value
 * @return
 */
protected <T> boolean testRule(ByteBuffer[][] ranges, ByteBuffer value, AbstractType<?> comparator) {
    if (value == null) {
        logger.warn("Null value");
        return false;
    }
    for (ByteBuffer[] range : ranges) {
        if (logger.isTraceEnabled()) {
            logger.trace("Checking {} against {} to {}", comparator.getString(value),
                    range[0] == null ? "*" : comparator.getString(range[0]),
                    range[1] == null ? "*" : comparator.getString(range[1]));
        }
        // Null values indicate unbounded.  range[0] is the lower bound inclusive, range[1] is the upper bound inclusive.
        // We are guaranteed both values exist before we get to this point.
        if ((range[0] == null || comparator.compare(range[0], value) <= 0)
                && (range[1] == null || comparator.compare(range[1], value) >= 0)) {
            return true;
        }
    }
    return false;
}

From source file:com.stratio.cassandra.index.query.SortField.java

License:Apache License

/**
 * 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}.
 *//* w  ww  .  j  a  v a 2  s  .  c  om*/
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.getRawValue();
            ByteBuffer value2 = column2.getRawValue();
            return reverse ? type.compare(value2, value1) : type.compare(value1, value2);
        }
    };
}

From source file:com.stratio.cassandra.index.query.SortingField.java

License:Apache License

public Comparator<Columns> comparator() {
    return new Comparator<Columns>() {
        public int compare(Columns o1, Columns o2) {

            if (o1 == null) {
                return o2 == null ? 0 : 1;
            }/*from  w  ww . j  a  va2  s.com*/
            if (o2 == null) {
                return -1;
            }

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

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

            AbstractType<?> type = column1.getType();
            ByteBuffer value1 = column1.getRawValue();
            ByteBuffer value2 = column2.getRawValue();
            return reverse ? type.compare(value2, value1) : type.compare(value1, value2);
        }
    };
}

From source file:com.stratio.cassandra.index.RowService.java

License:Apache License

/**
 * Returns {@code true} if the specified {@link Columns} satisfies the the specified {@link IndexExpression}, {@code
 * false} otherwise./*www  .j  av a  2  s.  c o m*/
 *
 * @param columns    A {@link Columns}
 * @param expression A {@link IndexExpression}s to be satisfied by {@code columns}.
 * @return {@code true} if the specified {@link Columns} satisfies the the specified {@link IndexExpression}, {@code
 * false} otherwise.
 */
private boolean accepted(Columns columns, IndexExpression expression) {

    ByteBuffer expectedValue = expression.value;

    ColumnDefinition def = metadata.getColumnDefinition(expression.column);
    String name = def.name.toString();

    Column column = columns.getColumn(name);
    if (column == null) {
        return false;
    }

    ByteBuffer actualValue = column.getRawValue();
    if (actualValue == null) {
        return false;
    }

    AbstractType<?> validator = def.type;
    int comparison = validator.compare(actualValue, expectedValue);
    switch (expression.operator) {
    case EQ:
        return comparison == 0;
    case GTE:
        return comparison >= 0;
    case GT:
        return comparison > 0;
    case LTE:
        return comparison <= 0;
    case LT:
        return comparison < 0;
    default:
        throw new IllegalStateException();
    }
}

From source file:com.stratio.cassandra.lucene.schema.mapping.UUIDMapperTest.java

License:Apache License

private void testSort(List<UUID> uuids, final AbstractType<UUID> type) {

    Collections.shuffle(uuids);//from  ww w  .j a  v a 2 s.  c  o m

    List<UUID> expectedList = new ArrayList<>(uuids);
    Collections.sort(expectedList, new Comparator<UUID>() {
        @Override
        public int compare(UUID o1, UUID o2) {
            return type.compare(type.decompose(o1), type.decompose(o2));
        }
    });

    List<UUID> actualList = new ArrayList<>(uuids);
    Collections.sort(actualList, new Comparator<UUID>() {
        @Override
        public int compare(UUID o1, UUID o2) {
            String s1 = UUIDMapper.serialize(o1);
            String s2 = UUIDMapper.serialize(o2);
            return s1.compareTo(s2);
        }
    });

    assertEquals(expectedList.size(), actualList.size());
    for (int i = 0; i < expectedList.size(); i++) {
        UUID expectedUUID = expectedList.get(i);
        UUID actualUUID = actualList.get(i);
        assertEquals(expectedUUID, actualUUID);
    }
}

From source file:com.stratio.cassandra.lucene.search.sort.SortField.java

License:Apache License

/**
 * 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}.
 *///from  ww w.j  a va2  s  .  com
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;
            }

            Columns columns1 = o1.getColumnsByName(field);
            Columns columns2 = o2.getColumnsByName(field);
            if (columns1.size() > 1 || columns2.size() > 1) {
                throw new RuntimeException("Sorting in multivalued columns is not supported");
            }
            Column<?> column1 = columns1.getFirst();
            Column<?> column2 = columns2.getFirst();

            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);
        }
    };
}

From source file:com.stratio.cassandra.lucene.service.RowService.java

License:Apache License

private boolean accepted(Column column, AbstractType<?> validator, Operator operator, ByteBuffer value) {

    if (column == null)
        return false;

    ByteBuffer actualValue = column.getDecomposedValue();
    if (actualValue == null)
        return false;

    int comparison = validator.compare(actualValue, value);
    switch (operator) {
    case EQ://from w  w w  . ja  v  a 2 s  . c  o  m
        return comparison == 0;
    case GTE:
        return comparison >= 0;
    case GT:
        return comparison > 0;
    case LTE:
        return comparison <= 0;
    case LT:
        return comparison < 0;
    default:
        throw new IllegalStateException();
    }
}