Search in sources :

Example 1 with Library

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

the class LibraryTupleTranslator method createLibraryFromARecord.

private Library createLibraryFromARecord(ARecord libraryRecord) {
    String dataverseName = ((AString) libraryRecord.getValueByPos(MetadataRecordTypes.LIBRARY_ARECORD_DATAVERSENAME_FIELD_INDEX)).getStringValue();
    String libraryName = ((AString) libraryRecord.getValueByPos(MetadataRecordTypes.LIBRARY_ARECORD_NAME_FIELD_INDEX)).getStringValue();
    return new Library(dataverseName, libraryName);
}
Also used : AString(org.apache.asterix.om.base.AString) Library(org.apache.asterix.metadata.entities.Library) AString(org.apache.asterix.om.base.AString)

Example 2 with Library

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

the class ExternalLibraryUtils method uninstallLibrary.

/**
     * Remove the library from metadata completely.
     * TODO Currently, external libraries only include functions and adapters. we need to extend this to include:
     * 1. external data source
     * 2. data parser
     *
     * @param dataverse
     * @param libraryName
     * @return true if the library was found and removed, false otherwise
     * @throws AsterixException
     * @throws RemoteException
     * @throws ACIDException
     */
protected static boolean uninstallLibrary(String dataverse, String libraryName) throws AsterixException, RemoteException, ACIDException {
    MetadataTransactionContext mdTxnCtx = null;
    try {
        // begin transaction
        mdTxnCtx = MetadataManager.INSTANCE.beginTransaction();
        // make sure dataverse exists
        Dataverse dv = MetadataManager.INSTANCE.getDataverse(mdTxnCtx, dataverse);
        if (dv == null) {
            return false;
        }
        // make sure library exists
        Library library = MetadataManager.INSTANCE.getLibrary(mdTxnCtx, dataverse, libraryName);
        if (library == null) {
            return false;
        }
        // get dataverse functions
        List<Function> functions = MetadataManager.INSTANCE.getDataverseFunctions(mdTxnCtx, dataverse);
        for (Function function : functions) {
            // does function belong to library?
            if (function.getName().startsWith(libraryName + "#")) {
                // drop the function
                MetadataManager.INSTANCE.dropFunction(mdTxnCtx, new FunctionSignature(dataverse, function.getName(), function.getArity()));
            }
        }
        // get the dataverse adapters
        List<DatasourceAdapter> adapters = MetadataManager.INSTANCE.getDataverseAdapters(mdTxnCtx, dataverse);
        for (DatasourceAdapter adapter : adapters) {
            // belong to the library?
            if (adapter.getAdapterIdentifier().getName().startsWith(libraryName + "#")) {
                // remove adapter <! we didn't check if there are feeds which use this adapter>
                MetadataManager.INSTANCE.dropAdapter(mdTxnCtx, dataverse, adapter.getAdapterIdentifier().getName());
            }
        }
        // drop the library itself
        MetadataManager.INSTANCE.dropLibrary(mdTxnCtx, dataverse, libraryName);
        MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
    } catch (Exception e) {
        MetadataManager.INSTANCE.abortTransaction(mdTxnCtx);
        throw new AsterixException(e);
    }
    return true;
}
Also used : Function(org.apache.asterix.metadata.entities.Function) LibraryFunction(org.apache.asterix.external.library.LibraryFunction) DatasourceAdapter(org.apache.asterix.metadata.entities.DatasourceAdapter) AsterixException(org.apache.asterix.common.exceptions.AsterixException) MetadataTransactionContext(org.apache.asterix.metadata.MetadataTransactionContext) Library(org.apache.asterix.metadata.entities.Library) ExternalLibrary(org.apache.asterix.external.library.ExternalLibrary) Dataverse(org.apache.asterix.metadata.entities.Dataverse) FunctionSignature(org.apache.asterix.common.functions.FunctionSignature) AsterixException(org.apache.asterix.common.exceptions.AsterixException) ACIDException(org.apache.asterix.common.exceptions.ACIDException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException)

Example 3 with Library

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

the class MetadataNode method getDataverseLibraries.

