Search in sources :

Example 16 with MutationCode

use of org.apache.phoenix.coprocessor.MetaDataProtocol.MutationCode in project phoenix by apache.

the class MetaDataClient method createFunction.

public MutationState createFunction(CreateFunctionStatement stmt) throws SQLException {
    boolean wasAutoCommit = connection.getAutoCommit();
    connection.rollback();
    try {
        PFunction function = new PFunction(stmt.getFunctionInfo(), stmt.isTemporary(), stmt.isReplace());
        connection.setAutoCommit(false);
        String tenantIdStr = connection.getTenantId() == null ? null : connection.getTenantId().getString();
        List<Mutation> functionData = Lists.newArrayListWithExpectedSize(function.getFunctionArguments().size() + 1);
        List<FunctionArgument> args = function.getFunctionArguments();
        try (PreparedStatement argUpsert = connection.prepareStatement(INSERT_FUNCTION_ARGUMENT)) {
            for (int i = 0; i < args.size(); i++) {
                FunctionArgument arg = args.get(i);
                addFunctionArgMutation(function.getFunctionName(), arg, argUpsert, i);
            }
            functionData.addAll(connection.getMutationState().toMutations().next().getSecond());
            connection.rollback();
        }
        try (PreparedStatement functionUpsert = connection.prepareStatement(CREATE_FUNCTION)) {
            functionUpsert.setString(1, tenantIdStr);
            functionUpsert.setString(2, function.getFunctionName());
            functionUpsert.setInt(3, function.getFunctionArguments().size());
            functionUpsert.setString(4, function.getClassName());
            functionUpsert.setString(5, function.getJarPath());
            functionUpsert.setString(6, function.getReturnType());
            functionUpsert.execute();
            functionData.addAll(connection.getMutationState().toMutations(null).next().getSecond());
            connection.rollback();
        }
        MetaDataMutationResult result = connection.getQueryServices().createFunction(functionData, function, stmt.isTemporary());
        MutationCode code = result.getMutationCode();
        switch(code) {
            case FUNCTION_ALREADY_EXISTS:
                if (!function.isReplace()) {
                    throw new FunctionAlreadyExistsException(function.getFunctionName(), result.getFunctions().get(0));
                } else {
                    connection.removeFunction(function.getTenantId(), function.getFunctionName(), result.getMutationTime());
                    addFunctionToCache(result);
                }
            case NEWER_FUNCTION_FOUND:
                // it to this connection as we can't see it.
                throw new NewerFunctionAlreadyExistsException(function.getFunctionName(), result.getFunctions().get(0));
            default:
                List<PFunction> functions = new ArrayList<PFunction>(1);
                functions.add(function);
                result = new MetaDataMutationResult(code, result.getMutationTime(), functions, true);
                if (function.isReplace()) {
                    connection.removeFunction(function.getTenantId(), function.getFunctionName(), result.getMutationTime());
                }
                addFunctionToCache(result);
        }
    } finally {
        connection.setAutoCommit(wasAutoCommit);
    }
    return new MutationState(1, 1000, connection);
}
Also used : PFunction(org.apache.phoenix.parse.PFunction) ArrayList(java.util.ArrayList) PreparedStatement(java.sql.PreparedStatement) IndexKeyConstraint(org.apache.phoenix.parse.IndexKeyConstraint) PrimaryKeyConstraint(org.apache.phoenix.parse.PrimaryKeyConstraint) ColumnDefInPkConstraint(org.apache.phoenix.parse.ColumnDefInPkConstraint) MutationCode(org.apache.phoenix.coprocessor.MetaDataProtocol.MutationCode) MutationState(org.apache.phoenix.execute.MutationState) Mutation(org.apache.hadoop.hbase.client.Mutation) FunctionArgument(org.apache.phoenix.parse.PFunction.FunctionArgument) MetaDataMutationResult(org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult)

Aggregations

MutationCode (org.apache.phoenix.coprocessor.MetaDataProtocol.MutationCode)16 MetaDataMutationResult (org.apache.phoenix.coprocessor.MetaDataProtocol.MetaDataMutationResult)15 Mutation (org.apache.hadoop.hbase.client.Mutation)13 SQLExceptionInfo (org.apache.phoenix.exception.SQLExceptionInfo)8 MutationState (org.apache.phoenix.execute.MutationState)8 PLong (org.apache.phoenix.schema.types.PLong)8 PUnsignedLong (org.apache.phoenix.schema.types.PUnsignedLong)8 PreparedStatement (java.sql.PreparedStatement)5 ColumnDefInPkConstraint (org.apache.phoenix.parse.ColumnDefInPkConstraint)5 IndexKeyConstraint (org.apache.phoenix.parse.IndexKeyConstraint)5 PrimaryKeyConstraint (org.apache.phoenix.parse.PrimaryKeyConstraint)5 IOException (java.io.IOException)4 ArrayList (java.util.ArrayList)4 Pair (org.apache.hadoop.hbase.util.Pair)4 MutationPlan (org.apache.phoenix.compile.MutationPlan)4 PostDDLCompiler (org.apache.phoenix.compile.PostDDLCompiler)4 ThreadFactoryBuilder (com.google.common.util.concurrent.ThreadFactoryBuilder)3 HashMap (java.util.HashMap)3 LinkedHashMap (java.util.LinkedHashMap)3 Delete (org.apache.hadoop.hbase.client.Delete)3