FrankChen021 commented on code in PR #18021:
URL: https://github.com/apache/druid/pull/18021#discussion_r2176340447


##########
extensions-contrib/druid-exact-cardinality/src/main/java/org/apache/druid/query/aggregation/exact/cardinality/bitmap64/Bitmap64ExactCardinalityObjectStrategy.java:
##########
@@ -0,0 +1,69 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one
+ * or more contributor license agreements.  See the NOTICE file
+ * distributed with this work for additional information
+ * regarding copyright ownership.  The ASF licenses this file
+ * to you under the Apache License, Version 2.0 (the
+ * "License"); you may not use this file except in compliance
+ * with the License.  You may obtain a copy of the License at
+ *
+ *   http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing,
+ * software distributed under the License is distributed on an
+ * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
+ * KIND, either express or implied.  See the License for the
+ * specific language governing permissions and limitations
+ * under the License.
+ */
+
+package org.apache.druid.query.aggregation.exact.cardinality.bitmap64;
+
+import org.apache.druid.segment.data.ObjectStrategy;
+
+import javax.annotation.Nullable;
+import java.nio.BufferUnderflowException;
+import java.nio.ByteBuffer;
+
+public class Bitmap64ExactCardinalityObjectStrategy implements 
ObjectStrategy<Bitmap64Counter>
+{
+
+  static final Bitmap64ExactCardinalityObjectStrategy STRATEGY = new 
Bitmap64ExactCardinalityObjectStrategy();
+
+  @Override
+  public Class<? extends Bitmap64Counter> getClazz()
+  {
+    return RoaringBitmap64Counter.class;
+  }
+
+  @Nullable
+  @Override
+  public Bitmap64Counter fromByteBuffer(ByteBuffer buffer, int numBytes)
+  {
+    final ByteBuffer readOnlyBuffer = buffer.asReadOnlyBuffer();
+
+    if (readOnlyBuffer.remaining() < numBytes) {
+      throw new BufferUnderflowException();
+    }
+
+    byte[] bytes = new byte[numBytes];
+    readOnlyBuffer.get(bytes, 0, numBytes);
+    return RoaringBitmap64Counter.fromBytes(bytes);

Review Comment:
   In the `fromBytes` method, it uses the `deserialize` method to turn the byte 
array into a bitmap object.
   ```java
         Roaring64NavigableMap bitmap = new Roaring64NavigableMap();
         bitmap.deserialize(in);
   ```
   
   The `deserialize` accepts `DataInput` as argument. In Netty, there's 
`io.netty.buffer.ByteBufInputStream` that wraps a ByteBuffer as `DataInput`. 
I'm wondering if we can use it or copies the idea from it.
   
   Maybe we can do sth like this?
   ```java
     if (buffer.hasArray()) {
       return new ByteArrayInputStream(
           buffer.array(), 
           buffer.arrayOffset() + buffer.position(), 
           buffer.remaining()
       );
     } else {
       // wrap the ByteBuffer into DataInput if possible
       // or copy the data from ByteBuffer as byte[]
     }
   ```



-- 
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