zhan7236 commented on code in PR #2955:
URL: https://github.com/apache/fory/pull/2955#discussion_r2577543023


##########
java/fory-core/src/main/java/org/apache/fory/serializer/collection/CollectionSerializers.java:
##########
@@ -690,6 +692,89 @@ public PriorityQueue newCollection(MemoryBuffer buffer) {
     }
   }
 
+  /**
+   * Serializer for {@link ArrayBlockingQueue}.
+   *
+   * <p>This serializer handles the capacity field which is essential for 
ArrayBlockingQueue since
+   * it's a bounded queue. The capacity is stored in the items array length 
and needs to be
+   * preserved during serialization.
+   */
+  public static class ArrayBlockingQueueSerializer
+      extends ConcurrentCollectionSerializer<ArrayBlockingQueue> {
+
+    public ArrayBlockingQueueSerializer(Fory fory, Class<ArrayBlockingQueue> 
cls) {
+      super(fory, cls, true);
+    }
+
+    @Override
+    public CollectionSnapshot onCollectionWrite(MemoryBuffer buffer, 
ArrayBlockingQueue value) {
+      CollectionSnapshot snapshot = super.onCollectionWrite(buffer, value);
+      // Write the capacity (remaining capacity + current size = total 
capacity)
+      int capacity = value.remainingCapacity() + value.size();
+      buffer.writeVarUint32Small7(capacity);
+      return snapshot;
+    }
+
+    @Override
+    public ArrayBlockingQueue newCollection(MemoryBuffer buffer) {
+      int numElements = buffer.readVarUint32Small7();
+      setNumElements(numElements);
+      int capacity = buffer.readVarUint32Small7();
+      ArrayBlockingQueue queue = new ArrayBlockingQueue<>(capacity);
+      fory.getRefResolver().reference(queue);
+      return queue;
+    }
+
+    @Override
+    public Collection newCollection(Collection collection) {
+      ArrayBlockingQueue abq = (ArrayBlockingQueue) collection;
+      // Capacity is remaining + current size
+      int capacity = abq.remainingCapacity() + abq.size();

Review Comment:
   You're right. I've opened a new PR https://github.com/apache/fory/pull/2956 
that handles the race condition. Thanks!



-- 
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: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


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

Reply via email to