org.apache.cassandra.db.marshal.CompositeType Java Examples

The following examples show how to use org.apache.cassandra.db.marshal.CompositeType. 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: DeepRecordReader.java    From deep-spark with Apache License 2.0 6 votes vote down vote up
/**
 * check whether current row is at the end of range
 *
 * @return the boolean
 */
private boolean reachEndRange() {
    // current row key
    ByteBuffer rowKey;

    if (keyValidator instanceof CompositeType) {
        ByteBuffer[] keys = new ByteBuffer[partitionBoundColumns.size()];
        for (int i = 0; i < partitionBoundColumns.size(); i++) {
            keys[i] = partitionBoundColumns.get(i).value.duplicate();
        }

        rowKey = CompositeType.build(keys);
    } else {
        rowKey = partitionBoundColumns.get(0).value;
    }

    String endToken = String.valueOf(split.getEndToken());
    String currentToken = partitioner.getToken(rowKey).toString();

    return endToken.equals(currentToken);
}
 
Example #2
Source File: CassandraUtils.java    From deep-spark with Apache License 2.0 6 votes vote down vote up
/**
 * Returns the partition key related to a given {@link Cells}.
 *
 * @param cells        {@link Cells} from Cassandra to extract the partition key.
 * @param keyValidator Cassandra key type.
 * @param numberOfKeys Number of keys.
 * @return Partition key.
 */
public static ByteBuffer getPartitionKey(Cells cells, AbstractType<?> keyValidator, int numberOfKeys) {
    ByteBuffer partitionKey;
    if (keyValidator instanceof CompositeType) {
        ByteBuffer[] keys = new ByteBuffer[numberOfKeys];

        for (int i = 0; i < cells.size(); i++) {
            Cell c = cells.getCellByIdx(i);

            if (c.isKey()) {
                keys[i] = DataType.serializeValue(c.getValue(), CassandraDeepJobConfig.PROTOCOL_VERSION);
            }
        }

        partitionKey = CompositeType.build(keys);
    } else {
        Cell cell = cells.getCellByIdx(0);
        partitionKey = DataType.serializeValue(cell.getValue(), CassandraDeepJobConfig.PROTOCOL_VERSION);
    }
    return partitionKey;
}
 
Example #3
Source File: ColumnIdentifier.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public ColumnIdentifier prepare(CFMetaData cfm)
{
    AbstractType<?> comparator = cfm.comparator.asAbstractType();
    if (cfm.getIsDense() || comparator instanceof CompositeType || comparator instanceof UTF8Type)
        return new ColumnIdentifier(text, true);

    // We have a Thrift-created table with a non-text comparator.  We need to parse column names with the comparator
    // to get the correct ByteBuffer representation.  However, this doesn't apply to key aliases, so we need to
    // make a special check for those and treat them normally.  See CASSANDRA-8178.
    ByteBuffer bufferName = ByteBufferUtil.bytes(text);
    for (ColumnDefinition def : cfm.partitionKeyColumns())
    {
        if (def.name.bytes.equals(bufferName))
            return new ColumnIdentifier(text, true);
    }
    return new ColumnIdentifier(comparator.fromString(rawText), text);
}
 
Example #4
Source File: CqlRecordWriter.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
private ByteBuffer getPartitionKey(Map<String, ByteBuffer> keyColumns)
{
    ByteBuffer partitionKey;
    if (keyValidator instanceof CompositeType)
    {
        ByteBuffer[] keys = new ByteBuffer[partitionKeyColumns.length];
        for (int i = 0; i< keys.length; i++)
            keys[i] = keyColumns.get(partitionKeyColumns[i]);

        partitionKey = CompositeType.build(keys);
    }
    else
    {
        partitionKey = keyColumns.get(partitionKeyColumns[0]);
    }
    return partitionKey;
}
 
