Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(java): Support copy capabilities for some classes without no-argument constructors #1794

Merged

Conversation

zhaommmmomo
Copy link
Member

@zhaommmmomo zhaommmmomo commented Aug 7, 2024

What does this PR do?

Some classes with no-argument constructors will report an error when calling copy().

This pr:

  • implement the copy method for the no-argument constructor serializer
  • add test case

Related issues

#1777
#1679

Does this PR introduce any user-facing change?

  • Does this PR introduce any public API change?
  • Does this PR introduce any binary protocol compatibility change?

Benchmark

@Override
public Object copy(Object value) {
Serializer serializer = classClassInfoHolderMap.get(value.getClass()).objectSerializer;
return fury.copyObject(value, serializer);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Do we need to invoke resolve method?

@@ -439,6 +441,28 @@ protected <K, V> void copyEntry(Map<K, V> originMap, Map<K, V> newMap) {
}
}

protected <K, V> void copyEntry(Map<K, V> originMap, Builder<K, V> builder) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

If we overwrite this method, we shoud add @Override annotation

@Override
public T copy(T originCollection) {
if (slotsSerializers.length == 0) {
return super.copy(originCollection);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This will return an object of super class type, which is not right

if (slotsSerializers.length == 0) {
return super.copy(originCollection);
}
return (T) slotsSerializers[0].copy(originCollection);
Copy link
Collaborator

Choose a reason for hiding this comment

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

This return an object of first field


@Override
public T copy(T originMap) {
if (slotsSerializers.length == 0) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Same issues

@@ -79,6 +79,21 @@ public void copyElements(T originCollection, Collection newCollection) {
}
}

public void copyElements(T originCollection, Object[] elements) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

add @Override annotation

@Override
public ByteBuffer copy(ByteBuffer value) {
ByteBuffer dst = ByteBuffer.allocate(value.capacity());
dst.put(value);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
dst.put(value);
dst.put(value.duplicate());

We need to duplicate, otherwise it will update offset in ByteBuffer

@@ -44,6 +44,13 @@ public void write(MemoryBuffer buffer, ByteBuffer value) {
fury.writeBufferObject(buffer, new BufferObject.ByteBufferBufferObject(value));
}

@Override
public ByteBuffer copy(ByteBuffer value) {
ByteBuffer dst = ByteBuffer.allocate(value.capacity());
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
ByteBuffer dst = ByteBuffer.allocate(value.capacity());
ByteBuffer dst = ByteBuffer.allocate(value.remaining());

Preconditions.checkNotNull(interfaces);
Preconditions.checkNotNull(invocationHandler);
Object proxy = Proxy.newProxyInstance(fury.getClassLoader(), interfaces, STUB_HANDLER);
Platform.putObject(proxy, PROXY_HANDLER_FIELD_OFFSET, invocationHandler);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Suggested change
Platform.putObject(proxy, PROXY_HANDLER_FIELD_OFFSET, invocationHandler);
Object proxy = Proxy.newProxyInstance(fury.getClassLoader(), interfaces, STUB_HANDLER);
fury.reference(value, proxy);
Platform.putObject(proxy, PROXY_HANDLER_FIELD_OFFSET,
fury.copyObject(invocationHandler));

@@ -542,6 +542,35 @@ public Collection newCollection() {
}
}

public void copyElements(Collection originCollection, Collection newCollection) {
Copy link
Collaborator

Choose a reason for hiding this comment

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

CollectionSerializer has same method, could we remove that method?

@Override
public T copy(T originCollection) {
if (objectSerializer == null) {
objectSerializer = new ObjectSerializer<>(fury, type, false);
Copy link
Collaborator

Choose a reason for hiding this comment

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

Could we update ObjectSerializer:

  public ObjectSerializer(Fury fury, Class<T> cls, boolean resolveParent) {
    super(fury, cls);
    // avoid recursive building serializers.
    // Use `setSerializerIfAbsent` to avoid overwriting existing serializer for class when used
    // as data serializer.
    if (resolveParent) {
      classResolver.setSerializerIfAbsent(cls, this);
    }

@chaokunyang
Copy link
Collaborator

Very nice work, thank you @zhaommmmomo

Copy link
Collaborator

@chaokunyang chaokunyang left a comment

Choose a reason for hiding this comment

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

LGTM, this is not an easy work, thanks for your contribution @zhaommmmomo

@chaokunyang chaokunyang merged commit 9a39cb3 into apache:main Aug 13, 2024
32 checks passed
@zhaommmmomo
Copy link
Member Author

LGTM, this is not an easy work, thanks for your contribution @zhaommmmomo

This is what I should do

@zhaommmmomo zhaommmmomo deleted the feat/support_without_no_arg_class_copy branch August 13, 2024 06:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants