ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From voze...@apache.org
Subject [22/22] ignite git commit: IGNITE-1770: Added derived raw offset.
Date Mon, 26 Oct 2015 12:58:05 GMT
IGNITE-1770: Added derived raw offset.


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

Branch: refs/heads/ignite-1770
Commit: 40aa800a285bfc49f0a297d0d96151b3bd35e48c
Parents: ddccbc6
Author: vozerov-gridgain <vozerov@gridgain.com>
Authored: Mon Oct 26 15:58:22 2015 +0300
Committer: vozerov-gridgain <vozerov@gridgain.com>
Committed: Mon Oct 26 15:58:22 2015 +0300

----------------------------------------------------------------------
 .../portable/GridPortableMarshaller.java        |  6 +--
 .../portable/PortableClassDescriptor.java       | 16 ++----
 .../internal/portable/PortableReaderExImpl.java |  6 +--
 .../ignite/internal/portable/PortableUtils.java | 56 ++++++++++++++++++++
 .../internal/portable/PortableWriterExImpl.java | 45 ++++++++--------
 .../portable/builder/PortableBuilderImpl.java   |  7 ++-
 .../portable/builder/PortableBuilderReader.java |  2 +-
 .../GridPortableMarshallerSelfTest.java         | 23 --------
 8 files changed, 94 insertions(+), 67 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/40aa800a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMarshaller.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMarshaller.java