@Override
public List<Library> getDataverseLibraries(JobId jobId, String dataverseName) throws MetadataException, RemoteException {
    try {
        ITupleReference searchKey = createTuple(dataverseName);
        LibraryTupleTranslator tupleReaderWriter = tupleTranslatorProvider.getLibraryTupleTranslator(false);
        IValueExtractor<Library> valueExtractor = new MetadataEntityValueExtractor<>(tupleReaderWriter);
        List<Library> results = new ArrayList<>();
        searchIndex(jobId, MetadataPrimaryIndexes.LIBRARY_DATASET, searchKey, valueExtractor, results);
        return results;
    } catch (HyracksDataException e) {
        throw new MetadataException(e);
    }
}
Also used : MetadataEntityValueExtractor(org.apache.asterix.metadata.valueextractors.MetadataEntityValueExtractor) ITupleReference(org.apache.hyracks.dataflow.common.data.accessors.ITupleReference) ArrayList(java.util.ArrayList) Library(org.apache.asterix.metadata.entities.Library) LibraryTupleTranslator(org.apache.asterix.metadata.entitytupletranslators.LibraryTupleTranslator) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException)

Example 4 with Library

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

the class ExternalLibraryUtils method installLibraryIfNeeded.

/**
     * Each element of a library is installed as part of a transaction. Any
     * failure in installing an element does not effect installation of other
     * libraries.
     */
protected static void installLibraryIfNeeded(String dataverse, final File libraryDir, Map<String, List<String>> uninstalledLibs) throws Exception {
    String libraryName = libraryDir.getName().trim();
    List<String> uninstalledLibsInDv = uninstalledLibs.get(dataverse);
    // was this library just un-installed?
    boolean wasUninstalled = uninstalledLibsInDv != null && uninstalledLibsInDv.contains(libraryName);
    MetadataTransactionContext mdTxnCtx = null;
    try {
        mdTxnCtx = MetadataManager.INSTANCE.beginTransaction();
        Library libraryInMetadata = MetadataManager.INSTANCE.getLibrary(mdTxnCtx, dataverse, libraryName);
        if (libraryInMetadata != null && !wasUninstalled) {
            // exists in metadata and was not un-installed, we return.
            // Another place which shows that our metadata transactions are broken
            // (we didn't call commit before!!!)
            MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
            return;
        }
        // Add library
        MetadataManager.INSTANCE.addLibrary(mdTxnCtx, new Library(dataverse, libraryName));
        if (LOGGER.isLoggable(Level.INFO)) {
            LOGGER.info("Added library " + libraryName + " to Metadata");
        }
        // Get the descriptor
        String[] libraryDescriptors = libraryDir.list((dir, name) -> name.endsWith(".xml"));
        if (libraryDescriptors == null) {
            throw new IOException("Unable to list files in directory " + libraryDir);
        }
        if (libraryDescriptors.length == 0) {
            // should be fine. library was installed but its content was not added to metadata
            MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
            return;
        } else if (libraryDescriptors.length > 1) {
            throw new IllegalStateException("More than 1 library descriptors defined");
        }
        ExternalLibrary library = getLibrary(new File(libraryDir + File.separator + libraryDescriptors[0]));
        // Get the dataverse
        Dataverse dv = MetadataManager.INSTANCE.getDataverse(mdTxnCtx, dataverse);
        if (dv == null) {
            MetadataManager.INSTANCE.addDataverse(mdTxnCtx, new Dataverse(dataverse, NonTaggedDataFormat.NON_TAGGED_DATA_FORMAT, MetadataUtil.PENDING_NO_OP));
        }
        // Add functions
        if (library.getLibraryFunctions() != null) {
            for (LibraryFunction function : library.getLibraryFunctions().getLibraryFunction()) {
                String[] fargs = function.getArguments().trim().split(",");
                List<String> args = new ArrayList<>();
                for (String arg : fargs) {
                    args.add(arg);
                }
                Function f = new Function(dataverse, libraryName + "#" + function.getName().trim(), args.size(), args, function.getReturnType().trim(), function.getDefinition().trim(), library.getLanguage().trim(), function.getFunctionType().trim(), 0);
                MetadataManager.INSTANCE.addFunction(mdTxnCtx, f);
                if (LOGGER.isLoggable(Level.INFO)) {
                    LOGGER.info("Installed function: " + libraryName + "#" + function.getName().trim());
                }
            }
        }
        if (LOGGER.isLoggable(Level.INFO)) {
            LOGGER.info("Installed functions in library :" + libraryName);
        }
        // Add adapters
        if (library.getLibraryAdapters() != null) {
            for (LibraryAdapter adapter : library.getLibraryAdapters().getLibraryAdapter()) {
                String adapterFactoryClass = adapter.getFactoryClass().trim();
                String adapterName = libraryName + "#" + adapter.getName().trim();
                AdapterIdentifier aid = new AdapterIdentifier(dataverse, adapterName);
                DatasourceAdapter dsa = new DatasourceAdapter(aid, adapterFactoryClass, IDataSourceAdapter.AdapterType.EXTERNAL);
                MetadataManager.INSTANCE.addAdapter(mdTxnCtx, dsa);
                if (LOGGER.isLoggable(Level.INFO)) {
                    LOGGER.info("Installed adapter: " + adapterName);
                }
            }
        }
        if (LOGGER.isLoggable(Level.INFO)) {
            LOGGER.info("Installed adapters in library :" + libraryName);
        }
        MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
    } catch (Exception e) {
        if (LOGGER.isLoggable(Level.SEVERE)) {
            LOGGER.log(Level.SEVERE, "Exception in installing library " + libraryName, e);
        }
        MetadataManager.INSTANCE.abortTransaction(mdTxnCtx);
    }
}
Also used : DatasourceAdapter(org.apache.asterix.metadata.entities.DatasourceAdapter) ArrayList(java.util.ArrayList) MetadataTransactionContext(org.apache.asterix.metadata.MetadataTransactionContext) IOException(java.io.IOException) Dataverse(org.apache.asterix.metadata.entities.Dataverse) LibraryFunction(org.apache.asterix.external.library.LibraryFunction) AsterixException(org.apache.asterix.common.exceptions.AsterixException) ACIDException(org.apache.asterix.common.exceptions.ACIDException) IOException(java.io.IOException) RemoteException(java.rmi.RemoteException) Function(org.apache.asterix.metadata.entities.Function) LibraryFunction(org.apache.asterix.external.library.LibraryFunction) ExternalLibrary(org.apache.asterix.external.library.ExternalLibrary) AdapterIdentifier(org.apache.asterix.external.dataset.adapter.AdapterIdentifier) Library(org.apache.asterix.metadata.entities.Library) ExternalLibrary(org.apache.asterix.external.library.ExternalLibrary) LibraryAdapter(org.apache.asterix.external.library.LibraryAdapter) File(java.io.File)

