LSM-RTree cleanup

git-svn-id: https://hyracks.googlecode.com/svn/branches/hyracks_lsm_tree@1046 123451ca-8445-de46-9d55-352943316053
diff --git a/hyracks-storage-am-lsmtree-common/src/main/java/edu/uci/ics/hyracks/storage/am/lsmtree/common/impls/LSMTreeBulkLoadContext.java b/hyracks-storage-am-lsmtree-common/src/main/java/edu/uci/ics/hyracks/storage/am/lsmtree/common/impls/LSMTreeBulkLoadContext.java
new file mode 100644
index 0000000..7a26e2d
--- /dev/null
+++ b/hyracks-storage-am-lsmtree-common/src/main/java/edu/uci/ics/hyracks/storage/am/lsmtree/common/impls/LSMTreeBulkLoadContext.java
@@ -0,0 +1,27 @@
+package edu.uci.ics.hyracks.storage.am.lsmtree.common.impls;
+
+import edu.uci.ics.hyracks.api.exceptions.HyracksDataException;
+import edu.uci.ics.hyracks.storage.am.common.api.IIndexBulkLoadContext;
+import edu.uci.ics.hyracks.storage.am.common.api.ITreeIndex;
+import edu.uci.ics.hyracks.storage.am.common.api.TreeIndexException;
+
+public class LSMTreeBulkLoadContext implements IIndexBulkLoadContext {
+    private final ITreeIndex tree;
+    private IIndexBulkLoadContext bulkLoadCtx;
+
+    public LSMTreeBulkLoadContext(ITreeIndex tree) {
+        this.tree = tree;
+    }
+
+    public void beginBulkLoad(float fillFactor) throws HyracksDataException, TreeIndexException {
+        bulkLoadCtx = tree.beginBulkLoad(fillFactor);
+    }
+
+    public ITreeIndex getTree() {
+        return tree;
+    }
+
+    public IIndexBulkLoadContext getBulkLoadCtx() {
+        return bulkLoadCtx;
+    }
+}
\ No newline at end of file
diff --git a/hyracks-storage-am-lsmtree-common/src/main/java/edu/uci/ics/hyracks/storage/am/lsmtree/common/impls/TreeFactory.java b/hyracks-storage-am-lsmtree-common/src/main/java/edu/uci/ics/hyracks/storage/am/lsmtree/common/impls/TreeFactory.java
index 8b59129..1200022 100644
--- a/hyracks-storage-am-lsmtree-common/src/main/java/edu/uci/ics/hyracks/storage/am/lsmtree/common/impls/TreeFactory.java
+++ b/hyracks-storage-am-lsmtree-common/src/main/java/edu/uci/ics/hyracks/storage/am/lsmtree/common/impls/TreeFactory.java
@@ -27,4 +27,7 @@
 
     public abstract ITreeIndex createIndexInstance(int fileId);
 
+    public IBufferCache getBufferCache() {
+        return bufferCache;
+    }
 }
diff --git a/hyracks-storage-am-lsmtree-rtree/src/main/java/edu/uci/ics/hyracks/storage/am/lsmtree/rtree/impls/LSMRTree.java b/hyracks-storage-am-lsmtree-rtree/src/main/java/edu/uci/ics/hyracks/storage/am/lsmtree/rtree/impls/LSMRTree.java
index 251c5a0..fb05090 100644
--- a/hyracks-storage-am-lsmtree-rtree/src/main/java/edu/uci/ics/hyracks/storage/am/lsmtree/rtree/impls/LSMRTree.java
+++ b/hyracks-storage-am-lsmtree-rtree/src/main/java/edu/uci/ics/hyracks/storage/am/lsmtree/rtree/impls/LSMRTree.java
@@ -7,7 +7,6 @@
 import edu.uci.ics.hyracks.api.io.FileReference;
 import edu.uci.ics.hyracks.dataflow.common.data.accessors.ITupleReference;
 import edu.uci.ics.hyracks.storage.am.btree.api.IBTreeLeafFrame;
