Tan-JiaLiang commented on code in PR #4956:
URL: https://github.com/apache/paimon/pull/4956#discussion_r1942384113


##########
paimon-common/src/main/java/org/apache/paimon/fileindex/bitmap/BitmapFileIndexMetaV2.java:
##########
@@ -0,0 +1,455 @@
+/*
+ * 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.paimon.fileindex.bitmap;
+
+import org.apache.paimon.data.BinaryString;
+import org.apache.paimon.fs.SeekableInputStream;
+import org.apache.paimon.options.Options;
+import org.apache.paimon.types.DataType;
+
+import java.io.BufferedInputStream;
+import java.io.DataInput;
+import java.io.DataInputStream;
+import java.io.DataOutput;
+import java.io.InputStream;
+import java.util.Comparator;
+import java.util.HashMap;
+import java.util.LinkedHashMap;
+import java.util.LinkedList;
+import java.util.List;
+import java.util.Map;
+import java.util.function.Function;
+
+/**
+ * When the bitmap-indexed column cardinality is high, using the first version 
of the bitmap index
+ * format will take a lot of time to read the entire dictionary. But in fact 
we don't need a full
+ * dictionary when dealing with a small number of predicates, the performance 
of predicate hits on
+ * the bitmap can be improved by creating a secondary index on the dictionary.
+ *
+ * <pre>
+ * Bitmap file index format (V2)
+ * +-------------------------------------------------+-----------------
+ * | version (1 byte) = 2                           |
+ * +-------------------------------------------------+
+ * | row count (4 bytes int)                        |
+ * +-------------------------------------------------+
+ * | non-null value bitmap number (4 bytes int)     |
+ * +-------------------------------------------------+
+ * | has null value (1 byte)                        |
+ * +-------------------------------------------------+
+ * | null value offset (4 bytes if has null value)  |       HEAD
+ * +-------------------------------------------------+
+ * | null bitmap length (4 bytes if has null value) |
+ * +-------------------------------------------------+
+ * | bitmap index block number (4 bytes int)        |
+ * +-------------------------------------------------+
+ * | value 1 | offset 1                             |
+ * +-------------------------------------------------+
+ * | value 2 | offset 2                             |
+ * +-------------------------------------------------+
+ * | ...                                            |
+ * +-------------------------------------------------+
+ * | bitmap blocks offset (4 bytes int)             |
+ * +-------------------------------------------------+-----------------
+ * | bitmap index block 1                           |
+ * +-------------------------------------------------+
+ * | bitmap index block 2                           |  INDEX BLOCKS
+ * +-------------------------------------------------+
+ * | ...                                            |
+ * +-------------------------------------------------+-----------------
+ * | serialized bitmap 1                            |
+ * +-------------------------------------------------+
+ * | serialized bitmap 2                            |
+ * +-------------------------------------------------+  BITMAP BLOCKS
+ * | serialized bitmap 3                            |
+ * +-------------------------------------------------+
+ * | ...                                            |
+ * +-------------------------------------------------+-----------------
+ *
+ * index block format:
+ * +-------------------------------------------------+
+ * | entry number (4 bytes int)                     |
+ * +-------------------------------------------------+
+ * | value 1 | offset 1 | length 1                  |
+ * +-------------------------------------------------+
+ * | value 2 | offset 2 | length 2                  |
+ * +-------------------------------------------------+
+ * | ...                                            |
+ * +-------------------------------------------------+
+ * </pre>
+ */
+public class BitmapFileIndexMetaV2 extends BitmapFileIndexMeta {
+
+    private int blockSizeLimit = 32 * 1024;
+
+    private LinkedList<BitmapIndexBlock> indexBlocks;
+    private long indexBlockStart;
+    private int nullBitmapLength;
+
+    public BitmapFileIndexMetaV2(DataType dataType, Options options) {
+        super(dataType, options);
+        this.nullBitmapLength = -1;
+    }
+
+    public BitmapFileIndexMetaV2(
+            DataType dataType,
+            Options options,
+            int rowCount,
+            int nonNullBitmapNumber,
+            boolean hasNullValue,
+            int nullValueOffset,
+            int nullBitmapLength,
+            LinkedHashMap<Object, Integer> bitmapOffsets,
+            int finalOffset) {
+        super(
+                dataType,
+                options,
+                rowCount,
+                nonNullBitmapNumber,
+                hasNullValue,
+                nullValueOffset,
+                bitmapOffsets);
+        this.nullBitmapLength = nullBitmapLength;
+        blockSizeLimit = options.getInteger(BitmapFileIndex.INDEX_BLOCK_SIZE, 
16 * 1024);
+        if (enableNextOffsetToSize) {
+            bitmapLengths = new HashMap<>();
+            Object lastValue = null;
+            int lastOffset = nullValueOffset;
+            for (Map.Entry<Object, Integer> entry : bitmapOffsets.entrySet()) {
+                Object value = entry.getKey();
+                Integer offset = entry.getValue();
+                if (offset >= 0) {
+                    if (lastOffset >= 0) {
+                        bitmapLengths.put(lastValue, offset - lastOffset);
+                    }
+                    lastValue = value;
+                    lastOffset = offset;
+                }
+            }
+            bitmapLengths.put(lastValue, finalOffset - lastOffset);
+        }
+    }
+
+    public static Comparator<Object> getComparator(DataType dataType) {
+        return dataType.accept(
+                new DataTypeVisitorAdapter<Comparator<Object>>() {
+                    @Override
+                    public Comparator<Object> visitBinaryString() {
+                        return Comparator.comparing(o -> ((BinaryString) o));
+                    }
+
+                    @Override
+                    public Comparator<Object> visitByte() {
+                        return Comparator.comparing(o -> ((Byte) o));
+                    }
+
+                    @Override
+                    public Comparator<Object> visitShort() {
+                        return Comparator.comparing(o -> ((Short) o));
+                    }
+
+                    @Override
+                    public Comparator<Object> visitInt() {
+                        return Comparator.comparing(o -> ((Integer) o));
+                    }
+
+                    @Override
+                    public Comparator<Object> visitLong() {
+                        return Comparator.comparing(o -> ((Long) o));
+                    }
+
+                    @Override
+                    public Comparator<Object> visitFloat() {
+                        return Comparator.comparing(o -> ((Float) o));
+                    }
+
+                    @Override
+                    public Comparator<Object> visitDouble() {
+                        return Comparator.comparing(o -> ((Double) o));
+                    }
+                });
+    }
+
+    @Override
+    public boolean contains(Object bitmapId) {
+        if (bitmapId == null) {
+            return hasNullValue;
+        }
+        BitmapIndexBlock block = findBlock(bitmapId);
+        return block != null && block.contains(bitmapId);
+    }
+
+    @Override
+    public int getOffset(Object bitmapId) {
+        if (bitmapId == null) {
+            return nullValueOffset;
+        }
+        BitmapIndexBlock block = findBlock(bitmapId);
+        return block.getOffset(bitmapId);
+    }
+
+    @Override
+    public int getLength(Object bitmapId) {
+        if (bitmapId == null) {
+            return nullBitmapLength;
+        }
+        BitmapIndexBlock block = findBlock(bitmapId);
+        return block.getLength(bitmapId);
+    }
+
+    private BitmapIndexBlock findBlock(Object bitmapId) {
+        Comparator<Object> comparator = getComparator(dataType);
+        BitmapIndexBlock prev = null;
+        for (BitmapIndexBlock block : indexBlocks) {
+            int cmp = comparator.compare(bitmapId, block.key);

Review Comment:
   `indexBlocks` is sorted, can we using `binarySearch` instead of `forEach`?



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

Reply via email to