You are viewing a plain text version of this content. The canonical link for it is here.
Posted to notifications@asterixdb.apache.org by AsterixDB Code Review <do...@asterix-gerrit.ics.uci.edu> on 2023/10/20 22:40:46 UTC

Change in asterixdb[master]: [ASTERIXDB-3259][MTD] update MetadataCache's addDatabaseIfNotExists

From Ali Alsuliman <al...@gmail.com>:

Ali Alsuliman has uploaded this change for review. ( https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/17870 )


Change subject: [ASTERIXDB-3259][MTD] update MetadataCache's addDatabaseIfNotExists
......................................................................

[ASTERIXDB-3259][MTD] update MetadataCache's addDatabaseIfNotExists

- user model changes: no
- storage format changes: no
- interface changes: no

Details:
update MetadataCache's addDatabaseIfNotExists to be
similar to addDataverseIfNotExists

- MetadataNode:
  add adapter check when confirming database drop.
- fix error used for dropping unknown database.
- minor refactoring in drop database in QueryTranslator.

Change-Id: I4a55c8b6d3606046183305891a8be7fed362c66a
---
M asterixdb/asterix-common/src/main/java/org/apache/asterix/common/exceptions/ErrorCode.java
M asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/QueryTranslator.java
M asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/bootstrap/MetadataBootstrap.java
M asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/MetadataManager.java
M asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/MetadataNode.java
M asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/MetadataCache.java
M asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/entities/DependencyKind.java
M asterixdb/asterix-common/src/main/resources/asx_errormsg/en.properties
8 files changed, 465 insertions(+), 386 deletions(-)



  git pull ssh://asterix-gerrit.ics.uci.edu:29418/asterixdb refs/changes/70/17870/1

diff --git a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/QueryTranslator.java b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/QueryTranslator.java
index f43fbde..917d752 100644
--- a/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/QueryTranslator.java
+++ b/asterixdb/asterix-app/src/main/java/org/apache/asterix/app/translator/QueryTranslator.java
@@ -650,14 +650,13 @@
         }
     }
 