-import edu.uci.ics.hyracks.storage.am.btree.exceptions.BTreeDuplicateKeyException;
 import edu.uci.ics.hyracks.storage.am.btree.impls.BTree;
 import edu.uci.ics.hyracks.storage.am.btree.impls.BTreeRangeSearchCursor;
 import edu.uci.ics.hyracks.storage.am.btree.impls.RangePredicate;
@@ -24,86 +23,79 @@
 import edu.uci.ics.hyracks.storage.am.common.ophelpers.MultiComparator;
 import edu.uci.ics.hyracks.storage.am.lsmtree.common.api.ILSMTree;
 import edu.uci.ics.hyracks.storage.am.lsmtree.common.freepage.InMemoryFreePageManager;
+import edu.uci.ics.hyracks.storage.am.lsmtree.common.impls.LSMTreeBulkLoadContext;
+import edu.uci.ics.hyracks.storage.am.lsmtree.common.impls.TreeFactory;
 import edu.uci.ics.hyracks.storage.am.rtree.api.IRTreeInteriorFrame;
 import edu.uci.ics.hyracks.storage.am.rtree.api.IRTreeLeafFrame;
 import edu.uci.ics.hyracks.storage.am.rtree.impls.RTree;
 import edu.uci.ics.hyracks.storage.am.rtree.impls.RTreeSearchCursor;
 import edu.uci.ics.hyracks.storage.am.rtree.impls.SearchPredicate;
 import edu.uci.ics.hyracks.storage.common.buffercache.IBufferCache;
