All Downloads are FREE. Search and download functionalities are using the official Maven repository.

org.apache.cassandra.cql3.selection.MapSelector Maven / Gradle / Ivy

Go to download

The Apache Cassandra Project develops a highly scalable second-generation distributed database, bringing together Dynamo's fully distributed design and Bigtable's ColumnFamily-based data model.

There is a newer version: 5.0-rc1
Show newest version
/*
 * 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.selection;

import java.nio.ByteBuffer;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.TreeMap;
import java.util.stream.Collectors;

import org.apache.cassandra.cql3.ColumnSpecification;
import org.apache.cassandra.cql3.Maps;
import org.apache.cassandra.cql3.QueryOptions;
import org.apache.cassandra.cql3.functions.Function;
import org.apache.cassandra.db.filter.ColumnFilter.Builder;
import org.apache.cassandra.db.marshal.AbstractType;
import org.apache.cassandra.db.marshal.MapType;
import org.apache.cassandra.exceptions.InvalidRequestException;
import org.apache.cassandra.schema.ColumnMetadata;
import org.apache.cassandra.serializers.CollectionSerializer;
import org.apache.cassandra.transport.ProtocolVersion;
import org.apache.cassandra.utils.Pair;

/**
 * Selector for literal map (e.g. {'min' : min(value), 'max' : max(value), 'count' : count(value)}).
 *
 */
final class MapSelector extends Selector
{
    /**
     * The map type.
     */
    private final MapType type;

    /**
     * The map elements
     */
    private final List> elements;

    public static Factory newFactory(final AbstractType type, final List> factories)
    {
        return new Factory()
        {
            protected String getColumnName()
            {
                return Maps.mapToString(factories, Factory::getColumnName);
            }

            protected AbstractType getReturnType()
            {
                return type;
            }

            protected final void addColumnMapping(SelectionColumnMapping mapping, ColumnSpecification resultsColumn)
            {
                SelectionColumnMapping tmpMapping = SelectionColumnMapping.newMapping();
                for (Pair entry : factories)
                {
                    entry.left.addColumnMapping(tmpMapping, resultsColumn);
                    entry.right.addColumnMapping(tmpMapping, resultsColumn);
                }

                if (tmpMapping.getMappings().get(resultsColumn).isEmpty())
                    // add a null mapping for cases where the collection is empty
                    mapping.addMapping(resultsColumn, (ColumnMetadata)null);
                else
                    // collate the mapped columns from the child factories & add those
                    mapping.addMapping(resultsColumn, tmpMapping.getMappings().values());
            }

            public Selector newInstance(final QueryOptions options)
            {
                return new MapSelector(type,
                                        factories.stream()
                                                 .map(p -> Pair.create(p.left.newInstance(options),
                                                                       p.right.newInstance(options)))
                                                 .collect(Collectors.toList()));
            }

            @Override
            public boolean isAggregateSelectorFactory()
            {
                for (Pair entry : factories)
                {
                    if (entry.left.isAggregateSelectorFactory() || entry.right.isAggregateSelectorFactory())
                        return true;
                }
                return false;
            }

            @Override
            public void addFunctionsTo(List functions)
            {
                for (Pair entry : factories)
                {
                    entry.left.addFunctionsTo(functions);
                    entry.right.addFunctionsTo(functions);
                }
            }

            @Override
            public boolean isWritetimeSelectorFactory()
            {
                for (Pair entry : factories)
                {
                    if (entry.left.isWritetimeSelectorFactory() || entry.right.isWritetimeSelectorFactory())
                        return true;
                }
                return false;
            }

            @Override
            public boolean isTTLSelectorFactory()
            {
                for (Pair entry : factories)
                {
                    if (entry.left.isTTLSelectorFactory() || entry.right.isTTLSelectorFactory())
                        return true;
                }
                return false;
            }

            @Override
            boolean areAllFetchedColumnsKnown()
            {
                for (Pair entry : factories)
                {
                    if (!entry.left.areAllFetchedColumnsKnown() || !entry.right.areAllFetchedColumnsKnown())
                        return false;
                }
                return true;
            }

            @Override
            void addFetchedColumns(Builder builder)
            {
                for (Pair entry : factories)
                {
                    entry.left.addFetchedColumns(builder);
                    entry.right.addFetchedColumns(builder);
                }
            }
        };
    }

    @Override
    public void addFetchedColumns(Builder builder)
    {
        for (int i = 0, m = elements.size(); i < m; i++)
        {
            Pair pair = elements.get(i);
            pair.left.addFetchedColumns(builder);
            pair.right.addFetchedColumns(builder);
        }
    }

    public void addInput(ProtocolVersion protocolVersion, ResultSetBuilder rs) throws InvalidRequestException
    {
        for (int i = 0, m = elements.size(); i < m; i++)
        {
            Pair pair = elements.get(i);
            pair.left.addInput(protocolVersion, rs);
            pair.right.addInput(protocolVersion, rs);
        }
    }

    public ByteBuffer getOutput(ProtocolVersion protocolVersion) throws InvalidRequestException
    {
        Map map = new TreeMap<>(type.getKeysType());
        for (int i = 0, m = elements.size(); i < m; i++)
        {
            Pair pair = elements.get(i);
            map.put(pair.left.getOutput(protocolVersion), pair.right.getOutput(protocolVersion));
        }

        List buffers = new ArrayList<>(elements.size() * 2);
        for (Map.Entry entry : map.entrySet())
        {
            buffers.add(entry.getKey());
            buffers.add(entry.getValue());
        }
        return CollectionSerializer.pack(buffers, elements.size(), protocolVersion);
    }

    public void reset()
    {
        for (int i = 0, m = elements.size(); i < m; i++)
        {
            Pair pair = elements.get(i);
            pair.left.reset();
            pair.right.reset();
        }
    }

    public AbstractType getType()
    {
        return type;
    }

    @Override
    public String toString()
    {
        return Maps.mapToString(elements);
    }

    private MapSelector(AbstractType type, List> elements)
    {
        this.type = (MapType) type;
        this.elements = elements;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy