ignite-db - refactor more

Project: http://git-wip-us.apache.org/repos/asf/ignite/repo
Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/7cb0605b
Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/7cb0605b
Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/7cb0605b

Branch: refs/heads/ignite-db-x-10884
Commit: 7cb0605bc28578ae822617165288abbf0a2d03f2
Parents: 43326c6
Author: S.Vladykin <[email protected]>
Authored: Wed Apr 13 02:28:30 2016 +0300
Committer: S.Vladykin <[email protected]>
Committed: Wed Apr 13 02:28:30 2016 +0300

----------------------------------------------------------------------
 .../cache/database/tree/io/BPlusIO.java         | 53 ++++++++++++++------
 .../cache/database/tree/io/BPlusInnerIO.java    | 42 +++++++---------
 .../cache/database/tree/io/BPlusLeafIO.java     | 28 +++--------
 .../query/h2/database/io/H2InnerIO.java         |  7 +--
 .../query/h2/database/io/H2LeafIO.java          |  2 +-
 5 files changed, 66 insertions(+), 66 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/7cb0605b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/database/tree/io/BPlusIO.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/database/tree/io/BPlusIO.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/database/tree/io/BPlusIO.java
index 6fb5333..bd09a0e 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/database/tree/io/BPlusIO.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/database/tree/io/BPlusIO.java
@@ -36,11 +36,30 @@ public abstract class BPlusIO<L> extends PageIO {
     /** */
     protected static final int ITEMS_OFF = REMOVE_ID_OFF + 8;
 
+    /** */
+    private final boolean canGetRow;
+
+    /** */
+    private final boolean leaf;
+
+    /** All the items must be of fixed size. */
+    protected final int itemSize;
+
     /**
      * @param ver Page format version.
+     * @param leaf If this is a leaf IO.
+     * @param canGetRow If we can get full row from this page.
+     * @param itemSize Single item size on page.
      */
-    protected BPlusIO(int ver) {
+    protected BPlusIO(int ver, boolean leaf, boolean canGetRow, int itemSize) {
         super(ver);
+
+        assert itemSize > 0 : itemSize;
+        assert canGetRow || !leaf: "leaf page always must be able to get full 
row";
+
+        this.leaf = leaf;
+        this.canGetRow = canGetRow;
+        this.itemSize = itemSize;
     }
 
     /** {@inheritDoc} */
@@ -56,7 +75,7 @@ public abstract class BPlusIO<L> extends PageIO {
      * @param buf Buffer.
      * @return Forward page ID.
      */
-    public long getForward(ByteBuffer buf) {
+    public final long getForward(ByteBuffer buf) {
         return buf.getLong(FORWARD_OFF);
     }
 
@@ -64,7 +83,7 @@ public abstract class BPlusIO<L> extends PageIO {
      * @param buf Buffer.
      * @param pageId Forward page ID.
      */
-    public void setForward(ByteBuffer buf, long pageId) {
+    public final void setForward(ByteBuffer buf, long pageId) {
         buf.putLong(FORWARD_OFF, pageId);
 
         assert getForward(buf) == pageId;
@@ -74,7 +93,7 @@ public abstract class BPlusIO<L> extends PageIO {
      * @param buf Buffer.
      * @return Remove ID.
      */
-    public long getRemoveId(ByteBuffer buf) {
+    public final long getRemoveId(ByteBuffer buf) {
         return buf.getLong(REMOVE_ID_OFF);
     }
 
@@ -82,7 +101,7 @@ public abstract class BPlusIO<L> extends PageIO {
      * @param buf Buffer.
      * @param rmvId Remove ID.
      */
-    public void setRemoveId(ByteBuffer buf, long rmvId) {
+    public final void setRemoveId(ByteBuffer buf, long rmvId) {
         buf.putLong(REMOVE_ID_OFF, rmvId);
 
         assert getRemoveId(buf) == rmvId;
@@ -92,7 +111,7 @@ public abstract class BPlusIO<L> extends PageIO {
      * @param buf Buffer.
      * @return Items count in the page.
      */
-    public int getCount(ByteBuffer buf) {
+    public final int getCount(ByteBuffer buf) {
         int cnt = buf.getShort(CNT_OFF) & 0xFFFF;
 
         assert cnt >= 0: cnt;
@@ -104,7 +123,7 @@ public abstract class BPlusIO<L> extends PageIO {
      * @param buf Buffer.
      * @param cnt Count.
      */
-    public void setCount(ByteBuffer buf, int cnt) {
+    public final void setCount(ByteBuffer buf, int cnt) {
         assert cnt >= 0: cnt;
 
         buf.putShort(CNT_OFF, (short)cnt);
@@ -113,9 +132,20 @@ public abstract class BPlusIO<L> extends PageIO {
     }
 
     /**
+     * @return {@code true} If we can get the whole row from this page using
+     * method {@link BPlusTree#getRow(BPlusIO, ByteBuffer, int)}.
+     * Must always be {@code true} for leaf pages.
+     */
+    public final boolean canGetRow() {
+        return canGetRow;
+    }
+
+    /**
      * @return {@code true} if it is a leaf page.
      */
-    public abstract boolean isLeaf();
+    public final boolean isLeaf() {
+        return leaf;
+    }
 
     /**
      * @param buf Buffer.
@@ -144,13 +174,6 @@ public abstract class BPlusIO<L> extends PageIO {
     public abstract void store(ByteBuffer dst, int dstIdx, BPlusIO<L> srcIo, 
ByteBuffer src, int srcIdx);
 
     /**
-     * @return {@code true} If we can get the whole row from this page using
-     * method {@link BPlusTree#getRow(BPlusIO, ByteBuffer, int)}.
-     * Must always be {@code true} for leaf pages.
-     */
-    public abstract boolean canGetRow();
-
-    /**
      * Copy items from source buffer to destination buffer. Both pages must be 
of the same type.
      *
      * @param src Source buffer.

http://git-wip-us.apache.org/repos/asf/ignite/blob/7cb0605b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/database/tree/io/BPlusInnerIO.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/database/tree/io/BPlusInnerIO.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/database/tree/io/BPlusInnerIO.java
index 9975be2..4ff00ed 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/database/tree/io/BPlusInnerIO.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/database/tree/io/BPlusInnerIO.java
@@ -24,33 +24,29 @@ import java.nio.ByteBuffer;
  */
 public abstract class BPlusInnerIO<L> extends BPlusIO<L> {
     /** */
-    protected static final int ITEM_SIZE = 16;
-
-    /** */
     protected static final int SHIFT_LEFT = ITEMS_OFF;
 
     /** */
-    protected static final int SHIFT_LINK = ITEMS_OFF + 8;
+    protected static final int SHIFT_LINK = SHIFT_LEFT + 8;
 
     /** */
-    protected static final int SHIFT_RIGHT = ITEMS_OFF + 16;
+    protected final int SHIFT_RIGHT = SHIFT_LINK + itemSize;
 
     /**
      * @param ver Page format version.
+     * @param canGetRow If we can get full row from this page.
+     * @param itemSize Single item size on page.
      */
-    protected BPlusInnerIO(int ver) {
-        super(ver);
-    }
-
-    /** {@inheritDoc} */
-    @Override public int getMaxCount(ByteBuffer buf) {
-        //  (capacity - ITEMS_OFF - RIGHTMOST_PAGE_ID_SLOT_SIZE) / ITEM_SIZE
-        return (buf.capacity() - ITEMS_OFF - 8) >>> 4;
+    protected BPlusInnerIO(int ver, boolean canGetRow, int itemSize) {
+        super(ver, false, canGetRow, itemSize);
     }
 
     /** {@inheritDoc} */
-    @Override public boolean isLeaf() {
-        return false;
+    @Override public final int getMaxCount(ByteBuffer buf) {
+        // The structure of the page is the following:
+        // |ITEMS_OFF|w|A|x|B|y|C|z|
+        // where capital letters are data items, lowercase letters are 8 byte 
page references.
+        return (buf.capacity() - ITEMS_OFF - 8) / (itemSize + 8);
     }
 
     /**
@@ -58,7 +54,7 @@ public abstract class BPlusInnerIO<L> extends BPlusIO<L> {
      * @param idx Index.
      * @return Page ID.
      */
-    public long getLeft(ByteBuffer buf, int idx) {
+    public final long getLeft(ByteBuffer buf, int idx) {
         return buf.getLong(offset(idx, SHIFT_LEFT));
     }
 
@@ -67,7 +63,7 @@ public abstract class BPlusInnerIO<L> extends BPlusIO<L> {
      * @param idx Index.
      * @param pageId Page ID.
      */
-    public void setLeft(ByteBuffer buf, int idx, long pageId) {
+    public final void setLeft(ByteBuffer buf, int idx, long pageId) {
         buf.putLong(offset(idx, SHIFT_LEFT), pageId);
 
         assert pageId == getLeft(buf, idx);
@@ -78,7 +74,7 @@ public abstract class BPlusInnerIO<L> extends BPlusIO<L> {
      * @param idx Index.
      * @return Page ID.
      */
-    public long getRight(ByteBuffer buf, int idx) {
+    public final long getRight(ByteBuffer buf, int idx) {
         return buf.getLong(offset(idx, SHIFT_RIGHT));
     }
 
@@ -87,14 +83,14 @@ public abstract class BPlusInnerIO<L> extends BPlusIO<L> {
      * @param idx Index.
      * @param pageId Page ID.
      */
-    public void setRight(ByteBuffer buf, int idx, long pageId) {
+    public final void setRight(ByteBuffer buf, int idx, long pageId) {
         buf.putLong(offset(idx, SHIFT_RIGHT), pageId);
 
         assert pageId == getRight(buf, idx);
     }
 
     /** {@inheritDoc} */
-    @Override public void copyItems(ByteBuffer src, ByteBuffer dst, int 
srcIdx, int dstIdx, int cnt,
+    @Override public final void copyItems(ByteBuffer src, ByteBuffer dst, int 
srcIdx, int dstIdx, int cnt,
         boolean cpLeft) {
         assert srcIdx != dstIdx || src != dst;
 
@@ -123,9 +119,7 @@ public abstract class BPlusInnerIO<L> extends BPlusIO<L> {
      * @param shift It can be either link itself or left or right page ID.
      * @return Offset from byte buffer begin in bytes.
      */
-    protected static int offset(int idx, int shift) {
-        assert idx >= 0: idx;
-
-        return shift + ITEM_SIZE * idx;
+    protected final int offset(int idx, int shift) {
+        return shift + (8 + itemSize) * idx;
     }
 }

http://git-wip-us.apache.org/repos/asf/ignite/blob/7cb0605b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/database/tree/io/BPlusLeafIO.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/database/tree/io/BPlusLeafIO.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/database/tree/io/BPlusLeafIO.java
index cc72a99..9e7e4fb 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/database/tree/io/BPlusLeafIO.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/processors/cache/database/tree/io/BPlusLeafIO.java
@@ -23,33 +23,21 @@ import java.nio.ByteBuffer;
  * Abstract IO routines for B+Tree leaf pages.
  */
 public abstract class BPlusLeafIO<L> extends BPlusIO<L> {
-    /** */
-    protected static final int ITEM_SIZE = 8;
-
     /**
      * @param ver Page format version.
+     * @param itemSize Single item size on page.
      */
-    protected BPlusLeafIO(int ver) {
-        super(ver);
-    }
-
-    /** {@inheritDoc} */
-    @Override public boolean isLeaf() {
-        return true;
-    }
-
-    /** {@inheritDoc} */
-    @Override public int getMaxCount(ByteBuffer buf) {
-        return (buf.capacity() - ITEMS_OFF) >>> 3; // divide by ITEM_SIZE
+    protected BPlusLeafIO(int ver, int itemSize) {
+        super(ver, true, true, itemSize);
     }
 
     /** {@inheritDoc} */
-    @Override public final boolean canGetRow() {
-        return true;
+    @Override public final int getMaxCount(ByteBuffer buf) {
+        return (buf.capacity() - ITEMS_OFF) / itemSize;
     }
 
     /** {@inheritDoc} */
-    @Override public void copyItems(ByteBuffer src, ByteBuffer dst, int 
srcIdx, int dstIdx, int cnt,
+    @Override public final void copyItems(ByteBuffer src, ByteBuffer dst, int 
srcIdx, int dstIdx, int cnt,
         boolean cpLeft) {
         assert srcIdx != dstIdx || src != dst;
 
@@ -67,9 +55,9 @@ public abstract class BPlusLeafIO<L> extends BPlusIO<L> {
      * @param idx Index of item.
      * @return Offset.
      */
-    protected static int offset(int idx) {
+    protected final int offset(int idx) {
         assert idx >= 0: idx;
 
-        return ITEMS_OFF + idx * ITEM_SIZE;
+        return ITEMS_OFF + idx * itemSize;
     }
 }

http://git-wip-us.apache.org/repos/asf/ignite/blob/7cb0605b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/database/io/H2InnerIO.java
----------------------------------------------------------------------
diff --git 
a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/database/io/H2InnerIO.java
 
b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/database/io/H2InnerIO.java
index 3fa51c1..67cf415 100644
--- 
a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/database/io/H2InnerIO.java
+++ 
b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/database/io/H2InnerIO.java
@@ -37,7 +37,7 @@ public class H2InnerIO extends BPlusInnerIO<SearchRow> 
implements H2RowLinkIO {
      * @param ver Page format version.
      */
     private H2InnerIO(int ver) {
-        super(ver);
+        super(ver, true, 8);
     }
 
     /** {@inheritDoc} */
@@ -62,11 +62,6 @@ public class H2InnerIO extends BPlusInnerIO<SearchRow> 
implements H2RowLinkIO {
     }
 
     /** {@inheritDoc} */
-    @Override public boolean canGetRow() {
-        return true; // We can get row from link.
-    }
-
-    /** {@inheritDoc} */
     @Override public long getLink(ByteBuffer buf, int idx) {
         assert idx < getCount(buf): idx;
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/7cb0605b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/database/io/H2LeafIO.java
----------------------------------------------------------------------
diff --git 
a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/database/io/H2LeafIO.java
 
b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/database/io/H2LeafIO.java
index 215d856..ecaab95 100644
--- 
a/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/database/io/H2LeafIO.java
+++ 
b/modules/indexing/src/main/java/org/apache/ignite/internal/processors/query/h2/database/io/H2LeafIO.java
@@ -37,7 +37,7 @@ public class H2LeafIO extends BPlusLeafIO<SearchRow> 
implements H2RowLinkIO {
      * @param ver Page format version.
      */
     protected H2LeafIO(int ver) {
-        super(ver);
+        super(ver, 8);
     }
 
     /** {@inheritDoc} */

Reply via email to