IGNITE-1917: Reader constructors simplification.

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

Branch: refs/heads/ignite-1917
Commit: ee88850d232610c905f1903375a43f6d6181abb2
Parents: f8f4d52
Author: vozerov-gridgain <voze...@gridgain.com>
Authored: Tue Nov 17 14:59:30 2015 +0300
Committer: vozerov-gridgain <voze...@gridgain.com>
Committed: Tue Nov 17 14:59:30 2015 +0300

----------------------------------------------------------------------
 .../internal/portable/BinaryObjectImpl.java     | 40 +++++-----
 .../portable/BinaryObjectOffheapImpl.java       | 83 ++++++++------------
 .../internal/portable/BinaryReaderExImpl.java   | 13 +--
 .../portable/GridPortableMarshaller.java        |  5 +-
 .../portable/builder/PortableBuilderReader.java |  5 +-
 5 files changed, 56 insertions(+), 90 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/ee88850d/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectImpl.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectImpl.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectImpl.java
index 86dc247..7455c70 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectImpl.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectImpl.java
@@ -257,17 +257,13 @@ public final class BinaryObjectImpl extends 
BinaryObjectEx implements Externaliz
     /** {@inheritDoc} */
     @SuppressWarnings("unchecked")
     @Nullable @Override public <F> F field(String fieldName) throws 
BinaryObjectException {
-        BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx, arr, start, 
null);
-
-        return (F)reader.unmarshalField(fieldName);
+        return (F)newReader().unmarshalField(fieldName);
     }
 
     /** {@inheritDoc} */
     @SuppressWarnings("unchecked")
     @Nullable @Override public <F> F field(int fieldId) throws 
BinaryObjectException {
-        BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx, arr, start, 
null);
-
-        return (F)reader.unmarshalField(fieldId);
+        return (F)newReader().unmarshalField(fieldId);
     }
 
     /** {@inheritDoc} */
@@ -400,11 +396,10 @@ public final class BinaryObjectImpl extends 
BinaryObjectEx implements Externaliz
 
                 break;
 
-            default: {
-                BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx, arr, 
start, null);
+            default:
+                val = newReader().unmarshalFieldByAbsolutePosition(fieldPos);
 
-                val = reader.unmarshalFieldByAbsolutePosition(fieldPos);
-            }
+                break;
         }
 
         return (F)val;
@@ -413,20 +408,14 @@ public final class BinaryObjectImpl extends 
BinaryObjectEx implements Externaliz
     /** {@inheritDoc} */
     @SuppressWarnings("unchecked")
     @Nullable @Override protected <F> F field(BinaryReaderHandles rCtx, String 
fieldName) {
-        BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx,
-            new PortableHeapInputStream(arr),
-            start,
-            null,
-            rCtx);
+        BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx, new 
PortableHeapInputStream(arr), start, null, rCtx);
 
         return (F)reader.unmarshalField(fieldName);
     }
 
     /** {@inheritDoc} */
     @Override public boolean hasField(String fieldName) {
-        BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx, arr, start, 
null);
-
-        return reader.hasField(fieldName);
+        return newReader().hasField(fieldName);
     }
 
     /** {@inheritDoc} */
@@ -458,9 +447,7 @@ public final class BinaryObjectImpl extends BinaryObjectEx 
implements Externaliz
 
     /** {@inheritDoc} */
     @Override protected PortableSchema createSchema() {
-        BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx, arr, start, 
null);
-
-        return reader.getOrCreateSchema();
+        return newReader().getOrCreateSchema();
     }
 
     /** {@inheritDoc} */
@@ -576,7 +563,7 @@ public final class BinaryObjectImpl extends BinaryObjectEx 
implements Externaliz
      */
     private Object deserializeValue() {
         // TODO: IGNITE-1272 - Deserialize with proper class loader.
-        BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx, arr, start, 
null);
+        BinaryReaderExImpl reader = newReader();
 
         Object obj0 = reader.deserialize();
 
@@ -589,4 +576,13 @@ public final class BinaryObjectImpl extends BinaryObjectEx 
implements Externaliz
 
         return obj0;
     }
+
+    /**
+     * Create new reader for this object.
+     *
+     * @return Reader.
+     */
+    private BinaryReaderExImpl newReader() {
+        return new BinaryReaderExImpl(ctx, new PortableHeapInputStream(arr), 
start, null);
+    }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/ee88850d/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectOffheapImpl.java
----------------------------------------------------------------------
diff --git 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectOffheapImpl.java
 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectOffheapImpl.java
index f97ab94..66cbf20 100644
--- 
a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectOffheapImpl.java
+++ 
b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryObjectOffheapImpl.java
@@ -17,18 +17,11 @@
 
 package org.apache.ignite.internal.portable;
 