-import edu.uci.ics.hyracks.storage.common.file.IFileMapManager;
+import edu.uci.ics.hyracks.storage.common.file.IFileMapProvider;
 
 public class LSMRTree implements ILSMTree {
 
-    private final IBufferCache bufferCache;
+    // In-memory components.
     private RTree memRTree;
-    private BTree memBTree;
-    private String rtreeFileName;
-    private String btreeFileName;
+    private final BTree memBTree;
+    private final InMemoryFreePageManager memFreePageManager;
     private int rtreeFileId;
     private int btreeFileId;
-    private boolean created;
 
-    private final InMemoryFreePageManager memFreePageManager;
+    // On-disk components.
+    private final String onDiskDir;
+    private final RTreeFactory diskRTreeFactory;
+    private final BTreeFactory diskBTreeFactory;
+    private final IBufferCache diskBufferCache;
+    private final IFileMapProvider diskFileMapProvider;
+    private LinkedList<RTree> onDiskRTrees = new LinkedList<RTree>();
+    private LinkedList<RTree> mergedRTrees = new LinkedList<RTree>();
+    private LinkedList<BTree> onDiskBTrees = new LinkedList<BTree>();
+    private LinkedList<BTree> mergedBTrees = new LinkedList<BTree>();
+    private int onDiskRTreeCount;
+    private int onDiskBTreeCount;
+
+    // Common for in-memory and on-disk components.
     private final ITreeIndexFrameFactory rtreeInteriorFrameFactory;
     private final ITreeIndexFrameFactory btreeInteriorFrameFactory;
     private final ITreeIndexFrameFactory rtreeLeafFrameFactory;
     private final ITreeIndexFrameFactory btreeLeafFrameFactory;
-    private final MultiComparator rtreeCmp;
     private final MultiComparator btreeCmp;
 
-    // TODO: change to private, it's public only for LSMTreeSearchTest
-    public LinkedList<ITreeIndex> inDiskRTreeList;
-    public LinkedList<ITreeIndex> inDiskBTreeList;
-
-    private LinkedList<ITreeIndex> mergedInDiskRTreeList;
-    private LinkedList<ITreeIndex> mergedInDiskBTreeList;
-    private int inDiskTreeCounter;
-    private final RTreeFactory rTreeFactory;
-    private final BTreeFactory bTreeFactory;
-    private final IFileMapManager fileMapManager;
+    // For dealing with concurrent accesses.
     private int threadRefCount;
     private boolean flushFlag;
 
-    public LSMRTree(IBufferCache rtreeMemCache, IBufferCache bufferCache, int fieldCount, MultiComparator rtreeCmp,
-            MultiComparator btreeCmp, InMemoryFreePageManager memFreePageManager,
-            ITreeIndexFrameFactory rtreeInteriorFrameFactory, ITreeIndexFrameFactory btreeInteriorFrameFactory,
-            ITreeIndexFrameFactory rtreeLeafFrameFactory, ITreeIndexFrameFactory btreeLeafFrameFactory,
-            RTreeFactory rTreeFactory, BTreeFactory bTreeFactory, IFileMapManager fileMapManager) {
-        this.bufferCache = bufferCache;
-        this.rtreeCmp = rtreeCmp;
-        this.btreeCmp = btreeCmp;
-        this.rtreeInteriorFrameFactory = rtreeInteriorFrameFactory;
-        this.btreeInteriorFrameFactory = btreeInteriorFrameFactory;
-        this.rtreeLeafFrameFactory = rtreeLeafFrameFactory;
-        this.btreeLeafFrameFactory = btreeLeafFrameFactory;
-        this.memFreePageManager = memFreePageManager;
-        this.rTreeFactory = rTreeFactory;
-        this.bTreeFactory = bTreeFactory;
-        this.inDiskRTreeList = new LinkedList<ITreeIndex>();
-        this.inDiskBTreeList = new LinkedList<ITreeIndex>();
-        this.inDiskTreeCounter = 0;
-        this.fileMapManager = fileMapManager;
-        this.threadRefCount = 0;
-        this.created = false;
-        this.flushFlag = false;
+    public LSMRTree(IBufferCache memBufferCache, InMemoryFreePageManager memFreePageManager,
+            ITreeIndexFrameFactory rtreeInteriorFrameFactory, ITreeIndexFrameFactory rtreeLeafFrameFactory,
+            ITreeIndexFrameFactory btreeInteriorFrameFactory, ITreeIndexFrameFactory btreeLeafFrameFactory,
+            String onDiskDir, RTreeFactory diskRTreeFactory, BTreeFactory diskBTreeFactory,
+            IFileMapProvider diskFileMapProvider, int fieldCount, MultiComparator rtreeCmp, MultiComparator btreeCmp) {
 
-        try {
-            this.rtreeFileName = this.fileMapManager.lookupFileName(this.rtreeFileId).toString();
-
-            this.btreeFileName = this.rtreeFileName + "-btree";
-            FileReference file = new FileReference(new File(this.btreeFileName));
-            this.bufferCache.createFile(file);
-            this.btreeFileId = fileMapManager.lookupFileId(file);
-            bufferCache.openFile(btreeFileId);
-
-        } catch (Exception e) {
-            e.printStackTrace();
-        }
-
-        memRTree = new RTree(rtreeMemCache, fieldCount, rtreeCmp, memFreePageManager, rtreeInteriorFrameFactory,
+        memRTree = new RTree(memBufferCache, fieldCount, rtreeCmp, memFreePageManager, rtreeInteriorFrameFactory,
                 rtreeLeafFrameFactory);
-        memBTree = new BTree(rtreeMemCache, fieldCount, btreeCmp, memFreePageManager, btreeInteriorFrameFactory,
+        memBTree = new BTree(memBufferCache, fieldCount, btreeCmp, memFreePageManager, btreeInteriorFrameFactory,
                 btreeLeafFrameFactory);
+
+        this.memFreePageManager = memFreePageManager;
+        this.rtreeInteriorFrameFactory = rtreeInteriorFrameFactory;
+        this.rtreeLeafFrameFactory = rtreeLeafFrameFactory;
+        this.btreeInteriorFrameFactory = btreeInteriorFrameFactory;
+        this.btreeLeafFrameFactory = btreeLeafFrameFactory;
+
+        this.diskBufferCache = diskRTreeFactory.getBufferCache();
+        this.diskFileMapProvider = diskFileMapProvider;
+        this.diskRTreeFactory = diskRTreeFactory;
+        this.diskBTreeFactory = diskBTreeFactory;
+        this.btreeCmp = btreeCmp;
+        this.onDiskRTreeCount = 0;
+        this.onDiskBTreeCount = 0;
+        this.threadRefCount = 0;
+        this.flushFlag = false;
+        if (!onDiskDir.endsWith(System.getProperty("file.separator"))) {
+            onDiskDir += System.getProperty("file.separator");
+        }
+        this.onDiskDir = onDiskDir;
     }
 
     @Override
@@ -113,19 +105,24 @@
 
     @Override
     public IIndexBulkLoadContext beginBulkLoad(float fillFactor) throws TreeIndexException, HyracksDataException {
-        // TODO Auto-generated method stub
-        return null;
+        RTree diskRTree = (RTree) createDiskTree(TreeType.rtree, diskRTreeFactory);
+        LSMTreeBulkLoadContext bulkLoadCtx = new LSMTreeBulkLoadContext(diskRTree);
+        bulkLoadCtx.beginBulkLoad(fillFactor);
+        return bulkLoadCtx;
     }
 
     @Override
     public void bulkLoadAddTuple(ITupleReference tuple, IIndexBulkLoadContext ictx) throws HyracksDataException {
-        // TODO Auto-generated method stub
+        LSMTreeBulkLoadContext bulkLoadCtx = (LSMTreeBulkLoadContext) ictx;
+        bulkLoadCtx.getTree().bulkLoadAddTuple(tuple, bulkLoadCtx.getBulkLoadCtx());
 
     }
 
     @Override
     public void endBulkLoad(IIndexBulkLoadContext ictx) throws HyracksDataException {
-        // TODO Auto-generated method stub
+        LSMTreeBulkLoadContext bulkLoadCtx = (LSMTreeBulkLoadContext) ictx;
+        bulkLoadCtx.getTree().endBulkLoad(bulkLoadCtx.getBulkLoadCtx());
+        onDiskRTrees.addFirst((RTree) bulkLoadCtx.getTree());
 
     }
 
@@ -167,14 +164,8 @@
 
     @Override
     public void create(int indexFileId) throws HyracksDataException {
-        if (created) {
-            return;
-        } else {
-            rtreeFileId = indexFileId;
-            memRTree.create(rtreeFileId);
-            memBTree.create(btreeFileId);
-            created = true;
-        }
+        memRTree.create(rtreeFileId);
+        memBTree.create(btreeFileId);
     }
 
     @Override
@@ -187,8 +178,34 @@
     public void close() {
         memRTree.close();
         memBTree.close();
-        this.rtreeFileId = -1;
+    }
 
+    private ITreeIndex createDiskTree(TreeType treeType, TreeFactory diskTreeFactory) throws HyracksDataException {
+        // Register the new tree file.
+        FileReference file = new FileReference(new File(getNextFileName(treeType)));
+        // TODO: Delete the file during cleanup.
+        diskBufferCache.createFile(file);
+        int diskTreeFileId = diskFileMapProvider.lookupFileId(file);
+        // TODO: Close the file during cleanup.
+        diskBufferCache.openFile(diskTreeFileId);
+        // Create new tree instance.
+        ITreeIndex diskTree = diskTreeFactory.createIndexInstance(diskTreeFileId);
+        diskTree.create(diskTreeFileId);
+        // TODO: Close the tree during cleanup.
+        diskTree.open(diskTreeFileId);
+        return diskTree;
+    }
+
+    private enum TreeType {
+        rtree, btree
+    }
+
+    private String getNextFileName(TreeType treeType) {
+        if (treeType == TreeType.rtree) {
+            return onDiskDir + treeType + "_" + onDiskRTreeCount++;
+        } else {
+            return onDiskDir + treeType + "_" + onDiskBTreeCount++;
+        }
     }
 
     @Override
@@ -196,68 +213,37 @@
 
         // Cursor setting -- almost the same as search, only difference is
         // "no cursor for in-memory tree"
-        int numberOfInDiskTrees;   
-        synchronized (inDiskRTreeList) {
-            numberOfInDiskTrees = inDiskRTreeList.size();
+        int numberOfInDiskTrees;
+        synchronized (onDiskRTrees) {
+            numberOfInDiskTrees = onDiskRTrees.size();
         }
-        
+
         RTreeSearchCursor[] rtreeCursors = new RTreeSearchCursor[numberOfInDiskTrees];
         BTreeRangeSearchCursor[] btreeCursors = new BTreeRangeSearchCursor[numberOfInDiskTrees];
-        
+
         for (int i = 0; i < numberOfInDiskTrees; i++) {
             rtreeCursors[i] = new RTreeSearchCursor((IRTreeInteriorFrame) rtreeInteriorFrameFactory.createFrame(),
                     (IRTreeLeafFrame) rtreeLeafFrameFactory.createFrame());
 
             btreeCursors[i] = new BTreeRangeSearchCursor((IBTreeLeafFrame) btreeLeafFrameFactory.createFrame(), false);
         }
-        
-        // Create a new in-Disk RTree
-        // Register the RTree information into system.
-        inDiskTreeCounter++;
-        FileReference rtreeFile = new FileReference(new File(getNextFileName(rtreeFileName, inDiskTreeCounter)));
-        // TODO: Delete the file during cleanup.
-        bufferCache.createFile(rtreeFile);
-        int newDiskRTreeId = fileMapManager.lookupFileId(rtreeFile);
-        // TODO: Close the file during cleanup.
-        bufferCache.openFile(newDiskRTreeId);
 
-        // Create new in-Disk RTree.
-        RTree mergedRTree = (RTree) rTreeFactory.createIndexInstance(newDiskRTreeId);
-        mergedRTree.create(newDiskRTreeId);
-        // TODO: Close the RTree during cleanup.
-        mergedRTree.open(newDiskRTreeId);
+        RTree mergedRTree = (RTree) createDiskTree(TreeType.rtree, diskRTreeFactory);
+        BTree mergedBTree = (BTree) createDiskTree(TreeType.btree, diskBTreeFactory);
 
-        
-        // Create a new in-Disk BTree, which have full fillfactor.
-        // Register the BTree information into system.
-        FileReference btreeFile = new FileReference(new File(getNextFileName(btreeFileName, inDiskTreeCounter)));
-        // TODO: Delete the file during cleanup.
-        bufferCache.createFile(btreeFile);
-        int newDiskBTreeId = fileMapManager.lookupFileId(btreeFile);
-        // TODO: Close the file during cleanup.
-        bufferCache.openFile(newDiskBTreeId);
-
-        // Create new in-Disk BTree.
-        BTree mergedBTree = (BTree) bTreeFactory.createIndexInstance(newDiskBTreeId);
-        mergedBTree.create(newDiskBTreeId);
-        // TODO: Close the BTree during cleanup.
-        mergedBTree.open(newDiskBTreeId);
-        
-        // BulkLoad the tuples from the RTree into the new disk RTree.
+        // BulkLoad the tuples from the trees into the new merged trees.
         IIndexBulkLoadContext rtreeBulkLoadCtx = mergedRTree.beginBulkLoad(1.0f);
-        
-        // BulkLoad the tuples from the in-memory tree into the new disk BTree.
         IIndexBulkLoadContext btreeBulkLoadCtx = mergedBTree.beginBulkLoad(1.0f);
-        
+
         for (int i = 0; i < numberOfInDiskTrees; i++) {
-            
+
             // scan the RTrees
             ITreeIndexCursor rtreeScanCursor = new RTreeSearchCursor(
                     (IRTreeInteriorFrame) rtreeInteriorFrameFactory.createFrame(),
                     (IRTreeLeafFrame) rtreeLeafFrameFactory.createFrame());
             SearchPredicate rtreeNullPredicate = new SearchPredicate(null, null);
 
-            ITreeIndexAccessor onDiskRTreeAccessor = inDiskRTreeList.get(i).createAccessor();
+            ITreeIndexAccessor onDiskRTreeAccessor = onDiskRTrees.get(i).createAccessor();
             onDiskRTreeAccessor.search(rtreeScanCursor, rtreeNullPredicate);
 
             try {
@@ -269,15 +255,14 @@
             } finally {
                 rtreeScanCursor.close();
             }
-           
 
             // scan the BTrees
             ITreeIndexCursor btreeScanCursor = new BTreeRangeSearchCursor(
                     (IBTreeLeafFrame) btreeLeafFrameFactory.createFrame(), false);
             RangePredicate btreeNullPredicate = new RangePredicate(true, null, null, true, true, null, null);
-            ITreeIndexAccessor onDiskBTreeAccessor = inDiskBTreeList.get(i).createAccessor();
+            ITreeIndexAccessor onDiskBTreeAccessor = onDiskBTrees.get(i).createAccessor();
             onDiskBTreeAccessor.search(btreeScanCursor, btreeNullPredicate);
-            
+
             try {
                 while (btreeScanCursor.hasNext()) {
                     btreeScanCursor.next();
@@ -287,7 +272,7 @@
             } finally {
                 btreeScanCursor.close();
             }
-            
+
         }
         mergedRTree.endBulkLoad(rtreeBulkLoadCtx);
         mergedBTree.endBulkLoad(btreeBulkLoadCtx);
@@ -298,7 +283,6 @@
 
     @Override
     public void flush() throws Exception {
-        inDiskTreeCounter++;
 
         // scan the RTree
         ITreeIndexCursor rtreeScanCursor = new RTreeSearchCursor(
@@ -309,86 +293,49 @@
         ITreeIndexAccessor memRTreeAccessor = memRTree.createAccessor();
         memRTreeAccessor.search(rtreeScanCursor, rtreeNullPredicate);
 
-        // Create a new in-Disk RTree
+        RTree diskRTree = (RTree) createDiskTree(TreeType.rtree, diskRTreeFactory);
 
-        // Register the RTree information into system.
-        FileReference rtreeFile = new FileReference(new File(getNextFileName(rtreeFileName, inDiskTreeCounter)));
-        // TODO: Delete the file during cleanup.
-        bufferCache.createFile(rtreeFile);
-        int newDiskRTreeId = fileMapManager.lookupFileId(rtreeFile);
-        // TODO: Close the file during cleanup.
-        bufferCache.openFile(newDiskRTreeId);
-
-        // Create new in-Disk RTree.
-        RTree inDiskRTree = (RTree) rTreeFactory.createIndexInstance(newDiskRTreeId);
-        inDiskRTree.create(newDiskRTreeId);
-        // TODO: Close the RTree during cleanup.
-        inDiskRTree.open(newDiskRTreeId);
-
-        // // BulkLoad the tuples from the in-memory tree into the new disk
-        // RTree.
-        IIndexBulkLoadContext rtreeBulkLoadCtx = inDiskRTree.beginBulkLoad(1.0f);
+        // BulkLoad the tuples from the in-memory tree into the new disk RTree.
+        IIndexBulkLoadContext rtreeBulkLoadCtx = diskRTree.beginBulkLoad(1.0f);
 
         try {
             while (rtreeScanCursor.hasNext()) {
                 rtreeScanCursor.next();
                 ITupleReference frameTuple = rtreeScanCursor.getTuple();
-                inDiskRTree.bulkLoadAddTuple(frameTuple, rtreeBulkLoadCtx);
+                diskRTree.bulkLoadAddTuple(frameTuple, rtreeBulkLoadCtx);
             }
         } finally {
             rtreeScanCursor.close();
         }
-        inDiskRTree.endBulkLoad(rtreeBulkLoadCtx);
+        diskRTree.endBulkLoad(rtreeBulkLoadCtx);
 
         // scan the BTree
         ITreeIndexCursor btreeScanCursor = new BTreeRangeSearchCursor(
                 (IBTreeLeafFrame) btreeLeafFrameFactory.createFrame(), false);
-        RangePredicate nullPred = new RangePredicate(true, null, null, true, true, null, null);
+        RangePredicate btreeNullPredicate = new RangePredicate(true, null, null, true, true, null, null);
         ITreeIndexAccessor memBTreeAccessor = memBTree.createAccessor();
-        memBTreeAccessor.search(btreeScanCursor, nullPred);
+        memBTreeAccessor.search(btreeScanCursor, btreeNullPredicate);
 
-        // Create a new in-Disk BTree, which have full fillfactor.
-
-        // Register the BTree information into system.
-        FileReference btreeFile = new FileReference(new File(getNextFileName(btreeFileName, inDiskTreeCounter)));
-        // TODO: Delete the file during cleanup.
-        bufferCache.createFile(btreeFile);
-        int newDiskBTreeId = fileMapManager.lookupFileId(btreeFile);
-        // TODO: Close the file during cleanup.
-        bufferCache.openFile(newDiskBTreeId);
-
-        // Create new in-Disk BTree.
-        BTree inDiskBTree = (BTree) bTreeFactory.createIndexInstance(newDiskBTreeId);
-        inDiskBTree.create(newDiskBTreeId);
-        // TODO: Close the BTree during cleanup.
-        inDiskBTree.open(newDiskBTreeId);
+        BTree diskBTree = (BTree) createDiskTree(TreeType.btree, diskBTreeFactory);
 
         // BulkLoad the tuples from the in-memory tree into the new disk BTree.
-        IIndexBulkLoadContext btreeBulkLoadCtx = inDiskBTree.beginBulkLoad(1.0f);
+        IIndexBulkLoadContext btreeBulkLoadCtx = diskBTree.beginBulkLoad(1.0f);
         try {
             while (btreeScanCursor.hasNext()) {
                 btreeScanCursor.next();
                 ITupleReference frameTuple = btreeScanCursor.getTuple();
-                inDiskBTree.bulkLoadAddTuple(frameTuple, btreeBulkLoadCtx);
+                diskBTree.bulkLoadAddTuple(frameTuple, btreeBulkLoadCtx);
             }
         } finally {
             btreeScanCursor.close();
         }
-        inDiskBTree.endBulkLoad(btreeBulkLoadCtx);
+        diskBTree.endBulkLoad(btreeBulkLoadCtx);
 
-        // After BulkLoading, Clear the in-memTrees
         resetInMemoryTrees();
 
-        synchronized (inDiskRTreeList) {
-            inDiskRTreeList.addFirst(inDiskRTree);
-        }
-        synchronized (inDiskBTreeList) {
-            inDiskBTreeList.addFirst(inDiskBTree);
-        }
-    }
+        onDiskRTrees.addFirst(diskRTree);
+        onDiskBTrees.addFirst(diskBTree);
 
-    private static final String getNextFileName(String fileName, int inDiskTreeCounter) {
-        return fileName + "-" + Integer.toString(inDiskTreeCounter);
     }
 
     public void resetInMemoryTrees() throws HyracksDataException {
@@ -396,11 +343,11 @@
         memRTree.create(rtreeFileId);
         memBTree.create(btreeFileId);
     }
-    
+
     public void threadEnter() {
         threadRefCount++;
     }
-    
+
     public void threadExit() throws Exception {
         synchronized (this) {
             threadRefCount--;
@@ -468,8 +415,8 @@
         }
 
         int numberOfInDiskTrees;
-        synchronized (inDiskRTreeList) {
-            numberOfInDiskTrees = inDiskRTreeList.size();
+        synchronized (onDiskRTrees) {
+            numberOfInDiskTrees = onDiskRTrees.size();
         }
 
         LSMRTreeCursorInitialState initialState = new LSMRTreeCursorInitialState(numberOfInDiskTrees + 1,
@@ -480,7 +427,7 @@
         ITreeIndexAccessor[] onDiskRTreeAccessors = new ITreeIndexAccessor[numberOfInDiskTrees];
 
         for (int i = 0; i < numberOfInDiskTrees; i++) {
-            onDiskRTreeAccessors[i] = inDiskRTreeList.get(i).createAccessor();
+            onDiskRTreeAccessors[i] = onDiskRTrees.get(i).createAccessor();
             onDiskRTreeAccessors[i].search(((LSMRTreeSearchCursor) cursor).getRTreeCursor(i + 1), rtreeSearchPred);
         }
 
@@ -489,8 +436,8 @@
                 rtreeSearchPred);
     }
 
-    public LinkedList<ITreeIndex> getInDiskBTreeList() {
-        return inDiskBTreeList;
+    public LinkedList<BTree> getOnDiskBTrees() {
+        return onDiskBTrees;
     }
 
     private LSMTreeOpContext createOpContext() {
@@ -549,4 +496,3 @@
     }
 
 }
-
diff --git a/hyracks-tests/hyracks-storage-am-lsmtree-rtree-test/src/test/java/edu/uci/ics/hyracks/storage/am/lsmtree/rtree/LSMRTreeTest.java b/hyracks-tests/hyracks-storage-am-lsmtree-rtree-test/src/test/java/edu/uci/ics/hyracks/storage/am/lsmtree/rtree/LSMRTreeTest.java
index cb21305..87ac1c6 100644
--- a/hyracks-tests/hyracks-storage-am-lsmtree-rtree-test/src/test/java/edu/uci/ics/hyracks/storage/am/lsmtree/rtree/LSMRTreeTest.java
+++ b/hyracks-tests/hyracks-storage-am-lsmtree-rtree-test/src/test/java/edu/uci/ics/hyracks/storage/am/lsmtree/rtree/LSMRTreeTest.java
@@ -53,7 +53,6 @@
 import edu.uci.ics.hyracks.storage.am.rtree.impls.SearchPredicate;
 import edu.uci.ics.hyracks.storage.am.rtree.util.RTreeUtils;
 import edu.uci.ics.hyracks.storage.common.buffercache.IBufferCache;
-import edu.uci.ics.hyracks.storage.common.file.IFileMapManager;
 import edu.uci.ics.hyracks.storage.common.file.IFileMapProvider;
 import edu.uci.ics.hyracks.test.support.TestStorageManagerComponentHolder;
 import edu.uci.ics.hyracks.test.support.TestUtils;
@@ -138,14 +137,14 @@
         LinkedListFreePageManagerFactory freePageManagerFactory = new LinkedListFreePageManagerFactory(bufferCache,
                 metaFrameFactory);
 
-        RTreeFactory rTreeFactory = new RTreeFactory(bufferCache, freePageManagerFactory, rtreeCmp, fieldCount,
+        RTreeFactory diskRTreeFactory = new RTreeFactory(bufferCache, freePageManagerFactory, rtreeCmp, fieldCount,
                 rtreeInteriorFrameFactory, rtreeLeafFrameFactory);
-        BTreeFactory bTreeFactory = new BTreeFactory(bufferCache, freePageManagerFactory, btreeCmp, fieldCount,
+        BTreeFactory diskBTreeFactory = new BTreeFactory(bufferCache, freePageManagerFactory, btreeCmp, fieldCount,
                 btreeInteriorFrameFactory, btreeLeafFrameFactory);
 
-        LSMRTree lsmRTree = new LSMRTree(memBufferCache, bufferCache, fieldCount, rtreeCmp, btreeCmp,
-                memFreePageManager, rtreeInteriorFrameFactory, btreeInteriorFrameFactory, rtreeLeafFrameFactory,
-                btreeLeafFrameFactory, rTreeFactory, bTreeFactory, (IFileMapManager) fmp);
+        LSMRTree lsmRTree = new LSMRTree(memBufferCache, memFreePageManager, rtreeInteriorFrameFactory,
+                rtreeLeafFrameFactory, btreeInteriorFrameFactory, btreeLeafFrameFactory, tmpDir, diskRTreeFactory,
+                diskBTreeFactory, fmp, fieldCount, rtreeCmp, btreeCmp);
 
         lsmRTree.create(fileId);
         lsmRTree.open(fileId);
@@ -336,5 +335,4 @@
         bufferCache.closeFile(fileId);
         memBufferCache.close();
     }
-
 }