This is an automated email from the ASF dual-hosted git repository.

chaokunyang pushed a commit to branch main
in repository https://gitbox.apache.org/repos/asf/incubator-fury.git


The following commit(s) were added to refs/heads/main by this push:
     new 3a85ccc0 feat(java): add custom serializer register in case of special 
serializer ctr (#1625)
3a85ccc0 is described below

commit 3a85ccc0652ccb307ecab8c6774b144ca17e4429
Author: Shuchang Li <[email protected]>
AuthorDate: Mon May 13 15:24:35 2024 +0800

    feat(java): add custom serializer register in case of special serializer 
ctr (#1625)
    
    
    
    ## What does this PR do?
    
    <!-- Describe the purpose of this PR. -->
    
    it has a problem when we use `ThreadSafeFury` and custom Serializer
    which special constructor, so we need a method provider `Fury` to create
    Serializer and add it.
    
    ## Related issues
    
    <!--
    Is there any related issue? Please attach here.
    
    - #xxxx0
    - #xxxx1
    - #xxxx2
    -->
    
    
    ## Does this PR introduce any user-facing change?
    
    <!--
    If any user-facing interface changes, please [open an
    issue](https://github.com/apache/incubator-fury/issues/new/choose)
    describing the need to do so and update the document if necessary.
    -->
    
    - [ ] Does this PR introduce any public API change?
    - [ ] Does this PR introduce any binary protocol compatibility change?
    
    
    ## Benchmark
    
    <!--
    When the PR has an impact on performance (if you don't know whether the
    PR will have an impact on performance, you can submit the PR first, and
    if it will have impact on performance, the code reviewer will explain
    it), be sure to attach a benchmark data here.
    -->
---
 .../org/apache/fury/AbstractThreadSafeFury.java    |  6 +++
 .../src/main/java/org/apache/fury/BaseFury.java    |  9 +++++
 .../src/main/java/org/apache/fury/Fury.java        |  6 +++
 .../apache/fury/resolver/ClassResolverTest.java    | 45 ++++++++++++++++++++++
 4 files changed, 66 insertions(+)

diff --git 
a/java/fury-core/src/main/java/org/apache/fury/AbstractThreadSafeFury.java 
b/java/fury-core/src/main/java/org/apache/fury/AbstractThreadSafeFury.java
index 4085e7d9..752a4dc2 100644
--- a/java/fury-core/src/main/java/org/apache/fury/AbstractThreadSafeFury.java
+++ b/java/fury-core/src/main/java/org/apache/fury/AbstractThreadSafeFury.java
@@ -20,6 +20,7 @@
 package org.apache.fury;
 
 import java.util.function.Consumer;
+import java.util.function.Function;
 import org.apache.fury.serializer.Serializer;
 import org.apache.fury.serializer.SerializerFactory;
 
@@ -54,6 +55,11 @@ public abstract class AbstractThreadSafeFury implements 
ThreadSafeFury {
     processCallback(fury -> fury.registerSerializer(type, serializer));
   }
 
+  @Override
+  public void registerSerializer(Class<?> type, Function<Fury, Serializer<?>> 
serializerCreator) {
+    processCallback(fury -> fury.registerSerializer(type, 
serializerCreator.apply(fury)));
+  }
+
   @Override
   public void setSerializerFactory(SerializerFactory serializerFactory) {
     processCallback(fury -> fury.setSerializerFactory(serializerFactory));
diff --git a/java/fury-core/src/main/java/org/apache/fury/BaseFury.java 
b/java/fury-core/src/main/java/org/apache/fury/BaseFury.java
index 98821df1..1ee1aec8 100644
--- a/java/fury-core/src/main/java/org/apache/fury/BaseFury.java
+++ b/java/fury-core/src/main/java/org/apache/fury/BaseFury.java
@@ -20,6 +20,7 @@
 package org.apache.fury;
 
 import java.io.OutputStream;
+import java.util.function.Function;
 import org.apache.fury.io.FuryInputStream;
 import org.apache.fury.io.FuryReadableChannel;
 import org.apache.fury.memory.MemoryBuffer;
@@ -71,6 +72,14 @@ public interface BaseFury {
 
   void registerSerializer(Class<?> type, Serializer<?> serializer);
 
+  /**
+   * Register a Serializer created by serializerCreator when fury created.
+   *
+   * @param type class needed to be serialized/deserialized.
+   * @param serializerCreator serializer creator with param {@link Fury}
+   */
+  void registerSerializer(Class<?> type, Function<Fury, Serializer<?>> 
serializerCreator);
+
   void setSerializerFactory(SerializerFactory serializerFactory);
 
   /** Return serialized <code>obj</code> as a byte array. */
diff --git a/java/fury-core/src/main/java/org/apache/fury/Fury.java 
b/java/fury-core/src/main/java/org/apache/fury/Fury.java
index ee3ed4c8..8115ed5a 100644
--- a/java/fury-core/src/main/java/org/apache/fury/Fury.java
+++ b/java/fury-core/src/main/java/org/apache/fury/Fury.java
@@ -27,6 +27,7 @@ import java.util.ArrayList;
 import java.util.Iterator;
 import java.util.List;
 import java.util.function.Consumer;
+import java.util.function.Function;
 import javax.annotation.concurrent.NotThreadSafe;
 import org.apache.fury.builder.JITContext;
 import org.apache.fury.config.CompatibleMode;
@@ -175,6 +176,11 @@ public final class Fury implements BaseFury {
     classResolver.registerSerializer(type, serializer);
   }
 
+  @Override
+  public void registerSerializer(Class<?> type, Function<Fury, Serializer<?>> 
serializerCreator) {
+    classResolver.registerSerializer(type, serializerCreator.apply(this));
+  }
+
   @Override
   public void setSerializerFactory(SerializerFactory serializerFactory) {
     classResolver.setSerializerFactory(serializerFactory);
diff --git 
a/java/fury-core/src/test/java/org/apache/fury/resolver/ClassResolverTest.java 
b/java/fury-core/src/test/java/org/apache/fury/resolver/ClassResolverTest.java
index 231d11a6..ecb35305 100644
--- 
a/java/fury-core/src/test/java/org/apache/fury/resolver/ClassResolverTest.java
+++ 
b/java/fury-core/src/test/java/org/apache/fury/resolver/ClassResolverTest.java
@@ -46,6 +46,7 @@ import lombok.EqualsAndHashCode;
 import lombok.ToString;
 import org.apache.fury.Fury;
 import org.apache.fury.FuryTestBase;
+import org.apache.fury.ThreadSafeFury;
 import org.apache.fury.builder.Generated;
 import org.apache.fury.config.Language;
 import org.apache.fury.logging.Logger;
@@ -365,4 +366,48 @@ public class ClassResolverTest extends FuryTestBase {
     
Assert.assertFalse(classResolver.isPrimitive(classResolver.getRegisteredClassId(String.class)));
     
Assert.assertFalse(classResolver.isPrimitive(classResolver.getRegisteredClassId(Date.class)));
   }
+
+  // without static for test
+  class FooCustomSerializer extends Serializer<Foo> {
+
+    public FooCustomSerializer(Fury fury, Class<Foo> type) {
+      super(fury, type);
+    }
+
+    @Override
+    public void write(MemoryBuffer buffer, Foo value) {
+      buffer.writeInt32(value.f1);
+    }
+
+    @Override
+    public Foo read(MemoryBuffer buffer) {
+      final Foo foo = new Foo();
+      foo.f1 = buffer.readInt32();
+      return foo;
+    }
+  }
+
+  @Test
+  public void testFooCustomSerializer() {
+    ThreadSafeFury threadSafeFury =
+        Fury.builder().withLanguage(Language.JAVA).buildThreadSafeFury();
+    Assert.assertThrows(
+        () -> threadSafeFury.registerSerializer(Foo.class, 
FooCustomSerializer.class));
+    threadSafeFury.registerSerializer(Foo.class, f -> new 
FooCustomSerializer(f, Foo.class));
+    final Foo foo = new Foo();
+    foo.setF1(100);
+
+    threadSafeFury.execute(
+        fury -> {
+          Assert.assertEquals(foo, serDe(fury, foo));
+          return null;
+        });
+    threadSafeFury.execute(
+        fury -> {
+          Assert.assertEquals(
+              fury.getClassResolver().getSerializer(foo.getClass()).getClass(),
+              FooCustomSerializer.class);
+          return null;
+        });
+  }
 }


---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]

Reply via email to