FrankChen021 commented on code in PR #18021: URL: https://github.com/apache/druid/pull/18021#discussion_r2176313315
########## extensions-contrib/druid-exact-count/src/main/java/org/apache/druid/query/aggregation/exact/count/bitmap64/ExposedByteArrayOutputStream.java: ########## @@ -0,0 +1,34 @@ +/* + * 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.count.bitmap64; + +import java.io.ByteArrayOutputStream; + +/** + * This class is used to expose the underlying byte array in the output stream, to prevent extra copying of + * the array. + */ +public class ExposedByteArrayOutputStream extends ByteArrayOutputStream +{ + byte[] getBuffer() Review Comment: this may not be correct since the valid length is stored in the `count` property. I think we need to return a ByteBuffer here which wraps the buf by given length. ########## extensions-contrib/druid-exact-count/src/main/java/org/apache/druid/query/aggregation/exact/count/bitmap64/RoaringBitmap64CounterJsonSerializer.java: ########## @@ -0,0 +1,43 @@ +/* + * 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.count.bitmap64; + +import com.fasterxml.jackson.core.JsonGenerator; +import com.fasterxml.jackson.databind.JsonSerializer; +import com.fasterxml.jackson.databind.SerializerProvider; + +import java.io.ByteArrayInputStream; +import java.io.IOException; + +public class RoaringBitmap64CounterJsonSerializer extends JsonSerializer<RoaringBitmap64Counter> +{ + + @Override + public void serialize( + final RoaringBitmap64Counter bitmap64Counter, + final JsonGenerator jgen, + final SerializerProvider provider + ) + throws IOException + { + ByteArrayInputStream inputStream = bitmap64Counter.toJsonSerializableInputStream(); Review Comment: no need to wrap the byte array as ByteArrayInputStream as JsonGenerator can accept byte[] directly ########## extensions-contrib/druid-exact-count/src/main/java/org/apache/druid/query/aggregation/exact/count/bitmap64/RoaringBitmap64Counter.java: ########## @@ -0,0 +1,107 @@ +/* + * 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.count.bitmap64; + +import org.apache.druid.java.util.common.logger.Logger; +import org.roaringbitmap.longlong.Roaring64NavigableMap; + +import java.io.ByteArrayInputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.nio.ByteBuffer; + +public class RoaringBitmap64Counter implements Bitmap64 +{ + private static final Logger log = new Logger(RoaringBitmap64Counter.class); + + private final Roaring64NavigableMap bitmap; + + public RoaringBitmap64Counter() + { + this.bitmap = new Roaring64NavigableMap(); + } + + private RoaringBitmap64Counter(Roaring64NavigableMap bitmap) + { + this.bitmap = bitmap; + } + + public static RoaringBitmap64Counter fromBytes(byte[] bytes) + { + ByteArrayInputStream inputStream = new ByteArrayInputStream(bytes); + try { + DataInputStream in = new DataInputStream(inputStream); + Roaring64NavigableMap bitmap = new Roaring64NavigableMap(); + bitmap.deserialize(in); + return new RoaringBitmap64Counter(bitmap); + } + catch (Exception e) { + log.error(e, "Failed to deserialize RoaringBitmap64Counter from bytes"); + throw new RuntimeException(e); + } + } + + @Override + public void add(long value) + { + bitmap.addLong(value); + } + + @Override + public long getCardinality() + { + return bitmap.getLongCardinality(); + } + + @Override + public Bitmap64 fold(Bitmap64 rhs) + { + if (rhs != null) { + bitmap.or(((RoaringBitmap64Counter) rhs).bitmap); + } + + return this; + } + + private ExposedByteArrayOutputStream toOutputStream() + { + bitmap.runOptimize(); + try { + final ExposedByteArrayOutputStream out = new ExposedByteArrayOutputStream(); + bitmap.serialize(new DataOutputStream(out)); + return out; + } + catch (Exception e) { + throw new RuntimeException(e); + } + } + + @Override + public ByteBuffer toByteBuffer() + { + return ByteBuffer.wrap(toOutputStream().toByteArray()); + } + + public ByteArrayInputStream toJsonSerializableInputStream() Review Comment: there's no need to define a method in this class because it's dedicated for json serialization use only. we can call the `toByteBuffer` in the json serialization to get the raw byte array and then do serialization. ########## extensions-contrib/druid-exact-count/src/main/java/org/apache/druid/query/aggregation/exact/count/bitmap64/ExposedByteArrayOutputStream.java: ########## @@ -0,0 +1,34 @@ +/* + * 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.count.bitmap64; + +import java.io.ByteArrayOutputStream; + +/** + * This class is used to expose the underlying byte array in the output stream, to prevent extra copying of + * the array. Review Comment: remove the new line character -- 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]