Example #5
Source File: BlobPlacementFactory.java    From emodb with Apache License 2.0 6 votes vote down vote up
@Override
public Placement newPlacement(String placement) throws ConnectionException {
    String[] parsed = PlacementUtil.parsePlacement(placement);
    String keyspaceName = parsed[0];
    String cfPrefix = parsed[1];

    CassandraKeyspace keyspace = _keyspaceMap.get(keyspaceName);
    if (keyspace == null) {
        throw new UnknownPlacementException(format(
                "Placement string refers to unknown or non-local Cassandra keyspace: %s", keyspaceName), placement);
    }

    KeyspaceDefinition keyspaceDef = keyspace.getAstyanaxKeyspace().describeKeyspace();
    ColumnFamily<ByteBuffer,Composite> columnFamily = getColumnFamily(keyspaceDef, cfPrefix, "blob", placement,
            new SpecificCompositeSerializer(CompositeType.getInstance(Arrays.<AbstractType<?>>asList(
                    AsciiType.instance, IntegerType.instance))));

    return new BlobPlacement(placement, keyspace, columnFamily);
}
 
Example #6
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 #7
Source File: QueryPagerTest.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
@Test
public void SliceQueryWithTombstoneTest() throws Exception
{
    // Testing for the bug of #6748
    String keyspace = "cql_keyspace";
    String table = "table2";
    ColumnFamilyStore cfs = Keyspace.open(keyspace).getColumnFamilyStore(table);
    CompositeType ct = (CompositeType)cfs.metadata.comparator.asAbstractType();

    // Insert rows but with a tombstone as last cell
    for (int i = 0; i < 5; i++)
        executeInternal(String.format("INSERT INTO %s.%s (k, c, v) VALUES ('k%d', 'c%d', null)", keyspace, table, 0, i));

    SliceQueryFilter filter = new SliceQueryFilter(ColumnSlice.ALL_COLUMNS_ARRAY, false, 100);
    QueryPager pager = QueryPagers.localPager(new SliceFromReadCommand(keyspace, bytes("k0"), table, 0, filter));

    for (int i = 0; i < 5; i++)
    {
        List<Row> page = pager.fetchPage(1);
        assertEquals(toString(page), 1, page.size());
        // The only live cell we should have each time is the row marker
        assertRow(page.get(0), "k0", ct.decompose("c" + i, ""));
    }
}
 
Example #8
Source File: ByteBufferUtils.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
/**
 * Returns a {@code String} representation of {@code byteBuffer} validated by {@code type}.
 *
 * @param byteBuffer the {@link java.nio.ByteBuffer} to be converted to {@code String}.
 * @param type       {@link AbstractType} of {@code byteBuffer}.
 * @return a {@code String} representation of {@code byteBuffer} validated by {@code type}.
 */
public static String toString(ByteBuffer byteBuffer, AbstractType<?> type) {
    if (type instanceof CompositeType) {
        CompositeType composite = (CompositeType) type;
        List<AbstractType<?>> types = composite.types;
        ByteBuffer[] components = composite.split(byteBuffer);
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < components.length; i++) {
            AbstractType<?> componentType = types.get(i);
            ByteBuffer component = components[i];
            sb.append(componentType.compose(component));
            if (i < types.size() - 1) {
                sb.append(':');
            }
        }
        return sb.toString();
    } else {
        return type.compose(byteBuffer).toString();
    }
}
 
Example #9
Source File: AbstractComposite.java    From stratio-cassandra with Apache License 2.0 6 votes vote down vote up
public ByteBuffer toByteBuffer()
{
    // This is the legacy format of composites.
    // See org.apache.cassandra.db.marshal.CompositeType for details.
    ByteBuffer result = ByteBuffer.allocate(dataSize() + 3 * size() + (isStatic() ? 2 : 0));
    if (isStatic())
        ByteBufferUtil.writeShortLength(result, CompositeType.STATIC_MARKER);

    for (int i = 0; i < size(); i++)
    {
        ByteBuffer bb = get(i);
        ByteBufferUtil.writeShortLength(result, bb.remaining());
        result.put(bb.duplicate());
        result.put((byte)0);
    }
    result.flip();
    return result;
}
 
