Fixed sisues 49 and 50.

git-svn-id: https://hyracks.googlecode.com/svn/branches/hyracks_btree_updates_next@807 123451ca-8445-de46-9d55-352943316053
diff --git a/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/dataflow/IndexRegistry.java b/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/dataflow/IndexRegistry.java
index df372f4..5f3b0b3 100644
--- a/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/dataflow/IndexRegistry.java
+++ b/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/dataflow/IndexRegistry.java
@@ -16,26 +16,15 @@
 package edu.uci.ics.hyracks.storage.am.common.dataflow;
 
 import java.util.HashMap;
-import java.util.concurrent.locks.Lock;
-import java.util.concurrent.locks.ReentrantLock;
 
 public class IndexRegistry<IndexType> {
 
 	private HashMap<Integer, IndexType> map = new HashMap<Integer, IndexType>();
-	private Lock registryLock = new ReentrantLock();
 
 	public IndexType get(int fileId) {
 		return map.get(fileId);
 	}
 
-	public void lock() {
-		registryLock.lock();
-	}
-
-	public void unlock() {
-		registryLock.unlock();
-	}
-
 	public void register(int fileId, IndexType index) {
 		map.put(fileId, index);
 	}
diff --git a/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/dataflow/TreeIndexDropOperatorNodePushable.java b/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/dataflow/TreeIndexDropOperatorNodePushable.java
index 6614fba..65b799e 100644
--- a/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/dataflow/TreeIndexDropOperatorNodePushable.java
+++ b/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/dataflow/TreeIndexDropOperatorNodePushable.java
@@ -30,87 +30,75 @@
 import edu.uci.ics.hyracks.storage.common.buffercache.IBufferCache;
 import edu.uci.ics.hyracks.storage.common.file.IFileMapProvider;
 
-public class TreeIndexDropOperatorNodePushable extends
-		AbstractOperatorNodePushable {
-	private static final Logger LOGGER = Logger
-			.getLogger(TreeIndexDropOperatorNodePushable.class.getName());
+public class TreeIndexDropOperatorNodePushable extends AbstractOperatorNodePushable {
+    private static final Logger LOGGER = Logger.getLogger(TreeIndexDropOperatorNodePushable.class.getName());
 
-	private final IHyracksTaskContext ctx;
-	private IIndexRegistryProvider<ITreeIndex> treeIndexRegistryProvider;
-	private IStorageManagerInterface storageManager;
-	private IFileSplitProvider fileSplitProvider;
-	private int partition;
+    private final IHyracksTaskContext ctx;
+    private IIndexRegistryProvider<ITreeIndex> treeIndexRegistryProvider;
+    private IStorageManagerInterface storageManager;
+    private IFileSplitProvider fileSplitProvider;
+    private int partition;
 
-	public TreeIndexDropOperatorNodePushable(IHyracksTaskContext ctx,
-			IStorageManagerInterface storageManager,
-			IIndexRegistryProvider<ITreeIndex> treeIndexRegistryProvider,
-			IFileSplitProvider fileSplitProvider, int partition) {
-		this.ctx = ctx;
-		this.storageManager = storageManager;
-		this.treeIndexRegistryProvider = treeIndexRegistryProvider;
-		this.fileSplitProvider = fileSplitProvider;
-		this.partition = partition;
-	}
+    public TreeIndexDropOperatorNodePushable(IHyracksTaskContext ctx, IStorageManagerInterface storageManager,
+            IIndexRegistryProvider<ITreeIndex> treeIndexRegistryProvider, IFileSplitProvider fileSplitProvider,
+            int partition) {
+        this.ctx = ctx;
+        this.storageManager = storageManager;
+        this.treeIndexRegistryProvider = treeIndexRegistryProvider;
+        this.fileSplitProvider = fileSplitProvider;
+        this.partition = partition;
+    }
 
-	@Override
-	public void deinitialize() throws HyracksDataException {
-	}
+    @Override
+    public void deinitialize() throws HyracksDataException {
+    }
 
-	@Override
-	public int getInputArity() {
-		return 0;
-	}
+    @Override
+    public int getInputArity() {
+        return 0;
+    }
 
-	@Override
-	public IFrameWriter getInputFrameWriter(int index) {
-		return null;
-	}
+    @Override
+    public IFrameWriter getInputFrameWriter(int index) {
+        return null;
+    }
 
-	@Override
-	public void initialize() throws HyracksDataException {
-		try {
+    @Override
+    public void initialize() throws HyracksDataException {
+        try {
+            IndexRegistry<ITreeIndex> treeIndexRegistry = treeIndexRegistryProvider.getRegistry(ctx);
+            IBufferCache bufferCache = storageManager.getBufferCache(ctx);
+            IFileMapProvider fileMapProvider = storageManager.getFileMapProvider(ctx);
+            
+            FileReference f = fileSplitProvider.getFileSplits()[partition].getLocalFile();
+            int indexFileId = -1;
+            synchronized (fileMapProvider) {
+                boolean fileIsMapped = fileMapProvider.isMapped(f);
+                if (!fileIsMapped) {
+                    throw new HyracksDataException("Cannot drop Tree with name " + f.toString()
+                            + ". No file mapping exists.");
+                }
+                indexFileId = fileMapProvider.lookupFileId(f);
+            }
+            // Unregister tree instance.
+            synchronized (treeIndexRegistry) {
+                treeIndexRegistry.unregister(indexFileId);
+            }
 
-			IndexRegistry<ITreeIndex> treeIndexRegistry = treeIndexRegistryProvider
-					.getRegistry(ctx);
-			IBufferCache bufferCache = storageManager.getBufferCache(ctx);
-			IFileMapProvider fileMapProvider = storageManager
-					.getFileMapProvider(ctx);
+            // remove name to id mapping
+            bufferCache.deleteFile(indexFileId);
+        }
+        // TODO: for the time being we don't throw,
+        // with proper exception handling (no hanging job problem) we should
+        // throw
+        catch (Exception e) {
+            if (LOGGER.isLoggable(Level.WARNING)) {
+                LOGGER.warning("Tree Drop Operator Failed Due To Exception: " + e.getMessage());
+            }
+        }
+    }
 
-			FileReference f = fileSplitProvider.getFileSplits()[partition]
-					.getLocalFile();
-
-			boolean fileIsMapped = fileMapProvider.isMapped(f);
-			if (!fileIsMapped) {
-				throw new HyracksDataException("Cannot drop Tree with name "
-						+ f.toString() + ". No file mapping exists.");
-			}
-
-			int indexFileId = fileMapProvider.lookupFileId(f);
-
-			// unregister tree instance
-			treeIndexRegistry.lock();
-			try {
-				treeIndexRegistry.unregister(indexFileId);
-			} finally {
-				treeIndexRegistry.unlock();
-			}
-
-			// remove name to id mapping
-			bufferCache.deleteFile(indexFileId);
-		}
-		// TODO: for the time being we don't throw,
-		// with proper exception handling (no hanging job problem) we should
-		// throw
-		catch (Exception e) {
-			if (LOGGER.isLoggable(Level.WARNING)) {
-				LOGGER.warning("Tree Drop Operator Failed Due To Exception: "
-						+ e.getMessage());
-			}
-		}
-	}
-
-	@Override
-	public void setOutputFrameWriter(int index, IFrameWriter writer,
-			RecordDescriptor recordDesc) {
-	}
+    @Override
+    public void setOutputFrameWriter(int index, IFrameWriter writer, RecordDescriptor recordDesc) {
+    }
 }
\ No newline at end of file
diff --git a/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/dataflow/TreeIndexInsertUpdateDeleteOperatorNodePushable.java b/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/dataflow/TreeIndexInsertUpdateDeleteOperatorNodePushable.java
index 487d7ab..ba4106b 100644
--- a/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/dataflow/TreeIndexInsertUpdateDeleteOperatorNodePushable.java
+++ b/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/dataflow/TreeIndexInsertUpdateDeleteOperatorNodePushable.java
@@ -27,106 +27,99 @@
 import edu.uci.ics.hyracks.storage.am.common.api.ITreeIndexAccessor;
 import edu.uci.ics.hyracks.storage.am.common.ophelpers.IndexOp;
 
-public class TreeIndexInsertUpdateDeleteOperatorNodePushable extends
-		AbstractUnaryInputUnaryOutputOperatorNodePushable {
-	private final TreeIndexOpHelper treeIndexOpHelper;
-	private FrameTupleAccessor accessor;
-	private final IRecordDescriptorProvider recordDescProvider;
-	private final IndexOp op;
-	private final PermutingFrameTupleReference tuple = new PermutingFrameTupleReference();
-	private ByteBuffer writeBuffer;
-	private ITreeIndexAccessor indexAccessor;
+public class TreeIndexInsertUpdateDeleteOperatorNodePushable extends AbstractUnaryInputUnaryOutputOperatorNodePushable {
+    private final TreeIndexOpHelper treeIndexOpHelper;
+    private FrameTupleAccessor accessor;
+    private final IRecordDescriptorProvider recordDescProvider;
+    private final IndexOp op;
+    private final PermutingFrameTupleReference tuple = new PermutingFrameTupleReference();
+    private ByteBuffer writeBuffer;
+    private ITreeIndexAccessor indexAccessor;
 
-	public TreeIndexInsertUpdateDeleteOperatorNodePushable(
-			AbstractTreeIndexOperatorDescriptor opDesc,
-			IHyracksTaskContext ctx, int partition, int[] fieldPermutation,
-			IRecordDescriptorProvider recordDescProvider, IndexOp op) {
-		treeIndexOpHelper = opDesc.getTreeIndexOpHelperFactory()
-				.createTreeIndexOpHelper(opDesc, ctx, partition,
-						IndexHelperOpenMode.OPEN);
-		this.recordDescProvider = recordDescProvider;
-		this.op = op;
-		tuple.setFieldPermutation(fieldPermutation);
-	}
+    public TreeIndexInsertUpdateDeleteOperatorNodePushable(AbstractTreeIndexOperatorDescriptor opDesc,
+            IHyracksTaskContext ctx, int partition, int[] fieldPermutation,
+            IRecordDescriptorProvider recordDescProvider, IndexOp op) {
+        treeIndexOpHelper = opDesc.getTreeIndexOpHelperFactory().createTreeIndexOpHelper(opDesc, ctx, partition,
+                IndexHelperOpenMode.OPEN);
+        this.recordDescProvider = recordDescProvider;
+        this.op = op;
+        tuple.setFieldPermutation(fieldPermutation);
+    }
 
-	@Override
-	public void open() throws HyracksDataException {
-		AbstractTreeIndexOperatorDescriptor opDesc = (AbstractTreeIndexOperatorDescriptor) treeIndexOpHelper
-				.getOperatorDescriptor();
-		RecordDescriptor inputRecDesc = recordDescProvider
-				.getInputRecordDescriptor(opDesc.getOperatorId(), 0);
-		accessor = new FrameTupleAccessor(treeIndexOpHelper
-				.getHyracksTaskContext().getFrameSize(), inputRecDesc);
-		writeBuffer = treeIndexOpHelper.getHyracksTaskContext().allocateFrame();
-		writer.open();
-		try {
-			treeIndexOpHelper.init();
-			treeIndexOpHelper.getTreeIndex().open(
-					treeIndexOpHelper.getIndexFileId());
-			indexAccessor = treeIndexOpHelper.getTreeIndex().createAccessor();
-		} catch (Exception e) {
-			// cleanup in case of failure
-			treeIndexOpHelper.deinit();
-			throw new HyracksDataException(e);
-		}
-	}
+    @Override
+    public void open() throws HyracksDataException {
+        AbstractTreeIndexOperatorDescriptor opDesc = (AbstractTreeIndexOperatorDescriptor) treeIndexOpHelper
+                .getOperatorDescriptor();
+        RecordDescriptor inputRecDesc = recordDescProvider.getInputRecordDescriptor(opDesc.getOperatorId(), 0);
+        accessor = new FrameTupleAccessor(treeIndexOpHelper.getHyracksTaskContext().getFrameSize(), inputRecDesc);
+        writeBuffer = treeIndexOpHelper.getHyracksTaskContext().allocateFrame();
+        writer.open();
+        try {
+            treeIndexOpHelper.init();
+            treeIndexOpHelper.getTreeIndex().open(treeIndexOpHelper.getIndexFileId());
+            indexAccessor = treeIndexOpHelper.getTreeIndex().createAccessor();
+        } catch (Exception e) {
+            // cleanup in case of failure
+            treeIndexOpHelper.deinit();
+            throw new HyracksDataException(e);
+        }
+    }
 
-	@Override
-	public void nextFrame(ByteBuffer buffer) throws HyracksDataException {
-		final ITreeIndex treeIndex = treeIndexOpHelper.getTreeIndex();
-		accessor.reset(buffer);
+    @Override
+    public void nextFrame(ByteBuffer buffer) throws HyracksDataException {
+        final ITreeIndex treeIndex = treeIndexOpHelper.getTreeIndex();
+        accessor.reset(buffer);
 
-		int tupleCount = accessor.getTupleCount();
-		for (int i = 0; i < tupleCount; i++) {
-			tuple.reset(accessor, i);
-			try {
-				switch (op) {
+        int tupleCount = accessor.getTupleCount();
+        for (int i = 0; i < tupleCount; i++) {
+            tuple.reset(accessor, i);
+            try {
+                switch (op) {
 
-				case INSERT: {
-					indexAccessor.insert(tuple);
-					break;
-				}
+                    case INSERT: {
+                        indexAccessor.insert(tuple);
+                        break;
+                    }
 
-				case UPDATE: {
-					indexAccessor.update(tuple);
-					break;
-				}
-				
-				case DELETE: {
-					indexAccessor.delete(tuple);
-					break;
-				}
-					
-				default: {
-					throw new HyracksDataException("Unsupported operation "
-							+ op + " in tree index InsertUpdateDelete operator");
-				}
+                    case UPDATE: {
+                        indexAccessor.update(tuple);
+                        break;
+                    }
 
-				}
-			} catch (HyracksDataException e) {
-				throw e;
-			} catch (Exception e) {
-				throw new HyracksDataException(e);
-			}
-		}
+                    case DELETE: {
+                        indexAccessor.delete(tuple);
+                        break;
+                    }
 
-		// pass a copy of the frame to next op
-		System.arraycopy(buffer.array(), 0, writeBuffer.array(), 0,
-				buffer.capacity());
-		FrameUtils.flushFrame(writeBuffer, writer);
-	}
+                    default: {
+                        throw new HyracksDataException("Unsupported operation " + op
+                                + " in tree index InsertUpdateDelete operator");
+                    }
 
-	@Override
-	public void close() throws HyracksDataException {
-		try {
-			writer.close();
-		} finally {
-			treeIndexOpHelper.deinit();
-		}
-	}
+                }
+            } catch (HyracksDataException e) {
+                throw e;
+            } catch (Exception e) {
+                throw new HyracksDataException(e);
+            }
+        }
 
-	@Override
-	public void fail() throws HyracksDataException {
-		writer.fail();
-	}
+        // pass a copy of the frame to next op
+        System.arraycopy(buffer.array(), 0, writeBuffer.array(), 0, buffer.capacity());
+        FrameUtils.flushFrame(writeBuffer, writer);
+    }
+
+    @Override
+    public void close() throws HyracksDataException {
+        try {
+            writer.close();
+        } finally {
+            treeIndexOpHelper.deinit();
+        }
+    }
+
+    @Override
+    public void fail() throws HyracksDataException {
+        writer.fail();
+    }
 }
\ No newline at end of file
diff --git a/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/dataflow/TreeIndexOpHelper.java b/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/dataflow/TreeIndexOpHelper.java
index 353b013..ff2f043 100644
--- a/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/dataflow/TreeIndexOpHelper.java
+++ b/hyracks-storage-am-common/src/main/java/edu/uci/ics/hyracks/storage/am/common/dataflow/TreeIndexOpHelper.java
@@ -57,19 +57,22 @@
         IFileSplitProvider fileSplitProvider = opDesc.getTreeIndexFileSplitProvider();
 
         FileReference f = fileSplitProvider.getFileSplits()[partition].getLocalFile();
-        boolean fileIsMapped = fileMapProvider.isMapped(f);
-        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.
+        int fileId = -1;
+        synchronized (fileMapProvider) {
+            boolean fileIsMapped = fileMapProvider.isMapped(f);
             if (!fileIsMapped) {
-                bufferCache.deleteFile(fileId);
+                bufferCache.createFile(f);
             }
-            throw e;
+            fileId = fileMapProvider.lookupFileId(f);
+            try {
+                bufferCache.openFile(fileId);
+            } catch (HyracksDataException e) {
+                // Revert state of buffer cache since file failed to open.
+                if (!fileIsMapped) {
+                    bufferCache.deleteFile(fileId);
+                }
+                throw e;
+            }
         }
 
         // Only set indexFileId member when openFile() succeeds,
@@ -77,22 +80,19 @@
         indexFileId = fileId;
         IndexRegistry<ITreeIndex> treeIndexRegistry = opDesc.getTreeIndexRegistryProvider().getRegistry(ctx);
         // Create new tree and register it.
-        treeIndexRegistry.lock();
-        try {
+        synchronized (treeIndexRegistry) {
             // Check if tree has already been registered by another thread.
             treeIndex = treeIndexRegistry.get(indexFileId);
             if (treeIndex != null) {
                 return;
-            }           
+            }
             cmp = IndexUtils.createMultiComparator(opDesc.getTreeIndexComparatorFactories());
             treeIndex = createTreeIndex();
             if (mode == IndexHelperOpenMode.CREATE) {
-            	treeIndex.create(indexFileId);
+                treeIndex.create(indexFileId);
             }
             treeIndex.open(indexFileId);
             treeIndexRegistry.register(indexFileId, treeIndex);
-        } finally {
-            treeIndexRegistry.unlock();
         }
     }
 
diff --git a/hyracks-storage-am-invertedindex/src/main/java/edu/uci/ics/hyracks/storage/am/invertedindex/dataflow/InvertedIndexOpHelper.java b/hyracks-storage-am-invertedindex/src/main/java/edu/uci/ics/hyracks/storage/am/invertedindex/dataflow/InvertedIndexOpHelper.java
index 33b0eef..baa3052 100644
--- a/hyracks-storage-am-invertedindex/src/main/java/edu/uci/ics/hyracks/storage/am/invertedindex/dataflow/InvertedIndexOpHelper.java
+++ b/hyracks-storage-am-invertedindex/src/main/java/edu/uci/ics/hyracks/storage/am/invertedindex/dataflow/InvertedIndexOpHelper.java
@@ -31,8 +31,8 @@
 
 public final class InvertedIndexOpHelper {
 
-	private final TreeIndexOpHelper btreeOpHelper;
-	
+    private final TreeIndexOpHelper btreeOpHelper;
+
     private InvertedIndex invIndex;
     private int invIndexFileId = -1;
     private int partition;
@@ -40,63 +40,61 @@
     private IInvertedIndexOperatorDescriptorHelper opDesc;
     private IHyracksTaskContext ctx;
 
-	public InvertedIndexOpHelper(TreeIndexOpHelper btreeOpHelper,
-			IInvertedIndexOperatorDescriptorHelper opDesc,
-			final IHyracksTaskContext ctx, int partition) {
-		this.btreeOpHelper = btreeOpHelper;
-		this.opDesc = opDesc;
-		this.ctx = ctx;
-		this.partition = partition;
-	}
+    public InvertedIndexOpHelper(TreeIndexOpHelper btreeOpHelper, IInvertedIndexOperatorDescriptorHelper opDesc,
+            final IHyracksTaskContext ctx, int partition) {
+        this.btreeOpHelper = btreeOpHelper;
+        this.opDesc = opDesc;
+        this.ctx = ctx;
+        this.partition = partition;
+    }
 
-	// TODO: This is very similar to TreeIndexOpHelper. Maybe we can somehow merge them?
+    // TODO: This is very similar to TreeIndexOpHelper. Maybe we can somehow
+    // merge them?
     public void init() throws HyracksDataException {
         IBufferCache bufferCache = opDesc.getStorageManager().getBufferCache(ctx);
         IFileMapProvider fileMapProvider = opDesc.getStorageManager().getFileMapProvider(ctx);
         IFileSplitProvider fileSplitProvider = opDesc.getInvListsFileSplitProvider();
 
         FileReference f = fileSplitProvider.getFileSplits()[partition].getLocalFile();
-        boolean fileIsMapped = fileMapProvider.isMapped(f);
-        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.
+        int fileId = -1;
+        synchronized (fileMapProvider) {
+            boolean fileIsMapped = fileMapProvider.isMapped(f);
             if (!fileIsMapped) {
-                bufferCache.deleteFile(fileId);
+                bufferCache.createFile(f);
             }
-            throw e;
+            fileId = fileMapProvider.lookupFileId(f);
+            try {
+                bufferCache.openFile(fileId);
+            } catch (HyracksDataException e) {
+                // Revert state of buffer cache since file failed to open.
+                if (!fileIsMapped) {
+                    bufferCache.deleteFile(fileId);
+                }
+                throw e;
+            }
         }
 
-		// only set btreeFileId member when openFile() succeeds,
-		// otherwise deinit() will try to close the file that failed to open
-		invIndexFileId = fileId;
-		IndexRegistry<InvertedIndex> invIndexRegistry = opDesc
-				.getInvIndexRegistryProvider().getRegistry(ctx);
-		// create new inverted index and register it
-		invIndexRegistry.lock();
-		try {
-			// check if inverted index has already been registered by
-			// another thread
-			invIndex = invIndexRegistry.get(invIndexFileId);
-			if (invIndex == null) {
-				// Create and register the inverted index.
-				MultiComparator cmp = IndexUtils.createMultiComparator(opDesc
-						.getInvListsComparatorFactories());
-				// Assumes btreeOpHelper.init() has already been called.
-				BTree btree = (BTree) btreeOpHelper.getTreeIndex();
-				invIndex = new InvertedIndex(bufferCache, btree,
-						opDesc.getInvListsTypeTraits(), cmp);
-				invIndex.open(invIndexFileId);
-				invIndexRegistry.register(invIndexFileId, invIndex);
-			}
-		} finally {
-			invIndexRegistry.unlock();
-		}
-	}
+        // only set btreeFileId member when openFile() succeeds,
+        // otherwise deinit() will try to close the file that failed to open
+        invIndexFileId = fileId;
+        IndexRegistry<InvertedIndex> invIndexRegistry = opDesc.getInvIndexRegistryProvider().getRegistry(ctx);
+        // create new inverted index and register it
+        synchronized (invIndexRegistry) {
+            // check if inverted index has already been registered by
+            // another thread
+            invIndex = invIndexRegistry.get(invIndexFileId);
+            if (invIndex != null) {
+                return;
+            }
+            // Create and register the inverted index.
+            MultiComparator cmp = IndexUtils.createMultiComparator(opDesc.getInvListsComparatorFactories());
+            // Assumes btreeOpHelper.init() has already been called.
+            BTree btree = (BTree) btreeOpHelper.getTreeIndex();
+            invIndex = new InvertedIndex(bufferCache, btree, opDesc.getInvListsTypeTraits(), cmp);
+            invIndex.open(invIndexFileId);
+            invIndexRegistry.register(invIndexFileId, invIndex);
+        }
+    }
 
     public void deinit() throws HyracksDataException {
         if (invIndexFileId != -1) {
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 2fc3929..15fcc26 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
@@ -666,14 +666,17 @@
 
     private boolean invalidateIfFileIdMatch(int fileId, CachedPage cPage, boolean flushDirtyPages) throws HyracksDataException {
         if (BufferedFileHandle.getFileId(cPage.dpid) == fileId) {
-            if (cPage.dirty.get()) {
+            int pinCount;
+        	if (cPage.dirty.get()) {
 				if (flushDirtyPages) {
 					write(cPage);
 				}
                 cPage.dirty.set(false);
-                cPage.pinCount.decrementAndGet();
+                pinCount = cPage.pinCount.decrementAndGet();
+            } else {
+            	pinCount = cPage.pinCount.get();
             }
-            if (cPage.pinCount.get() != 0) {
+            if (pinCount != 0) {
                 throw new IllegalStateException("Page is pinned and file is being closed");
             }
             cPage.invalidate();