From 9fa7ae4b0a13b106f2f093aa447e8c0d6460ebfa Mon Sep 17 00:00:00 2001 From: NSAmelchev Date: Mon, 30 Nov 2026 01:14:44 +0300 Subject: [PATCH] TC debug0 --- .../internal/binary/BinariesFactory.java | 25 ++ .../ignite/internal/binary/BinaryContext.java | 8 + .../ignite/internal/binary/BinaryUtils.java | 11 + .../internal/binary/BinaryWriterEx.java | 7 + .../internal/binary/GridBinaryMarshaller.java | 27 +- .../internal/binary/BinariesFactoryImpl.java | 15 + .../internal/binary/BinaryDirectReader.java | 178 ++++++++ .../internal/binary/BinaryDirectWriter.java | 420 ++++++++++++++++++ .../internal/binary/BinaryReaderExImpl.java | 314 ++----------- .../internal/binary/BinaryWriterExImpl.java | 387 ++++------------ 10 files changed, 795 insertions(+), 597 deletions(-) create mode 100644 modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/BinaryDirectReader.java create mode 100644 modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/BinaryDirectWriter.java diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinariesFactory.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinariesFactory.java index 10f004ca0654a..95e2d8bfe218d 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinariesFactory.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinariesFactory.java @@ -85,6 +85,31 @@ public BinaryReaderEx reader(BinaryContext ctx, @Nullable public Object unmarshal(BinaryInputStream in, BinaryContext ctx, @Nullable ClassLoader ldr) throws BinaryObjectException; + /** + * @param in Input stream positioned after the type flag. + * @param flag Type flag supported by the direct codec. + * @return Decoded value. + */ + public Object readDirectly(BinaryInputStream in, byte flag); + + /** + * Marshals a value without creating an object writer. + * + * @param obj Non-null object. + * @param flag Type flag supported by the direct codec. + * @return Serialized bytes. + */ + public byte[] marshalDirectly(Object obj, byte flag); + + /** + * Writes a value to the output stream without creating an object writer. + * + * @param obj Non-null object. + * @param flag Type flag supported by the direct codec. + * @param out Output stream. + */ + public void writeDirectly(Object obj, byte flag, BinaryOutputStream out); + /** * @param ctx Context. * @param failIfUnregistered Flag to fail while writing object of unregistered type. diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryContext.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryContext.java index 92e5c84d9c299..1c3e7fccfdfa3 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryContext.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryContext.java @@ -657,6 +657,14 @@ public void registerClassLocally(BinaryType binaryType, boolean failIfUnregister return createDescriptorForClass(cls); } + /** + * @param cls Class. + * @return Cached descriptor, or {@code null} if there is none. + */ + @Nullable BinaryClassDescriptor cachedDescriptor(Class cls) { + return descByCls.get(cls); + } + /** * @param cls Class to create a descriptor for. * @return A descriptor for the given class. The descriptor needs to be registered in order to be used. diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java index 2ba7139524567..52cf6df829807 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryUtils.java @@ -735,6 +735,17 @@ public static boolean isSpecialMap(Class cls) { return HashMap.class.equals(cls) || LinkedHashMap.class.equals(cls); } + /** */ + static boolean canUseDirectCodec(@Nullable BinaryClassDescriptor desc) { + return desc != null && desc.registered() && canUseDirectCodec(desc.mode.typeId()); + } + + /** Supports boxed primitives, strings, UUIDs, and arrays of primitives, strings, or UUIDs. */ + static boolean canUseDirectCodec(int flag) { + return (flag >= GridBinaryMarshaller.BYTE && flag <= GridBinaryMarshaller.UUID) || + (flag >= GridBinaryMarshaller.BYTE_ARR && flag <= GridBinaryMarshaller.UUID_ARR); + } + /** * @return Value. */ diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterEx.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterEx.java index 1ba862a8784d5..b18c061aa265c 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterEx.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/BinaryWriterEx.java @@ -71,6 +71,13 @@ public interface BinaryWriterEx extends BinaryWriter, BinaryRawWriter, ObjectOut */ void marshal(Object obj) throws BinaryObjectException; + /** + * @param obj Object. + * @param desc Class descriptor, or {@code null} to resolve it. + * @throws BinaryObjectException If failed. + */ + void marshal(Object obj, @Nullable BinaryClassDescriptor desc) throws BinaryObjectException; + /** * Writes a sub array of bytes. * @param b the data to be written diff --git a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java index 09fa3e096d150..4d31588e94f25 100644 --- a/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java +++ b/modules/binary/api/src/main/java/org/apache/ignite/internal/binary/GridBinaryMarshaller.java @@ -252,8 +252,13 @@ public byte[] marshal(@Nullable Object obj, boolean failIfUnregistered) throws B if (obj == null) return new byte[] { NULL }; + BinaryClassDescriptor desc = ctx.cachedDescriptor(obj.getClass()); + + if (BinaryUtils.canUseDirectCodec(desc)) + return BinaryUtils.binariesFactory.marshalDirectly(obj, (byte)desc.mode.typeId()); + try (BinaryWriterEx writer = BinaryUtils.binariesFactory.writer(ctx, failIfUnregistered)) { - writer.marshal(obj); + writer.marshal(obj, desc); return writer.out().arrayCopy(); } @@ -301,8 +306,16 @@ public void marshal(Object obj, BinaryOutputStream out) { return; } + BinaryClassDescriptor desc = ctx.cachedDescriptor(obj.getClass()); + + if (BinaryUtils.canUseDirectCodec(desc)) { + BinaryUtils.binariesFactory.writeDirectly(obj, (byte)desc.mode.typeId(), out); + + return; + } + try (BinaryWriterEx writer = BinaryUtils.binariesFactory.writer(ctx, false)) { - writer.marshal(obj); + writer.marshal(obj, desc); BinaryOutputStream s = writer.out(); @@ -351,6 +364,9 @@ public void marshal(Object obj, BinaryOutputStream out) { * @throws org.apache.ignite.binary.BinaryObjectException In case of error. */ @Nullable public T unwrapBinary(BinaryInputStream in, Class clazz) throws BinaryObjectException { + if (in.remaining() > 0 && BinaryUtils.canUseDirectCodec(in.readBytePositioned(in.position()))) + return (T)BinaryUtils.binariesFactory.readDirectly(in, in.readByte()); + BinaryReaderHandles hnds = new BinaryReaderHandles(); Object obj = deserialize(in, hnds); @@ -435,10 +451,15 @@ private Object[] unwrapArray(Object[] arr, BinaryReaderHandles hnds, Class ar if (arr[0] == NULL) return null; + BinaryInputStream in = BinaryStreams.inputStream(arr); + + if (BinaryUtils.canUseDirectCodec(arr[0])) + return (T)BinaryUtils.binariesFactory.readDirectly(in, in.readByte()); + BinaryContext oldCtx = pushContext(ctx); try { - return (T)BinaryUtils.reader(ctx, BinaryStreams.inputStream(arr), ldr, null, true).deserialize(); + return (T)BinaryUtils.reader(ctx, in, ldr, null, true).deserialize(); } finally { popContext(oldCtx); diff --git a/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/BinariesFactoryImpl.java b/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/BinariesFactoryImpl.java index 91435e89c3703..d0f76d12b961f 100644 --- a/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/BinariesFactoryImpl.java +++ b/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/BinariesFactoryImpl.java @@ -70,6 +70,21 @@ public class BinariesFactoryImpl implements BinariesFactory { return BinaryReaderExImpl.unmarshal(in, ctx, ldr); } + /** {@inheritDoc} */ + @Override public Object readDirectly(BinaryInputStream in, byte flag) { + return BinaryDirectReader.read(in, flag); + } + + /** {@inheritDoc} */ + @Override public byte[] marshalDirectly(Object obj, byte flag) { + return BinaryDirectWriter.marshal(obj, flag); + } + + /** {@inheritDoc} */ + @Override public void writeDirectly(Object obj, byte flag, BinaryOutputStream out) { + BinaryDirectWriter.write(obj, flag, out); + } + /** {@inheritDoc} */ @Override public BinaryWriterEx writer(BinaryContext ctx, boolean failIfUnregistered) { return newWriter(ctx, failIfUnregistered, GridBinaryMarshaller.UNREGISTERED_TYPE_ID); diff --git a/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/BinaryDirectReader.java b/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/BinaryDirectReader.java new file mode 100644 index 0000000000000..8be8e7f5adadb --- /dev/null +++ b/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/BinaryDirectReader.java @@ -0,0 +1,178 @@ +/* + * 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.util.UUID; +import org.apache.ignite.binary.BinaryObjectException; +import org.apache.ignite.internal.binary.streams.BinaryInputStream; + +/** Reads built-in values and arrays without object state or handles. */ +final class BinaryDirectReader { + /** */ + private BinaryDirectReader() { + // No-op. + } + + /** + * @param in Input stream positioned after the type flag. + * @param flag Type flag. + * @return Decoded value. + */ + static Object read(BinaryInputStream in, byte flag) { + switch (flag) { + case GridBinaryMarshaller.BYTE: + return in.readByte(); + + case GridBinaryMarshaller.SHORT: + return in.readShort(); + + case GridBinaryMarshaller.INT: + return in.readInt(); + + case GridBinaryMarshaller.LONG: + return in.readLong(); + + case GridBinaryMarshaller.FLOAT: + return in.readFloat(); + + case GridBinaryMarshaller.DOUBLE: + return in.readDouble(); + + case GridBinaryMarshaller.CHAR: + return in.readChar(); + + case GridBinaryMarshaller.BOOLEAN: + return in.readBoolean(); + + case GridBinaryMarshaller.STRING: + return BinaryUtils.doReadString(in); + + case GridBinaryMarshaller.UUID: + return readUuid(in); + + case GridBinaryMarshaller.BYTE_ARR: + return BinaryUtils.doReadByteArray(in); + + case GridBinaryMarshaller.SHORT_ARR: + return readShortArray(in); + + case GridBinaryMarshaller.INT_ARR: + return readIntArray(in); + + case GridBinaryMarshaller.LONG_ARR: + return readLongArray(in); + + case GridBinaryMarshaller.FLOAT_ARR: + return readFloatArray(in); + + case GridBinaryMarshaller.DOUBLE_ARR: + return readDoubleArray(in); + + case GridBinaryMarshaller.CHAR_ARR: + return readCharArray(in); + + case GridBinaryMarshaller.BOOLEAN_ARR: + return readBooleanArray(in); + + case GridBinaryMarshaller.STRING_ARR: + return BinaryUtils.doReadStringArray(in); + + case GridBinaryMarshaller.UUID_ARR: + return readUuidArray(in); + + default: + throw new BinaryObjectException("Unsupported type flag for direct reading: " + flag); + } + } + + /** */ + static UUID readUuid(BinaryInputStream in) { + return new UUID(in.readLong(), in.readLong()); + } + + /** */ + static short[] readShortArray(BinaryInputStream in) { + int len = in.readInt(); + + return in.readShortArray(len); + } + + /** */ + static int[] readIntArray(BinaryInputStream in) { + int len = in.readInt(); + + return in.readIntArray(len); + } + + /** */ + static long[] readLongArray(BinaryInputStream in) { + int len = in.readInt(); + + return in.readLongArray(len); + } + + /** */ + static float[] readFloatArray(BinaryInputStream in) { + int len = in.readInt(); + + return in.readFloatArray(len); + } + + /** */ + static double[] readDoubleArray(BinaryInputStream in) { + int len = in.readInt(); + + return in.readDoubleArray(len); + } + + /** */ + static char[] readCharArray(BinaryInputStream in) { + int len = in.readInt(); + + return in.readCharArray(len); + } + + /** */ + static boolean[] readBooleanArray(BinaryInputStream in) { + int len = in.readInt(); + + return in.readBooleanArray(len); + } + + /** */ + static UUID[] readUuidArray(BinaryInputStream in) throws BinaryObjectException { + int len = in.readInt(); + + UUID[] arr = new UUID[len]; + + for (int i = 0; i < len; i++) { + byte flag = in.readByte(); + + if (flag == GridBinaryMarshaller.NULL) + arr[i] = null; + else { + if (flag != GridBinaryMarshaller.UUID) + throw new BinaryObjectException("Invalid flag value: " + flag); + + arr[i] = readUuid(in); + } + } + + return arr; + } +} diff --git a/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/BinaryDirectWriter.java b/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/BinaryDirectWriter.java new file mode 100644 index 0000000000000..09c22e0336211 --- /dev/null +++ b/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/BinaryDirectWriter.java @@ -0,0 +1,420 @@ +/* + * 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.util.UUID; +import org.apache.ignite.internal.binary.streams.BinaryOutputStream; +import org.apache.ignite.internal.binary.streams.BinaryStreams; +import org.apache.ignite.internal.util.CommonUtils; +import org.jetbrains.annotations.Nullable; + +/** Writes built-in values and arrays without object state or handles. */ +final class BinaryDirectWriter { + /** */ + private BinaryDirectWriter() { + // No-op. + } + + /** + * @param obj Non-null object. + * @param flag Type flag supported by the direct codec. + * @return Serialized bytes. + */ + static byte[] marshal(Object obj, byte flag) { + byte[] bytes; + + switch (flag) { + case GridBinaryMarshaller.BYTE: + bytes = new byte[1 + Byte.BYTES]; + BinaryPrimitives.writeByte(bytes, 1, (Byte)obj); + + break; + + case GridBinaryMarshaller.SHORT: + bytes = new byte[1 + Short.BYTES]; + BinaryPrimitives.writeShort(bytes, 1, (Short)obj); + + break; + + case GridBinaryMarshaller.INT: + bytes = new byte[1 + Integer.BYTES]; + BinaryPrimitives.writeInt(bytes, 1, (Integer)obj); + + break; + + case GridBinaryMarshaller.LONG: + bytes = new byte[1 + Long.BYTES]; + BinaryPrimitives.writeLong(bytes, 1, (Long)obj); + + break; + + case GridBinaryMarshaller.FLOAT: + bytes = new byte[1 + Float.BYTES]; + BinaryPrimitives.writeFloat(bytes, 1, (Float)obj); + + break; + + case GridBinaryMarshaller.DOUBLE: + bytes = new byte[1 + Double.BYTES]; + BinaryPrimitives.writeDouble(bytes, 1, (Double)obj); + + break; + + case GridBinaryMarshaller.CHAR: + bytes = new byte[1 + Character.BYTES]; + BinaryPrimitives.writeChar(bytes, 1, (Character)obj); + + break; + + case GridBinaryMarshaller.BOOLEAN: + bytes = new byte[1 + Byte.BYTES]; + BinaryPrimitives.writeBoolean(bytes, 1, (Boolean)obj); + + break; + + case GridBinaryMarshaller.UUID: + bytes = new byte[1 + 2 * Long.BYTES]; + UUID uuid = (UUID)obj; + + BinaryPrimitives.writeLong(bytes, 1, uuid.getMostSignificantBits()); + BinaryPrimitives.writeLong(bytes, 1 + Long.BYTES, uuid.getLeastSignificantBits()); + + break; + + default: + try (BinaryOutputStream out = BinaryStreams.outputStream((int)CommonUtils.KB)) { + write(obj, flag, out); + + return out.arrayCopy(); + } + } + + bytes[0] = flag; + + return bytes; + } + + /** + * @param obj Non-null object. + * @param flag Type flag supported by the direct codec. + * @param out Output stream. + */ + static void write(Object obj, byte flag, BinaryOutputStream out) { + switch (flag) { + case GridBinaryMarshaller.BYTE: + writeByte((Byte)obj, out); + + return; + + case GridBinaryMarshaller.SHORT: + writeShort((Short)obj, out); + + return; + + case GridBinaryMarshaller.INT: + writeInt((Integer)obj, out); + + return; + + case GridBinaryMarshaller.LONG: + writeLong((Long)obj, out); + + return; + + case GridBinaryMarshaller.FLOAT: + writeFloat((Float)obj, out); + + return; + + case GridBinaryMarshaller.DOUBLE: + writeDouble((Double)obj, out); + + return; + + case GridBinaryMarshaller.CHAR: + writeChar((Character)obj, out); + + return; + + case GridBinaryMarshaller.BOOLEAN: + writeBoolean((Boolean)obj, out); + + return; + + case GridBinaryMarshaller.UUID: + writeUuid((UUID)obj, out); + + return; + + case GridBinaryMarshaller.STRING: + writeString((String)obj, out); + + return; + + case GridBinaryMarshaller.BYTE_ARR: + writeByteArray((byte[])obj, out); + + return; + + case GridBinaryMarshaller.SHORT_ARR: + writeShortArray((short[])obj, out); + + return; + + case GridBinaryMarshaller.INT_ARR: + writeIntArray((int[])obj, out); + + return; + + case GridBinaryMarshaller.LONG_ARR: + writeLongArray((long[])obj, out); + + return; + + case GridBinaryMarshaller.FLOAT_ARR: + writeFloatArray((float[])obj, out); + + return; + + case GridBinaryMarshaller.DOUBLE_ARR: + writeDoubleArray((double[])obj, out); + + return; + + case GridBinaryMarshaller.CHAR_ARR: + writeCharArray((char[])obj, out); + + return; + + case GridBinaryMarshaller.BOOLEAN_ARR: + writeBooleanArray((boolean[])obj, out); + + return; + + case GridBinaryMarshaller.STRING_ARR: + writeStringArray((String[])obj, out); + + return; + + case GridBinaryMarshaller.UUID_ARR: + writeUuidArray((UUID[])obj, out); + + return; + + default: + throw new IllegalArgumentException("Unsupported type flag for direct writing: " + flag); + } + } + + /** */ + static void writeByte(byte val, BinaryOutputStream out) { + writeHeader(GridBinaryMarshaller.BYTE, 1, out); + out.unsafeWriteByte(val); + } + + /** */ + static void writeShort(short val, BinaryOutputStream out) { + writeHeader(GridBinaryMarshaller.SHORT, 2, out); + out.unsafeWriteShort(val); + } + + /** */ + static void writeInt(int val, BinaryOutputStream out) { + writeHeader(GridBinaryMarshaller.INT, 4, out); + out.unsafeWriteInt(val); + } + + /** */ + static void writeLong(long val, BinaryOutputStream out) { + writeHeader(GridBinaryMarshaller.LONG, Long.BYTES, out); + out.unsafeWriteLong(val); + } + + /** */ + static void writeFloat(float val, BinaryOutputStream out) { + writeHeader(GridBinaryMarshaller.FLOAT, 4, out); + out.unsafeWriteFloat(val); + } + + /** */ + static void writeDouble(double val, BinaryOutputStream out) { + writeHeader(GridBinaryMarshaller.DOUBLE, 8, out); + out.unsafeWriteDouble(val); + } + + /** */ + static void writeChar(char val, BinaryOutputStream out) { + writeHeader(GridBinaryMarshaller.CHAR, 2, out); + out.unsafeWriteChar(val); + } + + /** */ + static void writeBoolean(boolean val, BinaryOutputStream out) { + writeHeader(GridBinaryMarshaller.BOOLEAN, 1, out); + out.unsafeWriteBoolean(val); + } + + /** */ + static void writeUuid(@Nullable UUID val, BinaryOutputStream out) { + if (val == null) + out.writeByte(GridBinaryMarshaller.NULL); + else { + writeHeader(GridBinaryMarshaller.UUID, 2 * Long.BYTES, out); + out.unsafeWriteLong(val.getMostSignificantBits()); + out.unsafeWriteLong(val.getLeastSignificantBits()); + } + } + + /** */ + private static void writeHeader(byte flag, int size, BinaryOutputStream out) { + out.unsafeEnsure(size + 1); + out.unsafeWriteByte(flag); + } + + /** */ + static void writeString(@Nullable String val, BinaryOutputStream out) { + if (val == null) + out.writeByte(GridBinaryMarshaller.NULL); + else if (BinaryWriterExImpl.ZERO_COPY && !BinaryUtils.USE_STR_SERIALIZATION_VER_2) + StringWriter.write(val, out); + else + StringWriter.writeStringLegacy(val, out); + } + + /** */ + static void writeByteArray(@Nullable byte[] val, BinaryOutputStream out) { + if (val == null) + out.writeByte(GridBinaryMarshaller.NULL); + else { + writeHeader(GridBinaryMarshaller.BYTE_ARR, Integer.BYTES, out); + out.unsafeWriteInt(val.length); + + out.writeByteArray(val); + } + } + + /** */ + static void writeShortArray(@Nullable short[] val, BinaryOutputStream out) { + if (val == null) + out.writeByte(GridBinaryMarshaller.NULL); + else { + writeHeader(GridBinaryMarshaller.SHORT_ARR, Integer.BYTES, out); + out.unsafeWriteInt(val.length); + + out.writeShortArray(val); + } + } + + /** */ + static void writeIntArray(@Nullable int[] val, BinaryOutputStream out) { + if (val == null) + out.writeByte(GridBinaryMarshaller.NULL); + else { + writeHeader(GridBinaryMarshaller.INT_ARR, Integer.BYTES, out); + out.unsafeWriteInt(val.length); + + out.writeIntArray(val); + } + } + + /** */ + static void writeLongArray(@Nullable long[] val, BinaryOutputStream out) { + if (val == null) + out.writeByte(GridBinaryMarshaller.NULL); + else { + writeHeader(GridBinaryMarshaller.LONG_ARR, Integer.BYTES, out); + out.unsafeWriteInt(val.length); + + out.writeLongArray(val); + } + } + + /** */ + static void writeFloatArray(@Nullable float[] val, BinaryOutputStream out) { + if (val == null) + out.writeByte(GridBinaryMarshaller.NULL); + else { + writeHeader(GridBinaryMarshaller.FLOAT_ARR, Integer.BYTES, out); + out.unsafeWriteInt(val.length); + + out.writeFloatArray(val); + } + } + + /** */ + static void writeDoubleArray(@Nullable double[] val, BinaryOutputStream out) { + if (val == null) + out.writeByte(GridBinaryMarshaller.NULL); + else { + writeHeader(GridBinaryMarshaller.DOUBLE_ARR, Integer.BYTES, out); + out.unsafeWriteInt(val.length); + + out.writeDoubleArray(val); + } + } + + /** */ + static void writeCharArray(@Nullable char[] val, BinaryOutputStream out) { + if (val == null) + out.writeByte(GridBinaryMarshaller.NULL); + else { + writeHeader(GridBinaryMarshaller.CHAR_ARR, Integer.BYTES, out); + out.unsafeWriteInt(val.length); + + out.writeCharArray(val); + } + } + + /** */ + static void writeBooleanArray(@Nullable boolean[] val, BinaryOutputStream out) { + if (val == null) + out.writeByte(GridBinaryMarshaller.NULL); + else { + writeHeader(GridBinaryMarshaller.BOOLEAN_ARR, Integer.BYTES, out); + out.unsafeWriteInt(val.length); + + out.writeBooleanArray(val); + } + } + + /** */ + static void writeStringArray(@Nullable String[] val, BinaryOutputStream out) { + if (val == null) + out.writeByte(GridBinaryMarshaller.NULL); + else { + writeHeader(GridBinaryMarshaller.STRING_ARR, Integer.BYTES, out); + out.unsafeWriteInt(val.length); + + for (String str : val) + writeString(str, out); + } + } + + /** */ + static void writeUuidArray(@Nullable UUID[] val, BinaryOutputStream out) { + if (val == null) + out.writeByte(GridBinaryMarshaller.NULL); + else { + writeHeader(GridBinaryMarshaller.UUID_ARR, Integer.BYTES, out); + out.unsafeWriteInt(val.length); + + for (UUID uuid : val) + writeUuid(uuid, out); + } + } +} diff --git a/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/BinaryReaderExImpl.java b/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/BinaryReaderExImpl.java index 8e4c96ee78b83..e46c435b9e187 100644 --- a/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/BinaryReaderExImpl.java +++ b/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/BinaryReaderExImpl.java @@ -551,7 +551,7 @@ boolean readBoolean(int fieldId) throws BinaryObjectException { @Nullable @Override public boolean[] readBooleanArray() throws BinaryObjectException { switch (checkFlag(BOOLEAN_ARR)) { case NORMAL: - return doReadBooleanArray(in); + return BinaryDirectReader.readBooleanArray(in); case HANDLE: return readHandleField(); @@ -617,7 +617,7 @@ short readShort(int fieldId) throws BinaryObjectException { @Nullable @Override public short[] readShortArray() throws BinaryObjectException { switch (checkFlag(SHORT_ARR)) { case NORMAL: - return doReadShortArray(in); + return BinaryDirectReader.readShortArray(in); case HANDLE: return readHandleField(); @@ -683,7 +683,7 @@ char readChar(int fieldId) throws BinaryObjectException { @Nullable @Override public char[] readCharArray() throws BinaryObjectException { switch (checkFlag(CHAR_ARR)) { case NORMAL: - return doReadCharArray(in); + return BinaryDirectReader.readCharArray(in); case HANDLE: return readHandleField(); @@ -749,7 +749,7 @@ int readInt(int fieldId) throws BinaryObjectException { @Nullable @Override public int[] readIntArray() throws BinaryObjectException { switch (checkFlag(INT_ARR)) { case NORMAL: - return doReadIntArray(in); + return BinaryDirectReader.readIntArray(in); case HANDLE: return readHandleField(); @@ -815,7 +815,7 @@ long readLong(int fieldId) throws BinaryObjectException { @Nullable @Override public long[] readLongArray() throws BinaryObjectException { switch (checkFlag(LONG_ARR)) { case NORMAL: - return doReadLongArray(in); + return BinaryDirectReader.readLongArray(in); case HANDLE: return readHandleField(); @@ -881,7 +881,7 @@ float readFloat(int fieldId) throws BinaryObjectException { @Nullable @Override public float[] readFloatArray() throws BinaryObjectException { switch (checkFlag(FLOAT_ARR)) { case NORMAL: - return doReadFloatArray(in); + return BinaryDirectReader.readFloatArray(in); case HANDLE: return readHandleField(); @@ -947,7 +947,7 @@ float readFloat(int fieldId) throws BinaryObjectException { @Nullable @Override public double[] readDoubleArray() throws BinaryObjectException { switch (checkFlag(DOUBLE_ARR)) { case NORMAL: - return doReadDoubleArray(in); + return BinaryDirectReader.readDoubleArray(in); case HANDLE: return readHandleField(); @@ -1092,7 +1092,7 @@ float readFloat(int fieldId) throws BinaryObjectException { /** {@inheritDoc} */ @Override @Nullable public UUID readUuid() throws BinaryObjectException { - return checkFlagNoHandles(UUID) == Flag.NORMAL ? doReadUuid(in) : null; + return checkFlagNoHandles(UUID) == Flag.NORMAL ? BinaryDirectReader.readUuid(in) : null; } /** {@inheritDoc} */ @@ -1118,7 +1118,7 @@ float readFloat(int fieldId) throws BinaryObjectException { @Override @Nullable public UUID[] readUuidArray() throws BinaryObjectException { switch (checkFlag(UUID_ARR)) { case NORMAL: - return doReadUuidArray(in); + return BinaryDirectReader.readUuidArray(in); case HANDLE: return readHandleField(); @@ -1730,6 +1730,9 @@ private String fieldFlagName(byte flag) { byte flag = in.readByte(); + if (BinaryUtils.canUseDirectCodec(flag)) + return BinaryDirectReader.read(in, flag); + switch (flag) { case NULL: obj = null; @@ -1768,61 +1771,11 @@ private String fieldFlagName(byte flag) { break; - case BYTE: - obj = in.readByte(); - - break; - - case SHORT: - obj = in.readShort(); - - break; - - case INT: - obj = in.readInt(); - - break; - - case LONG: - obj = in.readLong(); - - break; - - case FLOAT: - obj = in.readFloat(); - - break; - - case DOUBLE: - obj = in.readDouble(); - - break; - - case CHAR: - obj = in.readChar(); - - break; - - case BOOLEAN: - obj = in.readBoolean(); - - break; - case DECIMAL: obj = doReadDecimal(in); break; - case STRING: - obj = BinaryUtils.doReadString(in); - - break; - - case UUID: - obj = doReadUuid(in); - - break; - case DATE: obj = doReadDate(in); @@ -1838,61 +1791,11 @@ private String fieldFlagName(byte flag) { break; - case BYTE_ARR: - obj = BinaryUtils.doReadByteArray(in); - - break; - - case SHORT_ARR: - obj = doReadShortArray(in); - - break; - - case INT_ARR: - obj = doReadIntArray(in); - - break; - - case LONG_ARR: - obj = doReadLongArray(in); - - break; - - case FLOAT_ARR: - obj = doReadFloatArray(in); - - break; - - case DOUBLE_ARR: - obj = doReadDoubleArray(in); - - break; - - case CHAR_ARR: - obj = doReadCharArray(in); - - break; - - case BOOLEAN_ARR: - obj = doReadBooleanArray(in); - - break; - case DECIMAL_ARR: obj = doReadDecimalArray(in); break; - case STRING_ARR: - obj = BinaryUtils.doReadStringArray(in); - - break; - - case UUID_ARR: - obj = doReadUuidArray(in); - - break; - case DATE_ARR: obj = doReadDateArray(in); @@ -1967,18 +1870,6 @@ private String fieldFlagName(byte flag) { return obj; } - /** - * @param fieldId Field ID. - * @return Deserialized object. - * @throws BinaryObjectException If failed. - */ - @Nullable Object readField(int fieldId) throws BinaryObjectException { - if (!findFieldById(fieldId)) - return null; - - return new BinaryReaderExImpl(ctx, in, ldr, hnds, false, true).deserialize(); - } - /** * @param name Field name. * @return Field offset. @@ -2081,7 +1972,7 @@ public BinarySchema getOrCreateSchema() { return doReadDecimal(in); case GridBinaryMarshaller.UUID: - return doReadUuid(in); + return BinaryDirectReader.readUuid(in); case GridBinaryMarshaller.TIME: return doReadTime(in); @@ -2093,28 +1984,28 @@ public BinarySchema getOrCreateSchema() { return doReadDate(in); case GridBinaryMarshaller.BOOLEAN_ARR: - return doReadBooleanArray(in); + return BinaryDirectReader.readBooleanArray(in); case GridBinaryMarshaller.BYTE_ARR: return BinaryUtils.doReadByteArray(in); case GridBinaryMarshaller.CHAR_ARR: - return doReadCharArray(in); + return BinaryDirectReader.readCharArray(in); case GridBinaryMarshaller.SHORT_ARR: - return doReadShortArray(in); + return BinaryDirectReader.readShortArray(in); case GridBinaryMarshaller.INT_ARR: - return doReadIntArray(in); + return BinaryDirectReader.readIntArray(in); case GridBinaryMarshaller.LONG_ARR: - return doReadLongArray(in); + return BinaryDirectReader.readLongArray(in); case GridBinaryMarshaller.FLOAT_ARR: - return doReadFloatArray(in); + return BinaryDirectReader.readFloatArray(in); case GridBinaryMarshaller.DOUBLE_ARR: - return doReadDoubleArray(in); + return BinaryDirectReader.readDoubleArray(in); case GridBinaryMarshaller.STRING_ARR: return BinaryUtils.doReadStringArray(in); @@ -2123,7 +2014,7 @@ public BinarySchema getOrCreateSchema() { return doReadDecimalArray(in); case GridBinaryMarshaller.UUID_ARR: - return doReadUuidArray(in); + return BinaryDirectReader.readUuidArray(in); case GridBinaryMarshaller.TIME_ARR: return doReadTimeArray(in); @@ -2629,7 +2520,7 @@ private void readField(Object obj, BinaryFieldDescriptor fld) { case BINARY: default: - Object val = fld.dynamic ? readField(fld.id) : readFixedType(fld); + Object val = fld.dynamic ? readObject(fld.id) : readFixedType(fld); try { if (val != null || !fld.field.getType().isPrimitive()) @@ -2955,69 +2846,6 @@ private enum Flag { NULL } - /** - * @return Value. - */ - private static boolean[] doReadBooleanArray(BinaryInputStream in) { - int len = in.readInt(); - - return in.readBooleanArray(len); - } - - /** - * @return Value. - */ - private static short[] doReadShortArray(BinaryInputStream in) { - int len = in.readInt(); - - return in.readShortArray(len); - } - - /** - * @return Value. - */ - private static char[] doReadCharArray(BinaryInputStream in) { - int len = in.readInt(); - - return in.readCharArray(len); - } - - /** - * @return Value. - */ - private static int[] doReadIntArray(BinaryInputStream in) { - int len = in.readInt(); - - return in.readIntArray(len); - } - - /** - * @return Value. - */ - private static long[] doReadLongArray(BinaryInputStream in) { - int len = in.readInt(); - - return in.readLongArray(len); - } - - /** - * @return Value. - */ - private static float[] doReadFloatArray(BinaryInputStream in) { - int len = in.readInt(); - - return in.readFloatArray(len); - } - - /** - * @return Value. - */ - private static double[] doReadDoubleArray(BinaryInputStream in) { - int len = in.readInt(); - - return in.readDoubleArray(len); - } - /** * @return Value. */ @@ -3038,13 +2866,6 @@ private static BigDecimal doReadDecimal(BinaryInputStream in) { return new BigDecimal(intVal, scale); } - /** - * @return Value. - */ - private static UUID doReadUuid(BinaryInputStream in) { - return new UUID(in.readLong(), in.readLong()); - } - /** * @return Value. */ @@ -3102,31 +2923,6 @@ private static BigDecimal[] doReadDecimalArray(BinaryInputStream in) throws Bina return arr; } - /** - * @return Value. - * @throws BinaryObjectException In case of error. - */ - private static UUID[] doReadUuidArray(BinaryInputStream in) throws BinaryObjectException { - int len = in.readInt(); - - UUID[] arr = new UUID[len]; - - for (int i = 0; i < len; i++) { - byte flag = in.readByte(); - - if (flag == GridBinaryMarshaller.NULL) - arr[i] = null; - else { - if (flag != GridBinaryMarshaller.UUID) - throw new BinaryObjectException("Invalid flag value: " + flag); - - arr[i] = doReadUuid(in); - } - } - - return arr; - } - /** * @return Value. * @throws BinaryObjectException In case of error. @@ -3374,6 +3170,9 @@ private static Object[] doReadBinaryEnumArray(BinaryInputStream in, BinaryContex */ @Nullable private static Object doReadObject(BinaryInputStream in, BinaryContext ctx, ClassLoader ldr, BinaryReaderHandlesHolder handles) throws BinaryObjectException { + if (in.remaining() > 0 && BinaryUtils.canUseDirectCodec(in.readBytePositioned(in.position()))) + return BinaryDirectReader.read(in, in.readByte()); + return new BinaryReaderExImpl(ctx, in, ldr, handles.handles(), false, true).deserialize(); } @@ -3405,6 +3204,9 @@ private static Object[] doReadBinaryEnumArray(BinaryInputStream in, BinaryContex byte flag = in.readByte(); + if (BinaryUtils.canUseDirectCodec(flag)) + return BinaryDirectReader.read(in, flag); + switch (flag) { case GridBinaryMarshaller.NULL: return null; @@ -3456,39 +3258,9 @@ private static Object[] doReadBinaryEnumArray(BinaryInputStream in, BinaryContex return po; } - case GridBinaryMarshaller.BYTE: - return in.readByte(); - - case GridBinaryMarshaller.SHORT: - return in.readShort(); - - case GridBinaryMarshaller.INT: - return in.readInt(); - - case GridBinaryMarshaller.LONG: - return in.readLong(); - - case GridBinaryMarshaller.FLOAT: - return in.readFloat(); - - case GridBinaryMarshaller.DOUBLE: - return in.readDouble(); - - case GridBinaryMarshaller.CHAR: - return in.readChar(); - - case GridBinaryMarshaller.BOOLEAN: - return in.readBoolean(); - case GridBinaryMarshaller.DECIMAL: return doReadDecimal(in); - case GridBinaryMarshaller.STRING: - return BinaryUtils.doReadString(in); - - case GridBinaryMarshaller.UUID: - return doReadUuid(in); - case GridBinaryMarshaller.DATE: return doReadDate(in); @@ -3498,39 +3270,9 @@ private static Object[] doReadBinaryEnumArray(BinaryInputStream in, BinaryContex case GridBinaryMarshaller.TIME: return doReadTime(in); - case GridBinaryMarshaller.BYTE_ARR: - return BinaryUtils.doReadByteArray(in); - - case GridBinaryMarshaller.SHORT_ARR: - return doReadShortArray(in); - - case GridBinaryMarshaller.INT_ARR: - return doReadIntArray(in); - - case GridBinaryMarshaller.LONG_ARR: - return doReadLongArray(in); - - case GridBinaryMarshaller.FLOAT_ARR: - return doReadFloatArray(in); - - case GridBinaryMarshaller.DOUBLE_ARR: - return doReadDoubleArray(in); - - case GridBinaryMarshaller.CHAR_ARR: - return doReadCharArray(in); - - case GridBinaryMarshaller.BOOLEAN_ARR: - return doReadBooleanArray(in); - case GridBinaryMarshaller.DECIMAL_ARR: return doReadDecimalArray(in); - case GridBinaryMarshaller.STRING_ARR: - return BinaryUtils.doReadStringArray(in); - - case GridBinaryMarshaller.UUID_ARR: - return doReadUuidArray(in); - case GridBinaryMarshaller.DATE_ARR: return doReadDateArray(in); diff --git a/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java b/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java index c11f2fc7ef9ce..c93dd90cfa83d 100644 --- a/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java +++ b/modules/binary/impl/src/main/java/org/apache/ignite/internal/binary/BinaryWriterExImpl.java @@ -135,20 +135,26 @@ public class BinaryWriterExImpl implements BinaryWriterEx { /** {@inheritDoc} */ @Override public void marshal(Object obj) throws BinaryObjectException { - marshal(obj, true); + marshal(obj, null, true); + } + + /** {@inheritDoc} */ + @Override public void marshal(Object obj, @Nullable BinaryClassDescriptor desc) throws BinaryObjectException { + marshal(obj, desc, true); } /** * @param obj Object. + * @param desc Class descriptor, or {@code null} to resolve it. * @param enableReplace Object replacing enabled flag. * @throws org.apache.ignite.binary.BinaryObjectException In case of error. */ - private void marshal(Object obj, boolean enableReplace) throws BinaryObjectException { + private void marshal(Object obj, @Nullable BinaryClassDescriptor desc, boolean enableReplace) throws BinaryObjectException { String newName = ctx.igniteInstanceName(); String oldName = CommonUtils.setCurrentIgniteName(newName); try { - marshal0(obj, enableReplace); + marshal0(obj, desc, enableReplace); } finally { CommonUtils.restoreOldIgniteName(oldName, newName); @@ -157,15 +163,17 @@ private void marshal(Object obj, boolean enableReplace) throws BinaryObjectExcep /** * @param obj Object. + * @param desc Class descriptor, or {@code null} to resolve it. * @param enableReplace Object replacing enabled flag. * @throws org.apache.ignite.binary.BinaryObjectException In case of error. */ - private void marshal0(Object obj, boolean enableReplace) throws BinaryObjectException { + private void marshal0(Object obj, @Nullable BinaryClassDescriptor desc, boolean enableReplace) throws BinaryObjectException { assert obj != null; Class cls = obj.getClass(); - BinaryClassDescriptor desc = ctx.descriptorForClass(cls); + if (desc == null) + desc = ctx.descriptorForClass(cls); if (!desc.registered()) { if (failIfUnregistered) @@ -210,7 +218,7 @@ private void marshal0(Object obj, boolean enableReplace) throws BinaryObjectExce return; } - marshal(replacedObj, false); + marshal(replacedObj, null, false); return; } @@ -447,10 +455,7 @@ private void writeProxy(Proxy proxy, Class[] intfs) { /** @param val Value. */ private void writeByteFieldPrimitive(byte val) { - out.unsafeEnsure(1 + 1); - - out.unsafeWriteByte(GridBinaryMarshaller.BYTE); - out.unsafeWriteByte(val); + BinaryDirectWriter.writeByte(val, out); } /** @@ -486,10 +491,7 @@ private void writeClass(@Nullable Class val) { /** @param val Value. */ private void writeShortFieldPrimitive(short val) { - out.unsafeEnsure(1 + 2); - - out.unsafeWriteByte(GridBinaryMarshaller.SHORT); - out.unsafeWriteShort(val); + BinaryDirectWriter.writeShort(val, out); } /** @@ -504,10 +506,7 @@ void writeShortField(@Nullable Short val) { /** @param val Value. */ private void writeIntFieldPrimitive(int val) { - out.unsafeEnsure(1 + 4); - - out.unsafeWriteByte(GridBinaryMarshaller.INT); - out.unsafeWriteInt(val); + BinaryDirectWriter.writeInt(val, out); } /** @@ -522,10 +521,7 @@ void writeIntField(@Nullable Integer val) { /** @param val Value. */ private void writeLongFieldPrimitive(long val) { - out.unsafeEnsure(1 + 8); - - out.unsafeWriteByte(GridBinaryMarshaller.LONG); - out.unsafeWriteLong(val); + BinaryDirectWriter.writeLong(val, out); } /** @@ -540,10 +536,7 @@ void writeLongField(@Nullable Long val) { /** @param val Value. */ private void writeFloatFieldPrimitive(float val) { - out.unsafeEnsure(1 + 4); - - out.unsafeWriteByte(GridBinaryMarshaller.FLOAT); - out.unsafeWriteFloat(val); + BinaryDirectWriter.writeFloat(val, out); } /** @@ -558,10 +551,7 @@ void writeFloatField(@Nullable Float val) { /** @param val Value. */ private void writeDoubleFieldPrimitive(double val) { - out.unsafeEnsure(1 + 8); - - out.unsafeWriteByte(GridBinaryMarshaller.DOUBLE); - out.unsafeWriteDouble(val); + BinaryDirectWriter.writeDouble(val, out); } /** @@ -576,10 +566,7 @@ void writeDoubleField(@Nullable Double val) { /** @param val Value. */ private void writeCharFieldPrimitive(char val) { - out.unsafeEnsure(1 + 2); - - out.unsafeWriteByte(GridBinaryMarshaller.CHAR); - out.unsafeWriteChar(val); + BinaryDirectWriter.writeChar(val, out); } /** @@ -594,10 +581,7 @@ void writeCharField(@Nullable Character val) { /** @param val Value. */ private void writeBooleanFieldPrimitive(boolean val) { - out.unsafeEnsure(1 + 1); - - out.unsafeWriteByte(GridBinaryMarshaller.BOOLEAN); - out.unsafeWriteBoolean(val); + BinaryDirectWriter.writeBoolean(val, out); } /** @@ -759,12 +743,7 @@ public void writeBinaryObject(@Nullable BinaryObjectEx po) throws BinaryObjectEx /** {@inheritDoc} */ @Override public void writeString(@Nullable String val) throws BinaryObjectException { - if (val == null) - out.writeByte(GridBinaryMarshaller.NULL); - else if (ZERO_COPY && !BinaryUtils.USE_STR_SERIALIZATION_VER_2) - StringWriter.write(val, out); - else - StringWriter.writeStringLegacy(val, out); + BinaryDirectWriter.writeString(val, out); } /** {@inheritDoc} */ @@ -775,14 +754,7 @@ else if (ZERO_COPY && !BinaryUtils.USE_STR_SERIALIZATION_VER_2) /** {@inheritDoc} */ @Override public void writeUuid(@Nullable UUID val) throws BinaryObjectException { - if (val == null) - out.writeByte(GridBinaryMarshaller.NULL); - else { - out.unsafeEnsure(1 + 8 + 8); - out.unsafeWriteByte(GridBinaryMarshaller.UUID); - out.unsafeWriteLong(val.getMostSignificantBits()); - out.unsafeWriteLong(val.getLeastSignificantBits()); - } + BinaryDirectWriter.writeUuid(val, out); } /** {@inheritDoc} */ @@ -845,61 +817,53 @@ else if (ZERO_COPY && !BinaryUtils.USE_STR_SERIALIZATION_VER_2) /** {@inheritDoc} */ @Override public void writeObject(@Nullable Object obj) throws BinaryObjectException { - if (obj == null) + if (obj == null) { out.writeByte(GridBinaryMarshaller.NULL); - else { - // Store state. - int typeId0 = this.typeId; - int start0 = this.start; - int rawOffPos0 = this.rawOffPos; - int schemaId0 = this.schemaId; - int fieldCnt0 = this.fieldCnt; - BinaryInternalMapper mapper0 = this.mapper; - - // Handles not cleared, because, in this mode they shared down to hierarchy. - clearState(false); - - // Maybe recursive `writeObject` invocation which will change state. - marshal(obj); - - // Restore state. - this.typeId = typeId0; - this.start = start0; - this.rawOffPos = rawOffPos0; - this.schemaId = schemaId0; - this.fieldCnt = fieldCnt0; - this.mapper = mapper0; + + return; } + + BinaryClassDescriptor desc = ctx.cachedDescriptor(obj.getClass()); + + if (BinaryUtils.canUseDirectCodec(desc)) { + BinaryDirectWriter.write(obj, (byte)desc.mode.typeId(), out); + + return; + } + + // Store state. + int typeId0 = this.typeId; + int start0 = this.start; + int rawOffPos0 = this.rawOffPos; + int schemaId0 = this.schemaId; + int fieldCnt0 = this.fieldCnt; + BinaryInternalMapper mapper0 = this.mapper; + + clearState(); + + // Maybe recursive `writeObject` invocation which will change state. + marshal(obj, desc); + + // Restore state. + this.typeId = typeId0; + this.start = start0; + this.rawOffPos = rawOffPos0; + this.schemaId = schemaId0; + this.fieldCnt = fieldCnt0; + this.mapper = mapper0; } /** {@inheritDoc} */ @Override public void writeObjectDetached(@Nullable Object obj) throws BinaryObjectException { - if (obj == null) - out.writeByte(GridBinaryMarshaller.NULL); - else { - // Store state. - int typeId0 = this.typeId; - int start0 = this.start; - int rawOffPos0 = this.rawOffPos; - int schemaId0 = this.schemaId; - int fieldCnt0 = this.fieldCnt; - BinaryInternalMapper mapper0 = this.mapper; - BinaryWriterHandles handles0 = this.handles; - - // Handles cleared, because, in this mode they are NOT shared down to hierarchy. - clearState(true); - - // Maybe recursive `writeObject` invocation which will change state. - marshal(obj); - - // Restore state. - this.typeId = typeId0; - this.start = start0; - this.rawOffPos = rawOffPos0; - this.schemaId = schemaId0; - this.fieldCnt = fieldCnt0; - this.mapper = mapper0; - this.handles = handles0; + BinaryWriterHandles outerHandles = handles; + + handles = null; + + try { + writeObject(obj); + } + finally { + handles = outerHandles; } } @@ -911,15 +875,7 @@ else if (ZERO_COPY && !BinaryUtils.USE_STR_SERIALIZATION_VER_2) /** {@inheritDoc} */ @Override public void writeByteArray(@Nullable byte[] val) throws BinaryObjectException { - if (val == null) - out.writeByte(GridBinaryMarshaller.NULL); - else { - out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridBinaryMarshaller.BYTE_ARR); - out.unsafeWriteInt(val.length); - - out.writeByteArray(val); - } + BinaryDirectWriter.writeByteArray(val, out); } /** {@inheritDoc} */ @@ -930,15 +886,7 @@ else if (ZERO_COPY && !BinaryUtils.USE_STR_SERIALIZATION_VER_2) /** {@inheritDoc} */ @Override public void writeShortArray(@Nullable short[] val) throws BinaryObjectException { - if (val == null) - out.writeByte(GridBinaryMarshaller.NULL); - else { - out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridBinaryMarshaller.SHORT_ARR); - out.unsafeWriteInt(val.length); - - out.writeShortArray(val); - } + BinaryDirectWriter.writeShortArray(val, out); } /** {@inheritDoc} */ @@ -949,15 +897,7 @@ else if (ZERO_COPY && !BinaryUtils.USE_STR_SERIALIZATION_VER_2) /** {@inheritDoc} */ @Override public void writeIntArray(@Nullable int[] val) throws BinaryObjectException { - if (val == null) - out.writeByte(GridBinaryMarshaller.NULL); - else { - out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridBinaryMarshaller.INT_ARR); - out.unsafeWriteInt(val.length); - - out.writeIntArray(val); - } + BinaryDirectWriter.writeIntArray(val, out); } /** {@inheritDoc} */ @@ -968,15 +908,7 @@ else if (ZERO_COPY && !BinaryUtils.USE_STR_SERIALIZATION_VER_2) /** {@inheritDoc} */ @Override public void writeLongArray(@Nullable long[] val) throws BinaryObjectException { - if (val == null) - out.writeByte(GridBinaryMarshaller.NULL); - else { - out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridBinaryMarshaller.LONG_ARR); - out.unsafeWriteInt(val.length); - - out.writeLongArray(val); - } + BinaryDirectWriter.writeLongArray(val, out); } /** {@inheritDoc} */ @@ -987,15 +919,7 @@ else if (ZERO_COPY && !BinaryUtils.USE_STR_SERIALIZATION_VER_2) /** {@inheritDoc} */ @Override public void writeFloatArray(@Nullable float[] val) throws BinaryObjectException { - if (val == null) - out.writeByte(GridBinaryMarshaller.NULL); - else { - out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridBinaryMarshaller.FLOAT_ARR); - out.unsafeWriteInt(val.length); - - out.writeFloatArray(val); - } + BinaryDirectWriter.writeFloatArray(val, out); } /** {@inheritDoc} */ @@ -1007,15 +931,7 @@ else if (ZERO_COPY && !BinaryUtils.USE_STR_SERIALIZATION_VER_2) /** {@inheritDoc} */ @Override public void writeDoubleArray(@Nullable double[] val) throws BinaryObjectException { - if (val == null) - out.writeByte(GridBinaryMarshaller.NULL); - else { - out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridBinaryMarshaller.DOUBLE_ARR); - out.unsafeWriteInt(val.length); - - out.writeDoubleArray(val); - } + BinaryDirectWriter.writeDoubleArray(val, out); } /** {@inheritDoc} */ @@ -1026,15 +942,7 @@ else if (ZERO_COPY && !BinaryUtils.USE_STR_SERIALIZATION_VER_2) /** {@inheritDoc} */ @Override public void writeCharArray(@Nullable char[] val) throws BinaryObjectException { - if (val == null) - out.writeByte(GridBinaryMarshaller.NULL); - else { - out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridBinaryMarshaller.CHAR_ARR); - out.unsafeWriteInt(val.length); - - out.writeCharArray(val); - } + BinaryDirectWriter.writeCharArray(val, out); } /** {@inheritDoc} */ @@ -1046,15 +954,7 @@ else if (ZERO_COPY && !BinaryUtils.USE_STR_SERIALIZATION_VER_2) /** {@inheritDoc} */ @Override public void writeBooleanArray(@Nullable boolean[] val) throws BinaryObjectException { - if (val == null) - out.writeByte(GridBinaryMarshaller.NULL); - else { - out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridBinaryMarshaller.BOOLEAN_ARR); - out.unsafeWriteInt(val.length); - - out.writeBooleanArray(val); - } + BinaryDirectWriter.writeBooleanArray(val, out); } /** {@inheritDoc} */ @@ -1073,8 +973,8 @@ else if (ZERO_COPY && !BinaryUtils.USE_STR_SERIALIZATION_VER_2) out.unsafeWriteByte(GridBinaryMarshaller.DECIMAL_ARR); out.unsafeWriteInt(val.length); - for (BigDecimal str : val) - writeDecimal(str); + for (BigDecimal decimal : val) + writeDecimal(decimal); } } @@ -1087,16 +987,7 @@ else if (ZERO_COPY && !BinaryUtils.USE_STR_SERIALIZATION_VER_2) /** {@inheritDoc} */ @Override public void writeStringArray(@Nullable String[] val) throws BinaryObjectException { - if (val == null) - out.writeByte(GridBinaryMarshaller.NULL); - else { - out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridBinaryMarshaller.STRING_ARR); - out.unsafeWriteInt(val.length); - - for (String str : val) - writeString(str); - } + BinaryDirectWriter.writeStringArray(val, out); } /** {@inheritDoc} */ @@ -1107,16 +998,7 @@ else if (ZERO_COPY && !BinaryUtils.USE_STR_SERIALIZATION_VER_2) /** {@inheritDoc} */ @Override public void writeUuidArray(@Nullable UUID[] val) throws BinaryObjectException { - if (val == null) - out.writeByte(GridBinaryMarshaller.NULL); - else { - out.unsafeEnsure(1 + 4); - out.unsafeWriteByte(GridBinaryMarshaller.UUID_ARR); - out.unsafeWriteInt(val.length); - - for (UUID uuid : val) - writeUuid(uuid); - } + BinaryDirectWriter.writeUuidArray(val, out); } /** {@inheritDoc} */ @@ -1599,16 +1481,13 @@ public BinaryWriterExImpl newWriter(int typeId) { } /** Clears writer state. */ - private void clearState(boolean clearHandler) { + private void clearState() { this.typeId = GridBinaryMarshaller.UNREGISTERED_TYPE_ID; this.start = out.position(); this.rawOffPos = 0; this.schemaId = BinaryUtils.schemaInitialId(); this.fieldCnt = 0; this.mapper = null; - - if (clearHandler) - this.handles = null; } /** @@ -2005,69 +1884,11 @@ public void writePlainObject(Object val) { throw new IllegalArgumentException("Can't write object with type: " + val.getClass()); switch (flag) { - case GridBinaryMarshaller.BYTE: - writeByte(flag); - writeByte((Byte)val); - - break; - - case GridBinaryMarshaller.SHORT: - writeByte(flag); - writeShort((Short)val); - - break; - - case GridBinaryMarshaller.INT: - writeByte(flag); - writeInt((Integer)val); - - break; - - case GridBinaryMarshaller.LONG: - writeByte(flag); - writeLong((Long)val); - - break; - - case GridBinaryMarshaller.FLOAT: - writeByte(flag); - writeFloat((Float)val); - - break; - - case GridBinaryMarshaller.DOUBLE: - writeByte(flag); - writeDouble((Double)val); - - break; - - case GridBinaryMarshaller.CHAR: - writeByte(flag); - writeChar((Character)val); - - break; - - case GridBinaryMarshaller.BOOLEAN: - writeByte(flag); - writeBoolean((Boolean)val); - - break; - case GridBinaryMarshaller.DECIMAL: writeDecimal((BigDecimal)val); break; - case GridBinaryMarshaller.STRING: - writeString((String)val); - - break; - - case GridBinaryMarshaller.UUID: - writeUuid((UUID)val); - - break; - case GridBinaryMarshaller.DATE: writeDate((Date)val); @@ -2083,61 +1904,11 @@ public void writePlainObject(Object val) { break; - case GridBinaryMarshaller.BYTE_ARR: - writeByteArray((byte[])val); - - break; - - case GridBinaryMarshaller.SHORT_ARR: - writeShortArray((short[])val); - - break; - - case GridBinaryMarshaller.INT_ARR: - writeIntArray((int[])val); - - break; - - case GridBinaryMarshaller.LONG_ARR: - writeLongArray((long[])val); - - break; - - case GridBinaryMarshaller.FLOAT_ARR: - writeFloatArray((float[])val); - - break; - - case GridBinaryMarshaller.DOUBLE_ARR: - writeDoubleArray((double[])val); - - break; - - case GridBinaryMarshaller.CHAR_ARR: - writeCharArray((char[])val); - - break; - - case GridBinaryMarshaller.BOOLEAN_ARR: - writeBooleanArray((boolean[])val); - - break; - case GridBinaryMarshaller.DECIMAL_ARR: writeDecimalArray((BigDecimal[])val); break; - case GridBinaryMarshaller.STRING_ARR: - writeStringArray((String[])val); - - break; - - case GridBinaryMarshaller.UUID_ARR: - writeUuidArray((UUID[])val); - - break; - case GridBinaryMarshaller.DATE_ARR: writeDateArray((Date[])val); @@ -2154,7 +1925,7 @@ public void writePlainObject(Object val) { break; default: - throw new IllegalArgumentException("Can't write object with type: " + val.getClass()); + BinaryDirectWriter.write(val, flag, out); } }