Search in sources :

Example 1 with Terminal

use of org.apache.cassandra.cql3.Term.Terminal 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)

Aggregations

ByteBuffer (java.nio.ByteBuffer)1 java.util (java.util)1 MultiItemTerminal (org.apache.cassandra.cql3.Term.MultiItemTerminal)1 Terminal (org.apache.cassandra.cql3.Term.Terminal)1 Function (org.apache.cassandra.cql3.functions.Function)1 org.apache.cassandra.db.marshal (org.apache.cassandra.db.marshal)1 ProtocolVersion (org.apache.cassandra.transport.ProtocolVersion)1