Example #10
Source File: CassandraRecordUtils.java    From hdfs2cass with Apache License 2.0 5 votes vote down vote up
public static ByteBuffer toByteBuffer(final Object value) {
  if (value == null) {
    return ByteBufferUtil.EMPTY_BYTE_BUFFER;
  } else if (value instanceof CharSequence) {
    return ByteBufferUtil.bytes(value.toString());
  } else if (value instanceof Double) {
    return ByteBufferUtil.bytes((Double) value);
  } else if (value instanceof Float) {
    return ByteBufferUtil.bytes((Float) value);
  } else if (value instanceof Integer) {
    return ByteBufferUtil.bytes((Integer) value);
  } else if (value instanceof Long) {
    return ByteBufferUtil.bytes((Long) value);
  } else if (value instanceof ByteBuffer) {
    return ByteBufferUtil.clone((ByteBuffer) value);
  } else if (value instanceof GenericData.Array) {
    return serializeList((GenericData.Array)value);
  } else if (value instanceof SpecificRecord) {
    List<ByteBuffer> buffers = Lists.newArrayList();
    SpecificRecord record = (SpecificRecord) value;
    for (Schema.Field field : record.getSchema().getFields()) {
      buffers.add(toByteBuffer(record.get(field.pos())));
    }
    return CompositeType.build(buffers.toArray(new ByteBuffer[0]));
  } else if (value instanceof Map) {
    return serializeMap((Map<?, ?>) value);
  } else if (value instanceof Set) {
    return serializeSet((Set<?>) value);
  } else if (value instanceof List) {
    return serializeList((List<?>) value);
  } else if (value instanceof UUID) {
    return ByteBufferUtil.bytes((UUID) value);
  }


  throw new CrunchRuntimeException("Can not transform field (class: " + value.getClass() + ") to ByteBuffer");
}
 
Example #11
Source File: TriggerExecutorTest.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private static CFMetaData makeCfMetaData(String ks, String cf, TriggerDefinition trigger)
{

    CFMetaData metadata = CFMetaData.sparseCFMetaData(ks, cf, CompositeType.getInstance(UTF8Type.instance));

    metadata.keyValidator(UTF8Type.instance);
    metadata.addOrReplaceColumnDefinition(ColumnDefinition.partitionKeyDef(metadata,
                                                                           UTF8Type.instance.fromString("pkey"),
                                                                           UTF8Type.instance,
                                                                           null));
    metadata.addOrReplaceColumnDefinition(ColumnDefinition.regularDef(metadata,
                                                                      UTF8Type.instance.fromString("c1"),
                                                                      UTF8Type.instance,
                                                                      0));
    metadata.addOrReplaceColumnDefinition(ColumnDefinition.regularDef(metadata,
                                                                      UTF8Type.instance.fromString("c2"),
                                                                      UTF8Type.instance,
                                                                      0));
    try
    {
        if (trigger != null)
            metadata.addTriggerDefinition(trigger);
    }
    catch (InvalidRequestException e)
    {
        throw new AssertionError(e);
    }

    return metadata.rebuild();
}
 
Example #12
Source File: ByteBufferUtils.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@link java.nio.ByteBuffer}s contained in {@code byteBuffer} according to {@code type}.
 *
 * @param byteBuffer the {@link java.nio.ByteBuffer} to be split.
 * @param type       the {@link AbstractType} of {@code byteBuffer}.
 * @return the {@link java.nio.ByteBuffer}s contained in {@code byteBuffer} according to {@code type}.
 */
