Skip to content
Open

TC debug #13886

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*/
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down Expand Up @@ -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();

Expand Down Expand Up @@ -351,6 +364,9 @@ public void marshal(Object obj, BinaryOutputStream out) {
* @throws org.apache.ignite.binary.BinaryObjectException In case of error.
*/
@Nullable public <T> 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);

Expand Down Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Original file line number Diff line number Diff line change
@@ -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;
}
}
Loading
Loading