Merge branch 'master' into jarodwen/hotfix/issue425
diff --git a/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/DatasetLifecycleManager.java b/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/DatasetLifecycleManager.java
index 434835b..d7329bf 100644
--- a/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/DatasetLifecycleManager.java
+++ b/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/DatasetLifecycleManager.java
@@ -144,23 +144,28 @@
     }
 
     private boolean evictCandidateDataset() throws HyracksDataException {
-        // Why min()? As a heuristic for eviction, we will take an open index (an index consuming memory) 
+        // We will take a dataset that has no active transactions, it is open (a dataset consuming memory), 
         // that is not being used (refcount == 0) and has been least recently used. The sort order defined 
-        // for IndexInfo maintains this. See IndexInfo.compareTo().
-        DatasetInfo dsInfo = Collections.min(datasetInfos.values());
-        if (dsInfo.referenceCount == 0 && dsInfo.isOpen) {
-            for (IndexInfo iInfo : dsInfo.indexes.values()) {
-                if (iInfo.isOpen) {
-                    iInfo.index.deactivate(true);
-                    iInfo.isOpen = false;
-                }
-                assert iInfo.referenceCount == 0;
-            }
+        // for DatasetInfo maintains this. See DatasetInfo.compareTo().
 
-            IVirtualBufferCache vbc = getVirtualBufferCache(dsInfo.datasetID);
-            used -= vbc.getNumPages() * vbc.getPageSize();
-            dsInfo.isOpen = false;
-            return true;
+        List<DatasetInfo> datasetInfosList = new ArrayList<DatasetInfo>(datasetInfos.values());
+        Collections.sort(datasetInfosList);
+        for (DatasetInfo dsInfo : datasetInfosList) {
+            if (((PrimaryIndexOperationTracker) datasetOpTrackers.get(dsInfo.datasetID)).getNumActiveOperations() == 0
+                    && dsInfo.referenceCount == 0 && dsInfo.isOpen) {
+                for (IndexInfo iInfo : dsInfo.indexes.values()) {
+                    if (iInfo.isOpen) {
+                        iInfo.index.deactivate(true);
+                        iInfo.isOpen = false;
+                    }
+                    assert iInfo.referenceCount == 0;
+                }
+
+                IVirtualBufferCache vbc = getVirtualBufferCache(dsInfo.datasetID);
+                used -= vbc.getNumPages() * vbc.getPageSize();
+                dsInfo.isOpen = false;
+                return true;
+            }
         }
         return false;
     }
diff --git a/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/PrimaryIndexOperationTracker.java b/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/PrimaryIndexOperationTracker.java
index b924024..1a82d75 100644
--- a/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/PrimaryIndexOperationTracker.java
+++ b/asterix-common/src/main/java/edu/uci/ics/asterix/common/context/PrimaryIndexOperationTracker.java
@@ -113,4 +113,8 @@
         }
     }
 
+    public int getNumActiveOperations() {
+        return numActiveOperations.get();
+    }
+
 }