Search in sources :

Example 1 with SimpleSelectorFactory

use of org.apache.cassandra.cql3.selection.SimpleSelector.SimpleSelectorFactory in project cassandra by apache.

the class ElementsSelector method newSliceFactory.

/**
 * Creates a {@code Selector.Factory} for the selection of a slice of a collection.
 *
 * @param name a string representing the selection the factory is for. Something like "c[x..y]".
 * @param factory the {@code Selector.Factory} corresponding to the collection on which a slice
 * is selected.
 * @param type the type of the collection.
 * @param from the starting bound of the selected slice. This cannot be {@code null} but can be
 * {@code Constants.UNSET_VALUE} if the slice doesn't have a start.
 * @param to the ending bound of the selected slice. This cannot be {@code null} but can be
 * {@code Constants.UNSET_VALUE} if the slice doesn't have an end.
 * @return the created factory.
 */
public static Factory newSliceFactory(String name, Selector.Factory factory, CollectionType<?> type, final Term from, final Term to) {
    return new AbstractFactory(name, factory, type) {

        protected AbstractType<?> getReturnType() {
            return type;
        }

        public Selector newInstance(QueryOptions options) throws InvalidRequestException {
            ByteBuffer fromValue = from.bindAndGet(options);
            ByteBuffer toValue = to.bindAndGet(options);
            // Note that we use UNSET values to represent no bound, so null is truly invalid
            if (fromValue == null || toValue == null)
                throw new InvalidRequestException("Invalid null value for slice selection on " + factory.getColumnName());
            return new SliceSelector(factory.newInstance(options), from.bindAndGet(options), to.bindAndGet(options));
        }

        public boolean areAllFetchedColumnsKnown() {
            // 3) the bound of the selected slice are terminal.
            return factory.areAllFetchedColumnsKnown() && (!type.isMultiCell() || !factory.isSimpleSelectorFactory() || (from.isTerminal() && to.isTerminal()));
        }

        public void addFetchedColumns(ColumnFilter.Builder builder) {
            if (!type.isMultiCell() || !factory.isSimpleSelectorFactory()) {
                factory.addFetchedColumns(builder);
                return;
            }
            ColumnMetadata column = ((SimpleSelectorFactory) factory).getColumn();
            ByteBuffer fromBB = ((Term.Terminal) from).get(ProtocolVersion.V3);
            ByteBuffer toBB = ((Term.Terminal) to).get(ProtocolVersion.V3);
            builder.slice(column, isUnset(fromBB) ? CellPath.BOTTOM : CellPath.create(fromBB), isUnset(toBB) ? CellPath.TOP : CellPath.create(toBB));
        }
    };
}
Also used : ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) SimpleSelectorFactory(org.apache.cassandra.cql3.selection.SimpleSelector.SimpleSelectorFactory) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) QueryOptions(org.apache.cassandra.cql3.QueryOptions) ByteBuffer(java.nio.ByteBuffer)

Example 2 with SimpleSelectorFactory

use of org.apache.cassandra.cql3.selection.SimpleSelector.SimpleSelectorFactory in project cassandra by apache.

the class ElementsSelector method newElementFactory.

/**
 * Creates a {@code Selector.Factory} for the selection of an element of a collection.
 *
 * @param name a string representing the selection the factory is for. Something like "c[x]".
 * @param factory the {@code Selector.Factory} corresponding to the collection on which an element
 * is selected.
 * @param type the type of the collection.
 * @param key the element within the value represented by {@code factory} that is selected.
 * @return the created factory.
 */
public static Factory newElementFactory(String name, Selector.Factory factory, CollectionType<?> type, final Term key) {
    return new AbstractFactory(name, factory, type) {

        protected AbstractType<?> getReturnType() {
            return valueType(type);
        }

        public Selector newInstance(QueryOptions options) throws InvalidRequestException {
            ByteBuffer keyValue = key.bindAndGet(options);
            if (keyValue == null)
                throw new InvalidRequestException("Invalid null value for element selection on " + factory.getColumnName());
            if (keyValue == ByteBufferUtil.UNSET_BYTE_BUFFER)
                throw new InvalidRequestException("Invalid unset value for element selection on " + factory.getColumnName());
            return new ElementSelector(factory.newInstance(options), keyValue);
        }

        public boolean areAllFetchedColumnsKnown() {
            // 3) the element selected is terminal.
            return factory.areAllFetchedColumnsKnown() && (!type.isMultiCell() || !factory.isSimpleSelectorFactory() || key.isTerminal());
        }

        public void addFetchedColumns(ColumnFilter.Builder builder) {
            if (!type.isMultiCell() || !factory.isSimpleSelectorFactory()) {
                factory.addFetchedColumns(builder);
                return;
            }
            ColumnMetadata column = ((SimpleSelectorFactory) factory).getColumn();
            builder.select(column, CellPath.create(((Term.Terminal) key).get(ProtocolVersion.V3)));
        }
    };
}
Also used : ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) SimpleSelectorFactory(org.apache.cassandra.cql3.selection.SimpleSelector.SimpleSelectorFactory) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) QueryOptions(org.apache.cassandra.cql3.QueryOptions) ByteBuffer(java.nio.ByteBuffer)

Aggregations

ByteBuffer (java.nio.ByteBuffer)2 QueryOptions (org.apache.cassandra.cql3.QueryOptions)2 SimpleSelectorFactory (org.apache.cassandra.cql3.selection.SimpleSelector.SimpleSelectorFactory)2 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)2 ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)2