Cleanup Large Page Logic

Change-Id: I26f9a8e4530edfda1b5f78114a5c8038bf8a27f4
Reviewed-on: https://asterix-gerrit.ics.uci.edu/955
Reviewed-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Tested-by: Jenkins <jenkins@fulliautomatix.ics.uci.edu>
Reviewed-by: abdullah alamoudi <bamousaa@gmail.com>
diff --git a/hyracks-fullstack/hyracks/hyracks-storage-am-btree/src/main/java/org/apache/hyracks/storage/am/btree/frames/BTreeNSMLeafFrame.java b/hyracks-fullstack/hyracks/hyracks-storage-am-btree/src/main/java/org/apache/hyracks/storage/am/btree/frames/BTreeNSMLeafFrame.java
index 63f516e..f7af7ad 100644
--- a/hyracks-fullstack/hyracks/hyracks-storage-am-btree/src/main/java/org/apache/hyracks/storage/am/btree/frames/BTreeNSMLeafFrame.java
+++ b/hyracks-fullstack/hyracks/hyracks-storage-am-btree/src/main/java/org/apache/hyracks/storage/am/btree/frames/BTreeNSMLeafFrame.java
@@ -175,10 +175,7 @@
     }
 
     boolean isLargeTuple(int tupleSize) {
-        // TODO(mblow): make page size available to avoid calculating it
-        int pageSize = getLargeFlag() ? buf.capacity() / page.getFrameSizeMultiplier() : buf.capacity();
-
-        return tupleSize > getMaxTupleSize(pageSize);
+        return tupleSize > getMaxTupleSize(page.getPageSize());
     }
 
     @Override
@@ -247,9 +244,11 @@
                 targetFrame = this;
             }
             int tuplesToRight = tupleCount - tuplesToLeft;
-            if (getLargeFlag()) {
-                ((BTreeNSMLeafFrame) rightFrame).growCapacity(extraPageBlockHelper, bufferCache,
-                        buf.capacity() - rightFrame.getBuffer().capacity());
+
+            ((BTreeNSMLeafFrame) rightFrame).setLargeFlag(getLargeFlag());
+            int deltaPages = page.getFrameSizeMultiplier() - rightFrame.getPage().getFrameSizeMultiplier();
+            if (deltaPages > 0) {
+                ((BTreeNSMLeafFrame) rightFrame).growCapacity(extraPageBlockHelper, bufferCache, deltaPages);
             }
 
             ByteBuffer right = rightFrame.getBuffer();
@@ -298,18 +297,18 @@
 
     public void ensureCapacity(IBufferCache bufferCache, ITupleReference tuple,
                                IExtraPageBlockHelper extraPageBlockHelper) throws HyracksDataException {
+        // we call ensureCapacity() for large tuples- ensure large flag is set
+        setLargeFlag(true);
         int gapBytes = getBytesRequiredToWriteTuple(tuple) - getFreeContiguousSpace();
-        growCapacity(extraPageBlockHelper, bufferCache, gapBytes);
+        if (gapBytes > 0) {
+            int deltaPages = (int) Math.ceil((double) gapBytes / bufferCache.getPageSize());
+            growCapacity(extraPageBlockHelper, bufferCache, deltaPages);
+        }
     }
 
-    public void growCapacity(IExtraPageBlockHelper extraPageBlockHelper,
-            IBufferCache bufferCache, int delta) throws HyracksDataException {
-        setLargeFlag(true);
-        if (delta <= 0) {
-            return;
-        }
-        int deltaPages = (int) Math.ceil((double) delta / bufferCache.getPageSize());
-        int framePagesOld = getBuffer().capacity() / bufferCache.getPageSize();
+    private void growCapacity(IExtraPageBlockHelper extraPageBlockHelper,
+            IBufferCache bufferCache, int deltaPages) throws HyracksDataException {
+        int framePagesOld = page.getFrameSizeMultiplier();
         int newMultiplier = framePagesOld + deltaPages;
 
         // we need to get the old slot offsets before we grow
diff --git a/hyracks-fullstack/hyracks/hyracks-storage-am-lsm-common/src/main/java/org/apache/hyracks/storage/am/lsm/common/impls/VirtualBufferCache.java b/hyracks-fullstack/hyracks/hyracks-storage-am-lsm-common/src/main/java/org/apache/hyracks/storage/am/lsm/common/impls/VirtualBufferCache.java
index 40c7734..c8ce00f 100644
--- a/hyracks-fullstack/hyracks/hyracks-storage-am-lsm-common/src/main/java/org/apache/hyracks/storage/am/lsm/common/impls/VirtualBufferCache.java
+++ b/hyracks-fullstack/hyracks/hyracks-storage-am-lsm-common/src/main/java/org/apache/hyracks/storage/am/lsm/common/impls/VirtualBufferCache.java
@@ -405,6 +405,11 @@
         }
 
         @Override
+        public int getPageSize() {
+            return pageSize;
+        }
+
+        @Override
         public int getFrameSizeMultiplier() {
             return multiplier;
         }
diff --git a/hyracks-fullstack/hyracks/hyracks-storage-common/src/main/java/org/apache/hyracks/storage/common/buffercache/CachedPage.java b/hyracks-fullstack/hyracks/hyracks-storage-common/src/main/java/org/apache/hyracks/storage/common/buffercache/CachedPage.java
index 4541d52..f003649 100644
--- a/hyracks-fullstack/hyracks/hyracks-storage-common/src/main/java/org/apache/hyracks/storage/common/buffercache/CachedPage.java
+++ b/hyracks-fullstack/hyracks/hyracks-storage-common/src/main/java/org/apache/hyracks/storage/common/buffercache/CachedPage.java
@@ -165,6 +165,11 @@
     }
 
     @Override
+    public int getPageSize() {
+        return pageReplacementStrategy.getPageSize();
+    }
+
+    @Override
     public void setFrameSizeMultiplier(int multiplier) {
         this.multiplier = multiplier;
     }
diff --git a/hyracks-fullstack/hyracks/hyracks-storage-common/src/main/java/org/apache/hyracks/storage/common/buffercache/ICachedPage.java b/hyracks-fullstack/hyracks/hyracks-storage-common/src/main/java/org/apache/hyracks/storage/common/buffercache/ICachedPage.java
index 8107234..abbe233 100644
--- a/hyracks-fullstack/hyracks/hyracks-storage-common/src/main/java/org/apache/hyracks/storage/common/buffercache/ICachedPage.java
+++ b/hyracks-fullstack/hyracks/hyracks-storage-common/src/main/java/org/apache/hyracks/storage/common/buffercache/ICachedPage.java
@@ -38,5 +38,7 @@
 
     void setQueueInfo(IQueueInfo queueInfo);
 
+    int getPageSize();
+
     int getFrameSizeMultiplier();
 }