Fixed two bugs in the R-tree concurrency control and formatted the code

git-svn-id: https://hyracks.googlecode.com/svn/branches/hyracks_dev_next@530 123451ca-8445-de46-9d55-352943316053
diff --git a/hyracks-storage-am-rtree/src/main/java/edu/uci/ics/hyracks/storage/am/rtree/frames/RTreeNSMFrame.java b/hyracks-storage-am-rtree/src/main/java/edu/uci/ics/hyracks/storage/am/rtree/frames/RTreeNSMFrame.java
index 3623f04..17c10e5 100644
--- a/hyracks-storage-am-rtree/src/main/java/edu/uci/ics/hyracks/storage/am/rtree/frames/RTreeNSMFrame.java
+++ b/hyracks-storage-am-rtree/src/main/java/edu/uci/ics/hyracks/storage/am/rtree/frames/RTreeNSMFrame.java
@@ -154,7 +154,7 @@
                 }
             }
         }
-    }  
+    }
 
     @Override
     public ITreeIndexTupleReference createTupleReference() {
diff --git a/hyracks-storage-am-rtree/src/main/java/edu/uci/ics/hyracks/storage/am/rtree/impls/RTree.java b/hyracks-storage-am-rtree/src/main/java/edu/uci/ics/hyracks/storage/am/rtree/impls/RTree.java
index cf74ff9..8976409 100644
--- a/hyracks-storage-am-rtree/src/main/java/edu/uci/ics/hyracks/storage/am/rtree/impls/RTree.java
+++ b/hyracks-storage-am-rtree/src/main/java/edu/uci/ics/hyracks/storage/am/rtree/impls/RTree.java
@@ -795,6 +795,12 @@
                         if (tupleIndex == -1) {
                             ctx.traverseList.add(pageId, -1, parentIndex);
                             ctx.pathList.add(pageId, parentLsn, ctx.traverseList.size() - 1);
+
+                            node.releaseWriteLatch();
+                            incrementWriteLatchesReleased();
+                            bufferCache.unpin(node);
+                            incrementUnpins();
+                            continue;
                         } else {
                             ctx.pathList.clear();
                             fillPath(ctx, pageIndex);
@@ -872,8 +878,6 @@
     @Override
     public IIndexBulkLoadContext beginBulkLoad(float fillFactor, ITreeIndexFrame leafFrame,
             ITreeIndexFrame interiorFrame, ITreeIndexMetaDataFrame metaFrame) throws HyracksDataException {
-        // throw new HyracksDataException("RTree Bulkload not implemented.");
-
         if (loaded)
             throw new HyracksDataException("Trying to bulk-load RTree but has RTree already been loaded.");
 
@@ -884,20 +888,15 @@
 
     @Override
     public void bulkLoadAddTuple(IIndexBulkLoadContext ictx, ITupleReference tuple) throws HyracksDataException {
-        // throw new HyracksDataException("RTree Bulkload not implemented.");
-
         try {
             insert(tuple, ((BulkLoadContext) ictx).insertOpCtx);
         } catch (Exception e) {
-            // TODO Auto-generated catch block
-            e.printStackTrace();
+            throw new HyracksDataException("BulkLoad Error");
         }
     }
 
     @Override
     public void endBulkLoad(IIndexBulkLoadContext ictx) throws HyracksDataException {
-        // throw new HyracksDataException("RTree Bulkload not implemented.");
-
         loaded = true;
     }
 
diff --git a/hyracks-storage-am-rtree/src/main/java/edu/uci/ics/hyracks/storage/am/rtree/impls/RTreeSearchCursor.java b/hyracks-storage-am-rtree/src/main/java/edu/uci/ics/hyracks/storage/am/rtree/impls/RTreeSearchCursor.java
index 4edcaf8..86a7bfd 100644
--- a/hyracks-storage-am-rtree/src/main/java/edu/uci/ics/hyracks/storage/am/rtree/impls/RTreeSearchCursor.java
+++ b/hyracks-storage-am-rtree/src/main/java/edu/uci/ics/hyracks/storage/am/rtree/impls/RTreeSearchCursor.java
@@ -47,6 +47,7 @@
     private MultiComparator cmp;
 
     private ITreeIndexTupleReference frameTuple;
+    private boolean readLatched = false;
 
     private int pin = 0;
     private int unpin = 0;
@@ -59,9 +60,10 @@
 
     @Override
     public void close() throws Exception {
-        if (page != null) {
+        if (readLatched) {
             page.releaseReadLatch();
             bufferCache.unpin(page);
+            readLatched = false;
         }
         tupleIndex = 0;
         tupleIndexInc = 0;
@@ -79,6 +81,12 @@
     }
 
     public boolean fetchNextLeafPage() throws HyracksDataException {
+        if (readLatched) {
+            page.releaseReadLatch();
+            bufferCache.unpin(page);
+            unpin++;
+            readLatched = false;
+        }
         while (!pathList.isEmpty()) {
             int pageId = pathList.getLastPageId();
             int parentLsn = pathList.getLastPageLsn();
@@ -86,6 +94,7 @@
             ICachedPage node = bufferCache.pin(BufferedFileHandle.getDiskPageId(fileId, pageId), false);
             pin++;
             node.acquireReadLatch();
+            readLatched = true;
             interiorFrame.setPage(node);
             boolean isLeaf = interiorFrame.isLeaf();
             int pageLsn = interiorFrame.getPageLsn();
@@ -106,17 +115,13 @@
                     }
                 }
             } else {
-                if (page != null) {
-                    page.releaseReadLatch();
-                    bufferCache.unpin(page);
-                    unpin++;
-                }
                 page = node;
                 leafFrame.setPage(page);
                 tupleIndex = 0;
                 return true;
             }
             node.releaseReadLatch();
+            readLatched = false;
             bufferCache.unpin(node);
             unpin++;
         }
@@ -157,6 +162,7 @@
         // in case open is called multiple times without closing
         if (this.page != null) {
             this.page.releaseReadLatch();
+            readLatched = false;
             bufferCache.unpin(this.page);
             pathList.clear();
         }
@@ -192,4 +198,4 @@
     public void setFileId(int fileId) {
         this.fileId = fileId;
     }
-}
\ No newline at end of file
+}
diff --git a/hyracks-tests/hyracks-storage-am-rtree-test/src/test/java/edu/uci/ics/hyracks/storage/am/rtree/AbstractRTreeTest.java b/hyracks-tests/hyracks-storage-am-rtree-test/src/test/java/edu/uci/ics/hyracks/storage/am/rtree/AbstractRTreeTest.java
index 662d803..b2ab775 100644
--- a/hyracks-tests/hyracks-storage-am-rtree-test/src/test/java/edu/uci/ics/hyracks/storage/am/rtree/AbstractRTreeTest.java
+++ b/hyracks-tests/hyracks-storage-am-rtree-test/src/test/java/edu/uci/ics/hyracks/storage/am/rtree/AbstractRTreeTest.java
@@ -29,7 +29,7 @@
     protected final static String fileName = tmpDir + sep + simpleDateFormat.format(new Date());
 
     protected void print(String str) {
-        System.out.print(str);
+        System.err.print(str);
     }
 
     @AfterClass