-import java.io.Externalizable;
-import java.io.IOException;
-import java.io.ObjectInput;
-import java.io.ObjectOutput;
-import java.math.BigDecimal;
-import java.math.BigInteger;
-import java.nio.ByteBuffer;
-import java.sql.Timestamp;
-import java.util.Date;
-import java.util.UUID;
-
 import org.apache.ignite.IgniteCheckedException;
+import org.apache.ignite.binary.BinaryField;
+import org.apache.ignite.binary.BinaryObject;
+import org.apache.ignite.binary.BinaryObjectException;
+import org.apache.ignite.binary.BinaryType;
 import org.apache.ignite.internal.portable.streams.PortableOffheapInputStream;
 import org.apache.ignite.internal.processors.cache.CacheObject;
 import org.apache.ignite.internal.processors.cache.CacheObjectContext;
@@ -37,13 +30,20 @@ import org.apache.ignite.internal.util.typedef.internal.A;
 import org.apache.ignite.internal.util.typedef.internal.U;
 import org.apache.ignite.plugin.extensions.communication.MessageReader;
 import org.apache.ignite.plugin.extensions.communication.MessageWriter;
-import org.apache.ignite.binary.BinaryObjectException;
-import org.apache.ignite.binary.BinaryType;
-import org.apache.ignite.binary.BinaryObject;
-import org.apache.ignite.binary.BinaryField;
 import org.jetbrains.annotations.Nullable;
 import sun.misc.Unsafe;
 
+import java.io.Externalizable;
+import java.io.IOException;
+import java.io.ObjectInput;
+import java.io.ObjectOutput;
+import java.math.BigDecimal;
+import java.math.BigInteger;
+import java.nio.ByteBuffer;
+import java.sql.Timestamp;
+import java.util.Date;
+import java.util.UUID;
+
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static 
org.apache.ignite.internal.portable.GridPortableMarshaller.BOOLEAN;
 import static org.apache.ignite.internal.portable.GridPortableMarshaller.BYTE;
@@ -131,12 +131,7 @@ public class BinaryObjectOffheapImpl extends 
BinaryObjectEx implements Externali
 
     /** {@inheritDoc} */
     @Override protected PortableSchema createSchema() {
-        BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx,
-            new PortableOffheapInputStream(ptr, size, false),
-            start,
-            null);
-
-        return reader.getOrCreateSchema();
+        return newReader().getOrCreateSchema();
     }
 
     /** {@inheritDoc} */
@@ -177,23 +172,13 @@ public class BinaryObjectOffheapImpl extends 
BinaryObjectEx implements Externali
     /** {@inheritDoc} */
     @SuppressWarnings("unchecked")
     @Nullable @Override public <F> F field(String fieldName) throws 
BinaryObjectException {
-        BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx,
-            new PortableOffheapInputStream(ptr, size, false),
-            start,
-            null);
-
-        return (F)reader.unmarshalField(fieldName);
+        return (F)newReader().unmarshalField(fieldName);
     }
 
     /** {@inheritDoc} */
     @SuppressWarnings("unchecked")
     @Nullable @Override public <F> F field(int fieldId) throws 
BinaryObjectException {
-        BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx,
-            new PortableOffheapInputStream(ptr, size, false),
-            start,
-            null);
-
-        return (F)reader.unmarshalField(fieldId);
+        return (F)newReader().unmarshalField(fieldId);
     }
 
     /** {@inheritDoc} */
@@ -327,14 +312,10 @@ public class BinaryObjectOffheapImpl extends 
BinaryObjectEx implements Externali
 
                 break;
 
-            default: {
-                BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx,
-                    new PortableOffheapInputStream(ptr, size, false),
-                    start,
-                    null);
+            default:
+                val = newReader().unmarshalFieldByAbsolutePosition(fieldPos);
 
-                val = reader.unmarshalFieldByAbsolutePosition(fieldPos);
-            }
+                break;
         }
 
         return (F)val;
@@ -354,12 +335,7 @@ public class BinaryObjectOffheapImpl extends 
BinaryObjectEx implements Externali
 
     /** {@inheritDoc} */
     @Override public boolean hasField(String fieldName) {
-        BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx,
-            new PortableOffheapInputStream(ptr, size, false),
-            start,
-            null);
-
-        return reader.hasField(fieldName);
+        return newReader().hasField(fieldName);
     }
 
     /** {@inheritDoc} */
@@ -445,12 +421,15 @@ public class BinaryObjectOffheapImpl extends 
BinaryObjectEx implements Externali
      */
     private Object deserializeValue() {
         // TODO: IGNITE-1272 - Deserialize with proper class loader.
-        BinaryReaderExImpl reader = new BinaryReaderExImpl(
-            ctx,
-            new PortableOffheapInputStream(ptr, size, false),
-            start,
-            null);
+        return newReader().deserialize();
+    }
 