-    protected boolean doCreateDatabaseStatement(MetadataProvider metadataProvider,
-            CreateDatabaseStatement stmtCreateDatabase, IRequestParameters requestParameters) throws Exception {
+    protected boolean doCreateDatabaseStatement(MetadataProvider mdProvider, CreateDatabaseStatement stmtCreateDatabase,
+            IRequestParameters requestParameters) throws Exception {
         MetadataTransactionContext mdTxnCtx = MetadataManager.INSTANCE.beginTransaction();
-        metadataProvider.setMetadataTxnContext(mdTxnCtx);
+        mdProvider.setMetadataTxnContext(mdTxnCtx);
         try {
             String databaseName = stmtCreateDatabase.getDatabaseName().getValue();
-            Database database =
-                    MetadataManager.INSTANCE.getDatabase(metadataProvider.getMetadataTxnContext(), databaseName);
+            Database database = MetadataManager.INSTANCE.getDatabase(mdTxnCtx, databaseName);
             if (database != null) {
                 if (stmtCreateDatabase.ifNotExists()) {
                     MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
@@ -667,7 +666,7 @@
                             databaseName);
                 }
             }
-            MetadataManager.INSTANCE.addDatabase(metadataProvider.getMetadataTxnContext(),
+            MetadataManager.INSTANCE.addDatabase(mdTxnCtx,
                     new Database(databaseName, false, MetadataUtil.PENDING_NO_OP));
             MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
             return true;
@@ -1956,17 +1955,16 @@
         }
     }
 
-    protected boolean doDropDatabase(DatabaseDropStatement stmtDropDatabase, MetadataProvider metadataProvider,
+    protected boolean doDropDatabase(DatabaseDropStatement stmtDropDatabase, MetadataProvider mdProvider,
             IHyracksClientConnection hcc, IRequestParameters requestParameters) throws Exception {
-        SourceLocation sourceLoc = stmtDropDatabase.getSourceLocation();
+        SourceLocation srcLoc = stmtDropDatabase.getSourceLocation();
         String databaseName = stmtDropDatabase.getDatabaseName().getValue();
         ProgressState progress = ProgressState.NO_PROGRESS;
         MetadataTransactionContext mdTxnCtx = MetadataManager.INSTANCE.beginTransaction();
         boolean bActiveTxn = true;
-        metadataProvider.setMetadataTxnContext(mdTxnCtx);
-        List<FeedEventsListener> feedsToStop = new ArrayList<>();
-        List<JobSpecification> jobsToExecute = new ArrayList<>();
-        //TODO(DB): resolve database directory
+        mdProvider.setMetadataTxnContext(mdTxnCtx);
+        List<FeedEventsListener> stopFeeds = new ArrayList<>();
+        List<JobSpecification> dropJobs = new ArrayList<>();
         try {
             Database database = MetadataManager.INSTANCE.getDatabase(mdTxnCtx, databaseName);
             if (database == null) {
@@ -1974,57 +1972,14 @@
                     MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
                     return false;
                 } else {
-                    throw new CompilationException(ErrorCode.UNKNOWN_DATAVERSE, sourceLoc, databaseName);
+                    throw new CompilationException(ErrorCode.UNKNOWN_DATABASE, srcLoc, databaseName);
                 }
             }
 
-            validateDatabaseStateBeforeDrop(metadataProvider, database, sourceLoc);
+            validateDatabaseStateBeforeDrop(mdProvider, database, srcLoc);
 
-            // #. prepare jobs which will drop corresponding feed storage
-            ActiveNotificationHandler activeEventHandler =
-                    (ActiveNotificationHandler) appCtx.getActiveNotificationHandler();
-            IActiveEntityEventsListener[] activeListeners = activeEventHandler.getEventListeners();
-            for (IActiveEntityEventsListener listener : activeListeners) {
-                EntityId activeEntityId = listener.getEntityId();
-                if (activeEntityId.getExtensionName().equals(Feed.EXTENSION_NAME)
-                        && activeEntityId.getDatabaseName().equals(databaseName)) {
-                    FeedEventsListener feedListener = (FeedEventsListener) listener;
-                    feedsToStop.add(feedListener);
-                    jobsToExecute
-                            .add(FeedOperations.buildRemoveFeedStorageJob(metadataProvider, feedListener.getFeed()));
-                }
-            }
-
-            // #. prepare jobs which will drop corresponding datasets with indexes
-            List<Dataset> datasets = MetadataManager.INSTANCE.getDatabaseDatasets(mdTxnCtx, databaseName);
-            for (Dataset dataset : datasets) {
-                String datasetName = dataset.getDatasetName();
-                DatasetType dsType = dataset.getDatasetType();
-                switch (dsType) {
-                    case INTERNAL:
-                        List<Index> indexes = MetadataManager.INSTANCE.getDatasetIndexes(mdTxnCtx, databaseName,
-                                dataset.getDataverseName(), datasetName);
-                        for (Index index : indexes) {
-                            jobsToExecute
-                                    .add(IndexUtil.buildDropIndexJobSpec(index, metadataProvider, dataset, sourceLoc));
-                        }
-                        break;
-                    case EXTERNAL:
-                    case VIEW:
-                        break;
-                }
-            }
-
-            // #. prepare jobs which will drop corresponding libraries
-            //TODO(DB): library database
-            List<Library> libraries = MetadataManager.INSTANCE.getDatabaseLibraries(mdTxnCtx, databaseName);
-            for (Library library : libraries) {
-                jobsToExecute.add(ExternalLibraryJobUtils.buildDropLibraryJobSpec(library.getDataverseName(),
-                        library.getName(), metadataProvider));
-            }
-
-            // #. prepare jobs which will drop the database
-            jobsToExecute.add(DataverseUtil.dropDatabaseJobSpec(databaseName, metadataProvider));
+            List<Dataset> datasets =
+                    prepareDatabaseDropJobs(mdProvider, srcLoc, databaseName, mdTxnCtx, stopFeeds, dropJobs);
 
             // #. mark PendingDropOp on the database record by
             // first, deleting the database record from the 'Database' collection
@@ -2038,20 +1993,11 @@
             bActiveTxn = false;
             progress = ProgressState.ADDED_PENDINGOP_RECORD_TO_METADATA;
 
-            for (FeedEventsListener feedListener : feedsToStop) {
-                if (feedListener.getState() != ActivityState.STOPPED) {
-                    feedListener.stop(metadataProvider);
-                }
-                feedListener.unregister();
-            }
-
-            for (JobSpecification jobSpec : jobsToExecute) {
-                runJob(hcc, jobSpec);
-            }
+            runDropJobs(mdProvider, hcc, stopFeeds, dropJobs);
 
             mdTxnCtx = MetadataManager.INSTANCE.beginTransaction();
             bActiveTxn = true;
-            metadataProvider.setMetadataTxnContext(mdTxnCtx);
+            mdProvider.setMetadataTxnContext(mdTxnCtx);
 
             // #. finally, delete the database
             MetadataManager.INSTANCE.dropDatabase(mdTxnCtx, databaseName);
@@ -2059,18 +2005,17 @@
             // drop all node groups that no longer needed
             for (Dataset dataset : datasets) {
                 String nodeGroup = dataset.getNodeGroupName();
-                lockManager.acquireNodeGroupWriteLock(metadataProvider.getLocks(), nodeGroup);
+                lockManager.acquireNodeGroupWriteLock(mdProvider.getLocks(), nodeGroup);
                 if (MetadataManager.INSTANCE.getNodegroup(mdTxnCtx, nodeGroup) != null) {
                     MetadataManager.INSTANCE.dropNodegroup(mdTxnCtx, nodeGroup, true);
                 }
             }
 
-            //TODO(DB): switch active database to the DEFAULT if the dropped database is the currently active one
             if (activeNamespace.getDatabaseName().equals(databaseName)) {
                 activeNamespace = MetadataBuiltinEntities.DEFAULT_NAMESPACE;
             }
 
-            validateDatasetsStateAfterNamespaceDrop(metadataProvider, mdTxnCtx, datasets);
+            validateDatasetsStateAfterNamespaceDrop(mdProvider, mdTxnCtx, datasets);
 
             MetadataManager.INSTANCE.commitTransaction(mdTxnCtx);
             return true;
@@ -2087,7 +2032,7 @@
                 // #. execute compensation operations
                 // remove the all artifacts in NC
                 try {
-                    for (JobSpecification jobSpec : jobsToExecute) {
+                    for (JobSpecification jobSpec : dropJobs) {
                         runJob(hcc, jobSpec);
                     }
                 } catch (Exception e2) {
@@ -2235,16 +2180,7 @@
             bActiveTxn = false;
             progress = ProgressState.ADDED_PENDINGOP_RECORD_TO_METADATA;
 
-            for (FeedEventsListener feedListener : feedsToStop) {
-                if (feedListener.getState() != ActivityState.STOPPED) {
-                    feedListener.stop(metadataProvider);
-                }
-                feedListener.unregister();
-            }
-
-            for (JobSpecification jobSpec : jobsToExecute) {
-                runJob(hcc, jobSpec);
-            }
+            runDropJobs(metadataProvider, hcc, feedsToStop, jobsToExecute);
 
             mdTxnCtx = MetadataManager.INSTANCE.beginTransaction();
             bActiveTxn = true;
@@ -5571,6 +5507,82 @@
         ExternalDataUtils.validateAdapterSpecificProperties(configuration, srcLoc, warningCollector, appCtx);
     }
 
+    private List<Dataset> prepareDatabaseDropJobs(MetadataProvider metadataProvider, SourceLocation sourceLoc,
+            String databaseName, MetadataTransactionContext mdTxnCtx, List<FeedEventsListener> feedsToStop,
+            List<JobSpecification> jobsToExecute) throws AlgebricksException {
+        // #. prepare jobs which will drop corresponding feed storage
+        addFeedDropJob(metadataProvider, databaseName, feedsToStop, jobsToExecute);
+
+        // #. prepare jobs which will drop corresponding datasets with indexes
+        List<Dataset> datasets = addDatasetDropJob(metadataProvider, sourceLoc, databaseName, mdTxnCtx, jobsToExecute);
+
+        // #. prepare jobs which will drop corresponding libraries
+        //TODO(DB): library database
+        List<Library> libraries = MetadataManager.INSTANCE.getDatabaseLibraries(mdTxnCtx, databaseName);
+        for (Library library : libraries) {
+            jobsToExecute.add(ExternalLibraryJobUtils.buildDropLibraryJobSpec(library.getDataverseName(),
+                    library.getName(), metadataProvider));
+        }
+
+        // #. prepare jobs which will drop the database
+        jobsToExecute.add(DataverseUtil.dropDatabaseJobSpec(databaseName, metadataProvider));
+
+        return datasets;
+    }
+
+    private static List<Dataset> addDatasetDropJob(MetadataProvider metadataProvider, SourceLocation sourceLoc,
+            String databaseName, MetadataTransactionContext mdTxnCtx, List<JobSpecification> jobsToExecute)
+            throws AlgebricksException {
+        List<Dataset> datasets = MetadataManager.INSTANCE.getDatabaseDatasets(mdTxnCtx, databaseName);
+        for (Dataset dataset : datasets) {
+            String datasetName = dataset.getDatasetName();
+            DatasetType dsType = dataset.getDatasetType();
+            switch (dsType) {
+                case INTERNAL:
+                    List<Index> indexes = MetadataManager.INSTANCE.getDatasetIndexes(mdTxnCtx, databaseName,
+                            dataset.getDataverseName(), datasetName);
+                    for (Index index : indexes) {
+                        jobsToExecute.add(IndexUtil.buildDropIndexJobSpec(index, metadataProvider, dataset, sourceLoc));
+                    }
+                    break;
+                case EXTERNAL:
+                case VIEW:
+                    break;
+            }
+        }
+        return datasets;
+    }
+
+    private void addFeedDropJob(MetadataProvider metadataProvider, String databaseName,
+            List<FeedEventsListener> feedsToStop, List<JobSpecification> jobsToExecute) throws AlgebricksException {
+        ActiveNotificationHandler activeEventHandler =
+                (ActiveNotificationHandler) appCtx.getActiveNotificationHandler();
+        IActiveEntityEventsListener[] activeListeners = activeEventHandler.getEventListeners();
+        for (IActiveEntityEventsListener listener : activeListeners) {
+            EntityId activeEntityId = listener.getEntityId();
+            if (activeEntityId.getExtensionName().equals(Feed.EXTENSION_NAME)
+                    && activeEntityId.getDatabaseName().equals(databaseName)) {
+                FeedEventsListener feedListener = (FeedEventsListener) listener;
+                feedsToStop.add(feedListener);
+                jobsToExecute.add(FeedOperations.buildRemoveFeedStorageJob(metadataProvider, feedListener.getFeed()));
+            }
+        }
+    }
+
+    private void runDropJobs(MetadataProvider mdProvider, IHyracksClientConnection hcc,
+            List<FeedEventsListener> feedsToStop, List<JobSpecification> jobsToExecute) throws Exception {
+        for (FeedEventsListener feedListener : feedsToStop) {
+            if (feedListener.getState() != ActivityState.STOPPED) {
+                feedListener.stop(mdProvider);
+            }
+            feedListener.unregister();
+        }
+
+        for (JobSpecification jobSpec : jobsToExecute) {
+            runJob(hcc, jobSpec);
+        }
+    }
+
     protected enum CreateResult {
         NOOP,
         CREATED,
diff --git a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/exceptions/ErrorCode.java b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/exceptions/ErrorCode.java
index 83418a4..9ac8513 100644
--- a/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/exceptions/ErrorCode.java
+++ b/asterixdb/asterix-common/src/main/java/org/apache/asterix/common/exceptions/ErrorCode.java
@@ -281,6 +281,7 @@
     ILLEGAL_DML_OPERATION(1184),
     UNKNOWN_DATABASE(1185),
     DATABASE_EXISTS(1186),
+    CANNOT_DROP_DATABASE_DEPENDENT_EXISTS(1187),
 
     // Feed errors
     DATAFLOW_ILLEGAL_STATE(3001),
diff --git a/asterixdb/asterix-common/src/main/resources/asx_errormsg/en.properties b/asterixdb/asterix-common/src/main/resources/asx_errormsg/en.properties
index ab77067..122bff6 100644
--- a/asterixdb/asterix-common/src/main/resources/asx_errormsg/en.properties
+++ b/asterixdb/asterix-common/src/main/resources/asx_errormsg/en.properties
@@ -283,6 +283,7 @@
 1184 = Compilation error: %1$s: %2$s dataset is not supported on datasets with meta records
 1185 = Cannot find database with name %1$s
 1186 = A database with this name %1$s already exists
+1187 = Cannot drop database: %1$s %2$s being used by %3$s %4$s
 
 # Feed Errors
 3001 = Illegal state.
diff --git a/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/MetadataCache.java b/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/MetadataCache.java
index 4c6ac34..3484cc1 100644
--- a/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/MetadataCache.java
+++ b/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/MetadataCache.java
@@ -161,11 +161,21 @@
 
     public Database addDatabaseIfNotExists(Database database) {
         synchronized (databases) {
-            String databaseName = database.getDatabaseName();
-            if (!databases.containsKey(databaseName)) {
-                return databases.put(databaseName, database);
+            synchronized (dataverses) {
+                synchronized (datasets) {
+                    synchronized (datatypes) {
+                        String databaseName = database.getDatabaseName();
+                        if (!databases.containsKey(databaseName)) {
+                            dataverses.put(databaseName, new HashMap<>());
+                            datasets.put(databaseName, new HashMap<>());
+                            datatypes.put(databaseName, new HashMap<>());
+                            adapters.put(databaseName, new HashMap<>());
+                            return databases.put(databaseName, database);
+                        }
+                        return null;
+                    }
+                }
             }
-            return null;
         }
     }
 
@@ -259,21 +269,6 @@
         }
     }
 
-    public CompactionPolicy dropCompactionPolicy(CompactionPolicy compactionPolicy) {
-        synchronized (compactionPolicies) {
-            Map<DataverseName, Map<String, CompactionPolicy>> databaseDataverses =
-                    compactionPolicies.get(compactionPolicy.getDatabaseName());
-            if (databaseDataverses == null) {
-                return null;
-            }
-            Map<String, CompactionPolicy> p = databaseDataverses.get(compactionPolicy.getDataverseName());
-            if (p != null && p.get(compactionPolicy.getPolicyName()) != null) {
-                return p.remove(compactionPolicy.getPolicyName());
-            }
-            return null;
-        }
-    }
-
     public Database dropDatabase(Database database) {
         synchronized (databases) {
             synchronized (dataverses) {
@@ -484,6 +479,21 @@
         }
     }
 
+    public CompactionPolicy dropCompactionPolicy(CompactionPolicy compactionPolicy) {
+        synchronized (compactionPolicies) {
+            Map<DataverseName, Map<String, CompactionPolicy>> databaseDataverses =
+                    compactionPolicies.get(compactionPolicy.getDatabaseName());
+            if (databaseDataverses == null) {
+                return null;
+            }
+            Map<String, CompactionPolicy> p = databaseDataverses.get(compactionPolicy.getDataverseName());
+            if (p != null && p.get(compactionPolicy.getPolicyName()) != null) {
+                return p.remove(compactionPolicy.getPolicyName());
+            }
+            return null;
+        }
+    }
+
     public NodeGroup dropNodeGroup(NodeGroup nodeGroup) {
         synchronized (nodeGroups) {
             return nodeGroups.remove(nodeGroup.getNodeGroupName());
@@ -620,22 +630,6 @@
         }
     }
 
-    protected void doOperation(MetadataLogicalOperation op) {
-        if (op.isAdd) {
-            op.entity.addToCache(this);
-        } else {
-            op.entity.dropFromCache(this);
-        }
-    }
-
-    protected void undoOperation(MetadataLogicalOperation op) {
-        if (!op.isAdd) {
-            op.entity.addToCache(this);
-        } else {
-            op.entity.dropFromCache(this);
-        }
-    }
-
     public Function addFunctionIfNotExists(Function function) {
         synchronized (functions) {
             FunctionSignature signature = new FunctionSignature(function.getDatabaseName(), function.getDataverseName(),
@@ -938,6 +932,22 @@
         return null;
     }
 
+    protected void doOperation(MetadataLogicalOperation op) {
+        if (op.isAdd) {
+            op.entity.addToCache(this);
+        } else {
+            op.entity.dropFromCache(this);
+        }
+    }
+
+    protected void undoOperation(MetadataLogicalOperation op) {
+        if (!op.isAdd) {
+            op.entity.addToCache(this);
+        } else {
+            op.entity.dropFromCache(this);
+        }
+    }
+
     /**
      * Represents a logical operation against the metadata.
      */
diff --git a/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/MetadataManager.java b/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/MetadataManager.java
index 2c71084..b5abdf0 100644
--- a/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/MetadataManager.java
+++ b/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/MetadataManager.java
@@ -687,6 +687,7 @@
             // in the cache.
             return null;
         }
+        //TODO(DB): review this and other similar ones
         if (ctx.getDataverse(functionSignature.getDatabaseName(), functionSignature.getDataverseName()) != null) {
             // This transaction has dropped and subsequently created the same
             // dataverse.
diff --git a/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/MetadataNode.java b/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/MetadataNode.java
index 0571eff..7d85568 100644
--- a/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/MetadataNode.java
+++ b/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/MetadataNode.java
@@ -20,6 +20,41 @@
 package org.apache.asterix.metadata;
 
 import static org.apache.asterix.common.api.IIdentifierMapper.Modifier.PLURAL;
+import static org.apache.asterix.common.exceptions.ErrorCode.ADAPTER_EXISTS;
+import static org.apache.asterix.common.exceptions.ErrorCode.CANNOT_DROP_DATABASE_DEPENDENT_EXISTS;
+import static org.apache.asterix.common.exceptions.ErrorCode.CANNOT_DROP_DATAVERSE_DEPENDENT_EXISTS;
+import static org.apache.asterix.common.exceptions.ErrorCode.CANNOT_DROP_OBJECT_DEPENDENT_EXISTS;
+import static org.apache.asterix.common.exceptions.ErrorCode.COMPACTION_POLICY_EXISTS;
+import static org.apache.asterix.common.exceptions.ErrorCode.DATABASE_EXISTS;
+import static org.apache.asterix.common.exceptions.ErrorCode.DATASET_EXISTS;
+import static org.apache.asterix.common.exceptions.ErrorCode.DATAVERSE_EXISTS;
+import static org.apache.asterix.common.exceptions.ErrorCode.EXTERNAL_FILE_EXISTS;
+import static org.apache.asterix.common.exceptions.ErrorCode.FEED_CONNECTION_EXISTS;
+import static org.apache.asterix.common.exceptions.ErrorCode.FEED_EXISTS;
+import static org.apache.asterix.common.exceptions.ErrorCode.FEED_POLICY_EXISTS;
+import static org.apache.asterix.common.exceptions.ErrorCode.FULL_TEXT_DEFAULT_CONFIG_CANNOT_BE_DELETED_OR_CREATED;
+import static org.apache.asterix.common.exceptions.ErrorCode.FUNCTION_EXISTS;
+import static org.apache.asterix.common.exceptions.ErrorCode.INDEX_EXISTS;
+import static org.apache.asterix.common.exceptions.ErrorCode.LIBRARY_EXISTS;
+import static org.apache.asterix.common.exceptions.ErrorCode.METADATA_ERROR;
+import static org.apache.asterix.common.exceptions.ErrorCode.NODEGROUP_EXISTS;
+import static org.apache.asterix.common.exceptions.ErrorCode.NODE_EXISTS;
+import static org.apache.asterix.common.exceptions.ErrorCode.SYNONYM_EXISTS;
+import static org.apache.asterix.common.exceptions.ErrorCode.TYPE_EXISTS;
+import static org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_ADAPTER;
+import static org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_DATABASE;
+import static org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_DATASET_IN_DATAVERSE;
+import static org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_DATAVERSE;
+import static org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_EXTERNAL_FILE;
+import static org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_FEED;
+import static org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_FEED_CONNECTION;
+import static org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_FEED_POLICY;
+import static org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_FUNCTION;
+import static org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_INDEX;
+import static org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_LIBRARY;
+import static org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_NODEGROUP;
+import static org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_SYNONYM;
+import static org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_TYPE;
 import static org.apache.asterix.common.utils.IdentifierUtil.dataset;
 
 import java.rmi.RemoteException;
@@ -347,8 +382,7 @@
             ExtensionMetadataDatasetId datasetId) throws AlgebricksException {
         ExtensionMetadataDataset<T> index = (ExtensionMetadataDataset<T>) extensionDatasets.get(datasetId);
         if (index == null) {
-            throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.METADATA_ERROR,
-                    "Metadata Extension Index: " + datasetId + " was not found");
+            throw new AsterixException(METADATA_ERROR, "Metadata Extension Index: " + datasetId + " was not found");
         }
         return index;
     }
@@ -364,10 +398,9 @@
             insertTupleIntoIndex(txnId, mdIndexesProvider.getDatabaseEntity().getIndex(), tuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.DUPLICATE_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.DATABASE_EXISTS, e,
-                        database.getDatabaseName());
+                throw new AsterixException(DATABASE_EXISTS, e, database.getDatabaseName());
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -380,10 +413,10 @@
             insertTupleIntoIndex(txnId, mdIndexesProvider.getDataverseEntity().getIndex(), tuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.DUPLICATE_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.DATAVERSE_EXISTS, e,
-                        dataverse.getDataverseName());
+                //TODO(DB): consider adding the database name to the error message?
+                throw new AsterixException(DATAVERSE_EXISTS, e, dataverse.getDataverseName());
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -406,10 +439,9 @@
             }
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.DUPLICATE_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.DATASET_EXISTS, e,
-                        dataset.getDatasetName(), dataset.getDataverseName());
+                throw new AsterixException(DATASET_EXISTS, e, dataset.getDatasetName(), dataset.getDataverseName());
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -422,10 +454,9 @@
             insertTupleIntoIndex(txnId, mdIndexesProvider.getIndexEntity().getIndex(), tuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.DUPLICATE_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.INDEX_EXISTS, e,
-                        index.getIndexName());
+                throw new AsterixException(INDEX_EXISTS, e, index.getIndexName());
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -438,10 +469,9 @@
             insertTupleIntoIndex(txnId, mdIndexesProvider.getNodeEntity().getIndex(), tuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.DUPLICATE_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.NODE_EXISTS, e,
-                        node.getNodeName());
+                throw new AsterixException(NODE_EXISTS, e, node.getNodeName());
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -454,10 +484,9 @@
             modifyMetadataIndex(modificationOp, txnId, mdIndexesProvider.getNodeGroupEntity().getIndex(), tuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.DUPLICATE_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.NODEGROUP_EXISTS, e,
-                        nodeGroup.getNodeGroupName());
+                throw new AsterixException(NODEGROUP_EXISTS, e, nodeGroup.getNodeGroupName());
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -471,10 +500,9 @@
             insertTupleIntoIndex(txnId, mdIndexesProvider.getDatatypeEntity().getIndex(), tuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.DUPLICATE_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.TYPE_EXISTS, e,
-                        datatype.getDatatypeName());
+                throw new AsterixException(TYPE_EXISTS, e, datatype.getDatatypeName());
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -489,10 +517,9 @@
             insertTupleIntoIndex(txnId, mdIndexesProvider.getFunctionEntity().getIndex(), functionTuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.DUPLICATE_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.FUNCTION_EXISTS, e,
-                        function.getName());
+                throw new AsterixException(FUNCTION_EXISTS, e, function.getName());
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -520,7 +547,7 @@
             }
             return results.get(0);
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -539,7 +566,7 @@
             ITupleReference key = createTuple(database, dataverseName, filterName);
             deleteTupleFromIndex(txnId, mdIndexesProvider.getFullTextFilterEntity().getIndex(), key);
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -551,7 +578,7 @@
             ITupleReference configTuple = tupleReaderWriter.getTupleFromMetadataEntity(config);
             insertTupleIntoIndex(txnId, mdIndexesProvider.getFullTextConfigEntity().getIndex(), configTuple);
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -563,18 +590,14 @@
             ITupleReference filterTuple = tupleReaderWriter.getTupleFromMetadataEntity(filter);
             insertTupleIntoIndex(txnId, mdIndexesProvider.getFullTextFilterEntity().getIndex(), filterTuple);
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
     @Override
     public void addFullTextConfig(TxnId txnId, FullTextConfigMetadataEntity config)
             throws AlgebricksException, RemoteException {
-        try {
-            insertFullTextConfigMetadataEntityToCatalog(txnId, config);
-        } catch (AlgebricksException e) {
-            throw new AlgebricksException(e, ErrorCode.ERROR_PROCESSING_TUPLE);
-        }
+        insertFullTextConfigMetadataEntityToCatalog(txnId, config);
     }
 
     @Override
@@ -592,7 +615,7 @@
             searchIndex(txnId, mdIndexesProvider.getFullTextConfigEntity().getIndex(), searchKey, valueExtractor,
                     results);
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
 
         if (results.isEmpty()) {
@@ -618,7 +641,7 @@
             ITupleReference key = createTuple(database, dataverseName, configName);
             deleteTupleFromIndex(txnId, mdIndexesProvider.getFullTextConfigEntity().getIndex(), key);
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -632,6 +655,11 @@
         modifyMetadataIndex(Operation.UPSERT, txnId, metadataIndex, tuple);
     }
 
+    private void deleteTupleFromIndex(TxnId txnId, IMetadataIndex metadataIndex, ITupleReference tuple)
+            throws HyracksDataException {
+        modifyMetadataIndex(Operation.DELETE, txnId, metadataIndex, tuple);
+    }
+
     private void modifyMetadataIndex(Operation op, TxnId txnId, IMetadataIndex metadataIndex, ITupleReference tuple)
             throws HyracksDataException {
         String resourceName = metadataIndex.getFile().getRelativePath();
@@ -711,16 +739,7 @@
 
             // Drop all feeds and connections in this database.
             // Feeds may depend on datatypes and adapters
-            List<Feed> databaseFeeds = getDatabaseFeeds(txnId, databaseName);
-            for (Feed feed : databaseFeeds) {
-                List<FeedConnection> feedConnections =
-                        getFeedConnections(txnId, databaseName, feed.getDataverseName(), feed.getFeedName());
-                for (FeedConnection feedConnection : feedConnections) {
-                    dropFeedConnection(txnId, databaseName, feedConnection.getDataverseName(), feed.getFeedName(),
-                            feedConnection.getDatasetName());
-                }
-                dropFeed(txnId, databaseName, feed.getDataverseName(), feed.getFeedName());
-            }
+            dropDatabaseFeeds(txnId, databaseName);
 
             // Drop all feed ingestion policies in this database.
             List<FeedPolicyEntity> feedPolicies = getDatabaseFeedPolicies(txnId, databaseName);
@@ -748,7 +767,7 @@
             // Drop all libraries in this database.
             List<Library> databaseLibraries = getDatabaseLibraries(txnId, databaseName);
             for (Library lib : databaseLibraries) {
-                dropLibrary(txnId, lib.getDatabaseName(), lib.getDataverseName(), lib.getName());
+                dropLibrary(txnId, lib.getDatabaseName(), lib.getDataverseName(), lib.getName(), true);
             }
 
             // Drop all synonyms in this database.
@@ -804,14 +823,26 @@
             deleteTupleFromIndex(txnId, mdIndexesProvider.getDatabaseEntity().getIndex(), tuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.UPDATE_OR_DELETE_NON_EXISTENT_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_DATAVERSE, e,
-                        databaseName);
+                throw new AsterixException(UNKNOWN_DATABASE, e, databaseName);
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
 
+    private void dropDatabaseFeeds(TxnId txnId, String databaseName) throws AlgebricksException {
+        List<Feed> databaseFeeds = getDatabaseFeeds(txnId, databaseName);
+        for (Feed feed : databaseFeeds) {
+            List<FeedConnection> feedConnections =
+                    getFeedConnections(txnId, databaseName, feed.getDataverseName(), feed.getFeedName());
+            for (FeedConnection feedConnection : feedConnections) {
+                dropFeedConnection(txnId, databaseName, feedConnection.getDataverseName(), feed.getFeedName(),
+                        feedConnection.getDatasetName());
+            }
+            dropFeed(txnId, databaseName, feed.getDataverseName(), feed.getFeedName());
+        }
+    }
+
     @Override
     public void dropDataverse(TxnId txnId, String database, DataverseName dataverseName) throws AlgebricksException {
         try {
@@ -853,6 +884,7 @@
             }
 
             // Drop all libraries in this dataverse.
+            //TODO(DB): should may be check the library dependency above
             List<Library> dataverseLibraries = getDataverseLibraries(txnId, database, dataverseName);
             for (Library lib : dataverseLibraries) {
                 dropLibrary(txnId, lib.getDatabaseName(), lib.getDataverseName(), lib.getName());
@@ -903,10 +935,9 @@
             forceDropDataverse(txnId, database, dataverseName);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.UPDATE_OR_DELETE_NON_EXISTENT_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_DATAVERSE, e,
-                        dataverseName);
+                throw new AsterixException(UNKNOWN_DATAVERSE, e, dataverseName);
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -939,8 +970,7 @@
             boolean force) throws AlgebricksException {
         Dataset dataset = getDataset(txnId, database, dataverseName, datasetName);
         if (dataset == null) {
-            throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_DATASET_IN_DATAVERSE,
-                    datasetName, dataverseName);
+            throw new AsterixException(UNKNOWN_DATASET_IN_DATAVERSE, datasetName, dataverseName);
         }
         if (!force) {
             String datasetTypeDisplayName = DatasetUtil.getDatasetTypeDisplayName(dataset.getDatasetType());
@@ -990,16 +1020,15 @@
                         break;
                 }
             } catch (HyracksDataException hde) {
-                // ignore this exception and continue deleting all relevant
-                // artifacts.
+                // ignore this exception and continue deleting all relevant artifacts
                 if (!hde.matches(ErrorCode.UPDATE_OR_DELETE_NON_EXISTENT_KEY)) {
-                    throw new AlgebricksException(hde);
+                    throw new AsterixException(METADATA_ERROR, hde, hde.getMessage());
                 }
             } finally {
                 deleteTupleFromIndex(txnId, mdIndexesProvider.getDatasetEntity().getIndex(), datasetTuple);
             }
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -1015,9 +1044,9 @@
             deleteTupleFromIndex(txnId, mdIndexesProvider.getIndexEntity().getIndex(), tuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.UPDATE_OR_DELETE_NON_EXISTENT_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_INDEX, e, indexName);
+                throw new AsterixException(UNKNOWN_INDEX, e, indexName);
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -1029,9 +1058,8 @@
             if (failSilently) {
                 return false;
             }
-            throw new AsterixException(
-                    org.apache.asterix.common.exceptions.ErrorCode.CANNOT_DROP_OBJECT_DEPENDENT_EXISTS, "node group",
-                    nodeGroupName, dataset(PLURAL),
+            throw new AsterixException(CANNOT_DROP_OBJECT_DEPENDENT_EXISTS, "node group", nodeGroupName,
+                    dataset(PLURAL),
                     datasets.stream().map(DatasetUtil::getFullyQualifiedDisplayName).collect(Collectors.joining(", ")));
         }
         try {
@@ -1044,10 +1072,9 @@
             return true;
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.UPDATE_OR_DELETE_NON_EXISTENT_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_NODEGROUP, e,
-                        nodeGroupName);
+                throw new AsterixException(UNKNOWN_NODEGROUP, e, nodeGroupName);
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -1082,10 +1109,9 @@
             }
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.UPDATE_OR_DELETE_NON_EXISTENT_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_TYPE, e,
-                        datatypeName);
+                throw new AsterixException(UNKNOWN_TYPE, e, datatypeName);
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -1101,19 +1127,13 @@
             deleteTupleFromIndex(txnId, mdIndexesProvider.getDatatypeEntity().getIndex(), tuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.UPDATE_OR_DELETE_NON_EXISTENT_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_TYPE, e,
-                        datatypeName);
+                throw new AsterixException(UNKNOWN_TYPE, e, datatypeName);
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
 
