You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@ignite.apache.org by sb...@apache.org on 2015/05/28 16:11:56 UTC

[2/2] incubator-ignite git commit: ignite-950: writing footer for serializable and plain objects

ignite-950: writing footer for serializable and plain objects


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

Branch: refs/heads/ignite-950
Commit: 0183c008c78164448b4f0c400d52c83732e16f96
Parents: 1e676bc
Author: Denis Magda <dm...@gridgain.com>
Authored: Thu May 28 17:11:16 2015 +0300
Committer: Denis Magda <dm...@gridgain.com>
Committed: Thu May 28 17:11:16 2015 +0300

----------------------------------------------------------------------
 .../ignite/internal/util/io/GridDataInput.java  | 23 +++++++
 .../internal/util/io/GridUnsafeDataInput.java   | 70 ++++++++++++--------
 .../optimized/OptimizedMarshaller.java          | 28 ++++++++
 .../optimized/OptimizedMarshallerUtils.java     |  3 +-
 .../optimized/OptimizedObjectInputStream.java   | 50 +++++++++++++-
 .../optimized/OptimizedObjectOutputStream.java  |  6 +-
 .../marshaller/GridMarshallerAbstractTest.java  |  2 +-
 .../optimized/OptimizedMarshallerSelfTest.java  | 13 ++++
 8 files changed, 161 insertions(+), 34 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0183c008/modules/core/src/main/java/org/apache/ignite/internal/util/io/GridDataInput.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/io/GridDataInput.java b/modules/core/src/main/java/org/apache/ignite/internal/util/io/GridDataInput.java
