Search in sources :

Example 11 with Function

use of org.apache.asterix.metadata.entities.Function in project asterixdb by apache.

the class QueryTranslator method handleFunctionDropStatement.

protected void handleFunctionDropStatement(MetadataProvider metadataProvider, Statement stmt) throws Exception {
    FunctionDropStatement stmtDropFunction = (FunctionDropStatement) stmt;
    FunctionSignature signature = stmtDropFunction.getFunctionSignature();
    signature.setNamespace(getActiveDataverseName(signature.getNamespace()));
    MetadataTransactionContext mdTxnCtx = MetadataManager.INSTANCE.beginTransaction();
    metadataProvider.setMetadataTxnContext(mdTxnCtx);
    MetadataLockManager.INSTANCE.functionStatementBegin(metadataProvider.getLocks(), signature.getNamespace(), signature.getNamespace() + "." + signature.getName());
    try {
        Function function = MetadataManager.INSTANCE.getFunction(mdTxnCtx, signature);
        if (function == null) {
            if (!stmtDropFunction.getIfExists()) {
                throw new AlgebricksException("Unknonw function " + signature);
            }
        } else if (function.getReferenceCount() != 0) {
            throw new AlgebricksException("Function " + signature + " is being used. It cannot be dropped.");
        } else {
            MetadataManager.INSTANCE.dropFunction(mdTxnCtx, signature);
        }
        MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
    } catch (Exception e) {
        abort(e, e, mdTxnCtx);
        throw e;
    } finally {
        metadataProvider.getLocks().unlock();
    }
}
Also used : Function(org.apache.asterix.metadata.entities.Function) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) MetadataTransactionContext(org.apache.asterix.metadata.MetadataTransactionContext) FunctionDropStatement(org.apache.asterix.lang.common.statement.FunctionDropStatement) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) ACIDException(org.apache.asterix.common.exceptions.ACIDException) MetadataException(org.apache.asterix.metadata.MetadataException) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) CompilationException(org.apache.asterix.common.exceptions.CompilationException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) AsterixException(org.apache.asterix.common.exceptions.AsterixException)

Example 12 with Function

use of org.apache.asterix.metadata.entities.Function in project asterixdb by apache.

the class QueryTranslator method handleCreateFunctionStatement.

protected void handleCreateFunctionStatement(MetadataProvider metadataProvider, Statement stmt) throws Exception {
    CreateFunctionStatement cfs = (CreateFunctionStatement) stmt;
    String dataverse = getActiveDataverseName(cfs.getSignature().getNamespace());
    cfs.getSignature().setNamespace(dataverse);
    String functionName = cfs.getaAterixFunction().getName();
    MetadataTransactionContext mdTxnCtx = MetadataManager.INSTANCE.beginTransaction();
    metadataProvider.setMetadataTxnContext(mdTxnCtx);
    MetadataLockManager.INSTANCE.functionStatementBegin(metadataProvider.getLocks(), dataverse, dataverse + "." + functionName);
    try {
        Dataverse dv = MetadataManager.INSTANCE.getDataverse(mdTxnCtx, dataverse);
        if (dv == null) {
            throw new AlgebricksException("There is no dataverse with this name " + dataverse + ".");
        }
        // If the function body contains function calls, theirs reference count won't be increased.
        Function function = new Function(dataverse, functionName, cfs.getaAterixFunction().getArity(), cfs.getParamList(), Function.RETURNTYPE_VOID, cfs.getFunctionBody(), Function.LANGUAGE_AQL, FunctionKind.SCALAR.toString(), 0);
        MetadataManager.INSTANCE.addFunction(mdTxnCtx, function);
        MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
    } catch (Exception e) {
        abort(e, e, mdTxnCtx);
        throw e;
    } finally {
        metadataProvider.getLocks().unlock();
    }
}
Also used : Function(org.apache.asterix.metadata.entities.Function) CreateFunctionStatement(org.apache.asterix.lang.common.statement.CreateFunctionStatement) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) MetadataTransactionContext(org.apache.asterix.metadata.MetadataTransactionContext) Dataverse(org.apache.asterix.metadata.entities.Dataverse) ACIDException(org.apache.asterix.common.exceptions.ACIDException) MetadataException(org.apache.asterix.metadata.MetadataException) AlgebricksException(org.apache.hyracks.algebricks.common.exceptions.AlgebricksException) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) CompilationException(org.apache.asterix.common.exceptions.CompilationException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) AsterixException(org.apache.asterix.common.exceptions.AsterixException)

Example 13 with Function

use of org.apache.asterix.metadata.entities.Function in project asterixdb by apache.

the class MetadataCache method dropFunction.

public Function dropFunction(Function function) {
    synchronized (functions) {
        FunctionSignature signature = new FunctionSignature(function.getDataverseName(), function.getName(), function.getArity());
        Function fun = functions.get(signature);
        if (fun == null) {
            return null;
        }
        return functions.remove(signature);
    }
}
Also used : Function(org.apache.asterix.metadata.entities.Function) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature)

Example 14 with Function

use of org.apache.asterix.metadata.entities.Function in project asterixdb by apache.

the class MetadataCache method addFunctionIfNotExists.

public Function addFunctionIfNotExists(Function function) {
    synchronized (functions) {
        FunctionSignature signature = new FunctionSignature(function.getDataverseName(), function.getName(), function.getArity());
        Function fun = functions.get(signature);
        if (fun == null) {
            return functions.put(signature, function);
        }
        return null;
    }
}
Also used : Function(org.apache.asterix.metadata.entities.Function) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature)

Example 15 with Function

use of org.apache.asterix.metadata.entities.Function in project asterixdb by apache.

the class MetadataNode method dropDataverse.

@Override
public void dropDataverse(JobId jobId, String dataverseName) throws MetadataException, RemoteException {
    try {
        confirmDataverseCanBeDeleted(jobId, dataverseName);
        List<Dataset> dataverseDatasets;
        Dataset ds;
        dataverseDatasets = getDataverseDatasets(jobId, dataverseName);
        // Drop all datasets in this dataverse.
        for (int i = 0; i < dataverseDatasets.size(); i++) {
            ds = dataverseDatasets.get(i);
            dropDataset(jobId, dataverseName, ds.getDatasetName());
        }
        //After dropping datasets, drop datatypes
        List<Datatype> dataverseDatatypes;
        // As a side effect, acquires an S lock on the 'datatype' dataset
        // on behalf of txnId.
        dataverseDatatypes = getDataverseDatatypes(jobId, dataverseName);
        // Drop all types in this dataverse.
        for (int i = 0; i < dataverseDatatypes.size(); i++) {
            forceDropDatatype(jobId, dataverseName, dataverseDatatypes.get(i).getDatatypeName());
        }
        // As a side effect, acquires an S lock on the 'Function' dataset
        // on behalf of txnId.
        List<Function> dataverseFunctions = getDataverseFunctions(jobId, dataverseName);
        // Drop all functions in this dataverse.
        for (Function function : dataverseFunctions) {
            dropFunction(jobId, new FunctionSignature(dataverseName, function.getName(), function.getArity()));
        }
        // As a side effect, acquires an S lock on the 'Adapter' dataset
        // on behalf of txnId.
        List<DatasourceAdapter> dataverseAdapters = getDataverseAdapters(jobId, dataverseName);
        // Drop all functions in this dataverse.
        for (DatasourceAdapter adapter : dataverseAdapters) {
            dropAdapter(jobId, dataverseName, adapter.getAdapterIdentifier().getName());
        }
        List<Feed> dataverseFeeds;
        List<FeedConnection> feedConnections;
        Feed feed;
        dataverseFeeds = getDataverseFeeds(jobId, dataverseName);
        // Drop all feeds&connections in this dataverse.
        for (int i = 0; i < dataverseFeeds.size(); i++) {
            feed = dataverseFeeds.get(i);
            feedConnections = getFeedConnections(jobId, dataverseName, feed.getFeedName());
            for (FeedConnection feedConnection : feedConnections) {
                dropFeedConnection(jobId, dataverseName, feed.getFeedName(), feedConnection.getDatasetName());
            }
            dropFeed(jobId, dataverseName, feed.getFeedName());
        }
        List<FeedPolicyEntity> feedPolicies = getDataversePolicies(jobId, dataverseName);
        if (feedPolicies != null && feedPolicies.size() > 0) {
            // Drop all feed ingestion policies in this dataverse.
            for (FeedPolicyEntity feedPolicy : feedPolicies) {
                dropFeedPolicy(jobId, dataverseName, feedPolicy.getPolicyName());
            }
        }
        // Delete the dataverse entry from the 'dataverse' dataset.
        ITupleReference searchKey = createTuple(dataverseName);
        // As a side effect, acquires an S lock on the 'dataverse' dataset
        // on behalf of txnId.
        ITupleReference tuple = getTupleToBeDeleted(jobId, MetadataPrimaryIndexes.DATAVERSE_DATASET, searchKey);
        deleteTupleFromIndex(jobId, MetadataPrimaryIndexes.DATAVERSE_DATASET, tuple);
    // TODO: Change this to be a BTree specific exception, e.g.,
    // BTreeKeyDoesNotExistException.
    } catch (HyracksDataException e) {
        if (e.getComponent().equals(ErrorCode.HYRACKS) && e.getErrorCode() == ErrorCode.UPDATE_OR_DELETE_NON_EXISTENT_KEY) {
            throw new MetadataException("Cannot drop dataverse '" + dataverseName + "' because it doesn't exist.", e);
        } else {
            throw new MetadataException(e);
        }
    } catch (ACIDException e) {
        throw new MetadataException(e);
    }
}
Also used : DatasourceAdapter(org.apache.asterix.metadata.entities.DatasourceAdapter) FeedConnection(org.apache.asterix.metadata.entities.FeedConnection) ExtensionMetadataDataset(org.apache.asterix.metadata.api.ExtensionMetadataDataset) Dataset(org.apache.asterix.metadata.entities.Dataset) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) Datatype(org.apache.asterix.metadata.entities.Datatype) ACIDException(org.apache.asterix.common.exceptions.ACIDException) Function(org.apache.asterix.metadata.entities.Function) FeedPolicyEntity(org.apache.asterix.metadata.entities.FeedPolicyEntity) ITupleReference(org.apache.hyracks.dataflow.common.data.accessors.ITupleReference) Feed(org.apache.asterix.metadata.entities.Feed)

