Search in sources :

Example 1 with CompositeType

use of org.apache.cassandra.db.marshal.CompositeType in project cassandra by apache.

the class JsonTransformer method serializePartitionKey.

private void serializePartitionKey(DecoratedKey key) {
    AbstractType<?> keyValidator = metadata.partitionKeyType;
    objectIndenter.setCompact(true);
    try {
        arrayIndenter.setCompact(true);
        json.writeStartArray();
        if (keyValidator instanceof CompositeType) {
            // if a composite type, the partition has multiple keys.
            CompositeType compositeType = (CompositeType) keyValidator;
            ByteBuffer keyBytes = key.getKey().duplicate();
            // Skip static data if it exists.
            if (keyBytes.remaining() >= 2) {
                int header = ByteBufferUtil.getShortLength(keyBytes, keyBytes.position());
                if ((header & 0xFFFF) == 0xFFFF) {
                    ByteBufferUtil.readShortLength(keyBytes);
                }
            }
            int i = 0;
            while (keyBytes.remaining() > 0 && i < compositeType.getComponents().size()) {
                AbstractType<?> colType = compositeType.getComponents().get(i);
                ByteBuffer value = ByteBufferUtil.readBytesWithShortLength(keyBytes);
                String colValue = colType.getString(value);
                json.writeString(colValue);
                byte b = keyBytes.get();
                if (b != 0) {
                    break;
                }
                ++i;
            }
        } else {
            // if not a composite type, assume a single column partition key.
            assert metadata.partitionKeyColumns().size() == 1;
            json.writeString(keyValidator.getString(key.getKey()));
        }
        json.writeEndArray();
        objectIndenter.setCompact(false);
        arrayIndenter.setCompact(false);
    } catch (IOException e) {
        logger.error("Failure serializing partition key.", e);
    }
}
Also used : IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) CompositeType(org.apache.cassandra.db.marshal.CompositeType)

Example 2 with CompositeType

use of org.apache.cassandra.db.marshal.CompositeType in project cassandra by apache.

the class DataRange method appendKeyString.

// TODO: this is reused in SinglePartitionReadCommand but this should not really be here. Ideally
// we need a more "native" handling of composite partition keys.
public static void appendKeyString(StringBuilder sb, AbstractType<?> type, ByteBuffer key) {
    if (type instanceof CompositeType) {
        CompositeType ct = (CompositeType) type;
        ByteBuffer[] values = ct.split(key);
        for (int i = 0; i < ct.types.size(); i++) sb.append(i == 0 ? "" : ", ").append(ct.types.get(i).getString(values[i]));
    } else {
        sb.append(type.getString(key));
    }
}
Also used : ByteBuffer(java.nio.ByteBuffer) CompositeType(org.apache.cassandra.db.marshal.CompositeType)

Example 3 with CompositeType

use of org.apache.cassandra.db.marshal.CompositeType in project cassandra by apache.

the class CollectionEntryIndex method isStale.

public boolean isStale(Row data, ByteBuffer indexValue, int nowInSec) {
    ByteBuffer[] components = ((CompositeType) functions.getIndexedValueType(indexedColumn)).split(indexValue);
    ByteBuffer mapKey = components[0];
    ByteBuffer mapValue = components[1];
    ColumnMetadata columnDef = indexedColumn;
    Cell cell = data.getCell(columnDef, CellPath.create(mapKey));
    if (cell == null || !cell.isLive(nowInSec))
        return true;
    AbstractType<?> valueComparator = ((CollectionType) columnDef.type).valueComparator();
    return valueComparator.compare(mapValue, cell.value()) != 0;
}
Also used : ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) CollectionType(org.apache.cassandra.db.marshal.CollectionType) ByteBuffer(java.nio.ByteBuffer) Cell(org.apache.cassandra.db.rows.Cell) CompositeType(org.apache.cassandra.db.marshal.CompositeType)

Example 4 with CompositeType

use of org.apache.cassandra.db.marshal.CompositeType in project cassandra by apache.

the class PartitionKeyIndex method getIndexedValue.

public ByteBuffer getIndexedValue(ByteBuffer partitionKey, Clustering clustering, CellPath path, ByteBuffer cellValue) {
    CompositeType keyComparator = (CompositeType) baseCfs.metadata().partitionKeyType;
    ByteBuffer[] components = keyComparator.split(partitionKey);
    return components[indexedColumn.position()];
}
Also used : ByteBuffer(java.nio.ByteBuffer) CompositeType(org.apache.cassandra.db.marshal.CompositeType)

Example 5 with CompositeType

use of org.apache.cassandra.db.marshal.CompositeType in project stargate-core by tuplejump.

the class Fields method toString.

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();
    }
}
Also used : AbstractType(org.apache.cassandra.db.marshal.AbstractType) ByteBuffer(java.nio.ByteBuffer) CompositeType(org.apache.cassandra.db.marshal.CompositeType)

Aggregations

ByteBuffer (java.nio.ByteBuffer)6 CompositeType (org.apache.cassandra.db.marshal.CompositeType)6 AbstractType (org.apache.cassandra.db.marshal.AbstractType)2 IOException (java.io.IOException)1 CollectionType (org.apache.cassandra.db.marshal.CollectionType)1 Cell (org.apache.cassandra.db.rows.Cell)1 ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)1