Search in sources :

Example 26 with AbstractType

use of org.apache.cassandra.db.marshal.AbstractType in project brisk by riptano.

the class CassandraStorage method putNext.

public void putNext(Tuple t) throws ExecException, IOException {
    ByteBuffer key = objToBB(t.get(0));
    DefaultDataBag pairs = (DefaultDataBag) t.get(1);
    ArrayList<Mutation> mutationList = new ArrayList<Mutation>();
    CfDef cfDef = getCfDef();
    List<AbstractType> marshallers = getDefaultMarshallers(cfDef);
    Map<ByteBuffer, AbstractType> validators = getValidatorMap(cfDef);
    try {
        for (Tuple pair : pairs) {
            Mutation mutation = new Mutation();
            if (// supercolumn
            DataType.findType(pair.get(1)) == DataType.BAG) {
                org.apache.cassandra.thrift.SuperColumn sc = new org.apache.cassandra.thrift.SuperColumn();
                sc.name = objToBB(pair.get(0));
                ArrayList<org.apache.cassandra.thrift.Column> columns = new ArrayList<org.apache.cassandra.thrift.Column>();
                for (Tuple subcol : (DefaultDataBag) pair.get(1)) {
                    org.apache.cassandra.thrift.Column column = new org.apache.cassandra.thrift.Column();
                    column.name = objToBB(subcol.get(0));
                    column.value = objToBB(subcol.get(1));
                    column.setTimestamp(System.currentTimeMillis() * 1000);
                    columns.add(column);
                }
                if (// a deletion
                columns.isEmpty()) {
                    mutation.deletion = new Deletion();
                    mutation.deletion.super_column = objToBB(pair.get(0));
                    mutation.deletion.setTimestamp(System.currentTimeMillis() * 1000);
                } else {
                    sc.columns = columns;
                    mutation.column_or_supercolumn = new ColumnOrSuperColumn();
                    mutation.column_or_supercolumn.super_column = sc;
                }
            } else // assume column since it couldn't be anything else
            {
                if (pair.get(1) == null) {
                    mutation.deletion = new Deletion();
                    mutation.deletion.predicate = new org.apache.cassandra.thrift.SlicePredicate();
                    mutation.deletion.predicate.column_names = Arrays.asList(objToBB(pair.get(0)));
                    mutation.deletion.setTimestamp(System.currentTimeMillis() * 1000);
                } else {
                    org.apache.cassandra.thrift.Column column = new org.apache.cassandra.thrift.Column();
                    column.name = marshallers.get(0).decompose((pair.get(0)));
                    if (validators.get(column.name) == null)
                        // Have to special case BytesType to convert DataByteArray into ByteBuffer
                        if (marshallers.get(1) instanceof BytesType)
                            column.value = objToBB(pair.get(1));
                        else
                            column.value = marshallers.get(1).decompose(pair.get(1));
                    else
                        column.value = validators.get(column.name).decompose(pair.get(1));
                    column.setTimestamp(System.currentTimeMillis() * 1000);
                    mutation.column_or_supercolumn = new ColumnOrSuperColumn();
                    mutation.column_or_supercolumn.column = column;
                }
            }
            mutationList.add(mutation);
        }
    } catch (ClassCastException e) {
        throw new IOException(e + " Output must be (key, {(column,value)...}) for ColumnFamily or (key, {supercolumn:{(column,value)...}...}) for SuperColumnFamily");
    }
    try {
        writer.write(key, mutationList);
    } catch (InterruptedException e) {
        throw new IOException(e);
    }
}
Also used : Column(org.apache.cassandra.db.Column) IColumn(org.apache.cassandra.db.IColumn) ColumnOrSuperColumn(org.apache.cassandra.thrift.ColumnOrSuperColumn) SuperColumn(org.apache.cassandra.db.SuperColumn) org.apache.cassandra.thrift(org.apache.cassandra.thrift) Deletion(org.apache.cassandra.thrift.Deletion) BytesType(org.apache.cassandra.db.marshal.BytesType) ColumnOrSuperColumn(org.apache.cassandra.thrift.ColumnOrSuperColumn) IOException(java.io.IOException) ByteBuffer(java.nio.ByteBuffer) AbstractType(org.apache.cassandra.db.marshal.AbstractType) ColumnOrSuperColumn(org.apache.cassandra.thrift.ColumnOrSuperColumn) SuperColumn(org.apache.cassandra.db.SuperColumn) Mutation(org.apache.cassandra.thrift.Mutation)

Example 27 with AbstractType

use of org.apache.cassandra.db.marshal.AbstractType in project brisk by riptano.

the class CassandraStorage method columnToTuple.

private Tuple columnToTuple(ByteBuffer name, IColumn col, CfDef cfDef) throws IOException {
    Tuple pair = TupleFactory.getInstance().newTuple(2);
    List<AbstractType> marshallers = getDefaultMarshallers(cfDef);
    Map<ByteBuffer, AbstractType> validators = getValidatorMap(cfDef);
    if (col instanceof Column) {
        // standard
        pair.set(0, marshallers.get(0).compose(name));
        if (validators.get(name) == null)
            // Have to special case BytesType because compose returns a ByteBuffer
            if (marshallers.get(1) instanceof BytesType)
                pair.set(1, new DataByteArray(ByteBufferUtil.getArray(col.value())));
            else
                pair.set(1, marshallers.get(1).compose(col.value()));
        else
            pair.set(1, validators.get(name).compose(col.value()));
        return pair;
    }
    // super
    ArrayList<Tuple> subcols = new ArrayList<Tuple>();
    for (IColumn subcol : col.getSubColumns()) subcols.add(columnToTuple(subcol.name(), subcol, cfDef));
    pair.set(1, new DefaultDataBag(subcols));
    return pair;
}
Also used : Column(org.apache.cassandra.db.Column) IColumn(org.apache.cassandra.db.IColumn) ColumnOrSuperColumn(org.apache.cassandra.thrift.ColumnOrSuperColumn) SuperColumn(org.apache.cassandra.db.SuperColumn) IColumn(org.apache.cassandra.db.IColumn) AbstractType(org.apache.cassandra.db.marshal.AbstractType) ByteBuffer(java.nio.ByteBuffer) BytesType(org.apache.cassandra.db.marshal.BytesType)