public static ByteBuffer[] split(ByteBuffer byteBuffer, AbstractType<?> type) {
    if (type instanceof CompositeType) {
        return ((CompositeType) type).split(byteBuffer);
    } else {
        return new ByteBuffer[]{byteBuffer};
    }
}
 
Example #13
Source File: ByteBufferUtils.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
/**
 * Returns the {@link AbstractType}s contained in {@code type}.
 *
 * @param type the {@link AbstractType} to be split.
 * @return the {@link AbstractType}s contained in {@code type}.
 */
public static List<AbstractType<?>> split(AbstractType<?> type) {
    if (type instanceof CompositeType) {
        return type.getComponents();
    } else {
        List<AbstractType<?>> result = new ArrayList<>(1);
        result.add(type);
        return result;
    }
}
 
Example #14
Source File: AbstractNativeCell.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
protected void updateWithName(MessageDigest digest)
{
    // for simple sparse we just return our one name buffer
    switch (nametype())
    {
        case SIMPLE_DENSE:
        case SIMPLE_SPARSE:
            writeComponentTo(digest, 0, false);
            break;

        case COMPOUND_DENSE:
        case COMPOUND_SPARSE_STATIC:
        case COMPOUND_SPARSE:
            // This is the legacy format of composites.
            // See org.apache.cassandra.db.marshal.CompositeType for details.
            if (isStatic())
                FBUtilities.updateWithShort(digest, CompositeType.STATIC_MARKER);

            for (int i = 0; i < size(); i++)
            {
                writeComponentTo(digest, i, true);
                digest.update((byte) 0);
            }
            break;

        default:
            throw new AssertionError();
    }
}
 
Example #15
Source File: AbstractNativeCell.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public ByteBuffer toByteBuffer()
{
    // for simple sparse we just return our one name buffer
    switch (nametype())
    {
        case SIMPLE_DENSE:
        case SIMPLE_SPARSE:
            return get(0);
        case COMPOUND_DENSE:
        case COMPOUND_SPARSE_STATIC:
        case COMPOUND_SPARSE:
            // This is the legacy format of composites.
            // See org.apache.cassandra.db.marshal.CompositeType for details.
            ByteBuffer result = ByteBuffer.allocate(cellDataSize());
            if (isStatic())
                ByteBufferUtil.writeShortLength(result, CompositeType.STATIC_MARKER);

            for (int i = 0; i < size(); i++)
            {
                ByteBuffer bb = get(i);
                ByteBufferUtil.writeShortLength(result, bb.remaining());
                result.put(bb);
                result.put((byte) 0);
            }
            result.flip();
            return result;
        default:
            throw new AssertionError();
    }
}
 
Example #16
Source File: AbstractCompoundCellNameType.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private void maybeReadNext() throws IOException
{
    if (nextFull != null)
        return;

    nextIdx = 0;
    nextSize = 0;

    int length = in.readShort() & 0xFFFF;
    // Note that empty is ok because it marks the end of row
    if (length == 0)
    {
        nextFull = EMPTY;
        return;
    }

    nextFull = new byte[length];
    in.readFully(nextFull);

    // Is is a static?
    nextIsStatic = false;
    if (peekShort() == CompositeType.STATIC_MARKER)
    {
        nextIsStatic = true;
        readShort(); // Skip the static marker
    }
}
 
Example #17
Source File: AbstractCompoundCellNameType.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
@Override
public Composite fromByteBuffer(ByteBuffer bytes)
{
    if (!bytes.hasRemaining())
        return Composites.EMPTY;

    ByteBuffer[] elements = new ByteBuffer[fullSize];
    int idx = bytes.position(), i = 0;
    byte eoc = 0;

    boolean isStatic = false;
    if (CompositeType.isStaticName(bytes))
    {
        isStatic = true;
        idx += 2;
    }

    while (idx < bytes.limit())
    {
        checkRemaining(bytes, idx, 2);
        int length = bytes.getShort(idx) & 0xFFFF;
        idx += 2;

        checkRemaining(bytes, idx, length + 1);
        elements[i++] = sliceBytes(bytes, idx, length);
        idx += length;
        eoc = bytes.get(idx++);
    }

    return makeWith(elements, i, Composite.EOC.from(eoc), isStatic);
}
 
