http://git-wip-us.apache.org/repos/asf/ignite/blob/71ad9cea/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriteMode.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriteMode.java b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriteMode.java index a20b5ed..9a37bdb 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriteMode.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriteMode.java @@ -22,142 +22,142 @@ package org.apache.ignite.internal.binary; */ public enum BinaryWriteMode { /** Primitive byte. */ - P_BYTE(GridPortableMarshaller.BYTE), + P_BYTE(GridBinaryMarshaller.BYTE), /** Primitive boolean. */ - P_BOOLEAN(GridPortableMarshaller.BOOLEAN), + P_BOOLEAN(GridBinaryMarshaller.BOOLEAN), /** Primitive short. */ - P_SHORT(GridPortableMarshaller.SHORT), + P_SHORT(GridBinaryMarshaller.SHORT), /** Primitive char. */ - P_CHAR(GridPortableMarshaller.CHAR), + P_CHAR(GridBinaryMarshaller.CHAR), /** Primitive int. */ - P_INT(GridPortableMarshaller.INT), + P_INT(GridBinaryMarshaller.INT), /** Primitive long. */ - P_LONG(GridPortableMarshaller.LONG), + P_LONG(GridBinaryMarshaller.LONG), /** Primitive float. */ - P_FLOAT(GridPortableMarshaller.FLOAT), + P_FLOAT(GridBinaryMarshaller.FLOAT), /** Primitive int. */ - P_DOUBLE(GridPortableMarshaller.DOUBLE), + P_DOUBLE(GridBinaryMarshaller.DOUBLE), /** */ - BYTE(GridPortableMarshaller.BYTE), + BYTE(GridBinaryMarshaller.BYTE), /** */ - SHORT(GridPortableMarshaller.SHORT), + SHORT(GridBinaryMarshaller.SHORT), /** */ - INT(GridPortableMarshaller.INT), + INT(GridBinaryMarshaller.INT), /** */ - LONG(GridPortableMarshaller.LONG), + LONG(GridBinaryMarshaller.LONG), /** */ - FLOAT(GridPortableMarshaller.FLOAT), + FLOAT(GridBinaryMarshaller.FLOAT), /** */ - DOUBLE(GridPortableMarshaller.DOUBLE), + DOUBLE(GridBinaryMarshaller.DOUBLE), /** */ - CHAR(GridPortableMarshaller.CHAR), + CHAR(GridBinaryMarshaller.CHAR), /** */ - BOOLEAN(GridPortableMarshaller.BOOLEAN), + BOOLEAN(GridBinaryMarshaller.BOOLEAN), /** */ - DECIMAL(GridPortableMarshaller.DECIMAL), + DECIMAL(GridBinaryMarshaller.DECIMAL), /** */ - STRING(GridPortableMarshaller.STRING), + STRING(GridBinaryMarshaller.STRING), /** */ - UUID(GridPortableMarshaller.UUID), + UUID(GridBinaryMarshaller.UUID), /** */ - DATE(GridPortableMarshaller.DATE), + DATE(GridBinaryMarshaller.DATE), /** */ - TIMESTAMP(GridPortableMarshaller.TIMESTAMP), + TIMESTAMP(GridBinaryMarshaller.TIMESTAMP), /** */ - BYTE_ARR(GridPortableMarshaller.BYTE_ARR), + BYTE_ARR(GridBinaryMarshaller.BYTE_ARR), /** */ - SHORT_ARR(GridPortableMarshaller.SHORT_ARR), + SHORT_ARR(GridBinaryMarshaller.SHORT_ARR), /** */ - INT_ARR(GridPortableMarshaller.INT_ARR), + INT_ARR(GridBinaryMarshaller.INT_ARR), /** */ - LONG_ARR(GridPortableMarshaller.LONG_ARR), + LONG_ARR(GridBinaryMarshaller.LONG_ARR), /** */ - FLOAT_ARR(GridPortableMarshaller.FLOAT_ARR), + FLOAT_ARR(GridBinaryMarshaller.FLOAT_ARR), /** */ - DOUBLE_ARR(GridPortableMarshaller.DOUBLE_ARR), + DOUBLE_ARR(GridBinaryMarshaller.DOUBLE_ARR), /** */ - CHAR_ARR(GridPortableMarshaller.CHAR_ARR), + CHAR_ARR(GridBinaryMarshaller.CHAR_ARR), /** */ - BOOLEAN_ARR(GridPortableMarshaller.BOOLEAN_ARR), + BOOLEAN_ARR(GridBinaryMarshaller.BOOLEAN_ARR), /** */ - DECIMAL_ARR(GridPortableMarshaller.DECIMAL_ARR), + DECIMAL_ARR(GridBinaryMarshaller.DECIMAL_ARR), /** */ - STRING_ARR(GridPortableMarshaller.STRING_ARR), + STRING_ARR(GridBinaryMarshaller.STRING_ARR), /** */ - UUID_ARR(GridPortableMarshaller.UUID_ARR), + UUID_ARR(GridBinaryMarshaller.UUID_ARR), /** */ - DATE_ARR(GridPortableMarshaller.DATE_ARR), + DATE_ARR(GridBinaryMarshaller.DATE_ARR), /** */ - TIMESTAMP_ARR(GridPortableMarshaller.TIMESTAMP_ARR), + TIMESTAMP_ARR(GridBinaryMarshaller.TIMESTAMP_ARR), /** */ - OBJECT_ARR(GridPortableMarshaller.OBJ_ARR), + OBJECT_ARR(GridBinaryMarshaller.OBJ_ARR), /** */ - COL(GridPortableMarshaller.COL), + COL(GridBinaryMarshaller.COL), /** */ - MAP(GridPortableMarshaller.MAP), + MAP(GridBinaryMarshaller.MAP), /** */ - PORTABLE_OBJ(GridPortableMarshaller.OBJ), + PORTABLE_OBJ(GridBinaryMarshaller.OBJ), /** */ - ENUM(GridPortableMarshaller.ENUM), + ENUM(GridBinaryMarshaller.ENUM), /** Portable enum. */ - PORTABLE_ENUM(GridPortableMarshaller.ENUM), + PORTABLE_ENUM(GridBinaryMarshaller.ENUM), /** */ - ENUM_ARR(GridPortableMarshaller.ENUM_ARR), + ENUM_ARR(GridBinaryMarshaller.ENUM_ARR), /** */ - CLASS(GridPortableMarshaller.CLASS), + CLASS(GridBinaryMarshaller.CLASS), /** */ - PORTABLE(GridPortableMarshaller.PORTABLE_OBJ), + PORTABLE(GridBinaryMarshaller.PORTABLE_OBJ), /** */ - EXTERNALIZABLE(GridPortableMarshaller.OBJ), + EXTERNALIZABLE(GridBinaryMarshaller.OBJ), /** */ - OBJECT(GridPortableMarshaller.OBJ), + OBJECT(GridBinaryMarshaller.OBJ), /** */ - EXCLUSION(GridPortableMarshaller.OBJ); + EXCLUSION(GridBinaryMarshaller.OBJ); /** Type ID. */ private final int typeId;
http://git-wip-us.apache.org/repos/asf/ignite/blob/71ad9cea/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java index 25f4d70..f073dd0 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java @@ -22,9 +22,8 @@ import org.apache.ignite.binary.BinaryIdMapper; import org.apache.ignite.binary.BinaryObjectException; import org.apache.ignite.binary.BinaryRawWriter; import org.apache.ignite.binary.BinaryWriter; -import org.apache.ignite.internal.binary.streams.PortableOutputStream; -import org.apache.ignite.internal.binary.streams.PortableHeapOutputStream; -import org.apache.ignite.internal.binary.streams.PortableOutputStream; +import org.apache.ignite.internal.binary.streams.BinaryOutputStream; +import org.apache.ignite.internal.binary.streams.BinaryHeapOutputStream; import org.apache.ignite.internal.util.typedef.internal.A; import org.jetbrains.annotations.Nullable; @@ -40,45 +39,6 @@ import java.util.Map; import java.util.UUID; import static java.nio.charset.StandardCharsets.UTF_8; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.BOOLEAN; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.BOOLEAN_ARR; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.BYTE; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.BYTE_ARR; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.CHAR; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.CHAR_ARR; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.CLASS; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.COL; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.DATE; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.DATE_ARR; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.DECIMAL; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.DECIMAL_ARR; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.DFLT_HDR_LEN; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.DOUBLE; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.DOUBLE_ARR; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.ENUM; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.ENUM_ARR; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.FLOAT; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.FLOAT_ARR; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.INT; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.INT_ARR; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.LONG; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.LONG_ARR; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.MAP; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.NULL; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.OBJ; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.OBJ_ARR; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.OPTM_MARSH; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.PORTABLE_OBJ; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.PROTO_VER; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.SHORT; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.SHORT_ARR; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.STRING; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.STRING_ARR; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.TIMESTAMP; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.TIMESTAMP_ARR; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.UNREGISTERED_TYPE_ID; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.UUID; -import static org.apache.ignite.internal.binary.GridPortableMarshaller.UUID_ARR; /** * Portable writer implementation. @@ -91,10 +51,10 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje private static final int INIT_CAP = 1024; /** */ - private final PortableContext ctx; + private final BinaryContext ctx; /** Output stream. */ - private final PortableOutputStream out; + private final BinaryOutputStream out; /** Schema. */ private final BinaryWriterSchemaHolder schema; @@ -112,7 +72,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje private BinaryWriterHandles handles; /** Schema ID. */ - private int schemaId = PortableUtils.schemaInitialId(); + private int schemaId = BinaryUtils.schemaInitialId(); /** Amount of written fields. */ private int fieldCnt; @@ -123,7 +83,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje /** * @param ctx Context. */ - public BinaryWriterExImpl(PortableContext ctx) { + public BinaryWriterExImpl(BinaryContext ctx) { this(ctx, BinaryThreadLocalContext.get()); } @@ -131,8 +91,8 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje * @param ctx Context. * @param tlsCtx TLS context. */ - public BinaryWriterExImpl(PortableContext ctx, BinaryThreadLocalContext tlsCtx) { - this(ctx, new PortableHeapOutputStream(INIT_CAP, tlsCtx.chunk()), tlsCtx.schemaHolder(), null); + public BinaryWriterExImpl(BinaryContext ctx, BinaryThreadLocalContext tlsCtx) { + this(ctx, new BinaryHeapOutputStream(INIT_CAP, tlsCtx.chunk()), tlsCtx.schemaHolder(), null); } /** @@ -140,7 +100,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje * @param out Output stream. * @param handles Handles. */ - public BinaryWriterExImpl(PortableContext ctx, PortableOutputStream out, BinaryWriterSchemaHolder schema, + public BinaryWriterExImpl(BinaryContext ctx, BinaryOutputStream out, BinaryWriterSchemaHolder schema, BinaryWriterHandles handles) { this.ctx = ctx; this.out = out; @@ -182,19 +142,19 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje Class<?> cls = obj.getClass(); - PortableClassDescriptor desc = ctx.descriptorForClass(cls, false); + BinaryClassDescriptor desc = ctx.descriptorForClass(cls, false); if (desc == null) throw new BinaryObjectException("Object is not portable: [class=" + cls + ']'); if (desc.excluded()) { - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); return; } if (desc.useOptimizedMarshaller()) { - out.writeByte(GridPortableMarshaller.OPTM_MARSH); + out.writeByte(GridBinaryMarshaller.OPTM_MARSH); try { byte[] arr = ctx.optimizedMarsh().marshal(obj); @@ -227,7 +187,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje } if (replacedObj == null) { - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); return; } @@ -269,7 +229,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje * @param clsName Class name (optional). */ public void preWrite(@Nullable String clsName) { - out.position(out.position() + GridPortableMarshaller.DFLT_HDR_LEN); + out.position(out.position() + GridBinaryMarshaller.DFLT_HDR_LEN); if (clsName != null) doWriteString(clsName); @@ -288,11 +248,11 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje if (userType) { if (ctx.isCompactFooter()) { - flags = PortableUtils.FLAG_USR_TYP | PortableUtils.FLAG_COMPACT_FOOTER; + flags = BinaryUtils.FLAG_USR_TYP | BinaryUtils.FLAG_COMPACT_FOOTER; useCompactFooter = true; } else { - flags = PortableUtils.FLAG_USR_TYP; + flags = BinaryUtils.FLAG_USR_TYP; useCompactFooter = false; } } @@ -309,18 +269,18 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje offset = out.position() - start; // Write the schema. - flags |= PortableUtils.FLAG_HAS_SCHEMA; + flags |= BinaryUtils.FLAG_HAS_SCHEMA; int offsetByteCnt = schema.write(out, fieldCnt, useCompactFooter); - if (offsetByteCnt == PortableUtils.OFFSET_1) - flags |= PortableUtils.FLAG_OFFSET_ONE_BYTE; - else if (offsetByteCnt == PortableUtils.OFFSET_2) - flags |= PortableUtils.FLAG_OFFSET_TWO_BYTES; + if (offsetByteCnt == BinaryUtils.OFFSET_1) + flags |= BinaryUtils.FLAG_OFFSET_ONE_BYTE; + else if (offsetByteCnt == BinaryUtils.OFFSET_2) + flags |= BinaryUtils.FLAG_OFFSET_TWO_BYTES; // Write raw offset if needed. if (rawOffPos != 0) { - flags |= PortableUtils.FLAG_HAS_RAW; + flags |= BinaryUtils.FLAG_HAS_RAW; out.writeInt(rawOffPos - start); } @@ -331,7 +291,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje offset = rawOffPos - start; // If there is no schema, we are free to write raw offset to schema offset. - flags |= PortableUtils.FLAG_HAS_RAW; + flags |= BinaryUtils.FLAG_HAS_RAW; } else { finalSchemaId = 0; @@ -344,10 +304,10 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje out.unsafePosition(start); - out.unsafeWriteByte(GridPortableMarshaller.OBJ); - out.unsafeWriteByte(GridPortableMarshaller.PROTO_VER); + out.unsafeWriteByte(GridBinaryMarshaller.OBJ); + out.unsafeWriteByte(GridBinaryMarshaller.PROTO_VER); out.unsafeWriteShort(flags); - out.unsafeWriteInt(registered ? typeId : GridPortableMarshaller.UNREGISTERED_TYPE_ID); + out.unsafeWriteInt(registered ? typeId : GridBinaryMarshaller.UNREGISTERED_TYPE_ID); out.unsafeWriteInt(hashCode); out.unsafeWriteInt(retPos - start); out.unsafeWriteInt(finalSchemaId); @@ -389,11 +349,11 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ public void doWriteDecimal(@Nullable BigDecimal val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { out.unsafeEnsure(1 + 4 + 4); - out.unsafeWriteByte(GridPortableMarshaller.DECIMAL); + out.unsafeWriteByte(GridBinaryMarshaller.DECIMAL); BigInteger intVal = val.unscaledValue(); @@ -417,12 +377,12 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ public void doWriteString(@Nullable String val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { byte[] strArr = val.getBytes(UTF_8); out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridPortableMarshaller.STRING); + out.unsafeWriteByte(GridBinaryMarshaller.STRING); out.unsafeWriteInt(strArr.length); out.writeByteArray(strArr); @@ -434,10 +394,10 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ public void doWriteUuid(@Nullable UUID uuid) { if (uuid == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { out.unsafeEnsure(1 + 8 + 8); - out.unsafeWriteByte(GridPortableMarshaller.UUID); + out.unsafeWriteByte(GridBinaryMarshaller.UUID); out.unsafeWriteLong(uuid.getMostSignificantBits()); out.unsafeWriteLong(uuid.getLeastSignificantBits()); } @@ -448,10 +408,10 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ public void doWriteDate(@Nullable Date date) { if (date == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { out.unsafeEnsure(1 + 8); - out.unsafeWriteByte(GridPortableMarshaller.DATE); + out.unsafeWriteByte(GridBinaryMarshaller.DATE); out.unsafeWriteLong(date.getTime()); } } @@ -461,10 +421,10 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ public void doWriteTimestamp(@Nullable Timestamp ts) { if (ts== null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { out.unsafeEnsure(1 + 8 + 4); - out.unsafeWriteByte(GridPortableMarshaller.TIMESTAMP); + out.unsafeWriteByte(GridBinaryMarshaller.TIMESTAMP); out.unsafeWriteLong(ts.getTime()); out.unsafeWriteInt(ts.getNanos() % 1000000); } @@ -478,7 +438,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ public void doWriteObject(@Nullable Object obj) throws BinaryObjectException { if (obj == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, out, schema, handles()); @@ -491,10 +451,10 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void doWriteByteArray(@Nullable byte[] val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridPortableMarshaller.BYTE_ARR); + out.unsafeWriteByte(GridBinaryMarshaller.BYTE_ARR); out.unsafeWriteInt(val.length); out.writeByteArray(val); @@ -506,10 +466,10 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void doWriteShortArray(@Nullable short[] val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridPortableMarshaller.SHORT_ARR); + out.unsafeWriteByte(GridBinaryMarshaller.SHORT_ARR); out.unsafeWriteInt(val.length); out.writeShortArray(val); @@ -521,10 +481,10 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void doWriteIntArray(@Nullable int[] val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridPortableMarshaller.INT_ARR); + out.unsafeWriteByte(GridBinaryMarshaller.INT_ARR); out.unsafeWriteInt(val.length); out.writeIntArray(val); @@ -536,10 +496,10 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void doWriteLongArray(@Nullable long[] val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridPortableMarshaller.LONG_ARR); + out.unsafeWriteByte(GridBinaryMarshaller.LONG_ARR); out.unsafeWriteInt(val.length); out.writeLongArray(val); @@ -551,10 +511,10 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void doWriteFloatArray(@Nullable float[] val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridPortableMarshaller.FLOAT_ARR); + out.unsafeWriteByte(GridBinaryMarshaller.FLOAT_ARR); out.unsafeWriteInt(val.length); out.writeFloatArray(val); @@ -566,10 +526,10 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void doWriteDoubleArray(@Nullable double[] val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridPortableMarshaller.DOUBLE_ARR); + out.unsafeWriteByte(GridBinaryMarshaller.DOUBLE_ARR); out.unsafeWriteInt(val.length); out.writeDoubleArray(val); @@ -581,10 +541,10 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void doWriteCharArray(@Nullable char[] val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridPortableMarshaller.CHAR_ARR); + out.unsafeWriteByte(GridBinaryMarshaller.CHAR_ARR); out.unsafeWriteInt(val.length); out.writeCharArray(val); @@ -596,10 +556,10 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void doWriteBooleanArray(@Nullable boolean[] val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridPortableMarshaller.BOOLEAN_ARR); + out.unsafeWriteByte(GridBinaryMarshaller.BOOLEAN_ARR); out.unsafeWriteInt(val.length); out.writeBooleanArray(val); @@ -611,10 +571,10 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void doWriteDecimalArray(@Nullable BigDecimal[] val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridPortableMarshaller.DECIMAL_ARR); + out.unsafeWriteByte(GridBinaryMarshaller.DECIMAL_ARR); out.unsafeWriteInt(val.length); for (BigDecimal str : val) @@ -627,10 +587,10 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void doWriteStringArray(@Nullable String[] val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridPortableMarshaller.STRING_ARR); + out.unsafeWriteByte(GridBinaryMarshaller.STRING_ARR); out.unsafeWriteInt(val.length); for (String str : val) @@ -643,10 +603,10 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void doWriteUuidArray(@Nullable UUID[] val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridPortableMarshaller.UUID_ARR); + out.unsafeWriteByte(GridBinaryMarshaller.UUID_ARR); out.unsafeWriteInt(val.length); for (UUID uuid : val) @@ -659,10 +619,10 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void doWriteDateArray(@Nullable Date[] val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridPortableMarshaller.DATE_ARR); + out.unsafeWriteByte(GridBinaryMarshaller.DATE_ARR); out.unsafeWriteInt(val.length); for (Date date : val) @@ -675,10 +635,10 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void doWriteTimestampArray(@Nullable Timestamp[] val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridPortableMarshaller.TIMESTAMP_ARR); + out.unsafeWriteByte(GridBinaryMarshaller.TIMESTAMP_ARR); out.unsafeWriteInt(val.length); for (Timestamp ts : val) @@ -692,20 +652,20 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void doWriteObjectArray(@Nullable Object[] val) throws BinaryObjectException { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { if (tryWriteAsHandle(val)) return; - PortableClassDescriptor desc = ctx.descriptorForClass(val.getClass().getComponentType(), false); + BinaryClassDescriptor desc = ctx.descriptorForClass(val.getClass().getComponentType(), false); out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridPortableMarshaller.OBJ_ARR); + out.unsafeWriteByte(GridBinaryMarshaller.OBJ_ARR); if (desc.registered()) out.unsafeWriteInt(desc.typeId()); else { - out.unsafeWriteInt(GridPortableMarshaller.UNREGISTERED_TYPE_ID); + out.unsafeWriteInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); doWriteString(val.getClass().getComponentType().getName()); } @@ -723,13 +683,13 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void doWriteCollection(@Nullable Collection<?> col) throws BinaryObjectException { if (col == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { if (tryWriteAsHandle(col)) return; out.unsafeEnsure(1 + 4 + 1); - out.unsafeWriteByte(GridPortableMarshaller.COL); + out.unsafeWriteByte(GridBinaryMarshaller.COL); out.unsafeWriteInt(col.size()); out.unsafeWriteByte(ctx.collectionType(col.getClass())); @@ -744,13 +704,13 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void doWriteMap(@Nullable Map<?, ?> map) throws BinaryObjectException { if (map == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { if (tryWriteAsHandle(map)) return; out.unsafeEnsure(1 + 4 + 1); - out.unsafeWriteByte(GridPortableMarshaller.MAP); + out.unsafeWriteByte(GridBinaryMarshaller.MAP); out.unsafeWriteInt(map.size()); out.unsafeWriteByte(ctx.mapType(map.getClass())); @@ -766,18 +726,18 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void doWriteEnum(@Nullable Enum<?> val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { - PortableClassDescriptor desc = ctx.descriptorForClass(val.getClass(), false); + BinaryClassDescriptor desc = ctx.descriptorForClass(val.getClass(), false); out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridPortableMarshaller.ENUM); + out.unsafeWriteByte(GridBinaryMarshaller.ENUM); if (desc.registered()) out.unsafeWriteInt(desc.typeId()); else { - out.unsafeWriteInt(GridPortableMarshaller.UNREGISTERED_TYPE_ID); + out.unsafeWriteInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); doWriteString(val.getClass().getName()); } @@ -795,10 +755,10 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridPortableMarshaller.ENUM); + out.unsafeWriteByte(GridBinaryMarshaller.ENUM); out.unsafeWriteInt(typeId); - if (typeId == GridPortableMarshaller.UNREGISTERED_TYPE_ID) + if (typeId == GridBinaryMarshaller.UNREGISTERED_TYPE_ID) doWriteString(val.className()); out.writeInt(val.enumOrdinal()); @@ -811,18 +771,18 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje assert val == null || val.getClass().getComponentType().isEnum(); if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { - PortableClassDescriptor desc = ctx.descriptorForClass(val.getClass().getComponentType(), false); + BinaryClassDescriptor desc = ctx.descriptorForClass(val.getClass().getComponentType(), false); out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridPortableMarshaller.ENUM_ARR); + out.unsafeWriteByte(GridBinaryMarshaller.ENUM_ARR); if (desc.registered()) out.unsafeWriteInt(desc.typeId()); else { - out.unsafeWriteInt(GridPortableMarshaller.UNREGISTERED_TYPE_ID); + out.unsafeWriteInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); doWriteString(val.getClass().getComponentType().getName()); } @@ -840,18 +800,18 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void doWriteClass(@Nullable Class val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { - PortableClassDescriptor desc = ctx.descriptorForClass(val, false); + BinaryClassDescriptor desc = ctx.descriptorForClass(val, false); out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridPortableMarshaller.CLASS); + out.unsafeWriteByte(GridBinaryMarshaller.CLASS); if (desc.registered()) out.unsafeWriteInt(desc.typeId()); else { - out.unsafeWriteInt(GridPortableMarshaller.UNREGISTERED_TYPE_ID); + out.unsafeWriteInt(GridBinaryMarshaller.UNREGISTERED_TYPE_ID); doWriteString(val.getClass().getName()); } @@ -863,13 +823,13 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ public void doWritePortableObject(@Nullable BinaryObjectImpl po) { if (po == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { byte[] poArr = po.array(); out.unsafeEnsure(1 + 4 + poArr.length + 4); - out.unsafeWriteByte(GridPortableMarshaller.PORTABLE_OBJ); + out.unsafeWriteByte(GridBinaryMarshaller.PORTABLE_OBJ); out.unsafeWriteInt(poArr.length); out.writeByteArray(poArr); out.unsafeWriteInt(po.start()); @@ -882,7 +842,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje void writeByteFieldPrimitive(byte val) { out.unsafeEnsure(1 + 1); - out.unsafeWriteByte(GridPortableMarshaller.BYTE); + out.unsafeWriteByte(GridBinaryMarshaller.BYTE); out.unsafeWriteByte(val); } @@ -891,7 +851,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void writeByteField(@Nullable Byte val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else writeByteFieldPrimitive(val); } @@ -909,7 +869,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje void writeShortFieldPrimitive(short val) { out.unsafeEnsure(1 + 2); - out.unsafeWriteByte(GridPortableMarshaller.SHORT); + out.unsafeWriteByte(GridBinaryMarshaller.SHORT); out.unsafeWriteShort(val); } @@ -918,7 +878,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void writeShortField(@Nullable Short val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else writeShortFieldPrimitive(val); } @@ -929,7 +889,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje void writeIntFieldPrimitive(int val) { out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridPortableMarshaller.INT); + out.unsafeWriteByte(GridBinaryMarshaller.INT); out.unsafeWriteInt(val); } @@ -938,7 +898,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void writeIntField(@Nullable Integer val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else writeIntFieldPrimitive(val); } @@ -949,7 +909,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje void writeLongFieldPrimitive(long val) { out.unsafeEnsure(1 + 8); - out.unsafeWriteByte(GridPortableMarshaller.LONG); + out.unsafeWriteByte(GridBinaryMarshaller.LONG); out.unsafeWriteLong(val); } @@ -958,7 +918,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void writeLongField(@Nullable Long val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else writeLongFieldPrimitive(val); } @@ -969,7 +929,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje void writeFloatFieldPrimitive(float val) { out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridPortableMarshaller.FLOAT); + out.unsafeWriteByte(GridBinaryMarshaller.FLOAT); out.unsafeWriteFloat(val); } @@ -978,7 +938,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void writeFloatField(@Nullable Float val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else writeFloatFieldPrimitive(val); } @@ -989,7 +949,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje void writeDoubleFieldPrimitive(double val) { out.unsafeEnsure(1 + 8); - out.unsafeWriteByte(GridPortableMarshaller.DOUBLE); + out.unsafeWriteByte(GridBinaryMarshaller.DOUBLE); out.unsafeWriteDouble(val); } @@ -998,7 +958,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void writeDoubleField(@Nullable Double val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else writeDoubleFieldPrimitive(val); } @@ -1009,7 +969,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje void writeCharFieldPrimitive(char val) { out.unsafeEnsure(1 + 2); - out.unsafeWriteByte(GridPortableMarshaller.CHAR); + out.unsafeWriteByte(GridBinaryMarshaller.CHAR); out.unsafeWriteChar(val); } @@ -1018,7 +978,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void writeCharField(@Nullable Character val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else writeCharFieldPrimitive(val); } @@ -1029,7 +989,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje void writeBooleanFieldPrimitive(boolean val) { out.unsafeEnsure(1 + 1); - out.unsafeWriteByte(GridPortableMarshaller.BOOLEAN); + out.unsafeWriteByte(GridBinaryMarshaller.BOOLEAN); out.unsafeWriteBoolean(val); } @@ -1038,7 +998,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje */ void writeBooleanField(@Nullable Boolean val) { if (val == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else writeBooleanFieldPrimitive(val); } @@ -1380,7 +1340,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje /** {@inheritDoc} */ @Override public void writeObjectDetached(@Nullable Object obj) throws BinaryObjectException { if (obj == null) - out.writeByte(GridPortableMarshaller.NULL); + out.writeByte(GridBinaryMarshaller.NULL); else { BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx, out, schema, null); @@ -1601,7 +1561,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje } /** {@inheritDoc} */ - @Override public PortableOutputStream out() { + @Override public BinaryOutputStream out() { return out; } @@ -1698,7 +1658,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje int fieldOff = out.position() - start; // Advance schema hash. - schemaId = PortableUtils.updateSchemaId(schemaId, fieldId); + schemaId = BinaryUtils.updateSchemaId(schemaId, fieldId); schema.push(fieldId, fieldOff); @@ -1736,8 +1696,8 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje /** * @return Current writer's schema. */ - public PortableSchema currentSchema() { - PortableSchema.Builder builder = PortableSchema.Builder.newBuilder(); + public BinarySchema currentSchema() { + BinarySchema.Builder builder = BinarySchema.Builder.newBuilder(); if (schema != null) schema.build(builder, fieldCnt); @@ -1778,7 +1738,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje else { out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridPortableMarshaller.HANDLE); + out.unsafeWriteByte(GridBinaryMarshaller.HANDLE); out.unsafeWriteInt(pos - old); return true; @@ -1802,7 +1762,7 @@ public class BinaryWriterExImpl implements BinaryWriter, BinaryRawWriterEx, Obje /** * @return Portable context. */ - public PortableContext context() { + public BinaryContext context() { return ctx; } } http://git-wip-us.apache.org/repos/asf/ignite/blob/71ad9cea/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriterSchemaHolder.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriterSchemaHolder.java b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriterSchemaHolder.java index 7fd6442..22ae8e3 100644 --- a/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriterSchemaHolder.java +++ b/modules/core/src/main/java/org/apache/ignite/internal/binary/BinaryWriterSchemaHolder.java @@ -17,8 +17,7 @@ package org.apache.ignite.internal.binary; -import org.apache.ignite.internal.binary.streams.PortableOutputStream; -import org.apache.ignite.internal.binary.streams.PortableOutputStream; +import org.apache.ignite.internal.binary.streams.BinaryOutputStream; /** * Binary writer schema holder. @@ -66,7 +65,7 @@ public class BinaryWriterSchemaHolder { * @param builder Builder. * @param fieldCnt Fields count. */ - public void build(PortableSchema.Builder builder, int fieldCnt) { + public void build(BinarySchema.Builder builder, int fieldCnt) { for (int curIdx = idx - fieldCnt * 2; curIdx < idx; curIdx += 2) builder.addField(data[curIdx]); } @@ -79,7 +78,7 @@ public class BinaryWriterSchemaHolder { * @param compactFooter Whether footer should be written in compact form. * @return Amount of bytes dedicated to each field offset. Could be 1, 2 or 4. */ - public int write(PortableOutputStream out, int fieldCnt, boolean compactFooter) { + public int write(BinaryOutputStream out, int fieldCnt, boolean compactFooter) { int startIdx = idx - fieldCnt * 2; assert startIdx >= 0; @@ -95,19 +94,19 @@ public class BinaryWriterSchemaHolder { for (int curIdx = startIdx + 1; curIdx < idx; curIdx += 2) out.unsafeWriteByte((byte)data[curIdx]); - res = PortableUtils.OFFSET_1; + res = BinaryUtils.OFFSET_1; } else if (lastOffset < MAX_OFFSET_2) { for (int curIdx = startIdx + 1; curIdx < idx; curIdx += 2) out.unsafeWriteShort((short) data[curIdx]); - res = PortableUtils.OFFSET_2; + res = BinaryUtils.OFFSET_2; } else { for (int curIdx = startIdx + 1; curIdx < idx; curIdx += 2) out.unsafeWriteInt(data[curIdx]); - res = PortableUtils.OFFSET_4; + res = BinaryUtils.OFFSET_4; } } else { @@ -117,7 +116,7 @@ public class BinaryWriterSchemaHolder { out.unsafeWriteByte((byte) data[curIdx++]); } - res = PortableUtils.OFFSET_1; + res = BinaryUtils.OFFSET_1; } else if (lastOffset < MAX_OFFSET_2) { for (int curIdx = startIdx; curIdx < idx;) { @@ -125,7 +124,7 @@ public class BinaryWriterSchemaHolder { out.unsafeWriteShort((short) data[curIdx++]); } - res = PortableUtils.OFFSET_2; + res = BinaryUtils.OFFSET_2; } else { for (int curIdx = startIdx; curIdx < idx;) { @@ -133,7 +132,7 @@ public class BinaryWriterSchemaHolder { out.unsafeWriteInt(data[curIdx++]); } - res = PortableUtils.OFFSET_4; + res = BinaryUtils.OFFSET_4; } } http://git-wip-us.apache.org/repos/asf/ignite/blob/71ad9cea/modules/core/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java b/modules/core/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java new file mode 100644 index 0000000..277abd0 --- /dev/null +++ b/modules/core/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java @@ -0,0 +1,286 @@ +/* + * 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.binary; + +import org.apache.ignite.internal.binary.streams.BinaryHeapInputStream; +import org.apache.ignite.internal.binary.streams.BinaryInputStream; +import org.apache.ignite.internal.binary.streams.BinaryOutputStream; +import org.apache.ignite.binary.BinaryObjectException; +import org.jetbrains.annotations.Nullable; + +/** + * Portable objects marshaller. + */ +public class GridBinaryMarshaller { + /** */ + public static final ThreadLocal<Boolean> KEEP_PORTABLES = new ThreadLocal<Boolean>() { + @Override protected Boolean initialValue() { + return true; + } + }; + + /** */ + static final byte OPTM_MARSH = -2; + + /** */ + public static final byte BYTE = 1; + + /** */ + public static final byte SHORT = 2; + + /** */ + public static final byte INT = 3; + + /** */ + public static final byte LONG = 4; + + /** */ + public static final byte FLOAT = 5; + + /** */ + public static final byte DOUBLE = 6; + + /** */ + public static final byte CHAR = 7; + + /** */ + public static final byte BOOLEAN = 8; + + /** */ + public static final byte DECIMAL = 30; + + /** */ + public static final byte STRING = 9; + + /** */ + public static final byte UUID = 10; + + /** */ + public static final byte DATE = 11; + + /** */ + public static final byte BYTE_ARR = 12; + + /** */ + public static final byte SHORT_ARR = 13; + + /** */ + public static final byte INT_ARR = 14; + + /** */ + public static final byte LONG_ARR = 15; + + /** */ + public static final byte FLOAT_ARR = 16; + + /** */ + public static final byte DOUBLE_ARR = 17; + + /** */ + public static final byte CHAR_ARR = 18; + + /** */ + public static final byte BOOLEAN_ARR = 19; + + /** */ + public static final byte DECIMAL_ARR = 31; + + /** */ + public static final byte STRING_ARR = 20; + + /** */ + public static final byte UUID_ARR = 21; + + /** */ + public static final byte DATE_ARR = 22; + + /** */ + public static final byte OBJ_ARR = 23; + + /** */ + public static final byte COL = 24; + + /** */ + public static final byte MAP = 25; + + /** */ + public static final byte PORTABLE_OBJ = 27; + + /** */ + public static final byte ENUM = 28; + + /** */ + public static final byte ENUM_ARR = 29; + + /** */ + public static final byte CLASS = 32; + + /** Timestamp. */ + public static final byte TIMESTAMP = 33; + + /** Timestamp array. */ + public static final byte TIMESTAMP_ARR = 34; + + /** */ + public static final byte NULL = (byte)101; + + /** */ + public static final byte HANDLE = (byte)102; + + /** */ + public static final byte OBJ = (byte)103; + + /** */ + public static final byte USER_SET = -1; + + /** */ + public static final byte USER_COL = 0; + + /** */ + public static final byte ARR_LIST = 1; + + /** */ + public static final byte LINKED_LIST = 2; + + /** */ + public static final byte HASH_SET = 3; + + /** */ + public static final byte LINKED_HASH_SET = 4; + + /** */ + public static final byte HASH_MAP = 1; + + /** */ + public static final byte LINKED_HASH_MAP = 2; + + /** */ + public static final int OBJECT_TYPE_ID = -1; + + /** */ + public static final int UNREGISTERED_TYPE_ID = 0; + + /** Protocol version. */ + public static final byte PROTO_VER = 1; + + /** Protocol version position. */ + public static final int PROTO_VER_POS = 1; + + /** Flags position in header. */ + public static final int FLAGS_POS = 2; + + /** */ + public static final int TYPE_ID_POS = 4; + + /** */ + public static final int HASH_CODE_POS = 8; + + /** */ + public static final int TOTAL_LEN_POS = 12; + + /** */ + public static final int SCHEMA_ID_POS = 16; + + /** Schema or raw offset position. */ + public static final int SCHEMA_OR_RAW_OFF_POS = 20; + + /** */ + public static final byte DFLT_HDR_LEN = 24; + + /** */ + private final BinaryContext ctx; + + /** + * @param ctx Context. + */ + public GridBinaryMarshaller(BinaryContext ctx) { + this.ctx = ctx; + } + + /** + * @param obj Object to marshal. + * @return Byte array. + * @throws org.apache.ignite.binary.BinaryObjectException In case of error. + */ + public byte[] marshal(@Nullable Object obj) throws BinaryObjectException { + if (obj == null) + return new byte[] { NULL }; + + try (BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx)) { + writer.marshal(obj); + + return writer.array(); + } + } + + /** + * @param bytes Bytes array. + * @return Portable object. + * @throws org.apache.ignite.binary.BinaryObjectException In case of error. + */ + @SuppressWarnings("unchecked") + @Nullable public <T> T unmarshal(byte[] bytes, @Nullable ClassLoader clsLdr) throws BinaryObjectException { + assert bytes != null; + + return (T)BinaryUtils.unmarshal(BinaryHeapInputStream.create(bytes, 0), ctx, clsLdr); + } + + /** + * @param in Input stream. + * @return Portable object. + * @throws org.apache.ignite.binary.BinaryObjectException In case of error. + */ + @SuppressWarnings("unchecked") + @Nullable public <T> T unmarshal(BinaryInputStream in) throws BinaryObjectException { + return (T)BinaryUtils.unmarshal(in, ctx, null); + } + + /** + * @param arr Byte array. + * @param ldr Class loader. + * @return Deserialized object. + * @throws org.apache.ignite.binary.BinaryObjectException In case of error. + */ + @SuppressWarnings("unchecked") + @Nullable public <T> T deserialize(byte[] arr, @Nullable ClassLoader ldr) throws BinaryObjectException { + assert arr != null; + assert arr.length > 0; + + if (arr[0] == NULL) + return null; + + return (T)new BinaryReaderExImpl(ctx, BinaryHeapInputStream.create(arr, 0), ldr).deserialize(); + } + + /** + * Gets writer for the given output stream. + * + * @param out Output stream. + * @return Writer. + */ + public BinaryWriterExImpl writer(BinaryOutputStream out) { + return new BinaryWriterExImpl(ctx, out, BinaryThreadLocalContext.get().schemaHolder(), null); + } + + /** + * @return Context. + */ + public BinaryContext context() { + return ctx; + } +} http://git-wip-us.apache.org/repos/asf/ignite/blob/71ad9cea/modules/core/src/main/java/org/apache/ignite/internal/binary/GridPortableMarshaller.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/binary/GridPortableMarshaller.java b/modules/core/src/main/java/org/apache/ignite/internal/binary/GridPortableMarshaller.java deleted file mode 100644 index a57bb55..0000000 --- a/modules/core/src/main/java/org/apache/ignite/internal/binary/GridPortableMarshaller.java +++ /dev/null @@ -1,289 +0,0 @@ -/* - * 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.binary; - -import org.apache.ignite.internal.binary.streams.PortableHeapInputStream; -import org.apache.ignite.internal.binary.streams.PortableInputStream; -import org.apache.ignite.internal.binary.streams.PortableOutputStream; -import org.apache.ignite.internal.binary.streams.PortableHeapInputStream; -import org.apache.ignite.internal.binary.streams.PortableInputStream; -import org.apache.ignite.internal.binary.streams.PortableOutputStream; -import org.apache.ignite.binary.BinaryObjectException; -import org.jetbrains.annotations.Nullable; - -/** - * Portable objects marshaller. - */ -public class GridPortableMarshaller { - /** */ - public static final ThreadLocal<Boolean> KEEP_PORTABLES = new ThreadLocal<Boolean>() { - @Override protected Boolean initialValue() { - return true; - } - }; - - /** */ - static final byte OPTM_MARSH = -2; - - /** */ - public static final byte BYTE = 1; - - /** */ - public static final byte SHORT = 2; - - /** */ - public static final byte INT = 3; - - /** */ - public static final byte LONG = 4; - - /** */ - public static final byte FLOAT = 5; - - /** */ - public static final byte DOUBLE = 6; - - /** */ - public static final byte CHAR = 7; - - /** */ - public static final byte BOOLEAN = 8; - - /** */ - public static final byte DECIMAL = 30; - - /** */ - public static final byte STRING = 9; - - /** */ - public static final byte UUID = 10; - - /** */ - public static final byte DATE = 11; - - /** */ - public static final byte BYTE_ARR = 12; - - /** */ - public static final byte SHORT_ARR = 13; - - /** */ - public static final byte INT_ARR = 14; - - /** */ - public static final byte LONG_ARR = 15; - - /** */ - public static final byte FLOAT_ARR = 16; - - /** */ - public static final byte DOUBLE_ARR = 17; - - /** */ - public static final byte CHAR_ARR = 18; - - /** */ - public static final byte BOOLEAN_ARR = 19; - - /** */ - public static final byte DECIMAL_ARR = 31; - - /** */ - public static final byte STRING_ARR = 20; - - /** */ - public static final byte UUID_ARR = 21; - - /** */ - public static final byte DATE_ARR = 22; - - /** */ - public static final byte OBJ_ARR = 23; - - /** */ - public static final byte COL = 24; - - /** */ - public static final byte MAP = 25; - - /** */ - public static final byte PORTABLE_OBJ = 27; - - /** */ - public static final byte ENUM = 28; - - /** */ - public static final byte ENUM_ARR = 29; - - /** */ - public static final byte CLASS = 32; - - /** Timestamp. */ - public static final byte TIMESTAMP = 33; - - /** Timestamp array. */ - public static final byte TIMESTAMP_ARR = 34; - - /** */ - public static final byte NULL = (byte)101; - - /** */ - public static final byte HANDLE = (byte)102; - - /** */ - public static final byte OBJ = (byte)103; - - /** */ - public static final byte USER_SET = -1; - - /** */ - public static final byte USER_COL = 0; - - /** */ - public static final byte ARR_LIST = 1; - - /** */ - public static final byte LINKED_LIST = 2; - - /** */ - public static final byte HASH_SET = 3; - - /** */ - public static final byte LINKED_HASH_SET = 4; - - /** */ - public static final byte HASH_MAP = 1; - - /** */ - public static final byte LINKED_HASH_MAP = 2; - - /** */ - public static final int OBJECT_TYPE_ID = -1; - - /** */ - public static final int UNREGISTERED_TYPE_ID = 0; - - /** Protocol version. */ - public static final byte PROTO_VER = 1; - - /** Protocol version position. */ - public static final int PROTO_VER_POS = 1; - - /** Flags position in header. */ - public static final int FLAGS_POS = 2; - - /** */ - public static final int TYPE_ID_POS = 4; - - /** */ - public static final int HASH_CODE_POS = 8; - - /** */ - public static final int TOTAL_LEN_POS = 12; - - /** */ - public static final int SCHEMA_ID_POS = 16; - - /** Schema or raw offset position. */ - public static final int SCHEMA_OR_RAW_OFF_POS = 20; - - /** */ - public static final byte DFLT_HDR_LEN = 24; - - /** */ - private final PortableContext ctx; - - /** - * @param ctx Context. - */ - public GridPortableMarshaller(PortableContext ctx) { - this.ctx = ctx; - } - - /** - * @param obj Object to marshal. - * @return Byte array. - * @throws org.apache.ignite.binary.BinaryObjectException In case of error. - */ - public byte[] marshal(@Nullable Object obj) throws BinaryObjectException { - if (obj == null) - return new byte[] { NULL }; - - try (BinaryWriterExImpl writer = new BinaryWriterExImpl(ctx)) { - writer.marshal(obj); - - return writer.array(); - } - } - - /** - * @param bytes Bytes array. - * @return Portable object. - * @throws org.apache.ignite.binary.BinaryObjectException In case of error. - */ - @SuppressWarnings("unchecked") - @Nullable public <T> T unmarshal(byte[] bytes, @Nullable ClassLoader clsLdr) throws BinaryObjectException { - assert bytes != null; - - return (T)PortableUtils.unmarshal(PortableHeapInputStream.create(bytes, 0), ctx, clsLdr); - } - - /** - * @param in Input stream. - * @return Portable object. - * @throws org.apache.ignite.binary.BinaryObjectException In case of error. - */ - @SuppressWarnings("unchecked") - @Nullable public <T> T unmarshal(PortableInputStream in) throws BinaryObjectException { - return (T)PortableUtils.unmarshal(in, ctx, null); - } - - /** - * @param arr Byte array. - * @param ldr Class loader. - * @return Deserialized object. - * @throws org.apache.ignite.binary.BinaryObjectException In case of error. - */ - @SuppressWarnings("unchecked") - @Nullable public <T> T deserialize(byte[] arr, @Nullable ClassLoader ldr) throws BinaryObjectException { - assert arr != null; - assert arr.length > 0; - - if (arr[0] == NULL) - return null; - - return (T)new BinaryReaderExImpl(ctx, PortableHeapInputStream.create(arr, 0), ldr).deserialize(); - } - - /** - * Gets writer for the given output stream. - * - * @param out Output stream. - * @return Writer. - */ - public BinaryWriterExImpl writer(PortableOutputStream out) { - return new BinaryWriterExImpl(ctx, out, BinaryThreadLocalContext.get().schemaHolder(), null); - } - - /** - * @return Context. - */ - public PortableContext context() { - return ctx; - } -} http://git-wip-us.apache.org/repos/asf/ignite/blob/71ad9cea/modules/core/src/main/java/org/apache/ignite/internal/binary/PortableClassDescriptor.java ---------------------------------------------------------------------- diff --git a/modules/core/src/main/java/org/apache/ignite/internal/binary/PortableClassDescriptor.java b/modules/core/src/main/java/org/apache/ignite/internal/binary/PortableClassDescriptor.java deleted file mode 100644 index 9b4d444..0000000 --- a/modules/core/src/main/java/org/apache/ignite/internal/binary/PortableClassDescriptor.java +++ /dev/null @@ -1,813 +0,0 @@ -/* - * 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.binary; - -import java.io.Externalizable; -import java.io.IOException; -import java.io.ObjectInputStream; -import java.io.ObjectOutputStream; -import java.lang.reflect.Constructor; -import java.lang.reflect.Field; -import java.lang.reflect.InvocationTargetException; -import java.lang.reflect.Method; -import java.lang.reflect.Modifier; -import java.math.BigDecimal; -import java.sql.Timestamp; -import java.util.ArrayList; -import java.util.Collection; -import java.util.Collections; -import java.util.Date; -import java.util.HashMap; -import java.util.HashSet; -import java.util.Map; -import java.util.UUID; -import org.apache.ignite.IgniteCheckedException; -import org.apache.ignite.binary.BinaryIdMapper; -import org.apache.ignite.binary.BinaryObjectException; -import org.apache.ignite.binary.BinarySerializer; -import org.apache.ignite.binary.Binarylizable; -import org.apache.ignite.internal.processors.cache.CacheObjectImpl; -import org.apache.ignite.internal.util.GridUnsafe; -import org.apache.ignite.internal.util.typedef.internal.U; -import org.apache.ignite.marshaller.MarshallerExclusions; -import org.apache.ignite.marshaller.optimized.OptimizedMarshaller; -import org.jetbrains.annotations.Nullable; -import sun.misc.Unsafe; - -import static java.lang.reflect.Modifier.isStatic; -import static java.lang.reflect.Modifier.isTransient; - -/** - * Portable class descriptor. - */ -public class PortableClassDescriptor { - /** */ - public static final Unsafe UNSAFE = GridUnsafe.unsafe(); - - /** */ - private final PortableContext ctx; - - /** */ - private final Class<?> cls; - - /** */ - private final BinarySerializer serializer; - - /** ID mapper. */ - private final BinaryIdMapper idMapper; - - /** */ - private final BinaryWriteMode mode; - - /** */ - private final boolean userType; - - /** */ - private final int typeId; - - /** */ - private final String typeName; - - /** Affinity key field name. */ - private final String affKeyFieldName; - - /** */ - private final Constructor<?> ctor; - - /** */ - private final BinaryFieldAccessor[] fields; - - /** */ - private final Method writeReplaceMtd; - - /** */ - private final Method readResolveMtd; - - /** */ - private final Map<String, Integer> stableFieldsMeta; - - /** Object schemas. Initialized only for serializable classes and contains only 1 entry. */ - private final PortableSchema stableSchema; - - /** Schema registry. */ - private final PortableSchemaRegistry schemaReg; - - /** */ - private final boolean registered; - - /** */ - private final boolean useOptMarshaller; - - /** */ - private final boolean excluded; - - /** - * @param ctx Context. - * @param cls Class. - * @param userType User type flag. - * @param typeId Type ID. - * @param typeName Type name. - * @param affKeyFieldName Affinity key field name. - * @param idMapper ID mapper. - * @param serializer Serializer. - * @param metaDataEnabled Metadata enabled flag. - * @param registered Whether typeId has been successfully registered by MarshallerContext or not. - * @param predefined Whether the class is predefined or not. - * @throws BinaryObjectException In case of error. - */ - PortableClassDescriptor( - PortableContext ctx, - Class<?> cls, - boolean userType, - int typeId, - String typeName, - @Nullable String affKeyFieldName, - @Nullable BinaryIdMapper idMapper, - @Nullable BinarySerializer serializer, - boolean metaDataEnabled, - boolean registered, - boolean predefined - ) throws BinaryObjectException { - assert ctx != null; - assert cls != null; - assert idMapper != null; - - this.ctx = ctx; - this.cls = cls; - this.typeId = typeId; - this.userType = userType; - this.typeName = typeName; - this.affKeyFieldName = affKeyFieldName; - this.serializer = serializer; - this.idMapper = idMapper; - this.registered = registered; - - schemaReg = ctx.schemaRegistry(typeId); - - excluded = MarshallerExclusions.isExcluded(cls); - - useOptMarshaller = !predefined && initUseOptimizedMarshallerFlag(); - - if (excluded) - mode = BinaryWriteMode.EXCLUSION; - else { - if (cls == BinaryEnumObjectImpl.class) - mode = BinaryWriteMode.PORTABLE_ENUM; - else - mode = serializer != null ? BinaryWriteMode.PORTABLE : PortableUtils.mode(cls); - } - - switch (mode) { - case P_BYTE: - case P_BOOLEAN: - case P_SHORT: - case P_CHAR: - case P_INT: - case P_LONG: - case P_FLOAT: - case P_DOUBLE: - case BYTE: - case SHORT: - case INT: - case LONG: - case FLOAT: - case DOUBLE: - case CHAR: - case BOOLEAN: - case DECIMAL: - case STRING: - case UUID: - case DATE: - case TIMESTAMP: - case BYTE_ARR: - case SHORT_ARR: - case INT_ARR: - case LONG_ARR: - case FLOAT_ARR: - case DOUBLE_ARR: - case CHAR_ARR: - case BOOLEAN_ARR: - case DECIMAL_ARR: - case STRING_ARR: - case UUID_ARR: - case DATE_ARR: - case TIMESTAMP_ARR: - case OBJECT_ARR: - case COL: - case MAP: - case PORTABLE_OBJ: - case ENUM: - case PORTABLE_ENUM: - case ENUM_ARR: - case CLASS: - case EXCLUSION: - ctor = null; - fields = null; - stableFieldsMeta = null; - stableSchema = null; - - break; - - case PORTABLE: - case EXTERNALIZABLE: - ctor = constructor(cls); - fields = null; - stableFieldsMeta = null; - stableSchema = null; - - break; - - case OBJECT: - // Must not use constructor to honor transient fields semantics. - ctor = null; - ArrayList<BinaryFieldAccessor> fields0 = new ArrayList<>(); - stableFieldsMeta = metaDataEnabled ? new HashMap<String, Integer>() : null; - - PortableSchema.Builder schemaBuilder = PortableSchema.Builder.newBuilder(); - - Collection<String> names = new HashSet<>(); - Collection<Integer> ids = new HashSet<>(); - - for (Class<?> c = cls; c != null && !c.equals(Object.class); c = c.getSuperclass()) { - for (Field f : c.getDeclaredFields()) { - int mod = f.getModifiers(); - - if (!isStatic(mod) && !isTransient(mod)) { - f.setAccessible(true); - - String name = f.getName(); - - if (!names.add(name)) - throw new BinaryObjectException("Duplicate field name [fieldName=" + name + - ", cls=" + cls.getName() + ']'); - - int fieldId = idMapper.fieldId(typeId, name); - - if (!ids.add(fieldId)) - throw new BinaryObjectException("Duplicate field ID: " + name); - - BinaryFieldAccessor fieldInfo = BinaryFieldAccessor.create(f, fieldId); - - fields0.add(fieldInfo); - - schemaBuilder.addField(fieldId); - - if (metaDataEnabled) - stableFieldsMeta.put(name, fieldInfo.mode().typeId()); - } - } - } - - fields = fields0.toArray(new BinaryFieldAccessor[fields0.size()]); - - stableSchema = schemaBuilder.build(); - - break; - - default: - // Should never happen. - throw new BinaryObjectException("Invalid mode: " + mode); - } - - if (mode == BinaryWriteMode.PORTABLE || mode == BinaryWriteMode.EXTERNALIZABLE || - mode == BinaryWriteMode.OBJECT) { - readResolveMtd = U.findNonPublicMethod(cls, "readResolve"); - writeReplaceMtd = U.findNonPublicMethod(cls, "writeReplace"); - } - else { - readResolveMtd = null; - writeReplaceMtd = null; - } - } - - /** - * @return {@code True} if enum. - */ - boolean isEnum() { - return mode == BinaryWriteMode.ENUM; - } - - /** - * @return Described class. - */ - Class<?> describedClass() { - return cls; - } - - /** - * @return Type ID. - */ - public int typeId() { - return typeId; - } - - /** - * @return User type flag. - */ - public boolean userType() { - return userType; - } - - /** - * @return Fields meta data. - */ - Map<String, Integer> fieldsMeta() { - return stableFieldsMeta; - } - - /** - * @return Schema. - */ - PortableSchema schema() { - return stableSchema; - } - - /** - * @return Whether typeId has been successfully registered by MarshallerContext or not. - */ - public boolean registered() { - return registered; - } - - /** - * @return {@code true} if {@link OptimizedMarshaller} must be used instead of {@link BinaryMarshaller} - * for object serialization and deserialization. - */ - public boolean useOptimizedMarshaller() { - return useOptMarshaller; - } - - /** - * Checks whether the class values are explicitly excluded from marshalling. - * - * @return {@code true} if excluded, {@code false} otherwise. - */ - public boolean excluded() { - return excluded; - } - - /** - * @return portableWriteReplace() method - */ - @Nullable Method getWriteReplaceMethod() { - return writeReplaceMtd; - } - - /** - * @return portableReadResolve() method - */ - @SuppressWarnings("UnusedDeclaration") - @Nullable Method getReadResolveMethod() { - return readResolveMtd; - } - - /** - * @param obj Object. - * @param writer Writer. - * @throws BinaryObjectException In case of error. - */ - void write(Object obj, BinaryWriterExImpl writer) throws BinaryObjectException { - assert obj != null; - assert writer != null; - - writer.typeId(typeId); - - switch (mode) { - case P_BYTE: - case BYTE: - writer.writeByteFieldPrimitive((byte) obj); - - break; - - case P_SHORT: - case SHORT: - writer.writeShortFieldPrimitive((short)obj); - - break; - - case P_INT: - case INT: - writer.writeIntFieldPrimitive((int) obj); - - break; - - case P_LONG: - case LONG: - writer.writeLongFieldPrimitive((long) obj); - - break; - - case P_FLOAT: - case FLOAT: - writer.writeFloatFieldPrimitive((float) obj); - - break; - - case P_DOUBLE: - case DOUBLE: - writer.writeDoubleFieldPrimitive((double) obj); - - break; - - case P_CHAR: - case CHAR: - writer.writeCharFieldPrimitive((char) obj); - - break; - - case P_BOOLEAN: - case BOOLEAN: - writer.writeBooleanFieldPrimitive((boolean) obj); - - break; - - case DECIMAL: - writer.doWriteDecimal((BigDecimal)obj); - - break; - - case STRING: - writer.doWriteString((String)obj); - - break; - - case UUID: - writer.doWriteUuid((UUID)obj); - - break; - - case DATE: - writer.doWriteDate((Date)obj); - - break; - - case TIMESTAMP: - writer.doWriteTimestamp((Timestamp)obj); - - break; - - case BYTE_ARR: - writer.doWriteByteArray((byte[])obj); - - break; - - case SHORT_ARR: - writer.doWriteShortArray((short[]) obj); - - break; - - case INT_ARR: - writer.doWriteIntArray((int[]) obj); - - break; - - case LONG_ARR: - writer.doWriteLongArray((long[]) obj); - - break; - - case FLOAT_ARR: - writer.doWriteFloatArray((float[]) obj); - - break; - - case DOUBLE_ARR: - writer.doWriteDoubleArray((double[]) obj); - - break; - - case CHAR_ARR: - writer.doWriteCharArray((char[]) obj); - - break; - - case BOOLEAN_ARR: - writer.doWriteBooleanArray((boolean[]) obj); - - break; - - case DECIMAL_ARR: - writer.doWriteDecimalArray((BigDecimal[]) obj); - - break; - - case STRING_ARR: - writer.doWriteStringArray((String[]) obj); - - break; - - case UUID_ARR: - writer.doWriteUuidArray((UUID[]) obj); - - break; - - case DATE_ARR: - writer.doWriteDateArray((Date[]) obj); - - break; - - case TIMESTAMP_ARR: - writer.doWriteTimestampArray((Timestamp[]) obj); - - break; - - case OBJECT_ARR: - writer.doWriteObjectArray((Object[])obj); - - break; - - case COL: - writer.doWriteCollection((Collection<?>)obj); - - break; - - case MAP: - writer.doWriteMap((Map<?, ?>)obj); - - break; - - case ENUM: - writer.doWriteEnum((Enum<?>)obj); - - break; - - case PORTABLE_ENUM: - writer.doWritePortableEnum((BinaryEnumObjectImpl)obj); - - break; - - case ENUM_ARR: - writer.doWriteEnumArray((Object[])obj); - - break; - - case CLASS: - writer.doWriteClass((Class)obj); - - break; - - case PORTABLE_OBJ: - writer.doWritePortableObject((BinaryObjectImpl)obj); - - break; - - case PORTABLE: - if (preWrite(writer, obj)) { - try { - if (serializer != null) - serializer.writeBinary(obj, writer); - else - ((Binarylizable)obj).writeBinary(writer); - - postWrite(writer, obj); - - // Check whether we need to update metadata. - if (obj.getClass() != BinaryMetadata.class) { - int schemaId = writer.schemaId(); - - if (schemaReg.schema(schemaId) == null) { - // This is new schema, let's update metadata. - BinaryMetadataCollector collector = - new BinaryMetadataCollector(typeId, typeName, idMapper); - - if (serializer != null) - serializer.writeBinary(obj, collector); - else - ((Binarylizable)obj).writeBinary(collector); - - PortableSchema newSchema = collector.schema(); - - BinaryMetadata meta = new BinaryMetadata(typeId, typeName, collector.meta(), - affKeyFieldName, Collections.singleton(newSchema), false); - - ctx.updateMetadata(typeId, meta); - - schemaReg.addSchema(newSchema.schemaId(), newSchema); - } - } - } - finally { - writer.popSchema(); - } - } - - break; - - case EXTERNALIZABLE: - if (preWrite(writer, obj)) { - writer.rawWriter(); - - try { - ((Externalizable)obj).writeExternal(writer); - - postWrite(writer, obj); - } - catch (IOException e) { - throw new BinaryObjectException("Failed to write Externalizable object: " + obj, e); - } - finally { - writer.popSchema(); - } - } - - break; - - case OBJECT: - if (preWrite(writer, obj)) { - try { - for (BinaryFieldAccessor info : fields) - info.write(obj, writer); - - writer.schemaId(stableSchema.schemaId()); - - postWrite(writer, obj); - } - finally { - writer.popSchema(); - } - } - - break; - - default: - assert false : "Invalid mode: " + mode; - } - } - - /** - * @param reader Reader. - * @return Object. - * @throws BinaryObjectException If failed. - */ - Object read(BinaryReaderExImpl reader) throws BinaryObjectException { - assert reader != null; - - Object res; - - switch (mode) { - case PORTABLE: - res = newInstance(); - - reader.setHandle(res); - - if (serializer != null) - serializer.readBinary(res, reader); - else - ((Binarylizable)res).readBinary(reader); - - break; - - case EXTERNALIZABLE: - res = newInstance(); - - reader.setHandle(res); - - try { - ((Externalizable)res).readExternal(reader); - } - catch (IOException | ClassNotFoundException e) { - throw new BinaryObjectException("Failed to read Externalizable object: " + - res.getClass().getName(), e); - } - - break; - - case OBJECT: - res = newInstance(); - - reader.setHandle(res); - - for (BinaryFieldAccessor info : fields) - info.read(res, reader); - - break; - - default: - assert false : "Invalid mode: " + mode; - - return null; - } - - if (readResolveMtd != null) { - try { - res = readResolveMtd.invoke(res); - - reader.setHandle(res); - } - catch (IllegalAccessException e) { - throw new RuntimeException(e); - } - catch (InvocationTargetException e) { - if (e.getTargetException() instanceof BinaryObjectException) - throw (BinaryObjectException)e.getTargetException(); - - throw new BinaryObjectException("Failed to execute readResolve() method on " + res, e); - } - } - - return res; - } - - /** - * Pre-write phase. - * - * @param writer Writer. - * @param obj Object. - * @return Whether further write is needed. - */ - private boolean preWrite(BinaryWriterExImpl writer, Object obj) { - if (writer.tryWriteAsHandle(obj)) - return false; - - writer.preWrite(registered ? null : cls.getName()); - - return true; - } - - /** - * Post-write phase. - * - * @param writer Writer. - * @param obj Object. - */ - private void postWrite(BinaryWriterExImpl writer, Object obj) { - writer.postWrite(userType, registered, obj instanceof CacheObjectImpl ? 0 : obj.hashCode()); - } - - /** - * @return Instance. - * @throws BinaryObjectException In case of error. - */ - private Object newInstance() throws BinaryObjectException { - try { - return ctor != null ? ctor.newInstance() : UNSAFE.allocateInstance(cls); - } - catch (InstantiationException | InvocationTargetException | IllegalAccessException e) { - throw new BinaryObjectException("Failed to instantiate instance: " + cls, e); - } - } - - /** - * @param cls Class. - * @return Constructor. - * @throws BinaryObjectException If constructor doesn't exist. - */ - @SuppressWarnings("ConstantConditions") - @Nullable private static Constructor<?> constructor(Class<?> cls) throws BinaryObjectException { - assert cls != null; - - try { - Constructor<?> ctor = U.forceEmptyConstructor(cls); - - if (ctor == null) - throw new BinaryObjectException("Failed to find empty constructor for class: " + cls.getName()); - - ctor.setAccessible(true); - - return ctor; - } - catch (IgniteCheckedException e) { - throw new BinaryObjectException("Failed to get constructor for class: " + cls.getName(), e); - } - } - - /** - * Determines whether to use {@link OptimizedMarshaller} for serialization or - * not. - * - * @return {@code true} if to use, {@code false} otherwise. - */ - @SuppressWarnings("unchecked") - private boolean initUseOptimizedMarshallerFlag() { - for (Class c = cls; c != null && !c.equals(Object.class); c = c.getSuperclass()) { - try { - Method writeObj = c.getDeclaredMethod("writeObject", ObjectOutputStream.class); - Method readObj = c.getDeclaredMethod("readObject", ObjectInputStream.class); - - if (!Modifier.isStatic(writeObj.getModifiers()) && !Modifier.isStatic(readObj.getModifiers()) && - writeObj.getReturnType() == void.class && readObj.getReturnType() == void.class) - return true; - } - catch (NoSuchMethodException ignored) { - // No-op. - } - } - - return false; - } -}