Search in sources :

Example 1 with VariableSpecifications

use of org.apache.cassandra.cql3.VariableSpecifications in project cassandra by apache.

the class Terms method ofListMarker.

/**
 * Creates a {@code Terms} for the specified list marker.
 *
 * @param marker the list  marker
 * @param type the element type
 * @return a {@code Terms} for the specified list marker
 */
public static Terms ofListMarker(final Lists.Marker marker, final AbstractType<?> type) {
    return new Terms() {

        @Override
        public void addFunctionsTo(List<Function> functions) {
        }

        @Override
        public void collectMarkerSpecification(VariableSpecifications boundNames) {
            marker.collectMarkerSpecification(boundNames);
        }

        @Override
        public List<ByteBuffer> bindAndGet(QueryOptions options) {
            Terminal terminal = marker.bind(options);
            if (terminal == null)
                return null;
            if (terminal == Constants.UNSET_VALUE)
                return UNSET_LIST;
            return ((MultiItemTerminal) terminal).getElements();
        }

        @Override
        public List<Terminal> bind(QueryOptions options) {
            Terminal terminal = marker.bind(options);
            if (terminal == null)
                return null;
            if (terminal == Constants.UNSET_VALUE)
                return UNSET_LIST;
            java.util.function.Function<ByteBuffer, Term.Terminal> deserializer = deserializer(options.getProtocolVersion());
            List<ByteBuffer> boundValues = ((MultiItemTerminal) terminal).getElements();
            List<Term.Terminal> values = new ArrayList<>(boundValues.size());
            for (int i = 0, m = boundValues.size(); i < m; i++) {
                ByteBuffer buffer = boundValues.get(i);
                Term.Terminal value = buffer == null ? null : deserializer.apply(buffer);
                values.add(value);
            }
            return values;
        }

        public java.util.function.Function<ByteBuffer, Term.Terminal> deserializer(ProtocolVersion version) {
            if (type.isCollection()) {
                switch(((CollectionType<?>) type).kind) {
                    case LIST:
                        return e -> Lists.Value.fromSerialized(e, (ListType<?>) type, version);
                    case SET:
                        return e -> Sets.Value.fromSerialized(e, (SetType<?>) type, version);
                    case MAP:
                        return e -> Maps.Value.fromSerialized(e, (MapType<?, ?>) type, version);
                }
                throw new AssertionError();
            }
            return e -> new Constants.Value(e);
        }
    };
}
Also used : MultiItemTerminal(org.apache.cassandra.cql3.Term.MultiItemTerminal) Terminal(org.apache.cassandra.cql3.Term.Terminal) java.util(java.util) Function(org.apache.cassandra.cql3.functions.Function) org.apache.cassandra.db.marshal(org.apache.cassandra.db.marshal) MultiItemTerminal(org.apache.cassandra.cql3.Term.MultiItemTerminal) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) ByteBuffer(java.nio.ByteBuffer) ProtocolVersion(org.apache.cassandra.transport.ProtocolVersion) ByteBuffer(java.nio.ByteBuffer) Terminal(org.apache.cassandra.cql3.Term.Terminal) MultiItemTerminal(org.apache.cassandra.cql3.Term.MultiItemTerminal) Terminal(org.apache.cassandra.cql3.Term.Terminal)

Example 2 with VariableSpecifications

use of org.apache.cassandra.cql3.VariableSpecifications in project cassandra by apache.

the class SingleColumnRelation method newINRestriction.

@Override
protected Restriction newINRestriction(TableMetadata table, VariableSpecifications boundNames) {
    ColumnMetadata columnDef = table.getExistingColumn(entity);
    List<? extends ColumnSpecification> receivers = toReceivers(columnDef);
    List<Term> terms = toTerms(receivers, inValues, table.keyspace, boundNames);
    if (terms == null) {
        Term term = toTerm(receivers, value, table.keyspace, boundNames);
        return new SingleColumnRestriction.InRestrictionWithMarker(columnDef, (Lists.Marker) term);
    }
    // An IN restrictions with only one element is the same than an EQ restriction
    if (terms.size() == 1)
        return new SingleColumnRestriction.EQRestriction(columnDef, terms.get(0));
    return new SingleColumnRestriction.InRestrictionWithValues(columnDef, terms);
}
Also used : ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) SingleColumnRestriction(org.apache.cassandra.cql3.restrictions.SingleColumnRestriction)

Example 3 with VariableSpecifications

use of org.apache.cassandra.cql3.VariableSpecifications in project cassandra by apache.

the class SSTableGenerator method delete.

