You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by vo...@apache.org on 2015/11/18 14:40:06 UTC

[32/50] [abbrv] ignite git commit: IGNITE-1917: Partially added optimization based on confirmations.

IGNITE-1917: Partially added optimization based on confirmations.


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

Branch: refs/heads/ignite-1917
Commit: 37a1cbe1ed4a0625dc1381200b82683006ed0aa9
Parents: 19dddcc
Author: vozerov-gridgain <vo...@gridgain.com>
Authored: Wed Nov 18 09:58:48 2015 +0300
Committer: vozerov-gridgain <vo...@gridgain.com>
Committed: Wed Nov 18 09:58:48 2015 +0300

----------------------------------------------------------------------
 .../internal/portable/BinaryReaderExImpl.java   | 150 +++++++++++++++----
 .../internal/portable/PortableSchema.java       |  93 +++++++++++-
 2 files changed, 213 insertions(+), 30 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/37a1cbe1/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryReaderExImpl.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryReaderExImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryReaderExImpl.java
index ae540ee..3478ddb 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryReaderExImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryReaderExImpl.java
@@ -1044,9 +1044,14 @@ public class BinaryReaderExImpl implements BinaryReader, BinaryRawReaderEx, Obje
 
     /** {@inheritDoc} */
     @Override public int readInt(String fieldName) throws BinaryObjectException {
-        Integer val = readInt(fieldId(fieldName));
+        if (fieldOffset(fieldName) != 0) {
+            if (checkFlag(INT) == Flag.NULL)
+                return 0;
 
-        return val != null ? val : 0;
+            return in.readInt();
+        }
+        else
+            return 0;
     }
 
     /** {@inheritDoc} */
@@ -1129,7 +1134,14 @@ public class BinaryReaderExImpl implements BinaryReader, BinaryRawReaderEx, Obje
 
     /** {@inheritDoc} */
     @Nullable @Override public String readString(String fieldName) throws BinaryObjectException {
-        return readString(fieldId(fieldName));
+        if (fieldOffset(fieldName) != 0) {
+            if (checkFlag(STRING) == Flag.NULL)
+                return null;
+
+            return doReadString();
+        }
+        else
+            return null;
     }
 
     /** {@inheritDoc} */
@@ -2657,6 +2669,71 @@ public class BinaryReaderExImpl implements BinaryReader, BinaryRawReaderEx, Obje
     }
 
     /**
+     * @param name Field name.
+     * @return Offset.
+     */
+    private int fieldOffset(String name) {
+        assert hdrLen != 0;
+
+        if (footerLen == 0)
+            return 0;
+
+        if (userType) {
+            int order;
+
+            if (matching) {
+                int expOrder = matchingOrder++;
+
+                PortableSchema.Confirmation confirm = schema.confirmOrder(expOrder, name);
+
+                switch (confirm) {
+                    case CONFIRMED:
+                        // The best case: got order without ID calculation and (ID -> order) lookup.
+                        order = expOrder;
+
+                        break;
+
+                    case REJECTED:
+                        // Rejected, no more speculations are possible. Fallback to the slowest scenario.
+                        matching = false;
+
+                        order = schema.order(fieldId(name));
+
+                        break;
+
+                    default:
+                        // Field name is not know for this order. Need to calculate ID and repeat speculation.
+                        assert confirm == PortableSchema.Confirmation.CLARIFY;
+
+                        int requestedId = fieldId(name);
+                        int realId = schema.fieldId(expOrder);
+
+                        if (requestedId == realId) {
+                            // IDs matched, can register
+                            schema.clarifyFieldName(expOrder, name);
+
+                            order = expOrder;
+                        }
+                        else {
+                            // No match, stop further speculations.
+                            matching = false;
+
+                            order = schema.order(requestedId);
+                        }
+
+                        break;
+                }
+            }
+            else
+                order = schema.order(fieldId(name));
+
+            return userFieldPosition(order);
+        }
+        else
+            return systemFieldPosition(fieldId(name));
+    }
+
+    /**
      * @param id Field ID.
      * @return Field offset.
      */
@@ -2687,42 +2764,61 @@ public class BinaryReaderExImpl implements BinaryReader, BinaryRawReaderEx, Obje
             else
                 order = schema.order(id);
 
-            if (order != PortableSchema.ORDER_NOT_FOUND) {
-                int offsetPos = footerStart + order * (fieldIdLen + fieldOffsetLen) + fieldIdLen;
+            return userFieldPosition(order);
+        }
+        else
+            return systemFieldPosition(id);
+    }
 
-                int pos = start + PortableUtils.fieldOffsetRelative(in, offsetPos, fieldOffsetLen);
+    /**
+     * Set position for the given user field order and return it.
+     *
+     * @param order Order.
+     * @return Position.
+     */
+    private int userFieldPosition(int order) {
+        if (order != PortableSchema.ORDER_NOT_FOUND) {
+            int offsetPos = footerStart + order * (fieldIdLen + fieldOffsetLen) + fieldIdLen;
 
-                in.position(pos);
+            int pos = start + PortableUtils.fieldOffsetRelative(in, offsetPos, fieldOffsetLen);
 
-                return pos;
-            }
-            else
-                return 0;
+            in.position(pos);
+
+            return pos;
         }
-        else {
-            // System types are never written with compact footers because they do not have metadata.
-            assert footerLen == PortableUtils.FIELD_ID_LEN;
+        else
+            return 0;
+    }
 