diff --git a/hyracks-tests/hyracks-storage-am-rtree-test/src/test/java/edu/uci/ics/hyracks/storage/am/rtree/RTreeTest.java b/hyracks-tests/hyracks-storage-am-rtree-test/src/test/java/edu/uci/ics/hyracks/storage/am/rtree/RTreeTest.java
index 1ed8dd1..72c3f38 100644
--- a/hyracks-tests/hyracks-storage-am-rtree-test/src/test/java/edu/uci/ics/hyracks/storage/am/rtree/RTreeTest.java
+++ b/hyracks-tests/hyracks-storage-am-rtree-test/src/test/java/edu/uci/ics/hyracks/storage/am/rtree/RTreeTest.java
@@ -63,9 +63,9 @@
 
 public class RTreeTest extends AbstractRTreeTest {
 
-    private static final int PAGE_SIZE = 8192;
-    private static final int NUM_PAGES = 20;
-    private static final int MAX_OPEN_FILES = 20;
+    private static final int PAGE_SIZE = 256;
+    private static final int NUM_PAGES = 10;
+    private static final int MAX_OPEN_FILES = 10;
     private static final int HYRACKS_FRAME_SIZE = 128;
     private IHyracksStageletContext ctx = TestUtils.create(HYRACKS_FRAME_SIZE);
 
@@ -142,7 +142,7 @@
 
         Random rnd2 = new Random();
         rnd2.setSeed(50);
-        for (int i = 0; i < 10000; i++) {
+        for (int i = 0; i < 5000; i++) {
 
             double p1x = rnd.nextDouble();
             double p1y = rnd.nextDouble();
@@ -188,7 +188,7 @@
         }
 
         // rtree.printTree(leafFrame, interiorFrame, recDescSers);
-        // System.out.println();
+        // System.err.println();
 
         String rtreeStats = rtree.printStats();
         print(rtreeStats);
@@ -215,7 +215,7 @@
                 rtree.getRootPageId());
         TreeIndexStats stats = statsGatherer.gatherStats(leafFrame, interiorFrame, metaFrame);
         String string = stats.toString();
-        System.out.println(string);
+        System.err.println(string);
 
         rtree.close();
         bufferCache.closeFile(fileId);
@@ -294,7 +294,7 @@
         Random rnd = new Random();
         rnd.setSeed(50);
 
-        for (int i = 0; i < 10000; i++) {
+        for (int i = 0; i < 5000; i++) {
 
             double p1x = rnd.nextDouble();
             double p1y = rnd.nextDouble();
@@ -340,14 +340,14 @@
         }
 
         // rtree.printTree(leafFrame, interiorFrame, recDescSers);
-        // System.out.println();
+        // System.err.println();
 
         String rtreeStats = rtree.printStats();
         print(rtreeStats);
 
         RTreeOpContext deleteOpCtx = rtree.createOpContext(IndexOp.DELETE, leafFrame, interiorFrame, metaFrame);
         rnd.setSeed(50);
-        for (int i = 0; i < 10000; i++) {
+        for (int i = 0; i < 5000; i++) {
 
             double p1x = rnd.nextDouble();
             double p1y = rnd.nextDouble();
@@ -397,7 +397,7 @@
                 rtree.getRootPageId());
         TreeIndexStats stats = statsGatherer.gatherStats(leafFrame, interiorFrame, metaFrame);
         String string = stats.toString();
-        System.out.println(string);
+        System.err.println(string);
 
         rtree.close();
         bufferCache.closeFile(fileId);
@@ -481,7 +481,7 @@
         Random rnd = new Random();
         rnd.setSeed(50);
 
-        for (int i = 0; i < 10000; i++) {
+        for (int i = 0; i < 5000; i++) {
 
             double p1x = rnd.nextDouble();
             double p1y = rnd.nextDouble();
@@ -533,7 +533,7 @@
         }
 
         // rtree.printTree(leafFrame, interiorFrame, recDescSers);
-        // System.out.println();
+        // System.err.println();
 
         String rtreeStats = rtree.printStats();
         print(rtreeStats);
@@ -560,7 +560,7 @@
                 rtree.getRootPageId());
         TreeIndexStats stats = statsGatherer.gatherStats(leafFrame, interiorFrame, metaFrame);
         String string = stats.toString();
-        System.out.println(string);
+        System.err.println(string);
 
         rtree.close();
         bufferCache.closeFile(fileId);
diff --git a/hyracks-tests/hyracks-storage-am-rtree-test/src/test/java/edu/uci/ics/hyracks/storage/am/rtree/SearchCursorTest.java b/hyracks-tests/hyracks-storage-am-rtree-test/src/test/java/edu/uci/ics/hyracks/storage/am/rtree/SearchCursorTest.java
index 195213b..3060765 100644
--- a/hyracks-tests/hyracks-storage-am-rtree-test/src/test/java/edu/uci/ics/hyracks/storage/am/rtree/SearchCursorTest.java
+++ b/hyracks-tests/hyracks-storage-am-rtree-test/src/test/java/edu/uci/ics/hyracks/storage/am/rtree/SearchCursorTest.java
@@ -140,7 +140,7 @@
 
         Random rnd = new Random();
         rnd.setSeed(50);
-        for (int i = 0; i < 10000; i++) {
+        for (int i = 0; i < 5000; i++) {
 
             double p1x = rnd.nextDouble();
             double p1y = rnd.nextDouble();
@@ -177,11 +177,9 @@
             } catch (Exception e) {
                 e.printStackTrace();
             }
-
         }
 
         for (int i = 0; i < 50; i++) {
-
             double p1x = rnd.nextDouble();
             double p1y = rnd.nextDouble();
             double p2x = rnd.nextDouble();
@@ -232,7 +230,7 @@
                 searchCursor.close();
             }
 
-            System.out.println("There are " + results.size() + " objects that satisfy the query");
+            System.err.println("There are " + results.size() + " objects that satisfy the query");
         }
 
         rtree.close();