You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@cassandra.apache.org by sl...@apache.org on 2013/02/08 18:58:40 UTC
[3/7] CQL3 refactor to allow conversion function
http://git-wip-us.apache.org/repos/asf/cassandra/blob/31e669ab/src/java/org/apache/cassandra/cql3/operations/ColumnOperation.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/operations/ColumnOperation.java b/src/java/org/apache/cassandra/cql3/operations/ColumnOperation.java
deleted file mode 100644
index d53a6b0..0000000
--- a/src/java/org/apache/cassandra/cql3/operations/ColumnOperation.java
+++ /dev/null
@@ -1,161 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.cassandra.cql3.operations;
-
-import java.nio.ByteBuffer;
-import java.util.Collections;
-import java.util.List;
-
-import org.apache.cassandra.cql3.*;
-import org.apache.cassandra.db.ColumnFamily;
-import org.apache.cassandra.db.IColumn;
-import org.apache.cassandra.db.filter.QueryPath;
-import org.apache.cassandra.db.marshal.AbstractType;
-import org.apache.cassandra.db.marshal.CollectionType;
-import org.apache.cassandra.db.marshal.ListType;
-import org.apache.cassandra.db.marshal.MapType;
-import org.apache.cassandra.db.marshal.SetType;
-import org.apache.cassandra.db.marshal.LongType;
-import org.apache.cassandra.exceptions.InvalidRequestException;
-import org.apache.cassandra.utils.ByteBufferUtil;
-import org.apache.cassandra.utils.Pair;
-
-public class ColumnOperation implements Operation
-{
- enum Kind { SET, COUNTER_INC, COUNTER_DEC }
-
- private static final Operation setToEmptyOperation = new ColumnOperation(null, Kind.SET)
- {
- @Override
- protected void doSet(ColumnFamily cf, ColumnNameBuilder builder, AbstractType<?> validator, UpdateParameters params) throws InvalidRequestException
- {
- ByteBuffer colName = builder.build();
- QueryProcessor.validateColumnName(colName);
- cf.addColumn(params.makeColumn(colName, ByteBufferUtil.EMPTY_BYTE_BUFFER));
- }
- };
-
- private final Term value;
- private final Kind kind;
-
- private ColumnOperation(Term value, Kind kind)
- {
- this.value = value;
- this.kind = kind;
- }
-
- public void execute(ColumnFamily cf,
- ColumnNameBuilder builder,
- AbstractType<?> validator,
- UpdateParameters params,
- List<Pair<ByteBuffer, IColumn>> list) throws InvalidRequestException
- {
- switch (kind)
- {
- case SET:
- doSet(cf, builder, validator, params);
- break;
- case COUNTER_INC:
- case COUNTER_DEC:
- doCounter(cf, builder, params);
- break;
- default:
- throw new AssertionError("Unsupported operation: " + kind);
- }
- }
-
- protected void doSet(ColumnFamily cf, ColumnNameBuilder builder, AbstractType<?> validator, UpdateParameters params) throws InvalidRequestException
- {
- ByteBuffer colName = builder.build();
- QueryProcessor.validateColumnName(colName);
-
- ByteBuffer valueBytes = value.getByteBuffer(validator, params.variables);
- cf.addColumn(params.makeColumn(colName, valueBytes));
- }
-
- private void doCounter(ColumnFamily cf, ColumnNameBuilder builder, UpdateParameters params) throws InvalidRequestException
- {
- long val;
-
- try
- {
- val = ByteBufferUtil.toLong(value.getByteBuffer(LongType.instance, params.variables));
- }
- catch (NumberFormatException e)
- {
- throw new InvalidRequestException(String.format("'%s' is an invalid value, should be a long.", value.getText()));
- }
-
- if (kind == Kind.COUNTER_DEC)
- {
- if (val == Long.MIN_VALUE)
- throw new InvalidRequestException("The negation of " + val + " overflows supported integer precision (signed 8 bytes integer)");
- else
- val = -val;
- }
-
- cf.addCounter(new QueryPath(cf.metadata().cfName, null, builder.build()), val);
- }
-
- public Operation validateAndAddBoundNames(ColumnSpecification column, ColumnSpecification[] boundNames) throws InvalidRequestException
- {
- value.validateType(column.name.toString(), column.type);
-
- if (value.isBindMarker())
- boundNames[value.bindIndex] = column;
-
- return this;
- }
-
- public List<Term> getValues()
- {
- return Collections.singletonList(value);
- }
-
- public boolean requiresRead(AbstractType<?> validator)
- {
- return false;
- }
-
- public Type getType()
- {
- return kind == Kind.COUNTER_DEC || kind == Kind.COUNTER_INC ? Type.COUNTER : Type.COLUMN;
- }
-
- /* Utility methods */
-
- public static Operation Set(Term value)
- {
- return new ColumnOperation(value, Kind.SET);
- }
-
- public static Operation CounterInc(Term value)
- {
- return new ColumnOperation(value, Kind.COUNTER_INC);
- }
-
- public static Operation CounterDec(Term value)
- {
- return new ColumnOperation(value, Kind.COUNTER_DEC);
- }
-
- public static Operation SetToEmpty()
- {
- return setToEmptyOperation;
- }
-}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/31e669ab/src/java/org/apache/cassandra/cql3/operations/ListOperation.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/operations/ListOperation.java b/src/java/org/apache/cassandra/cql3/operations/ListOperation.java
deleted file mode 100644
index e5dd953..0000000
--- a/src/java/org/apache/cassandra/cql3/operations/ListOperation.java
+++ /dev/null
@@ -1,410 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.cassandra.cql3.operations;
-
-import java.nio.ByteBuffer;
-import java.util.Collections;
-import java.util.HashSet;
-import java.util.List;
-import java.util.Set;
-import java.util.concurrent.atomic.AtomicReference;
-
-import org.apache.cassandra.cql3.ColumnIdentifier;
-import org.apache.cassandra.cql3.ColumnNameBuilder;
-import org.apache.cassandra.cql3.ColumnSpecification;
-import org.apache.cassandra.cql3.Term;
-import org.apache.cassandra.cql3.UpdateParameters;
-import org.apache.cassandra.db.ColumnFamily;
-import org.apache.cassandra.db.IColumn;
-import org.apache.cassandra.db.marshal.AbstractType;
-import org.apache.cassandra.db.marshal.CollectionType;
-import org.apache.cassandra.db.marshal.Int32Type;
-import org.apache.cassandra.db.marshal.ListType;
-import org.apache.cassandra.db.marshal.MapType;
-import org.apache.cassandra.db.marshal.MarshalException;
-import org.apache.cassandra.exceptions.InvalidRequestException;
-import org.apache.cassandra.utils.Pair;
-import org.apache.cassandra.utils.UUIDGen;
-
-public class ListOperation implements Operation
-{
- // Our reference time (1 jan 2010, 00:00:00) in milliseconds.
- private static final long REFERENCE_TIME = 1262304000000L;
-
- /*
- * For prepend, we need to be able to generate unique but decreasing time
- * UUID, which is a bit challenging. To do that, given a time in milliseconds,
- * we adds a number representing the 100-nanoseconds precision and make sure
- * that within the same millisecond, that number is always decreasing. We
- * do rely on the fact that the user will only provide decreasing
- * milliseconds timestamp for that purpose.
- */
- private static class PrecisionTime
- {
- public final long millis;
- public final int nanos;
-
- public PrecisionTime(long millis, int nanos)
- {
- this.millis = millis;
- this.nanos = nanos;
- }
- }
-
- private static final AtomicReference<PrecisionTime> last = new AtomicReference<PrecisionTime>(new PrecisionTime(Long.MAX_VALUE, 0));
-
- private static PrecisionTime getNextTime(long millis)
- {
- while (true)
- {
- PrecisionTime current = last.get();
-
- assert millis <= current.millis;
- PrecisionTime next = millis < current.millis
- ? new PrecisionTime(millis, 9999)
- : new PrecisionTime(millis, Math.max(0, current.nanos - 1));
-
- if (last.compareAndSet(current, next))
- return next;
- }
- }
-
- enum Kind { SET, SET_IDX, APPEND, PREPEND, DISCARD, DISCARD_IDX }
-
- private final List<Term> values;
- private final Kind kind;
-
- private ListOperation(List<Term> values, Kind kind)
- {
- this.values = Collections.unmodifiableList(values);
- this.kind = kind;
- }
-
- public void execute(ColumnFamily cf,
- ColumnNameBuilder builder,
- AbstractType<?> validator,
- UpdateParameters params,
- List<Pair<ByteBuffer, IColumn>> list) throws InvalidRequestException
- {
- if (!(validator instanceof ListType || (kind == Kind.SET_IDX && validator instanceof MapType)))
- throw new InvalidRequestException("List operations are only supported on List typed columns, but " + validator + " given.");
-
- switch (kind)
- {
- case SET:
- cf.addAtom(params.makeTombstoneForOverwrite(builder.copy().build(), builder.copy().buildAsEndOfRange()));
- doAppend(cf, builder, (CollectionType)validator, params);
- break;
- case SET_IDX:
- // Since the parser couldn't disambiguate between a 'list set by idx'
- // and a 'map put by key', we have to do it now.
- if (validator instanceof MapType)
- {
- assert values.size() == 2;
- MapOperation.Put(values.get(0), values.get(1)).execute(cf, builder, validator, params, null);
- }
- else
- {
- doSet(cf, builder, params, (CollectionType)validator, list);
- }
- break;
- case APPEND:
- doAppend(cf, builder, (CollectionType)validator, params);
- break;
- case PREPEND:
- doPrepend(cf, builder, (CollectionType)validator, params);
- break;
- case DISCARD:
- doDiscard(cf, (CollectionType)validator, params, list);
- break;
- case DISCARD_IDX:
- doDiscardIdx(cf, params, list);
- break;
- default:
- throw new AssertionError("Unsupported List operation: " + kind);
- }
- }
-
- public static void doSetFromPrepared(ColumnFamily cf, ColumnNameBuilder builder, ListType validator, Term values, UpdateParameters params) throws InvalidRequestException
- {
- if (!values.isBindMarker())
- throw new InvalidRequestException("Can't apply operation on column with " + validator + " type.");
-
- cf.addAtom(params.makeTombstoneForOverwrite(builder.copy().build(), builder.copy().buildAsEndOfRange()));
- doAppendFromPrepared(cf, builder, validator, values, params);
- }
-
- public static void doAppendFromPrepared(ColumnFamily cf, ColumnNameBuilder builder, ListType validator, Term values, UpdateParameters params) throws InvalidRequestException
- {
- if (!values.isBindMarker())
- throw new InvalidRequestException("Can't apply operation on column with " + validator + " type.");
-
- try
- {
- List<?> l = validator.compose(params.variables.get(values.bindIndex));
-
- for (int i = 0; i < l.size(); i++)
- {
- ColumnNameBuilder b = i == l.size() - 1 ? builder : builder.copy();
- ByteBuffer uuid = ByteBuffer.wrap(UUIDGen.getTimeUUIDBytes());
- ByteBuffer name = b.add(uuid).build();
- cf.addColumn(params.makeColumn(name, validator.valueComparator().decompose(l.get(i))));
- }
- }
- catch (MarshalException e)
- {
- throw new InvalidRequestException(e.getMessage());
- }
- }
-
- public static void doPrependFromPrepared(ColumnFamily cf, ColumnNameBuilder builder, ListType validator, Term values, UpdateParameters params) throws InvalidRequestException
- {
- if (!values.isBindMarker())
- throw new InvalidRequestException("Can't apply operation on column with " + validator + " type.");
-
- long time = REFERENCE_TIME - (System.currentTimeMillis() - REFERENCE_TIME);
-
- try
- {
- List<?> l = validator.compose(params.variables.get(values.bindIndex));
-
- for (int i = 0; i < l.size(); i++)
- {
- ColumnNameBuilder b = i == l.size() - 1 ? builder : builder.copy();
- PrecisionTime pt = getNextTime(time);
- ByteBuffer uuid = ByteBuffer.wrap(UUIDGen.getTimeUUIDBytes(pt.millis, pt.nanos));
- ByteBuffer name = b.add(uuid).build();
- cf.addColumn(params.makeColumn(name, validator.valueComparator().decompose(l.get(i))));
- }
- }
- catch (MarshalException e)
- {
- throw new InvalidRequestException(e.getMessage());
- }
- }
-
- public static void doDiscardFromPrepared(ColumnFamily cf, ColumnNameBuilder builder, ListType validator, Term values, UpdateParameters params, List<Pair<ByteBuffer, IColumn>> list) throws InvalidRequestException
- {
- if (!values.isBindMarker())
- throw new InvalidRequestException("Can't apply operation on column with " + validator + " type.");
-
- if (list == null)
- return;
-
- try
- {
- List<?> l = validator.compose(params.variables.get(values.bindIndex));
-
- Set<ByteBuffer> toDiscard = new HashSet<ByteBuffer>();
- for (Object elt : l)
- toDiscard.add(validator.valueComparator().decompose(elt));
-
- for (Pair<ByteBuffer, IColumn> p : list)
- {
- IColumn c = p.right;
- if (toDiscard.contains(c.value()))
- cf.addColumn(params.makeTombstone(c.name()));
- }
- }
- catch (MarshalException e)
- {
- throw new InvalidRequestException(e.getMessage());
- }
- }
-
- private void doSet(ColumnFamily cf, ColumnNameBuilder builder, UpdateParameters params, CollectionType validator, List<Pair<ByteBuffer, IColumn>> list) throws InvalidRequestException
- {
- int idx = validateListIdx(values.get(0), list);
- Term value = values.get(1);
-
- ByteBuffer name = list.get(idx).right.name();
- cf.addColumn(params.makeColumn(name, value.getByteBuffer(validator.valueComparator(), params.variables)));
- }
-
- private void doAppend(ColumnFamily cf, ColumnNameBuilder builder, CollectionType validator, UpdateParameters params) throws InvalidRequestException
- {
- for (int i = 0; i < values.size(); i++)
- {
- ColumnNameBuilder b = i == values.size() - 1 ? builder : builder.copy();
- ByteBuffer uuid = ByteBuffer.wrap(UUIDGen.getTimeUUIDBytes());
- ByteBuffer name = b.add(uuid).build();
- cf.addColumn(params.makeColumn(name, values.get(i).getByteBuffer(validator.valueComparator(), params.variables)));
- }
- }
-
- private void doPrepend(ColumnFamily cf, ColumnNameBuilder builder, CollectionType validator, UpdateParameters params) throws InvalidRequestException
- {
- long time = REFERENCE_TIME - (System.currentTimeMillis() - REFERENCE_TIME);
-
- for (int i = 0; i < values.size(); i++)
- {
- ColumnNameBuilder b = i == values.size() - 1 ? builder : builder.copy();
- PrecisionTime pt = getNextTime(time);
- ByteBuffer uuid = ByteBuffer.wrap(UUIDGen.getTimeUUIDBytes(pt.millis, pt.nanos));
- ByteBuffer name = b.add(uuid).build();
- cf.addColumn(params.makeColumn(name, values.get(i).getByteBuffer(validator.valueComparator(), params.variables)));
- }
- }
-
- private void doDiscard(ColumnFamily cf, CollectionType validator, UpdateParameters params, List<Pair<ByteBuffer, IColumn>> list) throws InvalidRequestException
- {
- if (list == null)
- return;
-
- Set<ByteBuffer> toDiscard = new HashSet<ByteBuffer>();
-
- for (Term value : values)
- toDiscard.add(value.getByteBuffer(validator.valueComparator(), params.variables));
-
- for (Pair<ByteBuffer, IColumn> p : list)
- {
- IColumn c = p.right;
- if (toDiscard.contains(c.value()))
- cf.addColumn(params.makeTombstone(c.name()));
- }
- }
-
- private void doDiscardIdx(ColumnFamily cf, UpdateParameters params, List<Pair<ByteBuffer, IColumn>> list) throws InvalidRequestException
- {
- int idx = validateListIdx(values.get(0), list);
- cf.addColumn(params.makeTombstone(list.get(idx).right.name()));
- }
-
- public Operation validateAndAddBoundNames(ColumnSpecification column, ColumnSpecification[] boundNames) throws InvalidRequestException
- {
- // Since the parser couldn't disambiguate between a 'list set by idx'
- // and a 'map put by key', we have to do it now.
- if (kind == Kind.SET_IDX && (column.type instanceof MapType))
- {
- assert values.size() == 2;
- return MapOperation.Put(values.get(0), values.get(1)).validateAndAddBoundNames(column, boundNames);
- }
-
- if (!(column.type instanceof ListType))
- throw new InvalidRequestException(String.format("Cannot apply list operation on column %s of type %s", column, column.type));
-
- ListType lt = (ListType)column.type;
- if (kind == Kind.SET_IDX)
- {
- assert values.size() == 2;
- Term idx = values.get(0);
- idx.validateType("list index", Int32Type.instance);
-
- Term value = values.get(1);
- value.validateType(column + " element", lt.elements);
-
- if (idx.isBindMarker())
- boundNames[idx.bindIndex] = indexSpecOf(column);
- if (value.isBindMarker())
- boundNames[value.bindIndex] = valueSpecOf(column, lt);
- }
- else
- {
- for (Term t : values)
- {
- t.validateType(column + " element", lt.elements);
- if (t.isBindMarker())
- boundNames[t.bindIndex] = column;
- }
- }
- return this;
- }
-
- public static ColumnSpecification indexSpecOf(ColumnSpecification column)
- {
- return new ColumnSpecification(column.ksName, column.cfName, new ColumnIdentifier("idx(" + column.name + ")", true), Int32Type.instance);
- }
-
- public static ColumnSpecification valueSpecOf(ColumnSpecification column, ListType type)
- {
- return new ColumnSpecification(column.ksName, column.cfName, new ColumnIdentifier("value(" + column.name + ")", true), type.elements);
- }
-
- public List<Term> getValues()
- {
- return values;
- }
-
- public boolean requiresRead(AbstractType<?> validator)
- {
- return kind == Kind.DISCARD || kind == Kind.DISCARD_IDX || kind == Kind.SET_IDX;
- }
-
- public Type getType()
- {
- return Type.LIST;
- }
-
- /* Utility methods */
-
- public static Operation Set(List<Term> values)
- {
- return new ListOperation(values, Kind.SET);
- }
-
- public static Operation SetIndex(List<Term> values)
- {
- return new ListOperation(values, Kind.SET_IDX);
- }
-
- public static Operation Append(List<Term> values)
- {
- return new ListOperation(values, Kind.APPEND);
- }
-
- public static Operation Prepend(List<Term> values)
- {
- return new ListOperation(values, Kind.PREPEND);
- }
-
- public static Operation Discard(List<Term> values)
- {
- return new ListOperation(values, Kind.DISCARD);
- }
-
- public static Operation DiscardKey(List<Term> values)
- {
- return new ListOperation(values, Kind.DISCARD_IDX);
- }
-
- @Override
- public String toString()
- {
- return "ListOperation(" + kind + ", " + values + ")";
- }
-
- private int validateListIdx(Term value, List<Pair<ByteBuffer, IColumn>> list) throws InvalidRequestException
- {
- try
- {
- if (value.getType() != Term.Type.INTEGER)
- throw new InvalidRequestException(String.format("Invalid argument %s for %s, must be an integer.", value.getText(), getType()));
-
- int idx = Integer.parseInt(value.getText());
- if (list == null || list.size() <= idx)
- throw new InvalidRequestException(String.format("Invalid index %d, list has size %d", idx, list == null ? 0 : list.size()));
-
- return idx;
- }
- catch (NumberFormatException e)
- {
- // This should not happen, unless we screwed up the parser
- throw new RuntimeException();
- }
- }
-}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/31e669ab/src/java/org/apache/cassandra/cql3/operations/MapOperation.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/operations/MapOperation.java b/src/java/org/apache/cassandra/cql3/operations/MapOperation.java
deleted file mode 100644
index fcd0ec7..0000000
--- a/src/java/org/apache/cassandra/cql3/operations/MapOperation.java
+++ /dev/null
@@ -1,206 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.cassandra.cql3.operations;
-
-import java.nio.ByteBuffer;
-import java.util.ArrayList;
-import java.util.HashMap;
-import java.util.List;
-import java.util.Map;
-
-import org.apache.cassandra.cql3.ColumnIdentifier;
-import org.apache.cassandra.cql3.ColumnNameBuilder;
-import org.apache.cassandra.cql3.ColumnSpecification;
-import org.apache.cassandra.cql3.Term;
-import org.apache.cassandra.cql3.UpdateParameters;
-import org.apache.cassandra.db.ColumnFamily;
-import org.apache.cassandra.db.IColumn;
-import org.apache.cassandra.db.marshal.AbstractType;
-import org.apache.cassandra.db.marshal.CollectionType;
-import org.apache.cassandra.db.marshal.MapType;
-import org.apache.cassandra.db.marshal.MarshalException;
-import org.apache.cassandra.exceptions.InvalidRequestException;
-import org.apache.cassandra.utils.Pair;
-
-public class MapOperation implements Operation
-{
- enum Kind { SET, PUT, DISCARD }
-
- private final Map<Term, Term> values;
- private final Term discardKey;
- private final Kind kind;
-
- private MapOperation(Map<Term, Term> values, Kind kind)
- {
- this.values = values;
- this.discardKey = null;
- this.kind = kind;
- }
-
- private MapOperation(Term discardKey)
- {
- this.values = null;
- this.discardKey = discardKey;
- this.kind = Kind.DISCARD;
- }
-
- public void execute(ColumnFamily cf,
- ColumnNameBuilder builder,
- AbstractType<?> validator,
- UpdateParameters params,
- List<Pair<ByteBuffer, IColumn>> list) throws InvalidRequestException
- {
- if (!(validator instanceof MapType))
- throw new InvalidRequestException("Map operations are only supported on Map typed columns, but " + validator + " given.");
-
- switch (kind)
- {
- case SET: // fallthrough on purpose; remove previous Map before setting (PUT) the new one
- cf.addAtom(params.makeTombstoneForOverwrite(builder.copy().build(), builder.copy().buildAsEndOfRange()));
- case PUT:
- doPut(cf, builder, (CollectionType)validator, params);
- break;
- case DISCARD:
- doDiscard(cf, builder, (CollectionType)validator, params);
- break;
- default:
- throw new AssertionError("Unsupported Map operation: " + kind);
- }
- }
-
- public static void doSetFromPrepared(ColumnFamily cf, ColumnNameBuilder builder, MapType validator, Term values, UpdateParameters params) throws InvalidRequestException
- {
- if (!values.isBindMarker())
- throw new InvalidRequestException("Can't apply operation on column with " + validator + " type.");
-
- cf.addAtom(params.makeTombstoneForOverwrite(builder.copy().build(), builder.copy().buildAsEndOfRange()));
- doPutFromPrepared(cf, builder, validator, values, params);
- }
-
- public static void doPutFromPrepared(ColumnFamily cf, ColumnNameBuilder builder, MapType validator, Term values, UpdateParameters params) throws InvalidRequestException
- {
- if (!values.isBindMarker())
- throw new InvalidRequestException("Can't apply operation on column with " + validator + " type.");
-
- try
- {
- Map<?, ?> m = validator.compose(params.variables.get(values.bindIndex));
- for (Map.Entry<?, ?> entry : m.entrySet())
- {
- ByteBuffer name = builder.copy().add(validator.nameComparator().decompose(entry.getKey())).build();
- ByteBuffer value = validator.valueComparator().decompose(entry.getValue());
- cf.addColumn(params.makeColumn(name, value));
- }
- }
- catch (MarshalException e)
- {
- throw new InvalidRequestException(e.getMessage());
- }
- }
-
- private void doPut(ColumnFamily cf, ColumnNameBuilder builder, CollectionType validator, UpdateParameters params) throws InvalidRequestException
- {
- for (Map.Entry<Term, Term> entry : values.entrySet())
- {
- ByteBuffer name = builder.copy().add(entry.getKey().getByteBuffer(validator.nameComparator(), params.variables)).build();
- ByteBuffer value = entry.getValue().getByteBuffer(validator.valueComparator(), params.variables);
- cf.addColumn(params.makeColumn(name, value));
- }
- }
-
- private void doDiscard(ColumnFamily cf, ColumnNameBuilder builder, CollectionType validator, UpdateParameters params) throws InvalidRequestException
- {
- ByteBuffer name = builder.add(discardKey.getByteBuffer(validator.nameComparator(), params.variables)).build();
- cf.addColumn(params.makeTombstone(name));
- }
-
- public Operation validateAndAddBoundNames(ColumnSpecification column, ColumnSpecification[] boundNames) throws InvalidRequestException
- {
- if (!(column.type instanceof MapType))
- throw new InvalidRequestException(String.format("Cannot apply map operation on column %s of type %s", column, column.type));
-
- MapType mt = (MapType)column.type;
- for (Map.Entry<Term, Term> entry : values.entrySet())
- {
- Term key = entry.getKey();
- key.validateType(column + " key", mt.keys);
-
- Term value = entry.getValue();
- value.validateType(column + " value", mt.values);
-
- if (key.isBindMarker())
- boundNames[key.bindIndex] = keySpecOf(column, mt);
- if (value.isBindMarker())
- boundNames[value.bindIndex] = valueSpecOf(column, mt);
- }
- return this;
- }
-
- public static ColumnSpecification keySpecOf(ColumnSpecification column, MapType type)
- {
- return new ColumnSpecification(column.ksName, column.cfName, new ColumnIdentifier("key(" + column.name + ")", true), type.keys);
- }
-
- public static ColumnSpecification valueSpecOf(ColumnSpecification column, MapType type)
- {
- return new ColumnSpecification(column.ksName, column.cfName, new ColumnIdentifier("value(" + column.name + ")", true), type.values);
- }
-
- public List<Term> getValues()
- {
- List<Term> l = new ArrayList<Term>(2 * values.size());
- for (Map.Entry<Term, Term> entry : values.entrySet())
- {
- l.add(entry.getKey());
- l.add(entry.getValue());
- }
- return l;
- }
-
- public boolean requiresRead(AbstractType<?> validator)
- {
- return kind == Kind.SET || kind == Kind.DISCARD;
- }
-
- public Type getType()
- {
- return Type.MAP;
- }
-
- /* Utility methods */
-
- public static Operation Set(Map<Term, Term> values)
- {
- return new MapOperation(values, Kind.SET);
- }
-
- public static Operation Put(Map<Term, Term> values)
- {
- return new MapOperation(values, Kind.PUT);
- }
-
- public static Operation Put(final Term key, final Term value)
- {
- return Put(new HashMap<Term, Term>(1) {{ put(key, value); }});
- }
-
- public static Operation DiscardKey(Term discardKey)
- {
- return new MapOperation(discardKey);
- }
-}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/31e669ab/src/java/org/apache/cassandra/cql3/operations/Operation.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/operations/Operation.java b/src/java/org/apache/cassandra/cql3/operations/Operation.java
deleted file mode 100644
index 98ec503..0000000
--- a/src/java/org/apache/cassandra/cql3/operations/Operation.java
+++ /dev/null
@@ -1,51 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.cassandra.cql3.operations;
-
-import java.nio.ByteBuffer;
-import java.util.List;
-
-import org.apache.cassandra.cql3.ColumnNameBuilder;
-import org.apache.cassandra.cql3.ColumnSpecification;
-import org.apache.cassandra.cql3.Term;
-import org.apache.cassandra.cql3.UpdateParameters;
-import org.apache.cassandra.db.ColumnFamily;
-import org.apache.cassandra.db.IColumn;
-import org.apache.cassandra.db.marshal.AbstractType;
-import org.apache.cassandra.db.marshal.CollectionType;
-import org.apache.cassandra.exceptions.InvalidRequestException;
-import org.apache.cassandra.utils.Pair;
-
-public interface Operation
-{
- public static enum Type { COLUMN, COUNTER, LIST, SET, MAP, PREPARED }
-
- public void execute(ColumnFamily cf,
- ColumnNameBuilder builder,
- AbstractType<?> validator,
- UpdateParameters params,
- List<Pair<ByteBuffer, IColumn>> list) throws InvalidRequestException;
-
- public Operation validateAndAddBoundNames(ColumnSpecification column, ColumnSpecification[] boundNames) throws InvalidRequestException;
-
- public List<Term> getValues();
-
- public boolean requiresRead(AbstractType<?> validator);
-
- public Type getType();
-}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/31e669ab/src/java/org/apache/cassandra/cql3/operations/PreparedOperation.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/operations/PreparedOperation.java b/src/java/org/apache/cassandra/cql3/operations/PreparedOperation.java
deleted file mode 100644
index 77cd183..0000000
--- a/src/java/org/apache/cassandra/cql3/operations/PreparedOperation.java
+++ /dev/null
@@ -1,179 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.cassandra.cql3.operations;
-
-import java.nio.ByteBuffer;
-import java.util.Collections;
-import java.util.List;
-
-import org.apache.cassandra.cql3.*;
-import org.apache.cassandra.db.ColumnFamily;
-import org.apache.cassandra.db.IColumn;
-import org.apache.cassandra.db.marshal.AbstractType;
-import org.apache.cassandra.db.marshal.CollectionType;
-import org.apache.cassandra.db.marshal.CounterColumnType;
-import org.apache.cassandra.db.marshal.ListType;
-import org.apache.cassandra.db.marshal.MapType;
-import org.apache.cassandra.db.marshal.SetType;
-import org.apache.cassandra.exceptions.InvalidRequestException;
-import org.apache.cassandra.utils.Pair;
-
-public class PreparedOperation implements Operation
-{
- public enum Kind { SET, PREPARED_PLUS, PLUS_PREPARED, MINUS_PREPARED }
-
- private final Term preparedValue;
- private final Kind kind;
-
- public PreparedOperation(Term value, Kind kind)
- {
- assert value.isBindMarker();
- this.preparedValue = value;
- this.kind = kind;
- }
-
- public void execute(ColumnFamily cf,
- ColumnNameBuilder builder,
- AbstractType<?> validator,
- UpdateParameters params,
- List<Pair<ByteBuffer, IColumn>> list) throws InvalidRequestException
- {
- if (validator instanceof CollectionType)
- {
- switch (((CollectionType)validator).kind)
- {
- case LIST:
- switch (kind)
- {
- case SET:
- ListOperation.doSetFromPrepared(cf, builder, (ListType)validator, preparedValue, params);
- break;
- case PREPARED_PLUS:
- ListOperation.doPrependFromPrepared(cf, builder, (ListType)validator, preparedValue, params);
- break;
- case PLUS_PREPARED:
- ListOperation.doAppendFromPrepared(cf, builder, (ListType)validator, preparedValue, params);
- break;
- case MINUS_PREPARED:
- ListOperation.doDiscardFromPrepared(cf, builder, (ListType)validator, preparedValue, params, list);
- break;
- }
- break;
- case SET:
- switch (kind)
- {
- case SET:
- SetOperation.doSetFromPrepared(cf, builder, (SetType)validator, preparedValue, params);
- break;
- case PLUS_PREPARED:
- SetOperation.doAddFromPrepared(cf, builder, (SetType)validator, preparedValue, params);
- break;
- case MINUS_PREPARED:
- SetOperation.doDiscardFromPrepared(cf, builder, (SetType)validator, preparedValue, params);
- break;
- }
- break;
- case MAP:
- switch (kind)
- {
- case SET:
- MapOperation.doSetFromPrepared(cf, builder, (MapType)validator, preparedValue, params);
- break;
- case PLUS_PREPARED:
- MapOperation.doPutFromPrepared(cf, builder, (MapType)validator, preparedValue, params);
- break;
- }
- break;
- }
- }
- else
- {
- switch (kind)
- {
- case SET:
- ColumnOperation.Set(preparedValue).execute(cf, builder, validator, params, null);
- break;
- case PLUS_PREPARED:
- ColumnOperation.CounterInc(preparedValue).execute(cf, builder, validator, params, null);
- break;
- case MINUS_PREPARED:
- ColumnOperation.CounterDec(preparedValue).execute(cf, builder, validator, params, null);
- break;
- }
- }
- }
-
- public Operation validateAndAddBoundNames(ColumnSpecification column, ColumnSpecification[] boundNames) throws InvalidRequestException
- {
- if (column.type instanceof CollectionType)
- {
- switch (kind)
- {
- case PREPARED_PLUS:
- if (column.type instanceof MapType)
- throw new InvalidRequestException("Unsupported syntax, cannot put to a prepared map");
- if (column.type instanceof SetType)
- throw new InvalidRequestException("Unsupported syntax, cannot add to a prepared set");
- break;
- case MINUS_PREPARED:
- if (column.type instanceof MapType)
- throw new InvalidRequestException("Unsuppoted syntax, discard syntax for map not supported");
- break;
- }
- switch (((CollectionType)column.type).kind)
- {
- }
- }
- else if (column.type instanceof CounterColumnType)
- {
- if (kind == Kind.PREPARED_PLUS)
- throw new InvalidRequestException("Unsupported syntax for increment, must be of the form X = X + <value>");
- }
- else
- {
- // Any other operation than a set is invalid
- if (kind != Kind.SET)
- throw new InvalidRequestException(String.format("Invalid operation for %s of type %s", column, column.type));
- }
-
- if (preparedValue.isBindMarker())
- boundNames[preparedValue.bindIndex] = column;
- return this;
- }
-
- public List<Term> getValues()
- {
- return Collections.singletonList(preparedValue);
- }
-
- public boolean requiresRead(AbstractType<?> validator)
- {
- // Only prepared operation requiring a read is list discard
- return (validator instanceof ListType) && kind == Kind.MINUS_PREPARED;
- }
-
- public boolean isPotentialCounterOperation()
- {
- return kind == Kind.PLUS_PREPARED || kind == Kind.MINUS_PREPARED;
- }
-
- public Type getType()
- {
- return Type.PREPARED;
- }
-}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/31e669ab/src/java/org/apache/cassandra/cql3/operations/SetOperation.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/operations/SetOperation.java b/src/java/org/apache/cassandra/cql3/operations/SetOperation.java
deleted file mode 100644
index 30a96ae..0000000
--- a/src/java/org/apache/cassandra/cql3/operations/SetOperation.java
+++ /dev/null
@@ -1,216 +0,0 @@
-/**
- * Licensed to the Apache Software Foundation (ASF) under one
- * or more contributor license agreements. See the NOTICE file
- * distributed with this work for additional information
- * regarding copyright ownership. The ASF licenses this file
- * to you under the Apache License, Version 2.0 (the
- * "License"); you may not use this file except in compliance
- * with the License. You may obtain a copy of the License at
- *
- * http://www.apache.org/licenses/LICENSE-2.0
- *
- * Unless required by applicable law or agreed to in writing, software
- * distributed under the License is distributed on an "AS IS" BASIS,
- * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- * See the License for the specific language governing permissions and
- * limitations under the License.
- */
-package org.apache.cassandra.cql3.operations;
-
-import java.nio.ByteBuffer;
-import java.util.Collections;
-import java.util.Iterator;
-import java.util.List;
-import java.util.Set;
-
-import org.apache.cassandra.cql3.ColumnNameBuilder;
-import org.apache.cassandra.cql3.ColumnSpecification;
-import org.apache.cassandra.cql3.Term;
-import org.apache.cassandra.cql3.UpdateParameters;
-import org.apache.cassandra.db.ColumnFamily;
-import org.apache.cassandra.db.IColumn;
-import org.apache.cassandra.db.marshal.AbstractType;
-import org.apache.cassandra.db.marshal.CollectionType;
-import org.apache.cassandra.db.marshal.MarshalException;
-import org.apache.cassandra.db.marshal.SetType;
-import org.apache.cassandra.db.marshal.MapType;
-import org.apache.cassandra.exceptions.InvalidRequestException;
-import org.apache.cassandra.utils.ByteBufferUtil;
-import org.apache.cassandra.utils.Pair;
-
-public class SetOperation implements Operation
-{
- enum Kind { SET, ADD, DISCARD }
-
- private final List<Term> values;
- private final Kind kind;
-
- private SetOperation(List<Term> values, Kind kind)
- {
- this.values = values;
- this.kind = kind;
- }
-
- public void execute(ColumnFamily cf,
- ColumnNameBuilder builder,
- AbstractType<?> validator,
- UpdateParameters params,
- List<Pair<ByteBuffer, IColumn>> list) throws InvalidRequestException
- {
- if (!(validator instanceof SetType))
- throw new InvalidRequestException("Set operations are only supported on Set typed columns, but " + validator + " given.");
-
- switch (kind)
- {
- case SET: // fallthrough on purpose; remove previous Set before setting (ADD) the new one
- cf.addAtom(params.makeTombstoneForOverwrite(builder.copy().build(), builder.copy().buildAsEndOfRange()));
- case ADD:
- doAdd(cf, builder, (CollectionType)validator, params);
- break;
- case DISCARD:
- doDiscard(cf, builder, (CollectionType)validator, params);
- break;
- default:
- throw new AssertionError("Unsupported Set operation: " + kind);
- }
- }
-
- public static void doSetFromPrepared(ColumnFamily cf, ColumnNameBuilder builder, SetType validator, Term values, UpdateParameters params) throws InvalidRequestException
- {
- if (!values.isBindMarker())
- throw new InvalidRequestException("Can't apply operation on column with " + validator + " type.");
-
- cf.addAtom(params.makeTombstoneForOverwrite(builder.copy().build(), builder.copy().buildAsEndOfRange()));
- doAddFromPrepared(cf, builder, validator, values, params);
- }
-
- public static void doAddFromPrepared(ColumnFamily cf, ColumnNameBuilder builder, SetType validator, Term values, UpdateParameters params) throws InvalidRequestException
- {
- if (!values.isBindMarker())
- throw new InvalidRequestException("Can't apply operation on column with " + validator + " type.");
-
- try
- {
- Set<?> s = validator.compose(params.variables.get(values.bindIndex));
- Iterator<?> iter = s.iterator();
- while (iter.hasNext())
- {
- ColumnNameBuilder b = iter.hasNext() ? builder.copy() : builder;
- ByteBuffer name = b.add(validator.nameComparator().decompose(iter.next())).build();
- cf.addColumn(params.makeColumn(name, ByteBufferUtil.EMPTY_BYTE_BUFFER));
- }
- }
- catch (MarshalException e)
- {
- throw new InvalidRequestException(e.getMessage());
- }
- }
-
- public static void doDiscardFromPrepared(ColumnFamily cf, ColumnNameBuilder builder, SetType validator, Term values, UpdateParameters params) throws InvalidRequestException
- {
- if (!values.isBindMarker())
- throw new InvalidRequestException("Can't apply operation on column with " + validator + " type.");
-
- try
- {
- Set<?> s = validator.compose(params.variables.get(values.bindIndex));
- Iterator<?> iter = s.iterator();
- while (iter.hasNext())
- {
- ColumnNameBuilder b = iter.hasNext() ? builder.copy() : builder;
- ByteBuffer name = b.add(validator.nameComparator().decompose(iter.next())).build();
- cf.addColumn(params.makeTombstone(name));
- }
- }
- catch (MarshalException e)
- {
- throw new InvalidRequestException(e.getMessage());
- }
- }
-
- private void doAdd(ColumnFamily cf, ColumnNameBuilder builder, CollectionType validator, UpdateParameters params) throws InvalidRequestException
- {
- for (int i = 0; i < values.size(); ++i)
- {
- ColumnNameBuilder b = i == values.size() - 1 ? builder : builder.copy();
- ByteBuffer name = b.add(values.get(i).getByteBuffer(validator.nameComparator(), params.variables)).build();
- cf.addColumn(params.makeColumn(name, ByteBufferUtil.EMPTY_BYTE_BUFFER));
- }
- }
-
- private void doDiscard(ColumnFamily cf, ColumnNameBuilder builder, CollectionType validator, UpdateParameters params) throws InvalidRequestException
- {
- for (int i = 0; i < values.size(); ++i)
- {
- ColumnNameBuilder b = i == values.size() - 1 ? builder : builder.copy();
- ByteBuffer name = b.add(values.get(i).getByteBuffer(validator.nameComparator(), params.variables)).build();
- cf.addColumn(params.makeTombstone(name));
- }
- }
-
- public Operation validateAndAddBoundNames(ColumnSpecification column, ColumnSpecification[] boundNames) throws InvalidRequestException
- {
- // On the parser side, we're unable to differentiate an empty map from an empty set for add and set operations.
- // Fix it now that we have the actual type.
- if (column.type instanceof MapType && values.isEmpty())
- return toEmptyMapOperation().validateAndAddBoundNames(column, boundNames);
-
- if (!(column.type instanceof SetType))
- throw new InvalidRequestException(String.format("Cannot apply set operation on column %s of type %s", column, column.type));
-
- AbstractType<?> valuesType = ((SetType)column.type).elements;
-
- for (Term t : values)
- {
- t.validateType(column + " element", valuesType);
-
- if (t.isBindMarker())
- boundNames[t.bindIndex] = column;
- }
- return this;
- }
-
- private Operation toEmptyMapOperation()
- {
- switch (kind)
- {
- case SET:
- return MapOperation.Set(Collections.<Term, Term>emptyMap());
- case ADD:
- return MapOperation.Put(Collections.<Term, Term>emptyMap());
- }
- return this;
- }
-
- public List<Term> getValues()
- {
- return values;
- }
-
- public boolean requiresRead(AbstractType<?> validator)
- {
- return false;
- }
-
- public Type getType()
- {
- return Type.SET;
- }
-
- /* Utility methods */
-
- public static Operation Set(List<Term> values)
- {
- return new SetOperation(values, Kind.SET);
- }
-
- public static Operation Add(List<Term> values)
- {
- return new SetOperation(values, Kind.ADD);
- }
-
- public static Operation Discard(List<Term> values)
- {
- return new SetOperation(values, Kind.DISCARD);
- }
-}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/31e669ab/src/java/org/apache/cassandra/cql3/statements/DeleteStatement.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/statements/DeleteStatement.java b/src/java/org/apache/cassandra/cql3/statements/DeleteStatement.java
index ae47a40..c004d72 100644
--- a/src/java/org/apache/cassandra/cql3/statements/DeleteStatement.java
+++ b/src/java/org/apache/cassandra/cql3/statements/DeleteStatement.java
@@ -22,10 +22,6 @@ import java.util.*;
import org.apache.cassandra.cql3.*;
import org.apache.cassandra.config.CFMetaData;
-import org.apache.cassandra.cql3.operations.ListOperation;
-import org.apache.cassandra.cql3.operations.MapOperation;
-import org.apache.cassandra.cql3.operations.Operation;
-import org.apache.cassandra.cql3.operations.SetOperation;
import org.apache.cassandra.db.ConsistencyLevel;
import org.apache.cassandra.db.ColumnFamily;
import org.apache.cassandra.db.DeletionInfo;
@@ -41,20 +37,19 @@ import org.apache.cassandra.utils.Pair;
public class DeleteStatement extends ModificationStatement
{
private CFDefinition cfDef;
- private final List<Selector> columns;
+ private final List<Operation.RawDeletion> deletions;
private final List<Relation> whereClause;
- private final List<Pair<CFDefinition.Name, Term>> toRemove;
-
+ private final List<Operation> toRemove;
private final Map<ColumnIdentifier, List<Term>> processedKeys = new HashMap<ColumnIdentifier, List<Term>>();
- public DeleteStatement(CFName name, List<Selector> columns, List<Relation> whereClause, Attributes attrs)
+ public DeleteStatement(CFName name, List<Operation.RawDeletion> deletions, List<Relation> whereClause, Attributes attrs)
{
super(name, attrs);
- this.columns = columns;
+ this.deletions = deletions;
this.whereClause = whereClause;
- this.toRemove = new ArrayList<Pair<CFDefinition.Name, Term>>(columns.size());
+ this.toRemove = new ArrayList<Operation>(deletions.size());
}
protected void validateConsistency(ConsistencyLevel cl) throws InvalidRequestException
@@ -79,53 +74,49 @@ public class DeleteStatement extends ModificationStatement
boolean isRange = cfDef.isCompact ? !fullKey : (!fullKey || toRemove.isEmpty());
if (!toRemove.isEmpty() && isRange)
- throw new InvalidRequestException(String.format("Missing mandatory PRIMARY KEY part %s since %s specified", firstEmpty, toRemove.iterator().next().left));
+ throw new InvalidRequestException(String.format("Missing mandatory PRIMARY KEY part %s since %s specified", firstEmpty, toRemove.iterator().next().columnName));
- // Lists DISCARD operation incurs a read. Do that now.
Set<ByteBuffer> toRead = null;
- for (Pair<CFDefinition.Name, Term> p : toRemove)
+ for (Operation op : toRemove)
{
- CFDefinition.Name name = p.left;
- Term value = p.right;
-
- if ((name.type instanceof ListType) && value != null)
+ if (op.requiresRead())
{
if (toRead == null)
toRead = new TreeSet<ByteBuffer>(UTF8Type.instance);
- toRead.add(name.name.key);
+ toRead.add(op.columnName.key);
}
}
Map<ByteBuffer, ColumnGroupMap> rows = toRead != null ? readRows(keys, builder, toRead, (CompositeType)cfDef.cfm.comparator, local, cl) : null;
Collection<RowMutation> rowMutations = new ArrayList<RowMutation>(keys.size());
- UpdateParameters params = new UpdateParameters(variables, getTimestamp(now), -1);
+ UpdateParameters params = new UpdateParameters(variables, getTimestamp(now), -1, rows);
for (ByteBuffer key : keys)
- rowMutations.add(mutationForKey(cfDef, key, builder, isRange, params, rows == null ? null : rows.get(key)));
+ rowMutations.add(mutationForKey(cfDef, key, builder, isRange, params));
return rowMutations;
}
- public RowMutation mutationForKey(CFDefinition cfDef, ByteBuffer key, ColumnNameBuilder builder, boolean isRange, UpdateParameters params, ColumnGroupMap group)
+ public RowMutation mutationForKey(CFDefinition cfDef, ByteBuffer key, ColumnNameBuilder builder, boolean isRange, UpdateParameters params)
throws InvalidRequestException
{
QueryProcessor.validateKey(key);
RowMutation rm = new RowMutation(cfDef.cfm.ksName, key);
ColumnFamily cf = rm.addOrGet(columnFamily());
- if (columns.isEmpty() && builder.componentCount() == 0)
+ if (toRemove.isEmpty() && builder.componentCount() == 0)
{
- // No columns, delete the row
+ // No columns specified, delete the row
cf.delete(new DeletionInfo(params.timestamp, params.localDeletionTime));
}
else
{
if (isRange)
{
- ByteBuffer start = builder.copy().build();
+ assert toRemove.isEmpty();
+ ByteBuffer start = builder.build();
ByteBuffer end = builder.buildAsEndOfRange();
- QueryProcessor.validateColumnName(start); // If start is good, end is too
cf.addAtom(params.makeRangeTombstone(start, end));
}
else
@@ -134,63 +125,12 @@ public class DeleteStatement extends ModificationStatement
if (cfDef.isCompact)
{
ByteBuffer columnName = builder.build();
- QueryProcessor.validateColumnName(columnName);
cf.addColumn(params.makeTombstone(columnName));
}
else
{
- Iterator<Pair<CFDefinition.Name, Term>> iter = toRemove.iterator();
- while (iter.hasNext())
- {
- Pair<CFDefinition.Name, Term> p = iter.next();
- CFDefinition.Name column = p.left;
-
- if (column.type.isCollection())
- {
- CollectionType validator = (CollectionType) column.type;
- Term keySelected = p.right;
-
- if (keySelected == null)
- {
- // Delete the whole collection
- ByteBuffer start = builder.copy().add(column.name.key).build();
- QueryProcessor.validateColumnName(start);
- ColumnNameBuilder b = iter.hasNext() ? builder.copy() : builder;
- ByteBuffer end = b.add(column.name.key).buildAsEndOfRange();
- cf.addAtom(params.makeRangeTombstone(start, end));
- }
- else
- {
- builder.add(column.name.key);
- List<Term> args = Collections.singletonList(keySelected);
-
- Operation op;
- switch (validator.kind)
- {
- case LIST:
- op = ListOperation.DiscardKey(args);
- break;
- case SET:
- op = SetOperation.Discard(args);
- break;
- case MAP:
- op = MapOperation.DiscardKey(keySelected);
- break;
- default:
- throw new InvalidRequestException("Unknown collection type: " + validator.kind);
- }
-
- op.execute(cf, builder, validator, params, group == null ? null : group.getCollection(column.name.key));
- }
- }
- else
- {
- ColumnNameBuilder b = iter.hasNext() ? builder.copy() : builder;
- ByteBuffer columnName = b.add(column.name.key).build();
- QueryProcessor.validateColumnName(columnName);
- cf.addColumn(params.makeTombstone(columnName));
- }
- }
+ for (Operation op : toRemove)
+ op.execute(key, cf, builder.copy(), params);
}
}
}
@@ -206,40 +146,20 @@ public class DeleteStatement extends ModificationStatement
cfDef = metadata.getCfDef();
UpdateStatement.processKeys(cfDef, whereClause, processedKeys, boundNames);
- for (Selector column : columns)
+ for (Operation.RawDeletion deletion : deletions)
{
- CFDefinition.Name name = cfDef.get(column.id());
+ CFDefinition.Name name = cfDef.get(deletion.affectedColumn());
if (name == null)
- throw new InvalidRequestException(String.format("Unknown identifier %s", column));
+ throw new InvalidRequestException(String.format("Unknown identifier %s", name));
// For compact, we only have one value except the key, so the only form of DELETE that make sense is without a column
// list. However, we support having the value name for coherence with the static/sparse case
if (name.kind != CFDefinition.Name.Kind.COLUMN_METADATA && name.kind != CFDefinition.Name.Kind.VALUE_ALIAS)
- throw new InvalidRequestException(String.format("Invalid identifier %s for deletion (should not be a PRIMARY KEY part)", column));
-
- if (column.hasKey())
- {
- if (name.type instanceof ListType)
- {
- column.key().validateType("list index", Int32Type.instance);
-
- if (column.key().isBindMarker())
- boundNames[column.key().bindIndex] = ListOperation.indexSpecOf(name);
- }
- else if (name.type instanceof MapType)
- {
- column.key().validateType("map key", ((MapType)name.type).keys);
-
- if (column.key().isBindMarker())
- boundNames[column.key().bindIndex] = MapOperation.keySpecOf(name, (MapType)name.type);
- }
- else
- {
- throw new InvalidRequestException(String.format("Invalid selection %s since %s is neither a list or a map", column, column.id()));
- }
- }
+ throw new InvalidRequestException(String.format("Invalid identifier %s for deletion (should not be a PRIMARY KEY part)", name));
- toRemove.add(Pair.create(name, column.key()));
+ Operation op = deletion.prepare(name);
+ op.collectMarkerSpecification(boundNames);
+ toRemove.add(op);
}
return new ParsedStatement.Prepared(this, Arrays.<ColumnSpecification>asList(boundNames));
@@ -255,7 +175,7 @@ public class DeleteStatement extends ModificationStatement
{
return String.format("DeleteStatement(name=%s, columns=%s, keys=%s)",
cfName,
- columns,
+ deletions,
whereClause);
}
}
http://git-wip-us.apache.org/repos/asf/cassandra/blob/31e669ab/src/java/org/apache/cassandra/cql3/statements/RawSelector.java
----------------------------------------------------------------------
diff --git a/src/java/org/apache/cassandra/cql3/statements/RawSelector.java b/src/java/org/apache/cassandra/cql3/statements/RawSelector.java
new file mode 100644
index 0000000..2a7ceff
--- /dev/null
+++ b/src/java/org/apache/cassandra/cql3/statements/RawSelector.java
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements. See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership. The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License. You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied. See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+package org.apache.cassandra.cql3.statements;
+
+import java.util.List;
+
+import org.apache.cassandra.cql3.ColumnIdentifier;
+
+public interface RawSelector
+{
+ public static class WritetimeOrTTL implements RawSelector
+ {
+ public final ColumnIdentifier id;
+ public final boolean isWritetime;
+
+ public WritetimeOrTTL(ColumnIdentifier id, boolean isWritetime)
+ {
+ this.id = id;
+ this.isWritetime = isWritetime;
+ }
+
+ @Override
+ public String toString()
+ {
+ return (isWritetime ? "writetime" : "ttl") + "(" + id + ")";
+ }
+ }
+
+ public static class WithFunction implements RawSelector
+ {
+ public final String functionName;
+ public final List<RawSelector> args;
+
+ public WithFunction(String functionName, List<RawSelector> args)
+ {
+ this.functionName = functionName;
+ this.args = args;
+ }
+
+ @Override
+ public String toString()
+ {
+ StringBuilder sb = new StringBuilder();
+ sb.append(functionName).append("(");
+ for (int i = 0; i < args.size(); i++)
+ {
+ if (i > 0) sb.append(", ");
+ sb.append(args.get(i));
+ }
+ return sb.append(")").toString();
+ }
+ }
+}