Skip to content
Merged
Show file tree
Hide file tree
Changes from 10 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 @@ -30,4 +30,11 @@ public interface BinaryRawReaderEx extends BinaryRawReader {
* @throws org.apache.ignite.binary.BinaryObjectException In case of error.
*/
@Nullable public Object readObjectDetached() throws BinaryObjectException;

/**
* @param deserialize {@code True} if object should be deserialized during reading.
* @return Object.
* @throws org.apache.ignite.binary.BinaryObjectException In case of error.
*/
@Nullable public Object readObjectDetached(boolean deserialize) throws BinaryObjectException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -1333,7 +1333,12 @@ float readFloat(int fieldId) throws BinaryObjectException {

/** {@inheritDoc} */
@Nullable @Override public Object readObjectDetached() throws BinaryObjectException {
return BinaryUtils.unmarshal(in, ctx, ldr, this, true);
return readObjectDetached(false);
}

/** {@inheritDoc} */
@Nullable @Override public Object readObjectDetached(boolean deserialize) throws BinaryObjectException {
return BinaryUtils.unmarshal(in, ctx, ldr, this, true, deserialize);
}

/** {@inheritDoc} */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1853,6 +1853,15 @@ public static Object doReadOptimized(BinaryInputStream in, BinaryContext ctx, @N
*/
@Nullable public static Object unmarshal(BinaryInputStream in, BinaryContext ctx, ClassLoader ldr,
BinaryReaderHandlesHolder handles, boolean detach) throws BinaryObjectException {
return unmarshal(in, ctx, ldr, handles, detach, false);
}

/**
* @return Unmarshalled value.
* @throws BinaryObjectException In case of error.
*/
@Nullable public static Object unmarshal(BinaryInputStream in, BinaryContext ctx, ClassLoader ldr,
BinaryReaderHandlesHolder handles, boolean detach, boolean deserialize) throws BinaryObjectException {
int start = in.position();

byte flag = in.readByte();
Expand All @@ -1871,7 +1880,7 @@ public static Object doReadOptimized(BinaryInputStream in, BinaryContext ctx, @N

in.position(handlePos);

obj = unmarshal(in, ctx, ldr, handles, detach);
obj = unmarshal(in, ctx, ldr, handles, detach, deserialize);

in.position(retPos);
}
Expand Down Expand Up @@ -1992,13 +2001,13 @@ public static Object doReadOptimized(BinaryInputStream in, BinaryContext ctx, @N
return doReadTimeArray(in);

case GridBinaryMarshaller.OBJ_ARR:
return doReadObjectArray(in, ctx, ldr, handles, detach, false);
return doReadObjectArray(in, ctx, ldr, handles, detach, deserialize);

case GridBinaryMarshaller.COL:
return doReadCollection(in, ctx, ldr, handles, detach, false, null);
return doReadCollection(in, ctx, ldr, handles, detach, deserialize, null);

case GridBinaryMarshaller.MAP:
return doReadMap(in, ctx, ldr, handles, detach, false, null);
return doReadMap(in, ctx, ldr, handles, detach, deserialize, null);

case GridBinaryMarshaller.BINARY_OBJ:
return doReadBinaryObject(in, ctx, detach);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -271,6 +271,8 @@ private ServiceDescriptor findDescriptor(String name) {
assert arg != null;
assert arg instanceof ServiceProxyHolder;

ServiceProxyHolder svc = (ServiceProxyHolder)arg;

String mthdName = reader.readString();

Object[] args;
Expand All @@ -279,13 +281,13 @@ private ServiceDescriptor findDescriptor(String name) {
args = new Object[reader.readInt()];

for (int i = 0; i < args.length; i++)
args[i] = reader.readObjectDetached();
args[i] = reader.readObjectDetached(!srvKeepBinary && !svc.isPlatformService());
}
else
args = null;

try {
Object result = ((ServiceProxyHolder)arg).invoke(mthdName, srvKeepBinary, args);
Object result = svc.invoke(mthdName, srvKeepBinary, args);

PlatformUtils.writeInvocationResult(writer, result, null);
}
Expand Down Expand Up @@ -559,7 +561,7 @@ public static void convertArrayArgs(Object[] args, Method mtd) {
@SuppressWarnings({"unchecked", "rawtypes"})
private static class ServiceProxyHolder extends PlatformAbstractTarget {
/** */
private final Object proxy;
public final Object proxy;

/** */
private final Class serviceClass;
Expand Down Expand Up @@ -610,7 +612,7 @@ private ServiceProxyHolder(Object proxy, Class clazz, PlatformContext ctx) {
*/
public Object invoke(String mthdName, boolean srvKeepBinary, Object[] args)
throws IgniteCheckedException, NoSuchMethodException {
if (proxy instanceof PlatformService)
if (isPlatformService())
return ((PlatformService)proxy).invokeMethod(mthdName, srvKeepBinary, args);
else {
assert proxy instanceof GridServiceProxy;
Expand Down Expand Up @@ -715,6 +717,11 @@ private static boolean areMethodArgsCompatible(Class[] argTypes, Object[] args)
private static Class wrap(Class c) {
return c.isPrimitive() ? PRIMITIVES_TO_WRAPPERS.get(c) : c;
}

/** @return {@code True} if service is platform service. */
public boolean isPlatformService() {
return proxy instanceof PlatformService;
}
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

package org.apache.ignite.internal.processors.platform.utils;

import java.lang.reflect.Array;
import java.lang.reflect.Field;
import java.math.BigDecimal;
import java.sql.Timestamp;
Expand Down Expand Up @@ -989,7 +990,9 @@ private static Collection<Object> unwrapKnownCollection(Collection<Object> col)
* @return Result.
*/
public static Object[] unwrapBinariesInArray(Object[] arr) {
Object[] res = new Object[arr.length];
Class<?> compType = arr.getClass().getComponentType();

Object[] res = (Object[])Array.newInstance(compType, arr.length);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great! It was strange, that here was untyped array. Great catch!

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Now that we have a check for component type above, this can be changed back to Object[] res = new Object[arr.length];


for (int i = 0; i < arr.length; i++)
res[i] = unwrapBinary(arr[i]);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2969,7 +2969,7 @@ public void testReadDetachedMap() throws Exception {
assertTrue(map.containsKey(key));
assertEquals(val, map.get(key));
});
});
}, false);
}

/**
Expand All @@ -2989,7 +2989,15 @@ public void testReadDetachedCollection() throws Exception {

assertTrue(col.contains(val));
});
});
}, false);
}

/** @throws Exception If failed. */
@Test
public void testReadDetachedTypedArray() throws Exception {
Value[] arr = IntStream.range(0, 1000).mapToObj(Value::new).toArray(Value[]::new);

testReadDetachObjectProperly(arr, obj -> assertArrayEquals(arr, (Value[])obj), true);
}

/**
Expand All @@ -3009,7 +3017,7 @@ public void testReadDetachedArray() throws Exception {

assertEquals(arr[i], new Value(val.field("val")));
}
});
}, false);
}

/**
Expand All @@ -3019,7 +3027,7 @@ public void testReadDetachedArray() throws Exception {
* @param action Action to perform on object.
* @throws Exception If failed.
*/
private void testReadDetachObjectProperly(Object obj, IgniteThrowableConsumer<Object> action) throws Exception {
private void testReadDetachObjectProperly(Object obj, IgniteThrowableConsumer<Object> action, boolean deserialize) throws Exception {
BinaryMarshaller marsh = binaryMarshaller();

BinaryHeapOutputStream os = new BinaryHeapOutputStream(1024);
Expand All @@ -3032,7 +3040,7 @@ private void testReadDetachObjectProperly(Object obj, IgniteThrowableConsumer<Ob

BinaryReaderExImpl reader = marsh.binaryMarshaller().reader(is);

Object bObj = reader.readObjectDetached();
Object bObj = reader.readObjectDetached(deserialize);

Arrays.fill(os.array(), (byte)0);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,25 @@ public Address testAddress(Address addr) {
return addr;
}

/** */
public int testOverload(Integer count, Employee[] emps) {
assertNotNull(emps);
assertEquals((int)count, emps.length);

assertEquals("Sarah Connor", emps[0].getFio());
assertEquals(1, emps[0].getSalary());

assertEquals("John Connor", emps[1].getFio());
assertEquals(2, emps[1].getSalary());

return 42;
}

/** */
public int testOverload(int first, int second) {
return first + second;
}

/** */
public Employee[] testEmployees(Employee[] emps) {
if (emps == null)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,12 @@ public interface IJavaService
/** */
Address testAddress(Address addr);

/** */
int testOverload(int count, Employee[] emps);

/** */
int testOverload(int first, int second);

/** */
Employee[] testEmployees(Employee[] emps);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,18 @@ public Address testAddress(Address addr)
return _svc.testAddress(addr);
}

/** <inheritDoc /> */
public int testOverload(int count, Employee[] emps)
{
return _svc.testOverload(count, emps);
}

/** <inheritDoc /> */
public int testOverload(int first, int second)
{
return _svc.testOverload(first, second);
}

/** <inheritDoc /> */
public Employee[] testEmployees(Employee[] emps)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -127,13 +127,19 @@ private void doTestService(IJavaService svc)
Assert.AreEqual("127000", addr.Zip);
Assert.AreEqual("Moscow Akademika Koroleva 12", addr.Addr);

Employee[] emps = new[]
{
new Employee {Fio = "Sarah Connor", Salary = 1},
new Employee {Fio = "John Connor", Salary = 2}
};

Assert.AreEqual(42, svc.testOverload(2, emps));
Assert.AreEqual(3, svc.testOverload(1, 2));
Assert.AreEqual(5, svc.testOverload(3, 2));

Assert.IsNull(svc.testEmployees(null));

Employee[] emps = svc.testEmployees(new[]
{
new Employee { Fio = "Sarah Connor", Salary = 1 },
new Employee { Fio = "John Connor", Salary = 2 }
});
emps = svc.testEmployees(emps);

Assert.NotNull(emps);
Assert.AreEqual(1, emps.Length);
Expand Down