Example 28 with AbstractType

use of org.apache.cassandra.db.marshal.AbstractType in project brisk by riptano.

the class CassandraStorage method getDefaultMarshallers.

private List<AbstractType> getDefaultMarshallers(CfDef cfDef) throws IOException {
    ArrayList<AbstractType> marshallers = new ArrayList<AbstractType>();
    AbstractType comparator = null;
    AbstractType default_validator = null;
    try {
        comparator = TypeParser.parse(cfDef.comparator_type);
        default_validator = TypeParser.parse(cfDef.default_validation_class);
    } catch (ConfigurationException e) {
        throw new IOException(e);
    }
    marshallers.add(comparator);
    marshallers.add(default_validator);
    return marshallers;
}
Also used : ConfigurationException(org.apache.cassandra.config.ConfigurationException) AbstractType(org.apache.cassandra.db.marshal.AbstractType) IOException(java.io.IOException)

Example 29 with AbstractType

use of org.apache.cassandra.db.marshal.AbstractType in project brisk by riptano.

the class BriskServer method seekToSubColumn.

private Integer seekToSubColumn(CFMetaData metadata, FileDataInput file, ByteBuffer sblockId, List<IndexHelper.IndexInfo> indexList) throws IOException {
    // column count
    file.readInt();
    /* 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();
    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;
}
Also used : IndexHelper(org.apache.cassandra.io.sstable.IndexHelper) AbstractType(org.apache.cassandra.db.marshal.AbstractType)

Example 30 with AbstractType

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

the class DropAggregateStatement method announceMigration.

public Event.SchemaChange announceMigration(QueryState queryState, boolean isLocalOnly) throws RequestValidationException {
    Collection<Function> olds = Schema.instance.getFunctions(functionName);
    if (!argsPresent && olds != null && olds.size() > 1)
        throw new InvalidRequestException(String.format("'DROP AGGREGATE %s' matches multiple function definitions; " + "specify the argument types by issuing a statement like " + "'DROP AGGREGATE %s (type, type, ...)'. Hint: use cqlsh " + "'DESCRIBE AGGREGATE %s' command to find all overloads", functionName, functionName, functionName));
    Function old = null;
    if (argsPresent) {
        if (Schema.instance.getKeyspaceMetadata(functionName.keyspace) != null) {
            List<AbstractType<?>> argTypes = new ArrayList<>(argRawTypes.size());
            for (CQL3Type.Raw rawType : argRawTypes) argTypes.add(prepareType("arguments", rawType));
            old = Schema.instance.findFunction(functionName, argTypes).orElse(null);
        }
        if (old == null || !(old instanceof AggregateFunction)) {
            if (ifExists)
                return null;
            // just build a nicer error message
            StringBuilder sb = new StringBuilder();
            for (CQL3Type.Raw rawType : argRawTypes) {
                if (sb.length() > 0)
                    sb.append(", ");
                sb.append(rawType);
            }
            throw new InvalidRequestException(String.format("Cannot drop non existing aggregate '%s(%s)'", functionName, sb));
        }
    } else {
        if (olds == null || olds.isEmpty() || !(olds.iterator().next() instanceof AggregateFunction)) {
            if (ifExists)
                return null;
            throw new InvalidRequestException(String.format("Cannot drop non existing aggregate '%s'", functionName));
        }
        old = olds.iterator().next();
    }
    if (old.isNative())
        throw new InvalidRequestException(String.format("Cannot drop aggregate '%s' because it is a " + "native (built-in) function", functionName));
    MigrationManager.announceAggregateDrop((UDAggregate) old, isLocalOnly);
    return new Event.SchemaChange(Event.SchemaChange.Change.DROPPED, Event.SchemaChange.Target.AGGREGATE, old.name().keyspace, old.name().name, AbstractType.asCQLTypeStringList(old.argTypes()));
}
Also used : CQL3Type(org.apache.cassandra.cql3.CQL3Type) ArrayList(java.util.ArrayList) AbstractType(org.apache.cassandra.db.marshal.AbstractType) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException)

Aggregations

AbstractType (org.apache.cassandra.db.marshal.AbstractType)51 ByteBuffer (java.nio.ByteBuffer)20 ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)9 TableMetadata (org.apache.cassandra.schema.TableMetadata)8 List (java.util.List)6 Map (java.util.Map)6 CollectionType (org.apache.cassandra.db.marshal.CollectionType)6 CompositeType (org.apache.cassandra.db.marshal.CompositeType)6 IOException (java.io.IOException)5 ArrayList (java.util.ArrayList)5 Set (java.util.Set)5 Collectors (java.util.stream.Collectors)5 CQL3Type (org.apache.cassandra.cql3.CQL3Type)5 SerializationHeader (org.apache.cassandra.db.SerializationHeader)5 TupleType (org.apache.cassandra.db.marshal.TupleType)5 UserType (org.apache.cassandra.db.marshal.UserType)5 ColumnIdentifier (org.apache.cassandra.cql3.ColumnIdentifier)4 FieldIdentifier (org.apache.cassandra.cql3.FieldIdentifier)4 Test (org.junit.Test)4 AuditLogContext (org.apache.cassandra.audit.AuditLogContext)3