Search in sources :

Example 1 with Factory

use of org.apache.cassandra.cql3.selection.Selector.Factory in project cassandra by apache.

the class SystemKeyspace method getViewBuildStatus.

public static Map<Range<Token>, Pair<Token, Long>> getViewBuildStatus(String ksname, String viewName) {
    String req = "SELECT start_token, end_token, last_token, keys_built FROM system.%s WHERE keyspace_name = ? AND view_name = ?";
    Token.TokenFactory factory = ViewBuildsInProgress.partitioner.getTokenFactory();
    UntypedResultSet rs = executeInternal(format(req, VIEW_BUILDS_IN_PROGRESS), ksname, viewName);
    if (rs == null || rs.isEmpty())
        return Collections.emptyMap();
    Map<Range<Token>, Pair<Token, Long>> status = new HashMap<>();
    for (UntypedResultSet.Row row : rs) {
        Token start = factory.fromString(row.getString("start_token"));
        Token end = factory.fromString(row.getString("end_token"));
        Range<Token> range = new Range<>(start, end);
        Token lastToken = row.has("last_token") ? factory.fromString(row.getString("last_token")) : null;
        long keysBuilt = row.has("keys_built") ? row.getLong("keys_built") : 0;
        status.put(range, Pair.create(lastToken, keysBuilt));
    }
    return status;
}
Also used : UntypedResultSet(org.apache.cassandra.cql3.UntypedResultSet)

Example 2 with Factory

use of org.apache.cassandra.cql3.selection.Selector.Factory 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 3 with Factory

use of org.apache.cassandra.cql3.selection.Selector.Factory in project cassandra by apache.

the class AbstractFunctionSelector method newFactory.

public static Factory newFactory(final Function fun, final SelectorFactories factories) throws InvalidRequestException {
    if (fun.isAggregate()) {
        if (factories.doesAggregation())
            throw new InvalidRequestException("aggregate functions cannot be used as arguments of aggregate functions");
    }
    return new Factory() {

        protected String getColumnName() {
            return fun.columnName(factories.getColumnNames());
        }

        protected AbstractType<?> getReturnType() {
            return fun.returnType();
        }

        protected void addColumnMapping(SelectionColumnMapping mapping, ColumnSpecification resultsColumn) {
            SelectionColumnMapping tmpMapping = SelectionColumnMapping.newMapping();
            for (Factory factory : factories) factory.addColumnMapping(tmpMapping, resultsColumn);
            if (tmpMapping.getMappings().get(resultsColumn).isEmpty())
                // add a null mapping for cases where there are no
                // further selectors, such as no-arg functions and count
                mapping.addMapping(resultsColumn, (ColumnMetadata) null);
            else
                // collate the mapped columns from the child factories & add those
                mapping.addMapping(resultsColumn, tmpMapping.getMappings().values());
        }

        public void addFunctionsTo(List<Function> functions) {
            fun.addFunctionsTo(functions);
            factories.addFunctionsTo(functions);
        }

        public Selector newInstance(QueryOptions options) throws InvalidRequestException {
            return fun.isAggregate() ? new AggregateFunctionSelector(fun, factories.newInstances(options)) : new ScalarFunctionSelector(fun, factories.newInstances(options));
        }

        public boolean isWritetimeSelectorFactory() {
            return factories.containsWritetimeSelectorFactory();
        }

        public boolean isTTLSelectorFactory() {
            return factories.containsTTLSelectorFactory();
        }

        public boolean isAggregateSelectorFactory() {
            return fun.isAggregate() || factories.doesAggregation();
        }

        @Override
        public boolean areAllFetchedColumnsKnown() {
            return Iterables.all(factories, f -> f.areAllFetchedColumnsKnown());
        }

        @Override
        public void addFetchedColumns(ColumnFilter.Builder builder) {
            for (Selector.Factory factory : factories) factory.addFetchedColumns(builder);
        }
    };
}
Also used : ColumnSpecification(org.apache.cassandra.cql3.ColumnSpecification) ColumnMetadata(org.apache.cassandra.schema.ColumnMetadata) StrBuilder(org.apache.commons.lang3.text.StrBuilder) InvalidRequestException(org.apache.cassandra.exceptions.InvalidRequestException) List(java.util.List) QueryOptions(org.apache.cassandra.cql3.QueryOptions)

Example 4 with Factory

use of org.apache.cassandra.cql3.selection.Selector.Factory 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)

Example 5 with Factory

use of org.apache.cassandra.cql3.selection.Selector.Factory 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

QueryOptions (org.apache.cassandra.cql3.QueryOptions)3 InvalidRequestException (org.apache.cassandra.exceptions.InvalidRequestException)3 ColumnMetadata (org.apache.cassandra.schema.ColumnMetadata)3 ByteBuffer (java.nio.ByteBuffer)2 ColumnSpecification (org.apache.cassandra.cql3.ColumnSpecification)2 SimpleSelectorFactory (org.apache.cassandra.cql3.selection.SimpleSelector.SimpleSelectorFactory)2 List (java.util.List)1 UntypedResultSet (org.apache.cassandra.cql3.UntypedResultSet)1 Factory (org.apache.cassandra.cql3.selection.Selector.Factory)1 TableMetadata (org.apache.cassandra.schema.TableMetadata)1 StrBuilder (org.apache.commons.lang3.text.StrBuilder)1