index a0c556e..81c6bcb 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/util/io/GridDataInput.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/util/io/GridDataInput.java
@@ -30,12 +30,35 @@ public interface GridDataInput extends DataInput {
     public void bytes(byte[] bytes, int len);
 
     /**
+     * @param bytes Bytes.
+     * @param off Offset.
+     * @param len Length.
+     */
+    public void bytes(byte[] bytes, int off, int len);
+
+    /**
      * @param in Underlying input stream.
      * @throws IOException In case of error.
      */
     public void inputStream(InputStream in) throws IOException;
 
     /**
+     * Sets offset to the new position. Supported only for {@code GridDataInput} backed by byte array.
+     *
+     * @param off Offset.
+     * @throws IOException In case of error.
+     */
+    public void offset(int off) throws IOException;
+
+    /**
+     * Size of {@code GridDataInput}. Supported only for instances backed by byte array.
+     *
+     * @return Total number of bytes available.
+     * @throws IOException In case of error.
+     */
+    public int size() throws IOException;
+
+    /**
      * Resets data output.
      *
      * @throws IOException In case of error.

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0183c008/modules/core/src/main/java/org/apache/ignite/internal/util/io/GridUnsafeDataInput.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/util/io/GridUnsafeDataInput.java b/modules/core/src/main/java/org/apache/ignite/internal/util/io/GridUnsafeDataInput.java
index fb498d6..5f11768 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/util/io/GridUnsafeDataInput.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/util/io/GridUnsafeDataInput.java
@@ -71,10 +71,10 @@ public class GridUnsafeDataInput extends InputStream implements GridDataInput {
     @GridToStringExclude
     private final char[] urfCBuf = new char[CHAR_BUF_SIZE];
 
-    /** Current offset into buf. */
+    /** Current increaseOffset into buf. */
     private int pos;
 
-    /** End offset of valid data in buf, or -1 if no more block data. */
+    /** End increaseOffset of valid data in buf, or -1 if no more block data. */
     private int end = -1;
 
     /** Bytes. */
@@ -106,18 +106,15 @@ public class GridUnsafeDataInput extends InputStream implements GridDataInput {
         bytes(bytes, 0, len);
     }
 
-    /**
-     * @param bytes Bytes.
-     * @param off Offset.
-     * @param len Length.
-     */
-    public void bytes(byte[] bytes, int off, int len) {
+    /** {@inheritDoc} */
+    @Override public void bytes(byte[] bytes, int off, int len) {
         buf = bytes;
 
         max = len;
         this.off = off;
     }
 
+
     /** {@inheritDoc} */
     @Override public void inputStream(InputStream in) throws IOException {
         this.in = in;
@@ -125,6 +122,7 @@ public class GridUnsafeDataInput extends InputStream implements GridDataInput {
         buf = inBuf;
     }
 
+
     /**
      * Reads from stream to buffer. If stream is {@code null}, this method is no-op.
      *
@@ -170,12 +168,28 @@ public class GridUnsafeDataInput extends InputStream implements GridDataInput {
         }
     }
 
+    /** {@inheritDoc} */
+    @Override public int size() throws IOException {
+        if (in != null)
+            throw new IOException("Unable to get bytes available in InputStream");
+
+        return max;
+    }
+
+    /** {@inheritDoc} */
+    @Override public void offset(int off) throws IOException {
+        if (in != null)
+            throw new IOException("Unable to change offset in InputStream");
+
+        this.off = off;
+    }
+
     /**
      * @param more Bytes to move forward.
      * @return Old offset value.
      * @throws IOException In case of error.
      */
-    private int offset(int more) throws IOException {
+    private int increaseOffset(int more) throws IOException {
         int old = off;
 
         off += more;
@@ -204,7 +218,7 @@ public class GridUnsafeDataInput extends InputStream implements GridDataInput {
 
         byte[] arr = new byte[arrSize];
 
-        UNSAFE.copyMemory(buf, byteArrOff + offset(arrSize), arr, byteArrOff, arrSize);
+        UNSAFE.copyMemory(buf, byteArrOff + increaseOffset(arrSize), arr, byteArrOff, arrSize);
 
         return arr;
     }
@@ -219,7 +233,7 @@ public class GridUnsafeDataInput extends InputStream implements GridDataInput {
 
         short[] arr = new short[arrSize];
 
-        UNSAFE.copyMemory(buf, byteArrOff + offset(bytesToCp), arr, shortArrOff, bytesToCp);
+        UNSAFE.copyMemory(buf, byteArrOff + increaseOffset(bytesToCp), arr, shortArrOff, bytesToCp);
 
         return arr;
     }
@@ -234,7 +248,7 @@ public class GridUnsafeDataInput extends InputStream implements GridDataInput {
 
         int[] arr = new int[arrSize];
 
-        UNSAFE.copyMemory(buf, byteArrOff + offset(bytesToCp), arr, intArrOff, bytesToCp);
+        UNSAFE.copyMemory(buf, byteArrOff + increaseOffset(bytesToCp), arr, intArrOff, bytesToCp);
 
         return arr;
     }
@@ -249,7 +263,7 @@ public class GridUnsafeDataInput extends InputStream implements GridDataInput {
 
         double[] arr = new double[arrSize];
 
-        UNSAFE.copyMemory(buf, byteArrOff + offset(bytesToCp), arr, doubleArrOff, bytesToCp);
+        UNSAFE.copyMemory(buf, byteArrOff + increaseOffset(bytesToCp), arr, doubleArrOff, bytesToCp);
 
         return arr;
     }
@@ -276,7 +290,7 @@ public class GridUnsafeDataInput extends InputStream implements GridDataInput {
 
         char[] arr = new char[arrSize];
 
-        UNSAFE.copyMemory(buf, byteArrOff + offset(bytesToCp), arr, charArrOff, bytesToCp);
+        UNSAFE.copyMemory(buf, byteArrOff + increaseOffset(bytesToCp), arr, charArrOff, bytesToCp);
 
         return arr;
     }
@@ -291,7 +305,7 @@ public class GridUnsafeDataInput extends InputStream implements GridDataInput {
 
         long[] arr = new long[arrSize];
 
-        UNSAFE.copyMemory(buf, byteArrOff + offset(bytesToCp), arr, longArrOff, bytesToCp);
+        UNSAFE.copyMemory(buf, byteArrOff + increaseOffset(bytesToCp), arr, longArrOff, bytesToCp);
 
         return arr;
     }
@@ -306,7 +320,7 @@ public class GridUnsafeDataInput extends InputStream implements GridDataInput {
 
         float[] arr = new float[arrSize];
 
-        UNSAFE.copyMemory(buf, byteArrOff + offset(bytesToCp), arr, floatArrOff, bytesToCp);
+        UNSAFE.copyMemory(buf, byteArrOff + increaseOffset(bytesToCp), arr, floatArrOff, bytesToCp);
 
         return arr;
     }
@@ -317,14 +331,14 @@ public class GridUnsafeDataInput extends InputStream implements GridDataInput {
 
         fromStream(len);
 
-        UNSAFE.copyMemory(buf, byteArrOff + offset(len), b, byteArrOff, len);
+        UNSAFE.copyMemory(buf, byteArrOff + increaseOffset(len), b, byteArrOff, len);
     }
 
     /** {@inheritDoc} */
     @Override public void readFully(byte[] b, int off, int len) throws IOException {
         fromStream(len);
 
-        UNSAFE.copyMemory(buf, byteArrOff + offset(len), b, byteArrOff + off, len);
+        UNSAFE.copyMemory(buf, byteArrOff + increaseOffset(len), b, byteArrOff + off, len);
     }
 
     /** {@inheritDoc} */
@@ -341,14 +355,14 @@ public class GridUnsafeDataInput extends InputStream implements GridDataInput {
     @Override public boolean readBoolean() throws IOException {
         fromStream(1);
 
-        return UNSAFE.getBoolean(buf, byteArrOff + offset(1));
+        return UNSAFE.getBoolean(buf, byteArrOff + increaseOffset(1));
     }
 
     /** {@inheritDoc} */
     @Override public byte readByte() throws IOException {
         fromStream(1);
 
-        return UNSAFE.getByte(buf, byteArrOff + offset(1));
+        return UNSAFE.getByte(buf, byteArrOff + increaseOffset(1));
     }
 
     /** {@inheritDoc} */
@@ -360,7 +374,7 @@ public class GridUnsafeDataInput extends InputStream implements GridDataInput {
     @Override public short readShort() throws IOException {
         fromStream(2);
 
-        return UNSAFE.getShort(buf, byteArrOff + offset(2));
+        return UNSAFE.getShort(buf, byteArrOff + increaseOffset(2));
     }
 
     /** {@inheritDoc} */
@@ -374,7 +388,7 @@ public class GridUnsafeDataInput extends InputStream implements GridDataInput {
 
         char v = UNSAFE.getChar(buf, byteArrOff + off);
 
-        offset(2);
+        increaseOffset(2);
 
         return v;
     }
@@ -383,28 +397,28 @@ public class GridUnsafeDataInput extends InputStream implements GridDataInput {
     @Override public int readInt() throws IOException {
         fromStream(4);
 
-        return UNSAFE.getInt(buf, byteArrOff + offset(4));
+        return UNSAFE.getInt(buf, byteArrOff + increaseOffset(4));
     }
 
     /** {@inheritDoc} */
     @Override public long readLong() throws IOException {
         fromStream(8);
 
-        return UNSAFE.getLong(buf, byteArrOff + offset(8));
+        return UNSAFE.getLong(buf, byteArrOff + increaseOffset(8));
     }
 
     /** {@inheritDoc} */
     @Override public float readFloat() throws IOException {
         fromStream(4);
 
-        return UNSAFE.getFloat(buf, byteArrOff + offset(4));
+        return UNSAFE.getFloat(buf, byteArrOff + increaseOffset(4));
     }
 
     /** {@inheritDoc} */
     @Override public double readDouble() throws IOException {
         fromStream(8);
 
-        return UNSAFE.getDouble(buf, byteArrOff + offset(8));
+        return UNSAFE.getDouble(buf, byteArrOff + increaseOffset(8));
     }
 
     /** {@inheritDoc} */
@@ -433,7 +447,7 @@ public class GridUnsafeDataInput extends InputStream implements GridDataInput {
         else {
             int toRead = Math.min(len, max - this.off);
 
-            UNSAFE.copyMemory(buf, byteArrOff + offset(toRead), b, byteArrOff + off, toRead);
+            UNSAFE.copyMemory(buf, byteArrOff + increaseOffset(toRead), b, byteArrOff + off, toRead);
 
             return toRead;
         }
@@ -511,7 +525,7 @@ public class GridUnsafeDataInput extends InputStream implements GridDataInput {
 
     /**
      * Reads span of UTF-encoded characters out of internal buffer
-     * (starting at offset pos and ending at or before offset end),
+     * (starting at increaseOffset pos and ending at or before increaseOffset end),
      * consuming no more than utfLen bytes. Appends read characters to
      * sbuf. Returns the number of bytes consumed.
      *

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0183c008/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedMarshaller.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedMarshaller.java b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedMarshaller.java
index accfeb7..1b60485 100644
--- a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedMarshaller.java
+++ b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedMarshaller.java
@@ -248,6 +248,34 @@ public class OptimizedMarshaller extends AbstractMarshaller {
         }
     }
 
+    //TODO:
+    public <T> T unmarshal(String fieldName, byte[] arr, @Nullable ClassLoader clsLdr) throws IgniteCheckedException {
+        assert arr != null && fieldName != null;
+
+        OptimizedObjectInputStream objIn = null;
+
+        try {
+            objIn = OptimizedObjectStreamRegistry.in();
+
+            objIn.context(clsMap, ctx, mapper, clsLdr != null ? clsLdr : dfltClsLdr);
+
+            objIn.in().bytes(arr, arr.length);
+
+            return (T)objIn.readField(fieldName);
+        }
+        catch (IOException e) {
+            throw new IgniteCheckedException("Failed to deserialize object with given class loader: " + clsLdr, e);
+        }
+        catch (ClassNotFoundException e) {
+            throw new IgniteCheckedException("Failed to find class with given class loader for unmarshalling " +
+                                                 "(make sure same version of all classes are available on all nodes or enable peer-class-loading): " +
+                                                 clsLdr, e);
+        }
+        finally {
+            OptimizedObjectStreamRegistry.closeIn(objIn);
+        }
+    }
+
     /**
      * Checks whether {@code GridOptimizedMarshaller} is able to work on the current JVM.
      * <p>

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0183c008/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedMarshallerUtils.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedMarshallerUtils.java b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedMarshallerUtils.java
index 6d66602..dd13ac5 100644
--- a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedMarshallerUtils.java
+++ b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedMarshallerUtils.java
@@ -147,7 +147,8 @@ class OptimizedMarshallerUtils {
     static final byte FOOTER_START = 1;
 
     /** */
-    static final byte EMPTY_FOOTER = 2;
+    static final byte EMPTY_FOOTER = -1;
+
 
     /** UTF-8 character name. */
     static final Charset UTF_8 = Charset.forName("UTF-8");

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0183c008/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectInputStream.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectInputStream.java b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectInputStream.java
index 8c825ac..f854dca 100644
--- a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectInputStream.java
+++ b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectInputStream.java
@@ -518,7 +518,7 @@ class OptimizedObjectInputStream extends ObjectInputStream {
             }
         }
 
-        byte flag = in.readByte();
+        byte flag = (byte)in.readInt();
 
         assert flag == EMPTY_FOOTER || flag == FOOTER_START;
 
@@ -942,6 +942,54 @@ class OptimizedObjectInputStream extends ObjectInputStream {
         return -1;
     }
 
+    //TODO
+    Object readField(String fieldName) throws IOException, ClassNotFoundException {
+        byte type = in.readByte();
+
+        if (type != SERIALIZABLE)
+            return -1;
+
+        int fieldId = resolveFieldId(fieldName);
+
+        int end = in.size() - 4;
+        in.offset(end);
+
+        int footerStartOff = in.readInt();
+
+        if (footerStartOff == EMPTY_FOOTER)
+            return null; //TODO
+
+        int pos = footerStartOff;
+        in.offset(footerStartOff);
+
+        assert in.readInt() == FOOTER_START;
+        in.readInt(); //TODO: do I need this? skip fields start offset
+
+        int fieldOff = -1;
+
+        while (pos < end) {
+            int id = in.readInt();
+            int len = in.readInt(); //TODO: do I need this?
+
+            if (fieldId == id) {
+                fieldOff = in.readInt();
+                break;
+            }
+            else
+                // skip field offset
+                in.skipBytes(4);
+
+            pos += 12;
+        }
+
+        if (fieldOff > 0) {
+            in.offset(fieldOff);
+            return readObject();
+        }
+
+        return null; //TODO
+    }
+
     /**
      * Returns objects that were added to handles table.
      * Used ONLY for test purposes.

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0183c008/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectOutputStream.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectOutputStream.java b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectOutputStream.java
index 525a2b4..6c1e824 100644
--- a/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectOutputStream.java
+++ b/modules/core/src/main/java/org/apache/ignite/marshaller/optimized/OptimizedObjectOutputStream.java
@@ -955,8 +955,8 @@ class OptimizedObjectOutputStream extends ObjectOutputStream {
                 return;
 
             data[pos++] = typeId;
-            data[pos++] = offset;
             data[pos++] = length;
+            data[pos++] = offset;
         }
 
         /**
@@ -966,11 +966,11 @@ class OptimizedObjectOutputStream extends ObjectOutputStream {
          */
         private void write() throws IOException {
             if (data == null)
-                writeByte(EMPTY_FOOTER);
+                writeInt(EMPTY_FOOTER);
             else {
                 int footerStartOff = out.size();
 
-                writeByte(FOOTER_START);
+                writeInt(FOOTER_START);
                 writeInt(fieldsStartOff);
 
                 for (int i = 0; i < data.length; i++)

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0183c008/modules/core/src/test/java/org/apache/ignite/marshaller/GridMarshallerAbstractTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/marshaller/GridMarshallerAbstractTest.java b/modules/core/src/test/java/org/apache/ignite/marshaller/GridMarshallerAbstractTest.java
index a89479b..b9f3535 100644
--- a/modules/core/src/test/java/org/apache/ignite/marshaller/GridMarshallerAbstractTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/marshaller/GridMarshallerAbstractTest.java
@@ -50,7 +50,7 @@ public abstract class GridMarshallerAbstractTest extends GridCommonAbstractTest
     private static final String CACHE_NAME = "namedCache";
 
     /** */
-    private static Marshaller marsh;
+    protected static Marshaller marsh;
 
     /** Closure job. */
     protected IgniteInClosure<String> c1 = new IgniteInClosure<String>() {

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/0183c008/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/OptimizedMarshallerSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/OptimizedMarshallerSelfTest.java b/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/OptimizedMarshallerSelfTest.java
index 7d09070..6c78506 100644
--- a/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/OptimizedMarshallerSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/marshaller/optimized/OptimizedMarshallerSelfTest.java
@@ -111,6 +111,19 @@ public class OptimizedMarshallerSelfTest extends GridMarshallerAbstractTest {
     }
 
     /**
+     * @throws Exception If failed.
+     */
+    public void testFieldUnmarshalling() throws Exception {
+        TestObject2 obj = new TestObject2(5);
+
+        byte[] data = marshal(obj);
+
+        Integer i = ((OptimizedMarshaller)marsh).unmarshal("i", data, Thread.currentThread().getContextClassLoader());
+
+        assertEquals(obj.i, (int)i);
+    }
+
+    /**
      * Class for nested execution test.
      */
     private static class NestedTestObject implements Serializable {