Repository: ignite Updated Branches: refs/heads/ignite-1770 37269c1f5 -> d6a5d8e31
IGNITE-1770: WIP on schema. Project: http://git-wip-us.apache.org/repos/asf/ignite/repo Commit: http://git-wip-us.apache.org/repos/asf/ignite/commit/22e50156 Tree: http://git-wip-us.apache.org/repos/asf/ignite/tree/22e50156 Diff: http://git-wip-us.apache.org/repos/asf/ignite/diff/22e50156 Branch: refs/heads/ignite-1770 Commit: 22e50156be3a4f117f1c38e7e1ec852ef27b48b6 Parents: 37269c1 Author: vozerov-gridgain <[email protected]> Authored: Wed Oct 28 16:29:47 2015 +0300 Committer: vozerov-gridgain <[email protected]> Committed: Wed Oct 28 16:29:47 2015 +0300 ---------------------------------------------------------------------- .../portable/PortableClassDescriptor.java | 120 +++++++++++++++---- .../internal/portable/PortableObjectSchema.java | 46 +++++++ .../java/org/apache/ignite/MyBenchmark.java | 19 ++- 3 files changed, 159 insertions(+), 26 deletions(-) ---------------------------------------------------------------------- http://git-wip-us.apache.org/repos/asf/ignite/blob/22e50156/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 25ec856..02c408e 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 @@ -38,6 +38,7 @@ import java.util.UUID; import org.apache.ignite.IgniteCheckedException; import org.apache.ignite.internal.processors.cache.CacheObjectImpl; import org.apache.ignite.internal.util.typedef.internal.U; +import org.apache.ignite.lang.IgniteBiTuple; import org.apache.ignite.marshaller.MarshallerExclusions; import org.apache.ignite.marshaller.optimized.OptimizedMarshaller; import org.apache.ignite.marshaller.portable.PortableMarshaller; @@ -63,6 +64,9 @@ public class PortableClassDescriptor { /** */ private final PortableSerializer serializer; + /** ID mapper. */ + private final PortableIdMapper idMapper; + /** */ private final Mode mode; @@ -102,6 +106,9 @@ public class PortableClassDescriptor { /** */ private final boolean excluded; + /** Object schemas. */ + private volatile Object schemas; + /** * @param ctx Context. * @param cls Class. @@ -138,6 +145,7 @@ public class PortableClassDescriptor { this.typeId = typeId; this.typeName = typeName; this.serializer = serializer; + this.idMapper = idMapper; this.keepDeserialized = keepDeserialized; this.registered = registered; @@ -307,6 +315,74 @@ public class PortableClassDescriptor { } /** + * Get ID mapper. + * + * @return ID mapper. + */ + public PortableIdMapper idMapper() { + return idMapper; + } + + /** + * Get schema for the given schema ID. + * + * @param schemaId Schema ID. + * @return Schema or {@code null} if there are no such schema. + */ + @SuppressWarnings("unchecked") + @Nullable public PortableObjectSchema schema(int schemaId) { + Object schemas0 = schemas; + + if (schemas0 instanceof IgniteBiTuple) { + // The most common case goes first. + IgniteBiTuple<Integer, PortableObjectSchema> curSchema = + (IgniteBiTuple<Integer, PortableObjectSchema>)schemas0; + + if (curSchema.get1() == schemaId) + return curSchema.get2(); + } + else if (schemas0 instanceof Map) { + Map<Integer, PortableObjectSchema> curSchemas = (Map<Integer, PortableObjectSchema>)schemas0; + + return curSchemas.get(schemaId); + } + + return null; + } + + /** + * Add schema. + * + * @param schemaId Schema ID. + * @param fields Fields. + */ + @SuppressWarnings("unchecked") + public void addSchema(int schemaId, Map<Integer, Integer> fields) { + synchronized (this) { + if (schemas == null) + schemas = new IgniteBiTuple<>(schemaId, new PortableObjectSchema(schemaId, fields)); + else if (schemas instanceof IgniteBiTuple) { + IgniteBiTuple<Integer, PortableObjectSchema> curSchema = + (IgniteBiTuple<Integer, PortableObjectSchema>)schemas; + + if (curSchema.get1() != schemaId) { + Map newSchemas = new HashMap(); + + newSchemas.put(curSchema.get1(), curSchema.get2()); + newSchemas.put(schemaId, new PortableObjectSchema(schemaId, fields)); + + schemas = newSchemas; + } + } + else { + Map<Integer, PortableObjectSchema> curSchemas = (Map<Integer, PortableObjectSchema>)schemas; + + curSchemas.put(schemaId, new PortableObjectSchema(schemaId, fields)); + } + } + } + + /** * @return portableWriteReplace() method */ @Nullable Method getWriteReplaceMethod() { @@ -409,57 +485,57 @@ public class PortableClassDescriptor { break; case SHORT_ARR: - writer.doWriteShortArray((short[])obj); + writer.doWriteShortArray((short[]) obj); break; case INT_ARR: - writer.doWriteIntArray((int[])obj); + writer.doWriteIntArray((int[]) obj); break; case LONG_ARR: - writer.doWriteLongArray((long[])obj); + writer.doWriteLongArray((long[]) obj); break; case FLOAT_ARR: - writer.doWriteFloatArray((float[])obj); + writer.doWriteFloatArray((float[]) obj); break; case DOUBLE_ARR: - writer.doWriteDoubleArray((double[])obj); + writer.doWriteDoubleArray((double[]) obj); break; case CHAR_ARR: - writer.doWriteCharArray((char[])obj); + writer.doWriteCharArray((char[]) obj); break; case BOOLEAN_ARR: - writer.doWriteBooleanArray((boolean[])obj); + writer.doWriteBooleanArray((boolean[]) obj); break; case DECIMAL_ARR: - writer.doWriteDecimalArray((BigDecimal[])obj); + writer.doWriteDecimalArray((BigDecimal[]) obj); break; case STRING_ARR: - writer.doWriteStringArray((String[])obj); + writer.doWriteStringArray((String[]) obj); break; case UUID_ARR: - writer.doWriteUuidArray((UUID[])obj); + writer.doWriteUuidArray((UUID[]) obj); break; case DATE_ARR: - writer.doWriteDateArray((Date[])obj); + writer.doWriteDateArray((Date[]) obj); break; @@ -927,57 +1003,57 @@ public class PortableClassDescriptor { break; case SHORT_ARR: - writer.writeShortArrayField((short[])val); + writer.writeShortArrayField((short[]) val); break; case INT_ARR: - writer.writeIntArrayField((int[])val); + writer.writeIntArrayField((int[]) val); break; case LONG_ARR: - writer.writeLongArrayField((long[])val); + writer.writeLongArrayField((long[]) val); break; case FLOAT_ARR: - writer.writeFloatArrayField((float[])val); + writer.writeFloatArrayField((float[]) val); break; case DOUBLE_ARR: - writer.writeDoubleArrayField((double[])val); + writer.writeDoubleArrayField((double[]) val); break; case CHAR_ARR: - writer.writeCharArrayField((char[])val); + writer.writeCharArrayField((char[]) val); break; case BOOLEAN_ARR: - writer.writeBooleanArrayField((boolean[])val); + writer.writeBooleanArrayField((boolean[]) val); break; case DECIMAL_ARR: - writer.writeDecimalArrayField((BigDecimal[])val); + writer.writeDecimalArrayField((BigDecimal[]) val); break; case STRING_ARR: - writer.writeStringArrayField((String[])val); + writer.writeStringArrayField((String[]) val); break; case UUID_ARR: - writer.writeUuidArrayField((UUID[])val); + writer.writeUuidArrayField((UUID[]) val); break; case DATE_ARR: - writer.writeDateArrayField((Date[])val); + writer.writeDateArrayField((Date[]) val); break; http://git-wip-us.apache.org/repos/asf/ignite/blob/22e50156/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableObjectSchema.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableObjectSchema.java b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableObjectSchema.java new file mode 100644 index 0000000..a661781 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/portable/PortableObjectSchema.java @@ -0,0 +1,46 @@ +package org.apache.ignite.internal.portable; + +import java.util.Map; + +/** + * Portable object schema. + */ +public class PortableObjectSchema { + /** Schema ID. */ + private final int schemaId; + + /** Fields. */ + private final Map<Integer, Integer> fields; + + /** + * Constructor. + * + * @param schemaId Schema ID. + * @param fields Fields. + */ + public PortableObjectSchema(int schemaId, Map<Integer, Integer> fields) { + this.schemaId = schemaId; + this.fields = fields; + } + + /** + * Get schema ID. + * + * @return Schema ID. + */ + public int schemaId() { + return schemaId; + } + + /** + * Get field offset position. + * + * @param fieldId Field ID. + * @return Field offset position. + */ + public int fieldOffsetPosition(int fieldId) { + Integer pos = fields.get(fieldId); + + return pos != null ? pos : 0; + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/22e50156/modules/microbench/src/main/java/org/apache/ignite/MyBenchmark.java ---------------------------------------------------------------------- diff --git a/modules/microbench/src/main/java/org/apache/ignite/MyBenchmark.java b/modules/microbench/src/main/java/org/apache/ignite/MyBenchmark.java index 1f3dce4..662acee 100644 --- a/modules/microbench/src/main/java/org/apache/ignite/MyBenchmark.java +++ b/modules/microbench/src/main/java/org/apache/ignite/MyBenchmark.java @@ -31,16 +31,20 @@ package org.apache.ignite; +import org.apache.ignite.internal.portable.GridPortableMarshaller; import org.apache.ignite.internal.portable.PortableContext; import org.apache.ignite.internal.portable.PortableMetaDataHandler; +import org.apache.ignite.internal.portable.PortableObjectImpl; import org.apache.ignite.internal.portable.streams.PortableSimpleMemoryAllocator; import org.apache.ignite.internal.util.IgniteUtils; +import org.apache.ignite.internal.util.typedef.internal.U; import org.apache.ignite.marshaller.optimized.OptimizedMarshaller; import org.apache.ignite.marshaller.portable.PortableMarshaller; import org.apache.ignite.portable.PortableException; import org.apache.ignite.portable.PortableMarshalAware; import org.apache.ignite.portable.PortableMetadata; +import org.apache.ignite.portable.PortableObject; import org.apache.ignite.portable.PortableReader; import org.apache.ignite.portable.PortableWriter; import org.apache.ignite.util.MarshallerContextMicrobenchImpl; @@ -84,7 +88,7 @@ public class MyBenchmark { private static byte[] marshAddrBytes; - private static byte[] optMarshAddrBytes; + private static PortableObject marshPortable; @Setup public static void setup() throws Exception { @@ -104,7 +108,9 @@ public class MyBenchmark { optMarsh.setContext(new MarshallerContextMicrobenchImpl(null)); marshAddrBytes = marsh.marshal(new Address()); - optMarshAddrBytes = optMarsh.marshal(new Address()); + + marshPortable = new PortableObjectImpl(U.<GridPortableMarshaller>field(marsh, "impl").context(), + marshAddrBytes, 0); byte[] data = marsh.marshal(new Address()); @@ -116,9 +122,14 @@ public class MyBenchmark { // return marsh.marshal(new Address()); // } +// @Benchmark +// public Address testAddressRead() throws Exception { +// return marsh.unmarshal(marshAddrBytes, null); +// } + @Benchmark - public Address testAddressRead() throws Exception { - return marsh.unmarshal(marshAddrBytes, null); + public Object testFieldRead() throws Exception { + return marshPortable.field("street"); } private static final Address addr = new Address();
