ignite-commits mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From sboi...@apache.org
Subject incubator-ignite git commit: ignite-950: implemented fieldOffset, hasField functions for ObjectInputStream
Date Fri, 05 Jun 2015 14:00:47 GMT
Repository: incubator-ignite
Updated Branches:
  refs/heads/ignite-950 dee21a458 -> 8592c8d42


ignite-950: implemented fieldOffset, hasField functions for ObjectInputStream


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

Branch: refs/heads/ignite-950
Commit: 8592c8d42fbd9beb09efe7d876b14afb81cf9f48
Parents: dee21a4
Author: Denis Magda <dmagda@gridgain.com>
Authored: Fri Jun 5 17:00:34 2015 +0300
Committer: Denis Magda <dmagda@gridgain.com>
Committed: Fri Jun 5 17:00:34 2015 +0300

----------------------------------------------------------------------
 .../optimized/OptimizedMarshallerUtils.java     | 11 ++++
 .../optimized/OptimizedObjectInputStream.java   | 69 +++++++++++++++-----
 2 files changed, 64 insertions(+), 16 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/8592c8d4/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 6e7f6c2..a0e8a71 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
@@ -146,6 +146,8 @@ class OptimizedMarshallerUtils {
     /** */
     static final byte EMPTY_FOOTER = -1;
 
+    /** */
+    static final byte FOOTER_LEN_OFF = 4;
 
     /** UTF-8 character name. */
     static final Charset UTF_8 = Charset.forName("UTF-8");
@@ -233,6 +235,15 @@ class OptimizedMarshallerUtils {
     }
 
     /**
+     * Checks whether a given type supports and should include footer with fields info into
the serialized form.
+     *
+     * @param type Type.
+     * @return {@code true} if supported, {@code false} otherwise.
+     */
+    public static boolean supportsFooter(int type) {
+        return type == SERIALIZABLE;
+    }
+    /**
      * @param fieldName Field name.
      * @return Field ID.
      */

http://git-wip-us.apache.org/repos/asf/incubator-ignite/blob/8592c8d4/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 27222c9..6f4df7d 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
@@ -961,38 +961,80 @@ class OptimizedObjectInputStream extends ObjectInputStream {
      *
      * @param fieldName Field name.
      * @return {@code true} if field exists, {@code false} otherwise.
+     * @throws IOException in case of error.
      */
-    public boolean hasField(String fieldName) {
+    public boolean hasField(String fieldName) throws IOException {
+        int pos = in.position();
 
-        return false;
+        // TODO: IGNITE-950, do we need move to start position?
+        if (!supportsFooter(in.readByte()))
+            return false;
+
+        int off = fieldOffset(fieldName);
+
+        in.position(pos);
+
+        return off > 0;
     }
 
-    //TODO
+    /**
+     * TODO: IGNITE-950
+     * @param fieldName
+     * @return
+     * @throws IOException
+     * @throws ClassNotFoundException
+     */
     Object readField(String fieldName) throws IOException, ClassNotFoundException {
-        byte type = in.readByte();
+        int pos = in.position();
 
-        if (type != SERIALIZABLE)
-            return -1;
+        // TODO: IGNITE-950, do we need move to start position?
+        if (!supportsFooter(in.readByte()))
+            return false;
+
+        int off = fieldOffset(fieldName);
+
+        Object obj = null;
+
+        if (off > 0) {
+            in.position(off);
+            obj = readObject();
+        }
+
+        in.position(pos);
 
+        return obj;
+    }
+
+    /**
+     * Returns field offset in the byte stream.
+     *
+     * @param fieldName Field name.
+     * @return positive offset or -1 if the object doesn't have such a field.
+     * @throws IOException in case of error.
+     */
+    private int fieldOffset(String fieldName) throws IOException {
         int fieldId = resolveFieldId(fieldName);
 
         int end = in.size();
-        in.position(end - 4);
+
+        in.position(end - FOOTER_LEN_OFF);
 
         int footerLen = in.readInt();
 
         if (footerLen == EMPTY_FOOTER)
-            return null; //TODO: IGNITE-950
+            return -1;
 
-        int footerStartOff = in.size() - footerLen;
+        int footerStartOff = end - footerLen;
         in.position(footerStartOff);
 
         int fieldsDataPos = in.readInt();
 
         int fieldOff = -1;
 
+        // 8 - size of footer len and object total len fields.
+        int bound = end - 8;
 
-        while (footerStartOff < end - 8) {
+        while (footerStartOff < bound) {
             int id = in.readInt();
 
             if (fieldId == id) {
@@ -1006,12 +1048,7 @@ class OptimizedObjectInputStream extends ObjectInputStream {
             footerStartOff += 12;
         }
 
-        if (fieldOff > 0) {
-            in.position(fieldOff);
-            return readObject();
-        }
-
-        return null; //TODO
+        return fieldOff;
     }
 
     /**


Mime
View raw message