Mutation delete(long lts, long pd, Query query) {
    Object[] partitionKey = schema.inflatePartitionKey(pd);
    WhereClause.Builder builder = new WhereClause.Builder();
    List<ColumnIdentifier> variableNames = new ArrayList<>();
    List<ByteBuffer> values = new ArrayList<>();
    for (int i = 0; i < partitionKey.length; i++) {
        String name = schema.partitionKeys.get(i).name;
        ColumnMetadata columnDef = metadata.getColumn(ByteBufferUtil.bytes(name));
        variableNames.add(columnDef.name);
        values.add(ByteBufferUtil.objectToBytes(partitionKey[i]));
        builder.add(new SingleColumnRelation(ColumnIdentifier.getInterned(name, true), toOperator(Relation.RelationKind.EQ), new AbstractMarker.Raw(values.size() - 1)));
    }
    for (Relation relation : query.relations) {
        String name = relation.column();
        ColumnMetadata columnDef = metadata.getColumn(ByteBufferUtil.bytes(relation.column()));
        variableNames.add(columnDef.name);
        values.add(ByteBufferUtil.objectToBytes(relation.value()));
        builder.add(new SingleColumnRelation(ColumnIdentifier.getInterned(name, false), toOperator(relation.kind), new AbstractMarker.Raw(values.size() - 1)));
    }
    StatementRestrictions restrictions = new StatementRestrictions(StatementType.DELETE, metadata, builder.build(), new VariableSpecifications(variableNames), false, false, false, false);
    QueryOptions options = QueryOptions.forInternalCalls(ConsistencyLevel.QUORUM, values);
    SortedSet<ClusteringBound<?>> startBounds = restrictions.getClusteringColumnsBounds(Bound.START, options);
    SortedSet<ClusteringBound<?>> endBounds = restrictions.getClusteringColumnsBounds(Bound.END, options);
    Slices slices = DeleteStatement.toSlices(metadata, startBounds, endBounds);
    assert slices.size() == 1;
    int deletionTime = FBUtilities.nowInSeconds();
    long rts = clock.rts(lts);
    return new RowUpdateBuilder(metadata, deletionTime, rts, metadata.params.defaultTimeToLive, serializePartitionKey(store, partitionKey)).noRowMarker().addRangeTombstone(new RangeTombstone(slices.get(0), new DeletionTime(rts, deletionTime))).build();
}
Also used : ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) Slices(org.apache.cassandra.db.Slices) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) WhereClause(org.apache.cassandra.cql3.WhereClause) ArrayList(java.util.ArrayList) QueryOptions(org.apache.cassandra.cql3.QueryOptions) SingleColumnRelation(org.apache.cassandra.cql3.SingleColumnRelation) Relation(harry.operations.Relation) RowUpdateBuilder(org.apache.cassandra.db.RowUpdateBuilder) StatementRestrictions(org.apache.cassandra.cql3.restrictions.StatementRestrictions) RangeTombstone(org.apache.cassandra.db.RangeTombstone) DeletionTime(org.apache.cassandra.db.DeletionTime) ByteBuffer(java.nio.ByteBuffer) VariableSpecifications(org.apache.cassandra.cql3.VariableSpecifications) SingleColumnRelation(org.apache.cassandra.cql3.SingleColumnRelation) ClusteringBound(org.apache.cassandra.db.ClusteringBound) ColumnIdentifier(org.apache.cassandra.cql3.ColumnIdentifier)

Example 4 with VariableSpecifications

use of org.apache.cassandra.cql3.VariableSpecifications in project cassandra by apache.

the class AliasedSelectable method newSelectorFactory.

@Override
public Factory newSelectorFactory(TableMetadata table, AbstractType<?> expectedType, List<ColumnMetadata> defs, VariableSpecifications boundNames) {
    final Factory delegate = selectable.newSelectorFactory(table, expectedType, defs, boundNames);
    final ColumnSpecification columnSpec = delegate.getColumnSpecification(table).withAlias(alias);
    return new ForwardingFactory() {

        @Override
        protected Factory delegate() {
            return delegate;
        }

        @Override
        public ColumnSpecification getColumnSpecification(TableMetadata table) {
            return columnSpec;
        }
    };
}
Also used : TableMetadata(org.apache.cassandra.schema.TableMetadata) ColumnSpecification(org.apache.cassandra.cql3.ColumnSpecification) Factory(org.apache.cassandra.cql3.selection.Selector.Factory)

Aggregations

ByteBuffer (java.nio.ByteBuffer)2 ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)2 Relation (harry.operations.Relation)1 java.util (java.util)1 ArrayList (java.util.ArrayList)1 ColumnIdentifier (org.apache.cassandra.cql3.ColumnIdentifier)1 ColumnSpecification (org.apache.cassandra.cql3.ColumnSpecification)1 QueryOptions (org.apache.cassandra.cql3.QueryOptions)1 SingleColumnRelation (org.apache.cassandra.cql3.SingleColumnRelation)1 MultiItemTerminal (org.apache.cassandra.cql3.Term.MultiItemTerminal)1 Terminal (org.apache.cassandra.cql3.Term.Terminal)1 VariableSpecifications (org.apache.cassandra.cql3.VariableSpecifications)1 WhereClause (org.apache.cassandra.cql3.WhereClause)1 Function (org.apache.cassandra.cql3.functions.Function)1 SingleColumnRestriction (org.apache.cassandra.cql3.restrictions.SingleColumnRestriction)1 StatementRestrictions (org.apache.cassandra.cql3.restrictions.StatementRestrictions)1 Factory (org.apache.cassandra.cql3.selection.Selector.Factory)1 ClusteringBound (org.apache.cassandra.db.ClusteringBound)1 DeletionTime (org.apache.cassandra.db.DeletionTime)1 RangeTombstone (org.apache.cassandra.db.RangeTombstone)1