clintropolis commented on code in PR #18722:
URL: https://github.com/apache/druid/pull/18722#discussion_r2608100566


##########
processing/src/main/java/org/apache/druid/segment/column/BitmapIndexEncodingStrategy.java:
##########
@@ -0,0 +1,165 @@
+/*
+ * 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.segment.column;
+
+import com.fasterxml.jackson.annotation.JsonSubTypes;
+import com.fasterxml.jackson.annotation.JsonTypeInfo;
+import org.apache.druid.collections.bitmap.BitmapFactory;
+import org.apache.druid.collections.bitmap.ImmutableBitmap;
+import org.apache.druid.collections.bitmap.MutableBitmap;
+import org.apache.druid.error.DruidException;
+import org.apache.druid.segment.data.GenericIndexedWriter;
+import org.apache.druid.segment.file.SegmentFileBuilder;
+import org.apache.druid.segment.serde.Serializer;
+
+import javax.annotation.Nullable;
+import java.io.IOException;
+import java.nio.channels.WritableByteChannel;
+import java.util.Objects;
+
+@JsonTypeInfo(use = JsonTypeInfo.Id.NAME, property = "type")
+@JsonSubTypes({
+    @JsonSubTypes.Type(value = BitmapIndexEncodingStrategy.DictionaryId.class, 
name = "dictionaryId"),
+    @JsonSubTypes.Type(value = BitmapIndexEncodingStrategy.NullsOnly.class, 
name = "nullsOnly")
+})
+public abstract class BitmapIndexEncodingStrategy implements Serializer
+{
+  /**
+   * Assigned in {@link #init(BitmapFactory, int)}
+   */
+  @Nullable
+  protected MutableBitmap[] bitmaps;
+  /**
+   * Assigned in {@link #close(BitmapFactory, GenericIndexedWriter)}
+   */
+  @Nullable
+  GenericIndexedWriter<ImmutableBitmap> writer;
+
+  public abstract void init(BitmapFactory bitmapFactory, int dictionarySize);
+
+  public abstract void add(int row, int sortedId, @Nullable Object o);
+
+  public void close(BitmapFactory bitmapFactory, 
GenericIndexedWriter<ImmutableBitmap> writer) throws IOException
+  {
+    if (bitmaps == null) {
+      throw DruidException.defensive("Not initiated yet");
+    }
+    this.writer = writer;
+    for (int i = 0; i < bitmaps.length; i++) {
+      writer.write(bitmapFactory.makeImmutableBitmap(bitmaps[i]));
+      bitmaps[i] = null; // Reclaim memory
+    }
+    bitmaps = null;
+  }
+
+  @Override
+  public long getSerializedSize()
+  {
+    return writer.getSerializedSize();
+  }
+
+  @Override
+  public void writeTo(WritableByteChannel channel, SegmentFileBuilder 
fileBuilder) throws IOException
+  {
+    writer.writeTo(channel, fileBuilder);
+  }
+
+  public static class DictionaryId extends BitmapIndexEncodingStrategy
+  {
+    public static final DictionaryId INSTANCE = new DictionaryId();
+
+    @Override
+    public void init(BitmapFactory bitmapFactory, int dictionarySize)
+    {
+      bitmaps = new MutableBitmap[dictionarySize];
+      for (int index = 0; index < dictionarySize; index++) {
+        bitmaps[index] = bitmapFactory.makeEmptyMutableBitmap();
+      }
+    }
+
+    @Override
+    public void add(int row, int sortedId, @Nullable Object o)
+    {
+      bitmaps[sortedId].add(row);
+    }
+
+    @Override
+    public boolean equals(Object o)
+    {
+      if (this == o) {
+        return true;
+      }
+      return o != null && getClass() == o.getClass();
+    }
+
+    @Override
+    public int hashCode()
+    {
+      return Objects.hashCode(getClass());
+    }
+
+    @Override
+    public String toString()
+    {
+      return "DictionaryId";
+    }
+  }
+
+  public static class NullsOnly extends BitmapIndexEncodingStrategy

Review Comment:
   >are you thinking that the BitmapIndexType should handle deserialization?
   
   yes, but not in this PR, i have some changes in mind to do as a follow-up to 
overhaul these interfaces a bit.
   
   Since the indexes are stored at the end of the buffer, it wouldn't be that 
much trouble to switch to using `ByteBufferWriter` to just write the single 
bitmap and be consistent with other numeric columns and save some space 
compared to the overhead of `GenericIndexed` for fields that only have null 
value indexes. For now i think could just swap the index handling logic in 
`CompressedNestedDataComplexColumn.readNestedFieldColumn` to handle the index 
type at the part where we currently do `GenericIndexed.read`, and use 
`bitmapSerdeFactory.getObjectStrategy().fromByteBufferWithSize` if only has 
null index and to use the `NullValueIndexSupplier` as the index supplier. 



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