platinumhamburg commented on code in PR #2390: URL: https://github.com/apache/fluss/pull/2390#discussion_r2703040188
########## fluss-server/src/main/java/org/apache/fluss/server/utils/RoaringBitmapUtils.java: ########## @@ -0,0 +1,66 @@ +/* + * 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.fluss.utils; + +import org.roaringbitmap.RoaringBitmap; +import org.roaringbitmap.longlong.Roaring64Bitmap; + +import java.io.ByteArrayInputStream; +import java.io.ByteArrayOutputStream; +import java.io.DataInputStream; +import java.io.DataOutputStream; +import java.io.IOException; +import java.nio.ByteBuffer; + +/** Utility methods for serializing roaring bitmaps. */ +public final class RoaringBitmapUtils { + + private RoaringBitmapUtils() { + // Utility class, no instantiation + } + + public static byte[] serializeRoaringBitmap32(RoaringBitmap bitmap) throws IOException { + bitmap.runOptimize(); + ByteBuffer buffer = ByteBuffer.allocate(bitmap.serializedSizeInBytes()); + bitmap.serialize(buffer); + return buffer.array(); + } + + public static void deserializeRoaringBitmap32(RoaringBitmap bitmap, byte[] bytes) + throws IOException { + bitmap.deserialize(ByteBuffer.wrap(bytes)); + } + + public static byte[] serializeRoaringBitmap64(Roaring64Bitmap bitmap) throws IOException { + bitmap.runOptimize(); + try (ByteArrayOutputStream output = new ByteArrayOutputStream(); + DataOutputStream dataOutput = new DataOutputStream(output)) { + bitmap.serialize(dataOutput); Review Comment: This is due to API limitations in Roaring64Bitmap: RoaringBitmap64 returns sizeInBytes as a long, while the maximum allocation size of a ByteBuffer is Integer.MAX_VALUE. This can lead to a dangerous narrowing conversion, so we should avoid this pattern as much as possible. -- 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]
