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

org.apache.cassandra.cql3.functions.UDFContextImpl 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.2
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.functions;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

import com.google.common.base.Supplier;
import com.google.common.base.Suppliers;

import com.datastax.driver.core.DataType;
import com.datastax.driver.core.TupleType;
import com.datastax.driver.core.TupleValue;
import com.datastax.driver.core.TypeCodec;
import com.datastax.driver.core.UDTValue;
import com.datastax.driver.core.UserType;
import org.apache.cassandra.config.Schema;
import org.apache.cassandra.cql3.ColumnIdentifier;
import org.apache.cassandra.db.marshal.AbstractType;
import org.apache.cassandra.schema.CQLTypeParser;
import org.apache.cassandra.schema.KeyspaceMetadata;
import org.apache.cassandra.utils.ByteBufferUtil;

/**
 * Package private implementation of {@link UDFContext}
 */
public final class UDFContextImpl implements UDFContext
{
    private final Map> byName = new HashMap<>();
    private final TypeCodec[] argCodecs;
    private final TypeCodec returnCodec;
    private final String keyspace;

    /*
        metadata can not be retrieved within the constructor as the keyspace itself may be being
        constructed and an NPE will result.
     */
    private final Supplier metadata;

    UDFContextImpl(List argNames, TypeCodec[] argCodecs, TypeCodec returnCodec, String keyspace)
    {
        for (int i = 0; i < argNames.size(); i++)
            byName.put(argNames.get(i).toString(), argCodecs[i]);
        this.argCodecs = argCodecs;
        this.returnCodec = returnCodec;
        this.keyspace = keyspace;
        this.metadata = Suppliers.memoize(() -> Schema.instance.getKSMetaData(keyspace));
    }

    public UDTValue newArgUDTValue(String argName)
    {
        return newUDTValue(codecFor(argName));
    }

    public UDTValue newArgUDTValue(int argNum)
    {
        return newUDTValue(codecFor(argNum));
    }

    public UDTValue newReturnUDTValue()
    {
        return newUDTValue(returnCodec);
    }

    public UDTValue newUDTValue(String udtName)
    {
        Optional udtType = metadata.get().types.get(ByteBufferUtil.bytes(udtName));
        DataType dataType = UDHelper.driverType(udtType.orElseThrow(
                () -> new IllegalArgumentException("No UDT named " + udtName + " in keyspace " + keyspace)
            ));
        return newUDTValue(dataType);
    }

    public TupleValue newArgTupleValue(String argName)
    {
        return newTupleValue(codecFor(argName));
    }

    public TupleValue newArgTupleValue(int argNum)
    {
        return newTupleValue(codecFor(argNum));
    }

    public TupleValue newReturnTupleValue()
    {
        return newTupleValue(returnCodec);
    }

    public TupleValue newTupleValue(String cqlDefinition)
    {
        AbstractType abstractType = CQLTypeParser.parse(keyspace, cqlDefinition, metadata.get().types);
        DataType dataType = UDHelper.driverType(abstractType);
        return newTupleValue(dataType);
    }

    private TypeCodec codecFor(int argNum)
    {
        if (argNum < 0 || argNum >= argCodecs.length)
            throw new IllegalArgumentException("Function does not declare an argument with index " + argNum);
        return argCodecs[argNum];
    }

    private TypeCodec codecFor(String argName)
    {
        TypeCodec codec = byName.get(argName);
        if (codec == null)
            throw new IllegalArgumentException("Function does not declare an argument named '" + argName + '\'');
        return codec;
    }

    private static UDTValue newUDTValue(TypeCodec codec)
    {
        DataType dataType = codec.getCqlType();
        return newUDTValue(dataType);
    }

    private static UDTValue newUDTValue(DataType dataType)
    {
        if (!(dataType instanceof UserType))
            throw new IllegalStateException("Function argument is not a UDT but a " + dataType.getName());
        UserType userType = (UserType) dataType;
        return userType.newValue();
    }

    private static TupleValue newTupleValue(TypeCodec codec)
    {
        DataType dataType = codec.getCqlType();
        return newTupleValue(dataType);
    }

    private static TupleValue newTupleValue(DataType dataType)
    {
        if (!(dataType instanceof TupleType))
            throw new IllegalStateException("Function argument is not a tuple type but a " + dataType.getName());
        TupleType tupleType = (TupleType) dataType;
        return tupleType.newValue();
    }
}