Skip to content

Commit

Permalink
feat(java): Support CopyOnWriteArrayListSerializer (#1613)
Browse files Browse the repository at this point in the history

## What does this PR do?

<!-- Describe the purpose of this PR. -->

Support CopyOnWriteArrayListSerializer

## 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.
-->
  • Loading branch information
MrChang0 authored May 8, 2024
1 parent a87b902 commit 9687fd8
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
import java.util.TreeSet;
import java.util.Vector;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CopyOnWriteArrayList;
import org.apache.fury.Fury;
import org.apache.fury.config.Language;
import org.apache.fury.exception.FuryException;
Expand Down Expand Up @@ -255,6 +256,27 @@ public List<?> xread(MemoryBuffer buffer) {
}
}

public static class CopyOnWriteArrayListSerializer
extends CollectionSerializer<CopyOnWriteArrayList> {

public CopyOnWriteArrayListSerializer(Fury fury, Class<CopyOnWriteArrayList> type) {
super(fury, type);
}

@Override
public Collection newCollection(MemoryBuffer buffer) {
int numElements = buffer.readVarUint32Small7();
setNumElements(numElements);
return new CollectionContainer<>(numElements);
}

@Override
public CopyOnWriteArrayList onCollectionRead(Collection collection) {
Object[] elements = ((CollectionContainer) collection).elements;
return new CopyOnWriteArrayList(elements);
}
}

public static final class EmptySetSerializer extends CollectionSerializer<Set<?>> {

public EmptySetSerializer(Fury fury, Class<Set<?>> cls) {
Expand Down Expand Up @@ -624,5 +646,8 @@ public static void registerDefaultSerializers(Fury fury) {
fury.registerSerializer(BitSet.class, new BitSetSerializer(fury, BitSet.class));
fury.registerSerializer(
PriorityQueue.class, new PriorityQueueSerializer(fury, PriorityQueue.class));
fury.registerSerializer(
CopyOnWriteArrayList.class,
new CopyOnWriteArrayListSerializer(fury, CopyOnWriteArrayList.class));
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ Args=--initialize-at-build-time=org.apache.fury.memory.MemoryBuffer,\
org.apache.fury.serializer.collection.UnmodifiableSerializers$Offset,\
org.apache.fury.serializer.collection.SynchronizedSerializers$Offset,\
org.apache.fury.serializer.collection.CollectionSerializers$ArraysAsListSerializer,\
org.apache.fury.serializer.collection.CollectionSerializers$CopyOnWriteArrayListSerializer,\
org.apache.fury.serializer.collection.MapSerializers$EnumMapSerializer,\
org.apache.fury.serializer.JdkProxySerializer,\
org.apache.fury.serializer.StringSerializer$Offset,\
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@
import java.util.Vector;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ConcurrentSkipListSet;
import java.util.concurrent.CopyOnWriteArrayList;
import java.util.concurrent.LinkedBlockingQueue;
import java.util.stream.LongStream;
import lombok.AllArgsConstructor;
Expand Down Expand Up @@ -263,6 +264,16 @@ public void tesPriorityQueueSerializer() {
CollectionSerializers.PriorityQueueSerializer.class);
}

@Test
public void testCopyOnWriteArrayList() {
final CopyOnWriteArrayList<String> list =
new CopyOnWriteArrayList<>(new String[] {"a", "b", "c"});
Assert.assertEquals(list, serDe(getJavaFury(), list));
Assert.assertEquals(
getJavaFury().getClassResolver().getSerializerClass(CopyOnWriteArrayList.class),
CollectionSerializers.CopyOnWriteArrayListSerializer.class);
}

@Test
public void testSerializeJavaBlockingQueue() {
Fury fury =
Expand Down

0 comments on commit 9687fd8

Please sign in to comment.