Example #18
Source File: CompoundCType.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
public Composite fromByteBuffer(ByteBuffer bytes)
{
    if (!bytes.hasRemaining())
        return Composites.EMPTY;

    ByteBuffer[] elements = new ByteBuffer[size()];
    int idx = bytes.position(), i = 0;
    byte eoc = 0;

    boolean isStatic = false;
    if (CompositeType.isStaticName(bytes))
    {
        isStatic = true;
        idx += 2;
    }

    while (idx < bytes.limit())
    {
        checkRemaining(bytes, idx, 2);
        int length = bytes.getShort(idx) & 0xFFFF;
        idx += 2;

        checkRemaining(bytes, idx, length + 1);
        elements[i++] = sliceBytes(bytes, idx, length);
        idx += length;
        eoc = bytes.get(idx++);
    }
    return new CompoundComposite(elements, i, isStatic).withEOC(Composite.EOC.from(eoc));
}
 
Example #19
Source File: ColumnFamilyRecordReader.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private List<Cell> unthriftifySuperCounter(CounterSuperColumn super_column)
{
    List<Cell> cells = new ArrayList<Cell>(super_column.columns.size());
    for (CounterColumn column : super_column.columns)
    {
        Cell c = unthriftifyCounter(column);
        cells.add(c.withUpdatedName(CellNames.simpleDense(CompositeType.build(super_column.name, c.name().toByteBuffer()))));
    }
    return cells;
}
 
Example #20
Source File: ColumnFamilyRecordReader.java    From stratio-cassandra with Apache License 2.0 5 votes vote down vote up
private List<Cell> unthriftifySuper(SuperColumn super_column)
{
    List<Cell> cells = new ArrayList<Cell>(super_column.columns.size());
    for (org.apache.cassandra.thrift.Column column : super_column.columns)
    {
        Cell c = unthriftifySimple(column);
        cells.add(c.withUpdatedName(CellNames.simpleDense(CompositeType.build(super_column.name, c.name().toByteBuffer()))));
    }
    return cells;
}
 
Example #21
Source File: ClusteringKeyMapper.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
/**
 * Returns a new {@code ClusteringKeyMapper} according to the specified column family meta data.
 *
 * @param metadata The column family meta data.
 */
protected ClusteringKeyMapper(CFMetaData metadata) {
    this.metadata = metadata;
    this.cellNameType = metadata.comparator;
    this.compositeType = (CompositeType) cellNameType.asAbstractType();
}
 
Example #22
Source File: AbstractCompoundCellNameType.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public AbstractType<?> asAbstractType()
{
    return CompositeType.getInstance(fullType.types);
}
 
Example #23
Source File: CompoundCType.java    From stratio-cassandra with Apache License 2.0 4 votes vote down vote up
public AbstractType<?> asAbstractType()
{
    return CompositeType.getInstance(types);
}
 
Example #24
Source File: FullKeyMapper.java    From stratio-cassandra with Apache License 2.0 2 votes vote down vote up
/**
 * Returns a new {@link FullKeyMapper} using the specified column family metadata.
 *
 * @param partitionKeyMapper  A {@link PartitionKeyMapper}.
 * @param clusteringKeyMapper A {@link ClusteringKeyMapper}.
 */
private FullKeyMapper(PartitionKeyMapper partitionKeyMapper, ClusteringKeyMapper clusteringKeyMapper) {
    AbstractType<?> partitionKeyType = partitionKeyMapper.getType();
    AbstractType<?> clusteringKeyType = clusteringKeyMapper.getType().asAbstractType();
    type = CompositeType.getInstance(partitionKeyType, clusteringKeyType);
}