-            int searchPos = footerStart;
-            int searchTail = searchPos + footerLen;
+    /**
+     * Set position for the given system field ID and return it.
+     *
+     * @param id Field ID.
+     * @return Position.
+     */
+    private int systemFieldPosition(int id) {
+        // System types are never written with compact footers because they do not have metadata.
+        assert footerLen == PortableUtils.FIELD_ID_LEN;
 
-            while (true) {
-                if (searchPos >= searchTail)
-                    return 0;
+        int searchPos = footerStart;
+        int searchTail = searchPos + footerLen;
 
-                int id0 = in.readIntPositioned(searchPos);
+        while (true) {
+            if (searchPos >= searchTail)
+                return 0;
 
-                if (id0 == id) {
-                    int pos = start + PortableUtils.fieldOffsetRelative(in, searchPos + PortableUtils.FIELD_ID_LEN,
-                        fieldOffsetLen);
+            int id0 = in.readIntPositioned(searchPos);
 
-                    in.position(pos);
+            if (id0 == id) {
+                int pos = start + PortableUtils.fieldOffsetRelative(in, searchPos + PortableUtils.FIELD_ID_LEN,
+                    fieldOffsetLen);
 
-                    return pos;
-                }
+                in.position(pos);
 
-                searchPos += PortableUtils.FIELD_ID_LEN + fieldOffsetLen;
+                return pos;
             }
+
+            searchPos += PortableUtils.FIELD_ID_LEN + fieldOffsetLen;
         }
     }
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/37a1cbe1/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableSchema.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableSchema.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableSchema.java
index 2d840ad..ff7124e 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableSchema.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableSchema.java
@@ -44,9 +44,15 @@ public class PortableSchema implements Externalizable {
     /** Empty cell. */
     private static final int MAP_EMPTY = 0;
 
+    /** Schema ID. */
+    private int schemaId;
+
     /** IDs depending on order. */
     private int[] ids;
 
+    /** Interned names of associated fields. */
+    private String[] names;
+
     /** ID-to-order data. */
     private int[] idToOrderData;
 
@@ -77,9 +83,6 @@ public class PortableSchema implements Externalizable {
     /** ID 4. */
     private int id7;
 
-    /** Schema ID. */
-    private int schemaId;
-
     /**
      * {@link Externalizable} support.
      */
@@ -118,6 +121,8 @@ public class PortableSchema implements Externalizable {
 
             initializeMap(ids);
         }
+
+        names = new String[fieldIds.size()];
     }
 
     /**
@@ -128,6 +133,44 @@ public class PortableSchema implements Externalizable {
     }
 
     /**
+     * Try speculatively confirming order for the given field name.
+     *
+     * @param expOrder Expected order.
+     * @param expName Expected name.
+     * @return Field ID.
+     */
+    @SuppressWarnings("StringEquality")
+    public Confirmation confirmOrder(int expOrder, String expName) {
+        assert expName != null;
+
+        if (expOrder < names.length) {
+            String name = names[expOrder];
+
+            // Note that we use only reference equality assuming that field names are interned literals.
+            if (name == expName)
+                return Confirmation.CONFIRMED;
+
+            if (name == null)
+                return Confirmation.CLARIFY;
+        }
+
+        return Confirmation.REJECTED;
+    }
+
+    /**
+     * Add field name.
+     *
+     * @param order Order.
+     * @param name Name.
+     */
+    public void clarifyFieldName(int order, String name) {
+        assert name != null;
+        assert order < names.length;
+
+        names[order] = name.intern();
+    }
+
+    /**
      * Get field ID by order in footer.
      *
      * @param order Order.
@@ -167,6 +210,7 @@ public class PortableSchema implements Externalizable {
             }
         }
         else
+            // TODO: Fix possible out of bounds problem.
             return ids[order];
     }
 
@@ -274,14 +318,41 @@ public class PortableSchema implements Externalizable {
         schemaId = in.readInt();
 
         if (in.readBoolean()) {
+            int size = 0;
+
             id0 = in.readInt();
+            if (id0 != 0)
+                size++;
+
             id1 = in.readInt();
+            if (id1 != 0)
+                size++;
+
             id2 = in.readInt();
+            if (id2 != 0)
+                size++;
+
             id3 = in.readInt();
+            if (id3 != 0)
+                size++;
+
             id4 = in.readInt();
+            if (id4 != 0)
+                size++;
+
             id5 = in.readInt();
+            if (id5 != 0)
+                size++;
+
             id6 = in.readInt();
+            if (id6 != 0)
+                size++;
+
             id7 = in.readInt();
+            if (id7 != 0)
+                size++;
+
+            names = new String[size];
         }
         else {
             int size = in.readInt();
@@ -292,6 +363,8 @@ public class PortableSchema implements Externalizable {
                 ids[i] = in.readInt();
 
             initializeMap(ids);
+
+            names = new String[size];
         }
     }
 
@@ -464,6 +537,20 @@ public class PortableSchema implements Externalizable {
     }
 
     /**
+     * Order confirmation result.
+     */
+    public enum Confirmation {
+        /** Confirmed. */
+        CONFIRMED,
+
+        /** Denied. */
+        REJECTED,
+
+        /** Field name clarification is needed. */
+        CLARIFY
+    }
+
+    /**
      * Result of map parsing.
      */
     private static class ParseResult {