Added auto-enlistment of tree indexes, i.e., the in-memory state of tree indexes is now automatically (re-)created upon access to the index through an operator. Briefly tested that this feature works, but unittests still need to be added.

git-svn-id: https://hyracks.googlecode.com/svn/trunk@592 123451ca-8445-de46-9d55-352943316053
diff --git a/hyracks/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/dataflow/TreeIndexOpHelper.java b/hyracks/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/dataflow/TreeIndexOpHelper.java
index 281693d..2b53592 100644
--- a/hyracks/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/dataflow/TreeIndexOpHelper.java
+++ b/hyracks/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/dataflow/TreeIndexOpHelper.java
@@ -58,38 +58,21 @@
 
         FileReference f = fileSplitProvider.getFileSplits()[partition].getLocalFile();
         boolean fileIsMapped = fileMapProvider.isMapped(f);
-
-        switch (mode) {
-
-            case OPEN: {
-                if (!fileIsMapped) {
-                    throw new HyracksDataException("Trying to open tree index from unmapped file " + f.toString());
-                }
-            }
-                break;
-
-            case CREATE:
-            case ENLIST: {
-                if (!fileIsMapped) {
-                    bufferCache.createFile(f);
-                }
-            }
-                break;
-
+        if (!fileIsMapped) {
+            bufferCache.createFile(f);
         }
-
         int fileId = fileMapProvider.lookupFileId(f);
         try {
             bufferCache.openFile(fileId);
         } catch (HyracksDataException e) {
-            // revert state of buffer cache since file failed to open
+            // Revert state of buffer cache since file failed to open.
             if (!fileIsMapped) {
                 bufferCache.deleteFile(fileId);
             }
             throw e;
         }
 
-        // only set indexFileId member when openFile() succeeds,
+        // Only set indexFileId member when openFile() succeeds,
         // otherwise deinit() will try to close the file that failed to open
         indexFileId = fileId;
 
@@ -97,40 +80,33 @@
         leafFrame = opDesc.getTreeIndexLeafFactory().createFrame();
 
         IndexRegistry<ITreeIndex> treeIndexRegistry = opDesc.getTreeIndexRegistryProvider().getRegistry(ctx);
-        treeIndex = treeIndexRegistry.get(indexFileId);
-        if (treeIndex == null) {
-
-            // create new tree and register it
-            treeIndexRegistry.lock();
-            try {
-                // check if tree has already been registered by another thread
-                treeIndex = treeIndexRegistry.get(indexFileId);
-                if (treeIndex == null) {
-                    // this thread should create and register the tree
-
-                    IBinaryComparator[] comparators = new IBinaryComparator[opDesc.getTreeIndexComparatorFactories().length];
-                    for (int i = 0; i < opDesc.getTreeIndexComparatorFactories().length; i++) {
-                        comparators[i] = opDesc.getTreeIndexComparatorFactories()[i].createBinaryComparator();
-                    }
-
-                    cmp = createMultiComparator(comparators);
-
-                    treeIndex = createTreeIndex();
-                    if (mode == IndexHelperOpenMode.CREATE) {
-                        ITreeIndexMetaDataFrame metaFrame = treeIndex.getFreePageManager().getMetaDataFrameFactory()
-                                .createFrame();
-                        try {
-                            treeIndex.create(indexFileId, leafFrame, metaFrame);
-                        } catch (Exception e) {
-                            throw new HyracksDataException(e);
-                        }
-                    }
-                    treeIndex.open(indexFileId);
-                    treeIndexRegistry.register(indexFileId, treeIndex);
-                }
-            } finally {
-                treeIndexRegistry.unlock();
+        // Create new tree and register it.
+        treeIndexRegistry.lock();
+        try {
+            // Check if tree has already been registered by another thread.
+            treeIndex = treeIndexRegistry.get(indexFileId);
+            if (treeIndex != null) {
+                return;
             }
+            IBinaryComparator[] comparators = new IBinaryComparator[opDesc.getTreeIndexComparatorFactories().length];
+            for (int i = 0; i < opDesc.getTreeIndexComparatorFactories().length; i++) {
+                comparators[i] = opDesc.getTreeIndexComparatorFactories()[i].createBinaryComparator();
+            }
+            cmp = createMultiComparator(comparators);
+            treeIndex = createTreeIndex();
+            if (mode == IndexHelperOpenMode.CREATE) {
+                ITreeIndexMetaDataFrame metaFrame = treeIndex.getFreePageManager().getMetaDataFrameFactory()
+                        .createFrame();
+                try {
+                    treeIndex.create(indexFileId, leafFrame, metaFrame);
+                } catch (Exception e) {
+                    throw new HyracksDataException(e);
+                }
+            }
+            treeIndex.open(indexFileId);
+            treeIndexRegistry.register(indexFileId, treeIndex);
+        } finally {
+            treeIndexRegistry.unlock();
         }
     }