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/20 07:58:03 UTC

ignite git commit: IGNITE-1953: Reworked ID mappers handling.

Repository: ignite
Updated Branches:
  refs/heads/ignite-1282 4a1af37e3 -> d69b177de


IGNITE-1953: Reworked ID mappers handling.


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

Branch: refs/heads/ignite-1282
Commit: d69b177de2084b879f5b9b5c3fb981d00996f066
Parents: 4a1af37
Author: vozerov-gridgain <vo...@gridgain.com>
Authored: Fri Nov 20 09:58:40 2015 +0300
Committer: vozerov-gridgain <vo...@gridgain.com>
Committed: Fri Nov 20 09:58:40 2015 +0300

----------------------------------------------------------------------
 .../portable/BinaryInternalIdMapper.java        | 152 +++++++++++++++++++
 .../portable/PortableClassDescriptor.java       |   9 --
 .../internal/portable/PortableContext.java      | 121 ++-------------
 .../portable/BinaryObjectBuilderSelfTest.java   |   4 +-
 4 files changed, 166 insertions(+), 120 deletions(-)
----------------------------------------------------------------------


http://git-wip-us.apache.org/repos/asf/ignite/blob/d69b177d/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryInternalIdMapper.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryInternalIdMapper.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryInternalIdMapper.java
new file mode 100644
index 0000000..dd434ff
--- /dev/null
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/BinaryInternalIdMapper.java
@@ -0,0 +1,152 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+package org.apache.ignite.internal.portable;
+
+import org.apache.ignite.binary.BinaryIdMapper;
+import org.jetbrains.annotations.Nullable;
+
+/**
+ * Internal ID mapper. Mimics ID mapper interface, but provides default implementation and offers slightly better
+ * performance on micro-level in default case because it doesn't need virtual calls.
+ */
+public class BinaryInternalIdMapper implements BinaryIdMapper {
+    /** Maximum lower-case character. */
+    private static final char MAX_LOWER_CASE_CHAR = 0x7e;
+
+    /** Cached lower-case characters. */
+    private static final char[] LOWER_CASE_CHARS;
+
+    /** Default implementation. */
+    private static final BinaryInternalIdMapper DFLT = new BinaryInternalIdMapper();
+
+    /**
+     * Static initializer.
+     */
+    static {
+        LOWER_CASE_CHARS = new char[MAX_LOWER_CASE_CHAR + 1];
+
+        for (char c = 0; c <= MAX_LOWER_CASE_CHAR; c++)
+            LOWER_CASE_CHARS[c] = Character.toLowerCase(c);
+    }
+
+    /**
+     * Get default instance.
+     *
+     * @return Default instance.
+     */
+    public static BinaryInternalIdMapper defaultInstance() {
+        return DFLT;
+    }
+
+    /**
+     * Create internal mapper.
+     *
+     * @param mapper Public mapper.
+     * @return Internal mapper.
+     */
+    public static BinaryInternalIdMapper create(@Nullable BinaryIdMapper mapper) {
+        return mapper == null ? DFLT : new Wrapper(mapper);
+    }
+
+    /**
+     * Private constructor.
+     */
+    protected BinaryInternalIdMapper() {
+        // No-op.
+    }
+
+    /**
+     * Get type ID.
+     *
+     * @param typeName Type name.
+     * @return Type ID.
+     */
+    public int typeId(String typeName) {
+        assert typeName != null;
+
+        return lowerCaseHashCode(typeName);
+    }
+
+    /**
+     * Get field ID.
+     *
+     * @param typeId Type ID.
+     * @param fieldName Field name.
+     * @return Field ID.
+     */
+    public int fieldId(int typeId, String fieldName) {
+        assert fieldName != null;
+
+        return lowerCaseHashCode(fieldName);
+    }
+
+    /**
+     * Routine to calculate string hash code an
+     *
+     * @param str String.
+     * @return Hash code for given string converted to lower case.
+     */
+    private static int lowerCaseHashCode(String str) {
+        int len = str.length();
+
+        int h = 0;
+
+        for (int i = 0; i < len; i++) {
+            int c = str.charAt(i);
+
+            c = c <= MAX_LOWER_CASE_CHAR ? LOWER_CASE_CHARS[c] : Character.toLowerCase(c);
+
+            h = 31 * h + c;
+        }
+
+        return h;
+    }
+
+    /**
+     * Wrapping ID mapper.
+     */
+    private static class Wrapper extends BinaryInternalIdMapper {
+        /** Delegate. */
+        private final BinaryIdMapper mapper;
+
+        /**
+         * Constructor.
+         *
+         * @param mapper Delegate.
+         */
+        private Wrapper(BinaryIdMapper mapper) {
+            assert mapper != null;
+
+            this.mapper = mapper;
+        }
+
+        /** {@inheritDoc} */
+        @Override public int typeId(String typeName) {
+            int id = mapper.typeId(typeName);
+
+            return id != 0 ? id : super.typeId(typeName);
+        }
+
+        /** {@inheritDoc} */
+        @Override public int fieldId(int typeId, String fieldName) {
+            int id = mapper.fieldId(typeId, fieldName);
+
+            return id != 0 ? id : super.fieldId(typeId, fieldName);
+        }
+    }
+}