b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMarshaller.java
index e1621f0..9afe2ee 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMarshaller.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/GridPortableMarshaller.java
@@ -211,10 +211,10 @@ public class GridPortableMarshaller {
     public static final int TOTAL_LEN_POS = 12;
 
     /** */
-    public static final int RAW_DATA_OFF_POS = 16;
+    public static final int SCHEMA_ID_POS = 16;
 
-    /** Schema offset position. */
-    public static final int SCHEMA_OFF_POS = 20;
+    /** Schema or raw offset position. */
+    public static final int SCHEMA_OR_RAW_OFF_POS = 20;
 
     /** */
     public static final byte DFLT_HDR_LEN = 24;

http://git-wip-us.apache.org/repos/asf/ignite/blob/40aa800a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableClassDescriptor.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableClassDescriptor.java
b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableClassDescriptor.java
index 81c828c..17c677a 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableClassDescriptor.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableClassDescriptor.java
@@ -646,21 +646,13 @@ public class PortableClassDescriptor {
         if (writer.tryWriteAsHandle(obj))
             return false;
 
-        int pos = writer.position();
-
-        int reserved = PortableUtils.writeHeader(writer,
+        PortableUtils.writeHeader(
+            writer,
             userType,
             registered ? typeId : GridPortableMarshaller.UNREGISTERED_TYPE_ID,
             obj instanceof CacheObjectImpl ? 0 : obj.hashCode(),
-            registered ? null : cls.getName());
-
-        int current = writer.position();
-        int len = current - pos;
-
-        // Default raw offset (equal to header length).
-        writer.position(reserved + 4);
-        writer.doWriteInt(len);
-        writer.position(current);
+            registered ? null : cls.getName()
+        );
 
         return true;
     }

http://git-wip-us.apache.org/repos/asf/ignite/blob/40aa800a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableReaderExImpl.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableReaderExImpl.java
b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableReaderExImpl.java
index 9cf7a3c..2e16ee1 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableReaderExImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableReaderExImpl.java
@@ -96,7 +96,7 @@ import static org.apache.ignite.internal.portable.GridPortableMarshaller.OBJ_ARR
 import static org.apache.ignite.internal.portable.GridPortableMarshaller.OPTM_MARSH;
 import static org.apache.ignite.internal.portable.GridPortableMarshaller.PORTABLE_OBJ;
 import static org.apache.ignite.internal.portable.GridPortableMarshaller.PROPERTIES_MAP;
-import static org.apache.ignite.internal.portable.GridPortableMarshaller.RAW_DATA_OFF_POS;
+import static org.apache.ignite.internal.portable.GridPortableMarshaller.SCHEMA_ID_POS;
 import static org.apache.ignite.internal.portable.GridPortableMarshaller.SHORT;
 import static org.apache.ignite.internal.portable.GridPortableMarshaller.SHORT_ARR;
 import static org.apache.ignite.internal.portable.GridPortableMarshaller.STRING;
@@ -205,7 +205,7 @@ public class PortableReaderExImpl implements PortableReader, PortableRawReaderEx
 
         typeId = in.readInt();
 
-        rawOff = in.readInt(start + RAW_DATA_OFF_POS);
+        rawOff = PortableUtils.rawOffset(in, start);
 
         if (typeId == UNREGISTERED_TYPE_ID) {
             // Skip to the class name position.
@@ -2501,7 +2501,7 @@ public class PortableReaderExImpl implements PortableReader, PortableRawReaderEx
         assert hdrLen != 0;
 
         int searchHead = start + hdrLen;
-        int searchTail = start + in.readInt(start + RAW_DATA_OFF_POS);
+        int searchTail = start + PortableUtils.rawOffset(in, start);
 
         // TODO: Opto.
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/40aa800a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableUtils.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableUtils.java
b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableUtils.java
index c5b90c8..ed11574 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableUtils.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableUtils.java
@@ -17,7 +17,9 @@
 
 package org.apache.ignite.internal.portable;
 
+import org.apache.ignite.internal.portable.builder.PortableBuilderReader;
 import org.apache.ignite.internal.portable.builder.PortableLazyValue;
+import org.apache.ignite.internal.portable.streams.PortableInputStream;
 import org.apache.ignite.internal.util.typedef.internal.U;
 import org.apache.ignite.portable.PortableException;
 import org.apache.ignite.portable.PortableObject;
@@ -555,4 +557,58 @@ public class PortableUtils {
 
         return reserved;
     }
+
+    /**
+     * Get raw offset of the object.
+     *
+     * @param in Input stream.
+     * @param start Object start position inside the stream.
+     * @return Raw offset.
+     */
+    public static int rawOffset(PortableInputStream in, int start) {
+        int len = in.readInt(start + GridPortableMarshaller.TOTAL_LEN_POS);
+        int schemaId = in.readInt(start + GridPortableMarshaller.SCHEMA_ID_POS);
+
+        if (schemaId == 0)
+            // No schema, raw offset is located on schema offset position.
+            return in.readInt(start + GridPortableMarshaller.SCHEMA_OR_RAW_OFF_POS);
+        else {
+            // Schema exists.
+            int schemaOff = in.readInt(start + GridPortableMarshaller.SCHEMA_OR_RAW_OFF_POS);
+
+            if ((((len - schemaOff) >> 2) & 0x1) == 0x0)
+                // Even amount of records in schema => no raw offset.
+                return len;
+            else
+                // Odd amount of records in schema => raw offset is the very last 4 bytes
in object.
+                return in.readInt(start + len - 4);
+        }
+    }
+
+    /**
+     * Get raw offset of the object.
+     *
+     * @param in Input stream.
+     * @param start Object start position inside the stream.
+     * @return Raw offset.
+     */
+    public static int rawOffset(PortableBuilderReader in, int start) {
+        int len = in.readIntAbsolute(start + GridPortableMarshaller.TOTAL_LEN_POS);
+        int schemaId = in.readIntAbsolute(start + GridPortableMarshaller.SCHEMA_ID_POS);
+
+        if (schemaId == 0)
+            // No schema, raw offset is located on schema offset position.
+            return in.readIntAbsolute(start + GridPortableMarshaller.SCHEMA_OR_RAW_OFF_POS);
+        else {
+            // Schema exists.
+            int schemaOff = in.readIntAbsolute(start + GridPortableMarshaller.SCHEMA_OR_RAW_OFF_POS);
+
+            if ((((len - schemaOff) >> 2) & 0x1) == 0x0)
+                // Even amount of records in schema => no raw offset.
+                return len;
+            else
+                // Odd amount of records in schema => raw offset is the very last 4 bytes
in object.
+                return in.readIntAbsolute(start + len - 4);
+        }
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/40aa800a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableWriterExImpl.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableWriterExImpl.java
b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableWriterExImpl.java
index 1c1a185..110fe4f 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableWriterExImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableWriterExImpl.java
@@ -70,8 +70,8 @@ import static org.apache.ignite.internal.portable.GridPortableMarshaller.OBJ;
 import static org.apache.ignite.internal.portable.GridPortableMarshaller.OBJ_ARR;
 import static org.apache.ignite.internal.portable.GridPortableMarshaller.OPTM_MARSH;
 import static org.apache.ignite.internal.portable.GridPortableMarshaller.PORTABLE_OBJ;
-import static org.apache.ignite.internal.portable.GridPortableMarshaller.RAW_DATA_OFF_POS;
-import static org.apache.ignite.internal.portable.GridPortableMarshaller.SCHEMA_OFF_POS;
+import static org.apache.ignite.internal.portable.GridPortableMarshaller.SCHEMA_ID_POS;
+import static org.apache.ignite.internal.portable.GridPortableMarshaller.SCHEMA_OR_RAW_OFF_POS;
 import static org.apache.ignite.internal.portable.GridPortableMarshaller.SHORT;
 import static org.apache.ignite.internal.portable.GridPortableMarshaller.SHORT_ARR;
 import static org.apache.ignite.internal.portable.GridPortableMarshaller.STRING;
@@ -125,7 +125,10 @@ public class PortableWriterExImpl implements PortableWriter, PortableRawWriterEx
 
     /** Field infos. */
     // TODO: Optimize.
-    private List<Integer> tail;
+    private List<Integer> schema;
+
+    /** Schema ID. */
+    private int schemaId;
 
     /**
      * @param ctx Context.
@@ -336,25 +339,27 @@ public class PortableWriterExImpl implements PortableWriter, PortableRawWriterEx
      * - writing schema to the tail.
      */
     public void postWrite() {
-        // 1. Write raw offset.
-        out.writeInt(start + RAW_DATA_OFF_POS, (rawOffPos == 0 ? out.position() : rawOffPos)
- start);
+        if (schema != null) {
+            // Write schema ID.
+            out.writeInt(start + SCHEMA_ID_POS, schemaId);
 
-        if (tail != null) {
-            // 2. Write schema offset.
-            out.writeInt(start + SCHEMA_OFF_POS, out.position() - start);
+            // Write schema offset.
+            out.writeInt(start + SCHEMA_OR_RAW_OFF_POS, out.position() - start);
 
-            // 3. Write the schema.
-            for (Integer val : tail)
+            // Write the schema.
+            for (Integer val : schema)
                 out.writeInt(val);
 
-            // 4. Write raw offset if needed.
+            // Write raw offset if needed.
             if (rawOffPos != 0)
-                out.writeInt(rawOffPos);
+                out.writeInt(rawOffPos - start);
         }
+        else
+            // If there are no schema, we are free to write raw offset to schema offset.
+            out.writeInt(start + SCHEMA_OR_RAW_OFF_POS, (rawOffPos == 0 ? out.position()
: rawOffPos) - start);
 
         // 5. Write length.
         out.writeInt(start + TOTAL_LEN_POS, out.position() - start);
-
     }
 
     /**
@@ -1822,15 +1827,13 @@ public class PortableWriterExImpl implements PortableWriter, PortableRawWriterEx
       * @param off Offset starting from object head.
       */
     private void saveFieldInfo(int id, int off) {
-        if (tail == null) {
-            tail = new ArrayList<>(1);
+        if (schema == null)
+            schema = new ArrayList<>(2);
 
-            tail.add(id);
-        }
-        else {
-            tail.add(off);
-            tail.add(id);
-        }
+        schema.add(id);
+        schema.add(off);
+
+        schemaId = 31 * schemaId + id;
     }
 
      /**

http://git-wip-us.apache.org/repos/asf/ignite/blob/40aa800a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderImpl.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderImpl.java
b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderImpl.java
index 2c2173e..a9698eb 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderImpl.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderImpl.java
@@ -41,7 +41,6 @@ import java.util.Set;
 import static org.apache.ignite.internal.portable.GridPortableMarshaller.DFLT_HDR_LEN;
 import static org.apache.ignite.internal.portable.GridPortableMarshaller.HASH_CODE_POS;
 import static org.apache.ignite.internal.portable.GridPortableMarshaller.PROTO_VER_POS;
-import static org.apache.ignite.internal.portable.GridPortableMarshaller.RAW_DATA_OFF_POS;
 import static org.apache.ignite.internal.portable.GridPortableMarshaller.TOTAL_LEN_POS;
 import static org.apache.ignite.internal.portable.GridPortableMarshaller.TYPE_ID_POS;
 import static org.apache.ignite.internal.portable.GridPortableMarshaller.UNREGISTERED_TYPE_ID;
@@ -223,7 +222,7 @@ public class PortableBuilderImpl implements PortableBuilder {
             else
                 assignedFldsById = Collections.emptyMap();
 
-            int rawOff = start + reader.readIntAbsolute(start + RAW_DATA_OFF_POS);
+            int rawOff = start + PortableUtils.rawOffset(reader, start);
 
             reader.position(start + hdrLen);
 
@@ -383,7 +382,7 @@ public class PortableBuilderImpl implements PortableBuilder {
         writer.rawWriter();
 
         if (reader != null) {
-            int rawOff = reader.readIntAbsolute(start + RAW_DATA_OFF_POS);
+            int rawOff = PortableUtils.rawOffset(reader, start);
             int len = reader.readIntAbsolute(start + TOTAL_LEN_POS);
 
             if (rawOff < len)
@@ -408,7 +407,7 @@ public class PortableBuilderImpl implements PortableBuilder {
             Map<Integer, Object> readCache = new HashMap<>();
 
             int pos = start + hdrLen;
-            int end = start + reader.readIntAbsolute(start + RAW_DATA_OFF_POS);
+            int end = start + PortableUtils.rawOffset(reader, start);
 
             while (pos < end) {
                 int fieldId = reader.readIntAbsolute(pos);

http://git-wip-us.apache.org/repos/asf/ignite/blob/40aa800a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderReader.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderReader.java
b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderReader.java
index afa40a3..2a1e43e 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderReader.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/builder/PortableBuilderReader.java
@@ -37,7 +37,7 @@ import static org.apache.ignite.internal.portable.GridPortableMarshaller.STRING;
 /**
  *
  */
-class PortableBuilderReader {
+public class PortableBuilderReader {
     /** */
     private static final PortablePrimitives PRIM = PortablePrimitives.get();
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/40aa800a/modules/core/src/test/java/org/apache/ignite/internal/portable/GridPortableMarshallerSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/portable/GridPortableMarshallerSelfTest.java
b/modules/core/src/test/java/org/apache/ignite/internal/portable/GridPortableMarshallerSelfTest.java
index d73f792..3f97642 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/portable/GridPortableMarshallerSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/portable/GridPortableMarshallerSelfTest.java
@@ -714,29 +714,6 @@ public class GridPortableMarshallerSelfTest extends GridCommonAbstractTest
{
     /**
      * @throws Exception If failed.
      */
-    public void testInvalidClass() throws Exception {
-        byte[] arr = new byte[20];
-
-        arr[0] = 103;
-        arr[1] = 1;
-
-        U.intToBytes(Integer.reverseBytes(11111), arr, 4);
-
-        final PortableObject po = new PortableObjectImpl(initPortableContext(new PortableMarshaller()),
arr, 0);
-
-        GridTestUtils.assertThrows(log, new Callable<Object>() {
-                                       @Override public Object call() throws Exception {
-                                           po.deserialize();
-
-                                           return null;
-                                       }
-                                   }, PortableInvalidClassException.class, "Unknown type
ID: 11111"
-        );
-    }
-
-    /**
-     * @throws Exception If failed.
-     */
     public void testClassWithoutPublicConstructor() throws Exception {
         PortableMarshaller marsh = new PortableMarshaller();
 


Mime
View raw message