Aggregations

Function (org.apache.asterix.metadata.entities.Function)18 FunctionSignature (org.apache.asterix.common.functions.FunctionSignature)9 ACIDException (org.apache.asterix.common.exceptions.ACIDException)8 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)8 RemoteException (java.rmi.RemoteException)7 IOException (java.io.IOException)6 AsterixException (org.apache.asterix.common.exceptions.AsterixException)6 CompilationException (org.apache.asterix.common.exceptions.CompilationException)6 MetadataException (org.apache.asterix.metadata.MetadataException)6 MetadataTransactionContext (org.apache.asterix.metadata.MetadataTransactionContext)6 ArrayList (java.util.ArrayList)4 AlgebricksException (org.apache.hyracks.algebricks.common.exceptions.AlgebricksException)4 ITupleReference (org.apache.hyracks.dataflow.common.data.accessors.ITupleReference)4 EntityId (org.apache.asterix.active.EntityId)3 DatasourceAdapter (org.apache.asterix.metadata.entities.DatasourceAdapter)3 Dataverse (org.apache.asterix.metadata.entities.Dataverse)3 Feed (org.apache.asterix.metadata.entities.Feed)3 FeedConnection (org.apache.asterix.metadata.entities.FeedConnection)3 ActiveJobNotificationHandler (org.apache.asterix.active.ActiveJobNotificationHandler)2 ActiveLifecycleListener (org.apache.asterix.active.ActiveLifecycleListener)2