http://git-wip-us.apache.org/repos/asf/ignite/blob/d69b177d/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 3edf980..c233267 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
@@ -351,15 +351,6 @@ public class PortableClassDescriptor {
     }
 
     /**
-     * Get ID mapper.
-     *
-     * @return ID mapper.
-     */
-    public BinaryIdMapper idMapper() {
-        return idMapper;
-    }
-
-    /**
      * @return portableWriteReplace() method
      */
     @Nullable Method getWriteReplaceMethod() {

http://git-wip-us.apache.org/repos/asf/ignite/blob/d69b177d/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableContext.java
----------------------------------------------------------------------
diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableContext.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableContext.java
index e3caba4..a88498a 100644
--- a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableContext.java
+++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableContext.java
@@ -85,28 +85,6 @@ public class PortableContext implements Externalizable {
     private static final ClassLoader dfltLdr = U.gridClassLoader();
 
     /** */
-    static final BinaryIdMapper DFLT_ID_MAPPER = new IdMapperWrapper(null);
-
-    /** */
-    static final BinaryIdMapper BASIC_CLS_ID_MAPPER = new BasicClassIdMapper();
-
-    /** */
-    static final char[] LOWER_CASE_CHARS;
-
-    /** */
-    static final char MAX_LOWER_CASE_CHAR = 0x7e;
-
-    /**
-     *
-     */
-    static {
-        LOWER_CASE_CHARS = new char[MAX_LOWER_CASE_CHAR + 1];
-
-        for (char c = 0; c <= MAX_LOWER_CASE_CHAR; c++)
-            LOWER_CASE_CHARS[c] = Character.toLowerCase(c);
-    }
-
-    /** */
     private final ConcurrentMap<Class<?>, PortableClassDescriptor> descByCls = new ConcurrentHashMap8<>();
 
     /** Holds classes loaded by default class loader only. */
@@ -287,7 +265,7 @@ public class PortableContext implements Externalizable {
         TypeDescriptors descs = new TypeDescriptors();
 
         if (clsNames != null) {
-            BinaryIdMapper idMapper = new IdMapperWrapper(globalIdMapper);
+            BinaryIdMapper idMapper = BinaryInternalIdMapper.create(globalIdMapper);
 
             for (String clsName : clsNames) {
                 if (clsName.endsWith(".*")) { // Package wildcard
@@ -320,7 +298,7 @@ public class PortableContext implements Externalizable {
                 if (typeCfg.getIdMapper() != null)
                     idMapper = typeCfg.getIdMapper();
 
-                idMapper = new IdMapperWrapper(idMapper);
+                idMapper = BinaryInternalIdMapper.create(idMapper);
 
                 BinarySerializer serializer = globalSerializer;
 
@@ -510,7 +488,7 @@ public class PortableContext implements Externalizable {
                 clsName.hashCode(),
                 clsName,
                 null,
-                BASIC_CLS_ID_MAPPER,
+                BinaryInternalIdMapper.defaultInstance(),
                 null,
                 false,
                 keepDeserialized,
@@ -613,9 +591,9 @@ public class PortableContext implements Externalizable {
      * @return Type ID.
      */
     public int typeId(String typeName) {
-        String shortTypeName = typeName(typeName);
+        String typeName0 = typeName(typeName);
 
-        Integer id = predefinedTypeNames.get(shortTypeName);
+        Integer id = predefinedTypeNames.get(typeName0);
 
         if (id != null)
             return id;
@@ -623,7 +601,7 @@ public class PortableContext implements Externalizable {
         if (marshCtx.isSystemType(typeName))
             return typeName.hashCode();
 
-        return userTypeIdMapper(shortTypeName).typeId(shortTypeName);
+        return userTypeIdMapper(typeName0).typeId(typeName0);
     }
 
     /**
@@ -642,13 +620,7 @@ public class PortableContext implements Externalizable {
     public BinaryIdMapper userTypeIdMapper(int typeId) {
         BinaryIdMapper idMapper = mappers.get(typeId);
 
-        if (idMapper != null)
-            return idMapper;
-
-        if (predefinedTypes.containsKey(typeId))
-            return DFLT_ID_MAPPER;
-
-        return BASIC_CLS_ID_MAPPER;
+        return idMapper != null ? idMapper : BinaryInternalIdMapper.defaultInstance();
     }
 
     /**
@@ -658,7 +630,7 @@ public class PortableContext implements Externalizable {
     private BinaryIdMapper userTypeIdMapper(String typeName) {
         BinaryIdMapper idMapper = typeMappers.get(typeName);
 
-        return idMapper != null ? idMapper : DFLT_ID_MAPPER;
+        return idMapper != null ? idMapper : BinaryInternalIdMapper.defaultInstance();
     }
 
     /** {@inheritDoc} */
@@ -704,7 +676,7 @@ public class PortableContext implements Externalizable {
             id,
             typeName,
             null,
-            DFLT_ID_MAPPER,
+            BinaryInternalIdMapper.defaultInstance(),
             null,
             false,
             false,
@@ -746,7 +718,9 @@ public class PortableContext implements Externalizable {
             // No-op.
         }
 
-        int id = idMapper.typeId(clsName);
+        String typeName = typeName(clsName);
+
+        int id = idMapper.typeId(typeName);
 
         //Workaround for IGNITE-1358
         if (predefinedTypes.get(id) != null)
@@ -760,8 +734,6 @@ public class PortableContext implements Externalizable {
                 throw new BinaryObjectException("Duplicate type ID [clsName=" + clsName + ", id=" + id + ']');
         }
 
-        String typeName = typeName(clsName);
-
         typeMappers.put(typeName, idMapper);
 
         Map<String, Integer> fieldsMeta = null;
@@ -934,26 +906,6 @@ public class PortableContext implements Externalizable {
     }
 
     /**
-     * @param str String.
-     * @return Hash code for given string converted to lower case.
-     */
-    private static int lowerCaseHashCode(String str) {
-        int len = str.length();
-
-        int h = 0;
-
-        for (int i = 0; i < len; i++) {
-            int c = str.charAt(i);
-
-            c = c <= MAX_LOWER_CASE_CHAR ? LOWER_CASE_CHARS[c] : Character.toLowerCase(c);
-
-            h = 31 * h + c;
-        }
-
-        return h;
-    }
-
-    /**
      * Undeployment callback invoked when class loader is being undeployed.
      *
      * Some marshallers may want to clean their internal state that uses the undeployed class loader somehow.
@@ -970,55 +922,6 @@ public class PortableContext implements Externalizable {
     }
 
     /**
-     */
-    private static class IdMapperWrapper implements BinaryIdMapper {
-        /** */
-        private final BinaryIdMapper mapper;
-
-        /**
-         * @param mapper Custom ID mapper.
-         */
-        private IdMapperWrapper(@Nullable BinaryIdMapper mapper) {
-            this.mapper = mapper;
-        }
-
-        /** {@inheritDoc} */
-        @Override public int typeId(String clsName) {
-            int id = 0;
-
-            if (mapper != null)
-                id = mapper.typeId(clsName);
-
-            return id != 0 ? id : lowerCaseHashCode(typeName(clsName));
-        }
-
-        /** {@inheritDoc} */
-        @Override public int fieldId(int typeId, String fieldName) {
-            int id = 0;
-
-            if (mapper != null)
-                id = mapper.fieldId(typeId, fieldName);
-
-            return id != 0 ? id : lowerCaseHashCode(fieldName);
-        }
-    }
-
-    /**
-     * Basic class ID mapper.
-     */
-    private static class BasicClassIdMapper implements BinaryIdMapper {
-        /** {@inheritDoc} */
-        @Override public int typeId(String clsName) {
-            return clsName.hashCode();
-        }
-
-        /** {@inheritDoc} */
-        @Override public int fieldId(int typeId, String fieldName) {
-            return lowerCaseHashCode(fieldName);
-        }
-    }
-
-    /**
      * Type descriptors.
      */
     private static class TypeDescriptors {

http://git-wip-us.apache.org/repos/asf/ignite/blob/d69b177d/modules/core/src/test/java/org/apache/ignite/internal/portable/BinaryObjectBuilderSelfTest.java
----------------------------------------------------------------------
diff --git a/modules/core/src/test/java/org/apache/ignite/internal/portable/BinaryObjectBuilderSelfTest.java b/modules/core/src/test/java/org/apache/ignite/internal/portable/BinaryObjectBuilderSelfTest.java
index e88db99..2dfa6d0 100644
--- a/modules/core/src/test/java/org/apache/ignite/internal/portable/BinaryObjectBuilderSelfTest.java
+++ b/modules/core/src/test/java/org/apache/ignite/internal/portable/BinaryObjectBuilderSelfTest.java
@@ -75,11 +75,11 @@ public class BinaryObjectBuilderSelfTest extends GridCommonAbstractTest {
         customIdMapper.setClassName(CustomIdMapper.class.getName());
         customIdMapper.setIdMapper(new BinaryIdMapper() {
             @Override public int typeId(String clsName) {
-                return ~PortableContext.DFLT_ID_MAPPER.typeId(clsName);
+                return ~BinaryInternalIdMapper.defaultInstance().typeId(clsName);
             }
 
             @Override public int fieldId(int typeId, String fieldName) {
-                return typeId + ~PortableContext.DFLT_ID_MAPPER.fieldId(typeId, fieldName);
+                return typeId + ~BinaryInternalIdMapper.defaultInstance().fieldId(typeId, fieldName);
             }
         });