Example 5 with Library

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

the class MetadataNode method dropLibrary.

@Override
public void dropLibrary(JobId jobId, String dataverseName, String libraryName) throws MetadataException, RemoteException {
    Library library = getLibrary(jobId, dataverseName, libraryName);
    if (library == null) {
        throw new MetadataException("Cannot drop library '" + library + "' because it doesn't exist.");
    }
    try {
        // Delete entry from the 'Library' dataset.
        ITupleReference searchKey = createTuple(dataverseName, libraryName);
        // Searches the index for the tuple to be deleted. Acquires an S
        // lock on the 'Adapter' dataset.
        ITupleReference datasetTuple = getTupleToBeDeleted(jobId, MetadataPrimaryIndexes.LIBRARY_DATASET, searchKey);
        deleteTupleFromIndex(jobId, MetadataPrimaryIndexes.LIBRARY_DATASET, datasetTuple);
    // 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 library '" + libraryName, e);
        } else {
            throw new MetadataException(e);
        }
    } catch (ACIDException e) {
        throw new MetadataException(e);
    }
}
Also used : ITupleReference(org.apache.hyracks.dataflow.common.data.accessors.ITupleReference) Library(org.apache.asterix.metadata.entities.Library) HyracksDataException(org.apache.hyracks.api.exceptions.HyracksDataException) ACIDException(org.apache.asterix.common.exceptions.ACIDException)

Aggregations

Library (org.apache.asterix.metadata.entities.Library)7 ArrayList (java.util.ArrayList)3 ACIDException (org.apache.asterix.common.exceptions.ACIDException)3 HyracksDataException (org.apache.hyracks.api.exceptions.HyracksDataException)3 ITupleReference (org.apache.hyracks.dataflow.common.data.accessors.ITupleReference)3 IOException (java.io.IOException)2 RemoteException (java.rmi.RemoteException)2 AsterixException (org.apache.asterix.common.exceptions.AsterixException)2 ExternalLibrary (org.apache.asterix.external.library.ExternalLibrary)2 LibraryFunction (org.apache.asterix.external.library.LibraryFunction)2 MetadataTransactionContext (org.apache.asterix.metadata.MetadataTransactionContext)2 DatasourceAdapter (org.apache.asterix.metadata.entities.DatasourceAdapter)2 Dataverse (org.apache.asterix.metadata.entities.Dataverse)2 Function (org.apache.asterix.metadata.entities.Function)2 LibraryTupleTranslator (org.apache.asterix.metadata.entitytupletranslators.LibraryTupleTranslator)2 MetadataEntityValueExtractor (org.apache.asterix.metadata.valueextractors.MetadataEntityValueExtractor)2 File (java.io.File)1 FunctionSignature (org.apache.asterix.common.functions.FunctionSignature)1 AdapterIdentifier (org.apache.asterix.external.dataset.adapter.AdapterIdentifier)1 LibraryAdapter (org.apache.asterix.external.library.LibraryAdapter)1