valepakh commented on code in PR #5017: URL: https://github.com/apache/ignite-3/pull/5017#discussion_r1910410754
########## modules/client-common/src/main/java/org/apache/ignite/internal/client/proto/ClientComputeJobUnpacker.java: ########## @@ -52,97 +41,13 @@ public final class ClientComputeJobUnpacker { @Nullable Marshaller<?, byte[]> marshaller, @Nullable Class<?> resultClass ) { - if (unpacker.tryUnpackNil()) { - return null; - } - - // Underlying byte array expected to be in the following format: | typeId | value |. - int typeId = unpacker.unpackInt(); - ComputeJobDataType type = ComputeJobDataType.fromId(typeId); - if (type == null) { - throw new UnmarshallingException("Unsupported compute job type id: " + typeId); - } - - switch (type) { - case NATIVE: - if (marshaller != null) { - throw new UnmarshallingException( - "Can not unpack object because the marshaller is provided but the object was packed without marshaller." - ); - } - - return unpacker.unpackObjectFromBinaryTuple(); - - case TUPLE: - return TupleWithSchemaMarshalling.unmarshal(unpacker.readBinary()); + ComputeJobDataHolder holder = unpackJobArgumentWithoutMarshaller(unpacker); - case MARSHALLED_CUSTOM: - if (marshaller == null) { - throw new UnmarshallingException( - "Can not unpack object because the marshaller is not provided but the object was packed with marshaller." - ); - } - return tryUnmarshalOrCast(marshaller, unpacker.readBinary()); - - case POJO: - if (resultClass == null) { - throw new UnmarshallingException( - "Can not unpack object because the pojo class is not provided but the object was packed as pojo. " - + "Provide Job result type in JobDescriptor.resultClass." - ); - } - return unpackPojo(unpacker, resultClass); - - case TUPLE_COLLECTION: { - // TODO: IGNITE-24059 Deduplicate with ComputeUtils. - ByteBuffer collectionBuf = unpacker.readBinaryUnsafe().order(ByteOrder.LITTLE_ENDIAN); - int count = collectionBuf.getInt(); - BinaryTupleReader reader = new BinaryTupleReader(count, collectionBuf.slice().order(ByteOrder.LITTLE_ENDIAN)); - - List<Tuple> res = new ArrayList<>(count); - for (int i = 0; i < count; i++) { - ByteBuffer elementBytes = reader.bytesValueAsBuffer(i); - - if (elementBytes == null) { - res.add(null); - continue; - } - - res.add(TupleWithSchemaMarshalling.unmarshal(elementBytes)); - } - - return res; - } - - default: - throw new UnmarshallingException("Unsupported compute job type: " + type); - } - } - - private static Object unpackPojo(ClientMessageUnpacker unpacker, Class<?> pojoClass) { - try { - Object obj = pojoClass.getConstructor().newInstance(); - - fromTuple(obj, TupleWithSchemaMarshalling.unmarshal(unpacker.readBinary())); - - return obj; - } catch (NoSuchMethodException e) { - throw new UnmarshallingException("Class " + pojoClass.getName() + " doesn't have public default constructor. " - + "Add the default constructor or provide Marshaller for " + pojoClass.getName() + " in JobDescriptor.resultMarshaller", - e); - } catch (InvocationTargetException e) { - throw new UnmarshallingException("Constructor has thrown an exception", e); - } catch (InstantiationException e) { - throw new UnmarshallingException("Can't instantiate an object of class " + pojoClass.getName(), e); - } catch (IllegalAccessException e) { - throw new UnmarshallingException("Constructor is inaccessible", e); - } catch (PojoConversionException e) { - throw new UnmarshallingException("Can't unpack object", e); - } + return SharedComputeUtils.unmarshalArgOrResult(holder, marshaller, resultClass); } /** Unpacks compute job argument without marshaller. */ - public static @Nullable Object unpackJobArgumentWithoutMarshaller(ClientMessageUnpacker unpacker) { + public static @Nullable ComputeJobDataHolder unpackJobArgumentWithoutMarshaller(ClientMessageUnpacker unpacker) { Review Comment: I mean that we could move this method to the `SharedComputeUtils` and move the remainder of this class (and packer too) back to `client` module. -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: notifications-unsubscr...@ignite.apache.org For queries about this service, please contact Infrastructure at: us...@infra.apache.org