-        return reader.deserialize();
+    /**
+     * Create new reader for this object.
+     *
+     * @return Reader.
+     */
+    private BinaryReaderExImpl newReader() {
+        return new BinaryReaderExImpl(ctx, new PortableOffheapInputStream(ptr, 
size, false), start, null);
     }
 }
\ No newline at end of file

http://git-wip-us.apache.org/repos/asf/ignite/blob/ee88850d/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 d79df22..7cffe28 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
@@ -24,7 +24,6 @@ import org.apache.ignite.binary.BinaryObject;
 import org.apache.ignite.binary.BinaryObjectException;
 import org.apache.ignite.binary.BinaryRawReader;
 import org.apache.ignite.binary.BinaryReader;
-import org.apache.ignite.internal.portable.streams.PortableHeapInputStream;
 import org.apache.ignite.internal.portable.streams.PortableInputStream;
 import org.apache.ignite.internal.util.GridEnumCache;
 import org.apache.ignite.internal.util.lang.GridMapEntry;
@@ -173,20 +172,10 @@ public class BinaryReaderExImpl implements BinaryReader, 
BinaryRawReaderEx, Obje
 
     /**
      * @param ctx Context.
-     * @param arr Array.
-     * @param start Start.
-     * @param ldr Class loader.
-     */
-    public BinaryReaderExImpl(PortableContext ctx, byte[] arr, int start, 
ClassLoader ldr) {
-        this(ctx, new PortableHeapInputStream(arr), start, ldr, new 
BinaryReaderHandles());
-    }
-
-    /**
-     * @param ctx Context.
      * @param in Input stream.
      * @param start Start.
      */
-    BinaryReaderExImpl(PortableContext ctx, PortableInputStream in, int start, 
ClassLoader ldr) {
+    public BinaryReaderExImpl(PortableContext ctx, PortableInputStream in, int 
start, ClassLoader ldr) {
         this(ctx, in, start, ldr, new BinaryReaderHandles());
     }
 

http://git-wip-us.apache.org/repos/asf/ignite/blob/ee88850d/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 989f16d..36c76c4 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
@@ -17,6 +17,7 @@
 
 package org.apache.ignite.internal.portable;
 
+import org.apache.ignite.internal.portable.streams.PortableHeapInputStream;
 import org.apache.ignite.internal.portable.streams.PortableInputStream;
 import org.apache.ignite.internal.portable.streams.PortableOutputStream;
 import org.apache.ignite.binary.BinaryObjectException;
@@ -254,7 +255,7 @@ public class GridPortableMarshaller {
     @Nullable public <T> T unmarshal(byte[] bytes, @Nullable ClassLoader 
clsLdr) throws BinaryObjectException {
         assert bytes != null;
 
-        BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx, bytes, 0, 
clsLdr);
+        BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx, new 
PortableHeapInputStream(bytes), 0, clsLdr);
 
         return (T)reader.unmarshal();
     }
@@ -283,7 +284,7 @@ public class GridPortableMarshaller {
         if (arr[0] == NULL)
             return null;
 
-        BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx, arr, 0, ldr);
+        BinaryReaderExImpl reader = new BinaryReaderExImpl(ctx, new 
PortableHeapInputStream(arr), 0, ldr);
 
         return (T)reader.deserialize();
     }

http://git-wip-us.apache.org/repos/asf/ignite/blob/ee88850d/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 b6a6b54..fae9417 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
@@ -31,6 +31,7 @@ import org.apache.ignite.internal.portable.PortableSchema;
 import org.apache.ignite.internal.portable.PortableUtils;
 import org.apache.ignite.internal.portable.BinaryWriterExImpl;
 import org.apache.ignite.binary.BinaryObjectException;
+import org.apache.ignite.internal.portable.streams.PortableHeapInputStream;
 
 import static java.nio.charset.StandardCharsets.UTF_8;
 import static org.apache.ignite.internal.portable.GridPortableMarshaller.NULL;
@@ -64,7 +65,7 @@ public class PortableBuilderReader implements 
PortablePositionReadable {
         pos = objImpl.start();
 
         // TODO: IGNITE-1272 - Is class loader needed here?
-        reader = new BinaryReaderExImpl(ctx, arr, pos, null);
+        reader = new BinaryReaderExImpl(ctx, new PortableHeapInputStream(arr), 
pos, null);
     }
 
     /**
@@ -94,7 +95,7 @@ public class PortableBuilderReader implements 
PortablePositionReadable {
         if (start == pos)
             targetReader = reader;
         else
-            targetReader = new BinaryReaderExImpl(ctx, arr, start, null);
+            targetReader = new BinaryReaderExImpl(ctx, new 
PortableHeapInputStream(arr), start, null);
 
         return targetReader.getOrCreateSchema();
     }

Reply via email to