From 99097617a129a8c6c6bbdafb09ba427d7f3994ef Mon Sep 17 00:00:00 2001 From: Richard Startin Date: Wed, 9 Jan 2019 01:18:41 +0000 Subject: [PATCH] use RoaringBitmapWriter for RoaringBitmap construction (#6764) --- pom.xml | 4 +- .../bitmap/WrappedRoaringBitmap.java | 44 ++++++++++--------- 2 files changed, 26 insertions(+), 22 deletions(-) diff --git a/pom.xml b/pom.xml index 03bf81453c2..5a9f2c70d78 100644 --- a/pom.xml +++ b/pom.xml @@ -681,7 +681,7 @@ org.roaringbitmap RoaringBitmap - 0.7.30 + 0.7.36 org.ow2.asm @@ -987,7 +987,7 @@ org.codehaus.mojo animal-sniffer-maven-plugin - 1.15 + 1.17 check-java-api diff --git a/processing/src/main/java/org/apache/druid/collections/bitmap/WrappedRoaringBitmap.java b/processing/src/main/java/org/apache/druid/collections/bitmap/WrappedRoaringBitmap.java index bf20138c718..8690af38bb8 100644 --- a/processing/src/main/java/org/apache/druid/collections/bitmap/WrappedRoaringBitmap.java +++ b/processing/src/main/java/org/apache/druid/collections/bitmap/WrappedRoaringBitmap.java @@ -22,6 +22,7 @@ package org.apache.druid.collections.bitmap; import com.google.common.base.Throwables; import org.roaringbitmap.IntIterator; import org.roaringbitmap.RoaringBitmap; +import org.roaringbitmap.RoaringBitmapWriter; import org.roaringbitmap.buffer.MutableRoaringBitmap; import java.io.ByteArrayOutputStream; @@ -39,7 +40,7 @@ public class WrappedRoaringBitmap implements MutableBitmap /** * Underlying bitmap. */ - private MutableRoaringBitmap bitmap; + private RoaringBitmapWriter writer; /** * Creates a new WrappedRoaringBitmap wrapping an empty MutableRoaringBitmap @@ -56,17 +57,17 @@ public class WrappedRoaringBitmap implements MutableBitmap */ public WrappedRoaringBitmap(boolean compressRunOnSerialization) { - this.bitmap = new MutableRoaringBitmap(); + this.writer = RoaringBitmapWriter.bufferWriter().get(); this.compressRunOnSerialization = compressRunOnSerialization; } ImmutableBitmap toImmutableBitmap() { - MutableRoaringBitmap mrb = bitmap.clone(); + MutableRoaringBitmap bitmap = writer.get().clone(); if (compressRunOnSerialization) { - mrb.runOptimize(); + bitmap.runOptimize(); } - return new WrappedImmutableRoaringBitmap(mrb); + return new WrappedImmutableRoaringBitmap(bitmap.toImmutableRoaringBitmap()); } @Override @@ -74,6 +75,7 @@ public class WrappedRoaringBitmap implements MutableBitmap { try { final ByteArrayOutputStream out = new ByteArrayOutputStream(); + MutableRoaringBitmap bitmap = writer.get(); if (compressRunOnSerialization) { bitmap.runOptimize(); } @@ -88,21 +90,22 @@ public class WrappedRoaringBitmap implements MutableBitmap @Override public void clear() { - this.bitmap.clear(); + this.writer.reset(); } @Override public void or(MutableBitmap mutableBitmap) { WrappedRoaringBitmap other = (WrappedRoaringBitmap) mutableBitmap; - MutableRoaringBitmap unwrappedOtherBitmap = other.bitmap; - bitmap.or(unwrappedOtherBitmap); + MutableRoaringBitmap unwrappedOtherBitmap = other.writer.get(); + writer.get().or(unwrappedOtherBitmap); } @Override public int getSizeInBytes() { + MutableRoaringBitmap bitmap = writer.get(); if (compressRunOnSerialization) { bitmap.runOptimize(); } @@ -112,21 +115,22 @@ public class WrappedRoaringBitmap implements MutableBitmap @Override public void add(int entry) { - bitmap.add(entry); + writer.add(entry); } @Override public int size() { - return bitmap.getCardinality(); + return writer.get().getCardinality(); } public void serialize(ByteBuffer buffer) { - if (compressRunOnSerialization) { - bitmap.runOptimize(); - } try { + MutableRoaringBitmap bitmap = writer.get(); + if (compressRunOnSerialization) { + bitmap.runOptimize(); + } bitmap.serialize( new DataOutputStream( new OutputStream() @@ -180,38 +184,38 @@ public class WrappedRoaringBitmap implements MutableBitmap @Override public String toString() { - return getClass().getSimpleName() + bitmap; + return getClass().getSimpleName() + writer.getUnderlying(); } @Override public void remove(int entry) { - bitmap.remove(entry); + writer.get().remove(entry); } @Override public IntIterator iterator() { - return bitmap.getIntIterator(); + return writer.get().getIntIterator(); } @Override public boolean isEmpty() { - return bitmap.isEmpty(); + return writer.get().isEmpty(); } @Override public ImmutableBitmap intersection(ImmutableBitmap otherBitmap) { WrappedRoaringBitmap other = (WrappedRoaringBitmap) otherBitmap; - MutableRoaringBitmap unwrappedOtherBitmap = other.bitmap; - return new WrappedImmutableRoaringBitmap(MutableRoaringBitmap.and(bitmap, unwrappedOtherBitmap)); + MutableRoaringBitmap unwrappedOtherBitmap = other.writer.get(); + return new WrappedImmutableRoaringBitmap(MutableRoaringBitmap.and(writer.get(), unwrappedOtherBitmap)); } @Override public boolean get(int value) { - return bitmap.contains(value); + return writer.get().contains(value); } }