-    private void deleteTupleFromIndex(TxnId txnId, IMetadataIndex metadataIndex, ITupleReference tuple)
-            throws HyracksDataException {
-        modifyMetadataIndex(Operation.DELETE, txnId, metadataIndex, tuple);
-    }
-
     @Override
     public List<Database> getDatabases(TxnId txnId) throws AlgebricksException, RemoteException {
         try {
@@ -1123,7 +1143,7 @@
             searchIndex(txnId, mdIndexesProvider.getDatabaseEntity().getIndex(), null, valueExtractor, results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -1136,7 +1156,7 @@
             searchIndex(txnId, mdIndexesProvider.getDataverseEntity().getIndex(), null, valueExtractor, results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -1156,7 +1176,7 @@
             }
             return results.get(0);
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -1174,7 +1194,7 @@
             }
             return results.get(0);
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -1189,7 +1209,7 @@
             searchIndex(txnId, mdIndexesProvider.getDatasetEntity().getIndex(), searchKey, valueExtractor, results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -1202,7 +1222,7 @@
             searchIndex(txnId, mdIndexesProvider.getDataverseEntity().getIndex(), searchKey, valueExtractor, results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -1216,7 +1236,7 @@
             searchIndex(txnId, mdIndexesProvider.getDatasetEntity().getIndex(), searchKey, valueExtractor, results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -1231,7 +1251,7 @@
             searchIndex(txnId, mdIndexesProvider.getFeedEntity().getIndex(), searchKey, valueExtractor, results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -1244,7 +1264,7 @@
             searchIndex(txnId, mdIndexesProvider.getFeedEntity().getIndex(), searchKey, valueExtractor, results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -1259,7 +1279,7 @@
             searchIndex(txnId, mdIndexesProvider.getLibraryEntity().getIndex(), searchKey, valueExtractor, results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -1273,7 +1293,7 @@
             searchIndex(txnId, mdIndexesProvider.getLibraryEntity().getIndex(), searchKey, valueExtractor, results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -1288,7 +1308,7 @@
             searchIndex(txnId, mdIndexesProvider.getDatatypeEntity().getIndex(), searchKey, valueExtractor, results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -1302,7 +1322,7 @@
             searchIndex(txnId, mdIndexesProvider.getDatatypeEntity().getIndex(), searchKey, valueExtractor, results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -1318,7 +1338,7 @@
             searchIndex(txnId, mdIndexesProvider.getFullTextConfigEntity().getIndex(), searchKey, valueExtractor,
                     results);
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
         return results;
     }
@@ -1335,7 +1355,7 @@
             searchIndex(txnId, mdIndexesProvider.getFullTextConfigEntity().getIndex(), searchKey, valueExtractor,
                     results);
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
         return results;
     }
@@ -1352,7 +1372,7 @@
             searchIndex(txnId, mdIndexesProvider.getFullTextFilterEntity().getIndex(), searchKey, valueExtractor,
                     results);
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
         return results;
     }
@@ -1369,7 +1389,7 @@
             searchIndex(txnId, mdIndexesProvider.getFullTextFilterEntity().getIndex(), searchKey, valueExtractor,
                     results);
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
         return results;
     }
@@ -1388,7 +1408,7 @@
             }
             return results.get(0);
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -1400,7 +1420,7 @@
             searchIndex(txnId, mdIndexesProvider.getDatasetEntity().getIndex(), null, valueExtractor, results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -1417,7 +1437,7 @@
             searchIndex(txnId, mdIndexesProvider.getDatatypeEntity().getIndex(), null, valueExtractor, results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -1431,7 +1451,7 @@
                     results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -1444,51 +1464,61 @@
             searchIndex(txnId, mdIndexesProvider.getFeedConnectionEntity().getIndex(), null, valueExtractor, results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
     private void confirmDatabaseCanBeDeleted(TxnId txnId, String database) throws AlgebricksException {
-        // if a dataset from a DIFFERENT database uses a type from this database throw an error
-        List<Dataset> datasets = getAllDatasets(txnId);
-        for (Dataset otherDataset : datasets) {
-            if (otherDataset.getDatabaseName().equals(database)) {
-                continue;
-            }
-            if (otherDataset.getItemTypeDatabaseName().equals(database)) {
-                throw new AsterixException(
-                        org.apache.asterix.common.exceptions.ErrorCode.CANNOT_DROP_DATAVERSE_DEPENDENT_EXISTS, "type",
-                        TypeUtil.getFullyQualifiedDisplayName(otherDataset.getItemTypeDataverseName(),
-                                otherDataset.getItemTypeName()),
-                        dataset(), DatasetUtil.getFullyQualifiedDisplayName(otherDataset));
-            }
-            if (otherDataset.hasMetaPart() && otherDataset.getMetaItemTypeDatabaseName().equals(database)) {
-                throw new AsterixException(
-                        org.apache.asterix.common.exceptions.ErrorCode.CANNOT_DROP_DATAVERSE_DEPENDENT_EXISTS, "type",
-                        TypeUtil.getFullyQualifiedDisplayName(otherDataset.getMetaItemTypeDataverseName(),
-                                otherDataset.getMetaItemTypeName()),
-                        dataset(), DatasetUtil.getFullyQualifiedDisplayName(otherDataset));
-            }
-            if (otherDataset.getDatasetType() == DatasetType.VIEW) {
-                ViewDetails viewDetails = (ViewDetails) otherDataset.getDatasetDetails();
-                List<DependencyKind> dependenciesSchema = ViewDetails.DEPENDENCIES_SCHEMA;
-                List<List<DependencyFullyQualifiedName>> dependencies = viewDetails.getDependencies();
-                for (int i = 0, n = dependencies.size(); i < n; i++) {
-                    for (DependencyFullyQualifiedName dependency : dependencies.get(i)) {
-                        if (dependency.getDatabaseName().equals(database)) {
-                            DependencyKind dependencyKind = dependenciesSchema.get(i);
-                            throw new AsterixException(
-                                    org.apache.asterix.common.exceptions.ErrorCode.CANNOT_DROP_DATAVERSE_DEPENDENT_EXISTS,
-                                    dependencyKind, dependencyKind.getDependencyDisplayName(dependency), "view",
-                                    DatasetUtil.getFullyQualifiedDisplayName(otherDataset));
-                        }
-                    }
-                }
-            }
-        }
+        // if a dataset/view from a DIFFERENT database uses a type from this database throw an error
+        ensureNoDatasetUsingDatabase(txnId, database);
 
         // if a function from a DIFFERENT database uses datasets, functions, datatypes, or synonyms from this database
         // throw an error
+        ensureNoFunctionUsingDatabase(txnId, database);
+
+        // if a feed connection from a DIFFERENT database applies a function from this database then throw an error
+        ensureNoFeedUsingDatabase(txnId, database);
+
+        // if an adapter from a DIFFERENT database uses a library from this database then throw an error
+        ensureNoAdapterUsingDatabase(txnId, database);
+    }
+
+    private void ensureNoAdapterUsingDatabase(TxnId txnId, String database) throws AlgebricksException {
+        List<DatasourceAdapter> adapters = getAllAdapters(txnId);
+        for (DatasourceAdapter adapter : adapters) {
+            if (database.equals(adapter.getAdapterIdentifier().getDatabaseName())) {
+                // skip adapters in self database
+                continue;
+            }
+            if (database.equals(adapter.getLibraryDatabaseName())) {
+                throw new AsterixException(CANNOT_DROP_DATABASE_DEPENDENT_EXISTS, "library",
+                        MetadataUtil.getFullyQualifiedDisplayName(adapter.getLibraryDatabaseName(),
+                                adapter.getLibraryDataverseName(), adapter.getLibraryName()),
+                        "adapter",
+                        MetadataUtil.getFullyQualifiedDisplayName(adapter.getAdapterIdentifier().getDataverseName(),
+                                adapter.getAdapterIdentifier().getName()));
+            }
+        }
+    }
+
+    private void ensureNoFeedUsingDatabase(TxnId txnId, String database) throws AlgebricksException {
+        List<FeedConnection> feedConnections = getAllFeedConnections(txnId);
+        for (FeedConnection feedConnection : feedConnections) {
+            if (database.equals(feedConnection.getDatabaseName())) {
+                continue;
+            }
+            for (FunctionSignature functionSignature : feedConnection.getAppliedFunctions()) {
+                if (database.equals(functionSignature.getDatabaseName())) {
+                    throw new AsterixException(CANNOT_DROP_DATABASE_DEPENDENT_EXISTS, "function", functionSignature,
+                            "feed connection",
+                            MetadataUtil.getFullyQualifiedDisplayName(feedConnection.getDatabaseName(),
+                                    feedConnection.getDataverseName(), feedConnection.getFeedName()));
+                }
+            }
+        }
+    }
+
+    private void ensureNoFunctionUsingDatabase(TxnId txnId, String database) throws AlgebricksException {
         List<Function> functions = getAllFunctions(txnId);
         for (Function otherFunction : functions) {
             if (otherFunction.getDatabaseName().equals(database)) {
@@ -1500,27 +1530,53 @@
                 for (DependencyFullyQualifiedName dependency : dependencies.get(i)) {
                     if (dependency.getDatabaseName().equals(database)) {
                         DependencyKind dependencyKind = dependenciesSchema.get(i);
-                        throw new AsterixException(
-                                org.apache.asterix.common.exceptions.ErrorCode.CANNOT_DROP_DATAVERSE_DEPENDENT_EXISTS,
-                                dependencyKind, dependencyKind.getDependencyDisplayName(dependency), "function",
+                        throw new AsterixException(CANNOT_DROP_DATABASE_DEPENDENT_EXISTS, dependencyKind,
+                                dependencyKind.getDependencyDisplayName(dependency), "function",
                                 otherFunction.getSignature());
                     }
                 }
             }
+            if (database.equals(otherFunction.getLibraryDatabaseName())) {
+                throw new AsterixException(CANNOT_DROP_DATABASE_DEPENDENT_EXISTS, "library",
+                        MetadataUtil.getFullyQualifiedDisplayName(otherFunction.getLibraryDatabaseName(),
+                                otherFunction.getLibraryDataverseName(), otherFunction.getLibraryName()),
+                        "function", otherFunction.getSignature());
+            }
         }
+    }
 
-        // if a feed connection from a DIFFERENT database applies a function from this database then throw an error
-        List<FeedConnection> feedConnections = getAllFeedConnections(txnId);
-        for (FeedConnection feedConnection : feedConnections) {
-            if (database.equals(feedConnection.getDatabaseName())) {
+    private void ensureNoDatasetUsingDatabase(TxnId txnId, String database) throws AlgebricksException {
+        List<Dataset> datasets = getAllDatasets(txnId);
+        for (Dataset otherDataset : datasets) {
+            if (otherDataset.getDatabaseName().equals(database)) {
                 continue;
             }
-            for (FunctionSignature functionSignature : feedConnection.getAppliedFunctions()) {
-                if (database.equals(functionSignature.getDatabaseName())) {
-                    throw new AsterixException(
-                            org.apache.asterix.common.exceptions.ErrorCode.CANNOT_DROP_DATAVERSE_DEPENDENT_EXISTS,
-                            "function", functionSignature, "feed connection", MetadataUtil.getFullyQualifiedDisplayName(
-                                    feedConnection.getDataverseName(), feedConnection.getFeedName()));
+            if (otherDataset.getItemTypeDatabaseName().equals(database)) {
+                //TODO(DB): fix display to include the database conditionally
+                throw new AsterixException(CANNOT_DROP_DATABASE_DEPENDENT_EXISTS, "type",
+                        MetadataUtil.getFullyQualifiedDisplayName(otherDataset.getItemTypeDatabaseName(),
+                                otherDataset.getItemTypeDataverseName(), otherDataset.getItemTypeName()),
+                        dataset(), DatasetUtil.getFullyQualifiedDisplayName(otherDataset));
+            }
+            if (otherDataset.hasMetaPart() && otherDataset.getMetaItemTypeDatabaseName().equals(database)) {
+                throw new AsterixException(CANNOT_DROP_DATABASE_DEPENDENT_EXISTS, "type",
+                        MetadataUtil.getFullyQualifiedDisplayName(otherDataset.getItemTypeDatabaseName(),
+                                otherDataset.getMetaItemTypeDataverseName(), otherDataset.getMetaItemTypeName()),
+                        dataset(), DatasetUtil.getFullyQualifiedDisplayName(otherDataset));
+            }
+            if (otherDataset.getDatasetType() == DatasetType.VIEW) {
+                ViewDetails viewDetails = (ViewDetails) otherDataset.getDatasetDetails();
+                List<DependencyKind> dependenciesSchema = ViewDetails.DEPENDENCIES_SCHEMA;
+                List<List<DependencyFullyQualifiedName>> dependencies = viewDetails.getDependencies();
+                for (int i = 0, n = dependencies.size(); i < n; i++) {
+                    for (DependencyFullyQualifiedName dependency : dependencies.get(i)) {
+                        if (dependency.getDatabaseName().equals(database)) {
+                            DependencyKind dependencyKind = dependenciesSchema.get(i);
+                            throw new AsterixException(CANNOT_DROP_DATABASE_DEPENDENT_EXISTS, dependencyKind,
+                                    dependencyKind.getDependencyDisplayName(dependency), "view",
+                                    DatasetUtil.getFullyQualifiedDisplayName(otherDataset));
+                        }
+                    }
                 }
             }
         }
@@ -1536,16 +1592,14 @@
             }
             if (dataset.getItemTypeDataverseName().equals(dataverseName)
                     && dataset.getItemTypeDatabaseName().equals(database)) {
-                throw new AsterixException(
-                        org.apache.asterix.common.exceptions.ErrorCode.CANNOT_DROP_DATAVERSE_DEPENDENT_EXISTS, "type",
+                throw new AsterixException(CANNOT_DROP_DATAVERSE_DEPENDENT_EXISTS, "type",
                         TypeUtil.getFullyQualifiedDisplayName(dataset.getItemTypeDataverseName(),
                                 dataset.getItemTypeName()),
                         dataset(), DatasetUtil.getFullyQualifiedDisplayName(dataset));
             }
             if (dataset.hasMetaPart() && dataset.getMetaItemTypeDataverseName().equals(dataverseName)
                     && dataset.getMetaItemTypeDatabaseName().equals(database)) {
-                throw new AsterixException(
-                        org.apache.asterix.common.exceptions.ErrorCode.CANNOT_DROP_DATAVERSE_DEPENDENT_EXISTS, "type",
+                throw new AsterixException(CANNOT_DROP_DATAVERSE_DEPENDENT_EXISTS, "type",
                         TypeUtil.getFullyQualifiedDisplayName(dataset.getMetaItemTypeDataverseName(),
                                 dataset.getMetaItemTypeName()),
                         dataset(), DatasetUtil.getFullyQualifiedDisplayName(dataset));
@@ -1559,9 +1613,8 @@
                         if (dependency.getDataverseName().equals(dataverseName)
                                 && dependency.getDatabaseName().equals(database)) {
                             DependencyKind dependencyKind = dependenciesSchema.get(i);
-                            throw new AsterixException(
-                                    org.apache.asterix.common.exceptions.ErrorCode.CANNOT_DROP_DATAVERSE_DEPENDENT_EXISTS,
-                                    dependencyKind, dependencyKind.getDependencyDisplayName(dependency), "view",
+                            throw new AsterixException(CANNOT_DROP_DATAVERSE_DEPENDENT_EXISTS, dependencyKind,
+                                    dependencyKind.getDependencyDisplayName(dependency), "view",
                                     DatasetUtil.getFullyQualifiedDisplayName(dataset));
                         }
                     }
@@ -1583,9 +1636,8 @@
                     if (dependency.getDataverseName().equals(dataverseName)
                             && dependency.getDatabaseName().equals(database)) {
                         DependencyKind dependencyKind = dependenciesSchema.get(i);
-                        throw new AsterixException(
-                                org.apache.asterix.common.exceptions.ErrorCode.CANNOT_DROP_DATAVERSE_DEPENDENT_EXISTS,
-                                dependencyKind, dependencyKind.getDependencyDisplayName(dependency), "function",
+                        throw new AsterixException(CANNOT_DROP_DATAVERSE_DEPENDENT_EXISTS, dependencyKind,
+                                dependencyKind.getDependencyDisplayName(dependency), "function",
                                 function.getSignature());
                     }
                 }
@@ -1602,13 +1654,13 @@
             for (FunctionSignature functionSignature : feedConnection.getAppliedFunctions()) {
                 if (dataverseName.equals(functionSignature.getDataverseName())
                         && database.equals(functionSignature.getDatabaseName())) {
-                    throw new AsterixException(
-                            org.apache.asterix.common.exceptions.ErrorCode.CANNOT_DROP_DATAVERSE_DEPENDENT_EXISTS,
-                            "function", functionSignature, "feed connection", MetadataUtil.getFullyQualifiedDisplayName(
+                    throw new AsterixException(CANNOT_DROP_DATAVERSE_DEPENDENT_EXISTS, "function", functionSignature,
+                            "feed connection", MetadataUtil.getFullyQualifiedDisplayName(
                                     feedConnection.getDataverseName(), feedConnection.getFeedName()));
                 }
             }
         }
+        //TODO(DB): should check entities depending on libraries in this dataverse
     }
 
     private void confirmFunctionCanBeDeleted(TxnId txnId, FunctionSignature signature) throws AlgebricksException {
@@ -1619,10 +1671,9 @@
         List<FeedConnection> feedConnections = getAllFeedConnections(txnId);
         for (FeedConnection feedConnection : feedConnections) {
             if (feedConnection.containsFunction(signature)) {
-                throw new AsterixException(
-                        org.apache.asterix.common.exceptions.ErrorCode.CANNOT_DROP_OBJECT_DEPENDENT_EXISTS, "function",
-                        signature, "feed connection", MetadataUtil.getFullyQualifiedDisplayName(
-                                feedConnection.getDataverseName(), feedConnection.getFeedName()));
+                throw new AsterixException(CANNOT_DROP_OBJECT_DEPENDENT_EXISTS, "function", signature,
+                        "feed connection", MetadataUtil.getFullyQualifiedDisplayName(feedConnection.getDataverseName(),
+                                feedConnection.getFeedName()));
             }
         }
     }
@@ -1667,10 +1718,9 @@
                                 && dependency.getDatabaseName().equals(database)
                                 && dependency.getSubName1().equals(objectName)
                                 && (objectArg == null || objectArg.equals(dependency.getSubName2()))) {
-                            throw new AsterixException(
-                                    org.apache.asterix.common.exceptions.ErrorCode.CANNOT_DROP_OBJECT_DEPENDENT_EXISTS,
-                                    objectKindDisplayName, dependencyKind.getDependencyDisplayName(dependency),
-                                    "function", function.getSignature());
+                            throw new AsterixException(CANNOT_DROP_OBJECT_DEPENDENT_EXISTS, objectKindDisplayName,
+                                    dependencyKind.getDependencyDisplayName(dependency), "function",
+                                    function.getSignature());
                         }
                     }
                 }
@@ -1706,10 +1756,9 @@
                                     && dependency.getDatabaseName().equals(database)
                                     && dependency.getSubName1().equals(objectName)
                                     && (objectArg == null || objectArg.equals(dependency.getSubName2()))) {
-                                throw new AsterixException(
-                                        org.apache.asterix.common.exceptions.ErrorCode.CANNOT_DROP_OBJECT_DEPENDENT_EXISTS,
-                                        objectKindDisplayName, dependencyKind.getDependencyDisplayName(dependency),
-                                        "view", DatasetUtil.getFullyQualifiedDisplayName(dataset));
+                                throw new AsterixException(CANNOT_DROP_OBJECT_DEPENDENT_EXISTS, objectKindDisplayName,
+                                        dependencyKind.getDependencyDisplayName(dependency), "view",
+                                        DatasetUtil.getFullyQualifiedDisplayName(dataset));
                             }
                         }
                     }
@@ -1721,8 +1770,7 @@
     private void confirmFullTextConfigCanBeDeleted(TxnId txnId, String database,
             DataverseName dataverseNameFullTextConfig, String configName) throws AlgebricksException {
         if (Strings.isNullOrEmpty(configName)) {
-            throw new MetadataException(
-                    org.apache.asterix.common.exceptions.ErrorCode.FULL_TEXT_DEFAULT_CONFIG_CANNOT_BE_DELETED_OR_CREATED);
+            throw new MetadataException(FULL_TEXT_DEFAULT_CONFIG_CANNOT_BE_DELETED_OR_CREATED);
         }
 
         // If any index uses this full-text config, throw an error
@@ -1740,9 +1788,7 @@
                     if (index.getDataverseName().equals(dataverseNameFullTextConfig)
                             && index.getDatabaseName().equals(database) && !Strings.isNullOrEmpty(indexConfigName)
                             && indexConfigName.equals(configName)) {
-                        throw new AsterixException(
-                                org.apache.asterix.common.exceptions.ErrorCode.CANNOT_DROP_OBJECT_DEPENDENT_EXISTS,
-                                "full-text config",
+                        throw new AsterixException(CANNOT_DROP_OBJECT_DEPENDENT_EXISTS, "full-text config",
                                 MetadataUtil.getFullyQualifiedDisplayName(dataverseNameFullTextConfig, configName),
                                 "index", DatasetUtil.getFullyQualifiedDisplayName(index.getDataverseName(),
                                         index.getDatasetName()) + "." + index.getIndexName());
@@ -1782,8 +1828,7 @@
         for (Function function : functions) {
             if (libraryName.equals(function.getLibraryName()) && database.equals(function.getLibraryDatabaseName())
                     && dataverseName.equals(function.getLibraryDataverseName())) {
-                throw new AsterixException(
-                        org.apache.asterix.common.exceptions.ErrorCode.CANNOT_DROP_OBJECT_DEPENDENT_EXISTS, "library",
+                throw new AsterixException(CANNOT_DROP_OBJECT_DEPENDENT_EXISTS, "library",
                         MetadataUtil.getFullyQualifiedDisplayName(dataverseName, libraryName), "function",
                         function.getSignature());
             }
@@ -1796,8 +1841,7 @@
         for (DatasourceAdapter adapter : adapters) {
             if (libraryName.equals(adapter.getLibraryName()) && database.equals(adapter.getLibraryDatabaseName())
                     && dataverseName.equals(adapter.getLibraryDataverseName())) {
-                throw new AsterixException(
-                        org.apache.asterix.common.exceptions.ErrorCode.CANNOT_DROP_OBJECT_DEPENDENT_EXISTS, "library",
+                throw new AsterixException(CANNOT_DROP_OBJECT_DEPENDENT_EXISTS, "library",
                         MetadataUtil.getFullyQualifiedDisplayName(dataverseName, libraryName), "adapter",
                         MetadataUtil.getFullyQualifiedDisplayName(adapter.getAdapterIdentifier().getDataverseName(),
                                 adapter.getAdapterIdentifier().getName()));
@@ -1823,8 +1867,7 @@
                     || ((dataset.hasMetaPart() && dataset.getMetaItemTypeName().equals(datatypeName)
                             && dataset.getMetaItemTypeDataverseName().equals(dataverseName)
                             && dataset.getMetaItemTypeDatabaseName().equals(database)))) {
-                throw new AsterixException(
-                        org.apache.asterix.common.exceptions.ErrorCode.CANNOT_DROP_OBJECT_DEPENDENT_EXISTS, "type",
+                throw new AsterixException(CANNOT_DROP_OBJECT_DEPENDENT_EXISTS, "type",
                         TypeUtil.getFullyQualifiedDisplayName(dataverseName, datatypeName), dataset(),
                         DatasetUtil.getFullyQualifiedDisplayName(dataset));
             }
@@ -1848,14 +1891,14 @@
         for (Datatype dataType : datatypes) {
 
             // skip types in different dataverses as well as the type to be dropped itself
+            //TODO(DB): review this
             if (!dataType.getDataverseName().equals(dataverseName) || !dataType.getDatabaseName().equals(database)
                     || dataType.getDatatype().getTypeName().equals(datatypeName)) {
                 continue;
             }
             AbstractComplexType recType = (AbstractComplexType) dataType.getDatatype();
             if (recType.containsType(typeToBeDropped)) {
-                throw new AsterixException(
-                        org.apache.asterix.common.exceptions.ErrorCode.CANNOT_DROP_OBJECT_DEPENDENT_EXISTS, "type",
+                throw new AsterixException(CANNOT_DROP_OBJECT_DEPENDENT_EXISTS, "type",
                         TypeUtil.getFullyQualifiedDisplayName(dataverseName, datatypeName), "type",
                         TypeUtil.getFullyQualifiedDisplayName(dataverseName, recType.getTypeName()));
             }
@@ -1876,10 +1919,9 @@
             FullTextConfigDescriptor config = configMetadataEntity.getFullTextConfig();
             for (String filterName : config.getFilterNames()) {
                 if (filterName.equals(fullTextFilterName)) {
-                    throw new AlgebricksException("Cannot drop full-text filter "
-                            + TypeUtil.getFullyQualifiedDisplayName(dataverseName, fullTextFilterName)
-                            + " being used by full-text config "
-                            + TypeUtil.getFullyQualifiedDisplayName(dataverseName, config.getName()));
+                    throw new AsterixException(CANNOT_DROP_OBJECT_DEPENDENT_EXISTS, "full-text filter",
+                            TypeUtil.getFullyQualifiedDisplayName(dataverseName, fullTextFilterName),
+                            "full-text config", TypeUtil.getFullyQualifiedDisplayName(dataverseName, config.getName()));
                 }
             }
         }
@@ -1943,7 +1985,7 @@
             }
             return results.get(0);
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -1959,7 +2001,7 @@
             searchIndex(txnId, mdIndexesProvider.getIndexEntity().getIndex(), searchKey, valueExtractor, results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -1978,7 +2020,7 @@
             }
             return results.get(0);
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -1995,7 +2037,7 @@
             }
             return results.get(0);
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -2027,7 +2069,7 @@
             searchIndex(txnId, mdIndexesProvider.getFunctionEntity().getIndex(), searchKey, valueExtractor, results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -2052,10 +2094,9 @@
             deleteTupleFromIndex(txnId, mdIndexesProvider.getFunctionEntity().getIndex(), functionTuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.UPDATE_OR_DELETE_NON_EXISTENT_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_FUNCTION, e,
-                        functionSignature.toString());
+                throw new AsterixException(UNKNOWN_FUNCTION, e, functionSignature.toString());
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -2156,7 +2197,7 @@
             IValueExtractor<T> valueExtractor, List<T> results) throws AlgebricksException, HyracksDataException {
         IBinaryComparatorFactory[] comparatorFactories = index.getKeyBinaryComparatorFactory();
         if (index.getFile() == null) {
-            throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.METADATA_ERROR,
+            throw new AsterixException(METADATA_ERROR,
                     "No file for Index " + index.getDataverseName() + "." + index.getIndexName());
         }
         String resourceName = index.getFile().getRelativePath();
@@ -2218,7 +2259,7 @@
                 datasetLifecycleManager.close(resourceName);
             }
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
         DatasetIdFactory.initialize(mostRecentDatasetId);
     }
@@ -2330,10 +2371,9 @@
             insertTupleIntoIndex(txnId, mdIndexesProvider.getDatasourceAdapterEntity().getIndex(), adapterTuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.DUPLICATE_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.ADAPTER_EXISTS, e,
-                        adapter.getAdapterIdentifier().getName());
+                throw new AsterixException(ADAPTER_EXISTS, e, adapter.getAdapterIdentifier().getName());
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -2351,10 +2391,9 @@
             deleteTupleFromIndex(txnId, mdIndexesProvider.getDatasourceAdapterEntity().getIndex(), datasetTuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.UPDATE_OR_DELETE_NON_EXISTENT_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_ADAPTER, e,
-                        adapterName);
+                throw new AsterixException(UNKNOWN_ADAPTER, e, adapterName);
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -2375,7 +2414,7 @@
             }
             return results.get(0);
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -2390,10 +2429,9 @@
                     compactionPolicyTuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.DUPLICATE_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.COMPACTION_POLICY_EXISTS, e,
-                        compactionPolicy.getPolicyName());
+                throw new AsterixException(COMPACTION_POLICY_EXISTS, e, compactionPolicy.getPolicyName());
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -2414,7 +2452,7 @@
             }
             return null;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -2431,7 +2469,7 @@
                     results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -2446,7 +2484,7 @@
                     results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -2459,10 +2497,9 @@
             insertTupleIntoIndex(txnId, mdIndexesProvider.getLibraryEntity().getIndex(), libraryTuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.DUPLICATE_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.LIBRARY_EXISTS, e,
-                        library.getName());
+                throw new AsterixException(LIBRARY_EXISTS, e, library.getName());
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -2487,10 +2524,9 @@
             deleteTupleFromIndex(txnId, mdIndexesProvider.getLibraryEntity().getIndex(), datasetTuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.UPDATE_OR_DELETE_NON_EXISTENT_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_LIBRARY, e,
-                        libraryName);
+                throw new AsterixException(UNKNOWN_LIBRARY, e, libraryName);
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -2509,7 +2545,7 @@
             }
             return results.get(0);
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -2527,10 +2563,9 @@
             insertTupleIntoIndex(txnId, mdIndexesProvider.getFeedPolicyEntity().getIndex(), feedPolicyTuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.DUPLICATE_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.FEED_POLICY_EXISTS, e,
-                        feedPolicy.getPolicyName());
+                throw new AsterixException(FEED_POLICY_EXISTS, e, feedPolicy.getPolicyName());
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -2549,7 +2584,7 @@
             }
             return null;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -2562,10 +2597,10 @@
             insertTupleIntoIndex(txnId, mdIndexesProvider.getFeedConnectionEntity().getIndex(), feedConnTuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.DUPLICATE_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.FEED_CONNECTION_EXISTS, e,
-                        feedConnection.getFeedName(), feedConnection.getDatasetName());
+                throw new AsterixException(FEED_CONNECTION_EXISTS, e, feedConnection.getFeedName(),
+                        feedConnection.getDatasetName());
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -2583,7 +2618,7 @@
                     results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -2603,7 +2638,7 @@
             }
             return null;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -2617,10 +2652,9 @@
             deleteTupleFromIndex(txnId, mdIndexesProvider.getFeedConnectionEntity().getIndex(), tuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.UPDATE_OR_DELETE_NON_EXISTENT_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_FEED_CONNECTION, e,
-                        feedName, datasetName);
+                throw new AsterixException(UNKNOWN_FEED_CONNECTION, e, feedName, datasetName);
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -2634,10 +2668,9 @@
             insertTupleIntoIndex(txnId, mdIndexesProvider.getFeedEntity().getIndex(), feedTuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.DUPLICATE_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.FEED_EXISTS, e,
-                        feed.getFeedName());
+                throw new AsterixException(FEED_EXISTS, e, feed.getFeedName());
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -2656,7 +2689,7 @@
             }
             return null;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -2670,7 +2703,7 @@
             searchIndex(txnId, mdIndexesProvider.getFeedEntity().getIndex(), searchKey, valueExtractor, results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -2685,9 +2718,9 @@
             deleteTupleFromIndex(txnId, mdIndexesProvider.getFeedEntity().getIndex(), tuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.UPDATE_OR_DELETE_NON_EXISTENT_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_FEED, e, feedName);
+                throw new AsterixException(UNKNOWN_FEED, e, feedName);
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -2702,10 +2735,9 @@
             deleteTupleFromIndex(txnId, mdIndexesProvider.getFeedPolicyEntity().getIndex(), tuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.UPDATE_OR_DELETE_NON_EXISTENT_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_FEED_POLICY, e,
-                        policyName);
+                throw new AsterixException(UNKNOWN_FEED_POLICY, e, policyName);
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -2721,7 +2753,7 @@
             searchIndex(txnId, mdIndexesProvider.getFeedPolicyEntity().getIndex(), searchKey, valueExtractor, results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -2734,7 +2766,7 @@
             searchIndex(txnId, mdIndexesProvider.getFeedPolicyEntity().getIndex(), searchKey, valueExtractor, results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -2748,10 +2780,10 @@
             insertTupleIntoIndex(txnId, mdIndexesProvider.getExternalFileEntity().getIndex(), externalFileTuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.DUPLICATE_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.EXTERNAL_FILE_EXISTS, e,
-                        externalFile.getFileNumber(), externalFile.getDatasetName());
+                throw new AsterixException(EXTERNAL_FILE_EXISTS, e, externalFile.getFileNumber(),
+                        externalFile.getDatasetName());
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -2769,7 +2801,7 @@
                     results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -2786,10 +2818,9 @@
             deleteTupleFromIndex(txnId, mdIndexesProvider.getExternalFileEntity().getIndex(), datasetTuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.UPDATE_OR_DELETE_NON_EXISTENT_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_EXTERNAL_FILE, e,
-                        fileNumber, datasetName);
+                throw new AsterixException(UNKNOWN_EXTERNAL_FILE, e, fileNumber, datasetName);
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -2857,7 +2888,7 @@
             }
             return results.get(0);
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -2870,10 +2901,9 @@
             insertTupleIntoIndex(txnId, mdIndexesProvider.getSynonymEntity().getIndex(), synonymTuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.DUPLICATE_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.SYNONYM_EXISTS, e,
-                        synonym.getSynonymName());
+                throw new AsterixException(SYNONYM_EXISTS, e, synonym.getSynonymName());
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -2900,10 +2930,9 @@
             deleteTupleFromIndex(txnId, mdIndexesProvider.getSynonymEntity().getIndex(), synonymTuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.UPDATE_OR_DELETE_NON_EXISTENT_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_SYNONYM, e,
-                        synonymName);
+                throw new AsterixException(UNKNOWN_SYNONYM, e, synonymName);
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
@@ -2940,7 +2969,7 @@
             }
             return results.get(0);
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -2955,7 +2984,7 @@
             searchIndex(txnId, mdIndexesProvider.getSynonymEntity().getIndex(), searchKey, valueExtractor, results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -2968,7 +2997,7 @@
             searchIndex(txnId, mdIndexesProvider.getSynonymEntity().getIndex(), searchKey, valueExtractor, results);
             return results;
         } catch (HyracksDataException e) {
-            throw new AlgebricksException(e);
+            throw new AsterixException(METADATA_ERROR, e, e.getMessage());
         }
     }
 
@@ -2989,10 +3018,10 @@
             insertTupleIntoIndex(txnId, mdIndexesProvider.getDatasetEntity().getIndex(), datasetTuple);
         } catch (HyracksDataException e) {
             if (e.matches(ErrorCode.UPDATE_OR_DELETE_NON_EXISTENT_KEY)) {
-                throw new AsterixException(org.apache.asterix.common.exceptions.ErrorCode.UNKNOWN_DATASET_IN_DATAVERSE,
-                        e, dataset.getDatasetName(), dataset.getDataverseName());
+                throw new AsterixException(UNKNOWN_DATASET_IN_DATAVERSE, e, dataset.getDatasetName(),
+                        dataset.getDataverseName());
             } else {
-                throw new AlgebricksException(e);
+                throw new AsterixException(METADATA_ERROR, e, e.getMessage());
             }
         }
     }
diff --git a/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/bootstrap/MetadataBootstrap.java b/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/bootstrap/MetadataBootstrap.java
index b154287..843d7c3 100644
--- a/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/bootstrap/MetadataBootstrap.java
+++ b/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/bootstrap/MetadataBootstrap.java
@@ -472,6 +472,8 @@
      * Perform recovery of DDL operations metadata records
      */
     public static void startDDLRecovery() throws AlgebricksException {
+        //TODO(DB): include database in recovery
+
         // #. clean up any record which has pendingAdd/DelOp flag
         // as traversing all records from DATAVERSE_DATASET to DATASET_DATASET, and then
         // to INDEX_DATASET.
diff --git a/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/entities/DependencyKind.java b/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/entities/DependencyKind.java
index 555d1c7..5031183 100644
--- a/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/entities/DependencyKind.java
+++ b/asterixdb/asterix-metadata/src/main/java/org/apache/asterix/metadata/entities/DependencyKind.java
@@ -28,6 +28,7 @@
 import org.apache.asterix.metadata.utils.TypeUtil;
 
 public enum DependencyKind {
+    //TODO(DB): fix display to include the database conditionally
     DATASET(
             dependency -> DatasetUtil.getFullyQualifiedDisplayName(dependency.getDataverseName(),
                     dependency.getSubName1())),

-- 
To view, visit https://asterix-gerrit.ics.uci.edu/c/asterixdb/+/17870
To unsubscribe, or for help writing mail filters, visit https://asterix-gerrit.ics.uci.edu/settings

Gerrit-Project: asterixdb
Gerrit-Branch: master
Gerrit-Change-Id: I4a55c8b6d3606046183305891a8be7fed362c66a
Gerrit-Change-Number: 17870
Gerrit-PatchSet: 1
Gerrit-Owner: Ali Alsuliman <al...@gmail.com>
Gerrit-MessageType: newchange