Fixed issue in LSM indexes where OS file handles would not be closed properly if destroy() was called after deactivate(). This issue caused 'too many open files' during the Asterix runtime tests on a 32-bit Ubuntu.

git-svn-id: https://hyracks.googlecode.com/svn/branches/hyracks_lsm_tree@2418 123451ca-8445-de46-9d55-352943316053
diff --git a/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/impls/AbstractTreeIndex.java b/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/impls/AbstractTreeIndex.java
index b6da610..f90f37f 100644
--- a/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/impls/AbstractTreeIndex.java
+++ b/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/impls/AbstractTreeIndex.java
@@ -163,7 +163,7 @@
             throw new HyracksDataException("Failed to destroy the index since it is activated.");

         }

 

-        file.getFile().delete();

+        file.delete();

         if (fileId == -1) {

             return;

         }

diff --git a/hyracks-storage-am-lsm-btree/src/main/java/edu/uci/ics/hyracks/storage/am/lsm/btree/impls/LSMBTree.java b/hyracks-storage-am-lsm-btree/src/main/java/edu/uci/ics/hyracks/storage/am/lsm/btree/impls/LSMBTree.java
index 09b88ae..51a0332 100644
--- a/hyracks-storage-am-lsm-btree/src/main/java/edu/uci/ics/hyracks/storage/am/lsm/btree/impls/LSMBTree.java
+++ b/hyracks-storage-am-lsm-btree/src/main/java/edu/uci/ics/hyracks/storage/am/lsm/btree/impls/LSMBTree.java
@@ -123,7 +123,6 @@
         ILSMOperationTracker opTracker = opTrackerFactory.createOperationTracker(this);
         lsmHarness = new LSMHarness(this, flushController, mergePolicy, opTracker, ioScheduler);
         componentFinalizer = new TreeIndexComponentFinalizer(diskFileMapProvider);
-        diskBTrees = new LinkedList<Object>();
         isActivated = false;
     }
 
@@ -147,6 +146,7 @@
         ((InMemoryBufferCache) memBTree.getBufferCache()).open();
         memBTree.create();
         memBTree.activate();
+        diskBTrees.clear();
         List<Object> validFileNames = fileManager.cleanupAndGetValidFiles(componentFinalizer);
         for (Object o : validFileNames) {
             String fileName = (String) o;
@@ -175,8 +175,7 @@
         for (Object o : diskBTrees) {
             BTree btree = (BTree) o;
             btree.deactivate();
-        }
-        diskBTrees.clear();
+        }        
         memBTree.deactivate();
         memBTree.destroy();
         ((InMemoryBufferCache) memBTree.getBufferCache()).close();
diff --git a/hyracks-storage-am-lsm-invertedindex/src/main/java/edu/uci/ics/hyracks/storage/am/lsm/invertedindex/impls/LSMInvertedIndex.java b/hyracks-storage-am-lsm-invertedindex/src/main/java/edu/uci/ics/hyracks/storage/am/lsm/invertedindex/impls/LSMInvertedIndex.java
index 8862ea2..851451e 100644
--- a/hyracks-storage-am-lsm-invertedindex/src/main/java/edu/uci/ics/hyracks/storage/am/lsm/invertedindex/impls/LSMInvertedIndex.java
+++ b/hyracks-storage-am-lsm-invertedindex/src/main/java/edu/uci/ics/hyracks/storage/am/lsm/invertedindex/impls/LSMInvertedIndex.java
@@ -177,6 +177,7 @@
             memComponent.getInvIndex().activate();
             memComponent.getDeletedKeysBTree().create();
             memComponent.getDeletedKeysBTree().activate();
+            diskComponents.clear();
             List<Object> validFileNames = fileManager.cleanupAndGetValidFiles(componentFinalizer);
             for (Object o : validFileNames) {
                 LSMInvertedIndexFileNameComponent component = (LSMInvertedIndexFileNameComponent) o;
diff --git a/hyracks-storage-am-lsm-invertedindex/src/main/java/edu/uci/ics/hyracks/storage/am/lsm/invertedindex/ondisk/OnDiskInvertedIndex.java b/hyracks-storage-am-lsm-invertedindex/src/main/java/edu/uci/ics/hyracks/storage/am/lsm/invertedindex/ondisk/OnDiskInvertedIndex.java
index 26e6b6a..69e57be 100644
--- a/hyracks-storage-am-lsm-invertedindex/src/main/java/edu/uci/ics/hyracks/storage/am/lsm/invertedindex/ondisk/OnDiskInvertedIndex.java
+++ b/hyracks-storage-am-lsm-invertedindex/src/main/java/edu/uci/ics/hyracks/storage/am/lsm/invertedindex/ondisk/OnDiskInvertedIndex.java
@@ -202,7 +202,7 @@
         }
 
         btree.destroy();
-        invListsFile.getFile().delete();
+        invListsFile.delete();
         if (fileId == -1) {
             return;
         }
diff --git a/hyracks-storage-am-lsm-rtree/src/main/java/edu/uci/ics/hyracks/storage/am/lsm/rtree/impls/LSMRTree.java b/hyracks-storage-am-lsm-rtree/src/main/java/edu/uci/ics/hyracks/storage/am/lsm/rtree/impls/LSMRTree.java
index 58254e0..62e0d0f 100644
--- a/hyracks-storage-am-lsm-rtree/src/main/java/edu/uci/ics/hyracks/storage/am/lsm/rtree/impls/LSMRTree.java
+++ b/hyracks-storage-am-lsm-rtree/src/main/java/edu/uci/ics/hyracks/storage/am/lsm/rtree/impls/LSMRTree.java
@@ -96,6 +96,7 @@
     public synchronized void activate() throws HyracksDataException {
         super.activate();
         List<Object> validFileNames = fileManager.cleanupAndGetValidFiles(componentFinalizer);
+        diskComponents.clear();
         for (Object o : validFileNames) {
             LSMRTreeFileNameComponent component = (LSMRTreeFileNameComponent) o;
             FileReference rtreeFile = new FileReference(new File(component.getRTreeFileName()));
@@ -117,7 +118,6 @@
             rtree.deactivate();
             btree.deactivate();
         }
-        diskComponents.clear();
     }
 
     @Override
diff --git a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/BufferCache.java b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/BufferCache.java
index 58e2af2..bd4cb6e 100644
--- a/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/BufferCache.java
+++ b/hyracks-storage-common/src/main/java/edu/uci/ics/hyracks/storage/common/buffercache/BufferCache.java
@@ -772,7 +772,7 @@
                 if (fInfo != null) {
                     // Mark the fInfo as deleted, 
                     // such that when its pages are reclaimed in openFile(),
-                    // the pages are not flushed to disk but only invalidates.
+                    // the pages are not flushed to disk but only invalidated.
                     ioManager.close(fInfo.getFileHandle());
                     fInfo.markAsDeleted();
                 }