use RoaringBitmapWriter for RoaringBitmap construction (#6764)

This commit is contained in:
Richard Startin 2019-01-09 01:18:41 +00:00 committed by Clint Wylie
parent def823124c
commit 99097617a1
2 changed files with 26 additions and 22 deletions

View File

@ -681,7 +681,7 @@
<dependency> <dependency>
<groupId>org.roaringbitmap</groupId> <groupId>org.roaringbitmap</groupId>
<artifactId>RoaringBitmap</artifactId> <artifactId>RoaringBitmap</artifactId>
<version>0.7.30</version> <version>0.7.36</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.ow2.asm</groupId> <groupId>org.ow2.asm</groupId>
@ -987,7 +987,7 @@
<plugin> <plugin>
<groupId>org.codehaus.mojo</groupId> <groupId>org.codehaus.mojo</groupId>
<artifactId>animal-sniffer-maven-plugin</artifactId> <artifactId>animal-sniffer-maven-plugin</artifactId>
<version>1.15</version> <version>1.17</version>
<executions> <executions>
<execution> <execution>
<id>check-java-api</id> <id>check-java-api</id>

View File

@ -22,6 +22,7 @@ package org.apache.druid.collections.bitmap;
import com.google.common.base.Throwables; import com.google.common.base.Throwables;
import org.roaringbitmap.IntIterator; import org.roaringbitmap.IntIterator;
import org.roaringbitmap.RoaringBitmap; import org.roaringbitmap.RoaringBitmap;
import org.roaringbitmap.RoaringBitmapWriter;
import org.roaringbitmap.buffer.MutableRoaringBitmap; import org.roaringbitmap.buffer.MutableRoaringBitmap;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -39,7 +40,7 @@ public class WrappedRoaringBitmap implements MutableBitmap
/** /**
* Underlying bitmap. * Underlying bitmap.
*/ */
private MutableRoaringBitmap bitmap; private RoaringBitmapWriter<MutableRoaringBitmap> writer;
/** /**
* Creates a new WrappedRoaringBitmap wrapping an empty MutableRoaringBitmap * Creates a new WrappedRoaringBitmap wrapping an empty MutableRoaringBitmap
@ -56,17 +57,17 @@ public class WrappedRoaringBitmap implements MutableBitmap
*/ */
public WrappedRoaringBitmap(boolean compressRunOnSerialization) public WrappedRoaringBitmap(boolean compressRunOnSerialization)
{ {
this.bitmap = new MutableRoaringBitmap(); this.writer = RoaringBitmapWriter.bufferWriter().get();
this.compressRunOnSerialization = compressRunOnSerialization; this.compressRunOnSerialization = compressRunOnSerialization;
} }
ImmutableBitmap toImmutableBitmap() ImmutableBitmap toImmutableBitmap()
{ {
MutableRoaringBitmap mrb = bitmap.clone(); MutableRoaringBitmap bitmap = writer.get().clone();
if (compressRunOnSerialization) { if (compressRunOnSerialization) {
mrb.runOptimize(); bitmap.runOptimize();
} }
return new WrappedImmutableRoaringBitmap(mrb); return new WrappedImmutableRoaringBitmap(bitmap.toImmutableRoaringBitmap());
} }
@Override @Override
@ -74,6 +75,7 @@ public class WrappedRoaringBitmap implements MutableBitmap
{ {
try { try {
final ByteArrayOutputStream out = new ByteArrayOutputStream(); final ByteArrayOutputStream out = new ByteArrayOutputStream();
MutableRoaringBitmap bitmap = writer.get();
if (compressRunOnSerialization) { if (compressRunOnSerialization) {
bitmap.runOptimize(); bitmap.runOptimize();
} }
@ -88,21 +90,22 @@ public class WrappedRoaringBitmap implements MutableBitmap
@Override @Override
public void clear() public void clear()
{ {
this.bitmap.clear(); this.writer.reset();
} }
@Override @Override
public void or(MutableBitmap mutableBitmap) public void or(MutableBitmap mutableBitmap)
{ {
WrappedRoaringBitmap other = (WrappedRoaringBitmap) mutableBitmap; WrappedRoaringBitmap other = (WrappedRoaringBitmap) mutableBitmap;
MutableRoaringBitmap unwrappedOtherBitmap = other.bitmap; MutableRoaringBitmap unwrappedOtherBitmap = other.writer.get();
bitmap.or(unwrappedOtherBitmap); writer.get().or(unwrappedOtherBitmap);
} }
@Override @Override
public int getSizeInBytes() public int getSizeInBytes()
{ {
MutableRoaringBitmap bitmap = writer.get();
if (compressRunOnSerialization) { if (compressRunOnSerialization) {
bitmap.runOptimize(); bitmap.runOptimize();
} }
@ -112,21 +115,22 @@ public class WrappedRoaringBitmap implements MutableBitmap
@Override @Override
public void add(int entry) public void add(int entry)
{ {
bitmap.add(entry); writer.add(entry);
} }
@Override @Override
public int size() public int size()
{ {
return bitmap.getCardinality(); return writer.get().getCardinality();
} }
public void serialize(ByteBuffer buffer) public void serialize(ByteBuffer buffer)
{ {
try {
MutableRoaringBitmap bitmap = writer.get();
if (compressRunOnSerialization) { if (compressRunOnSerialization) {
bitmap.runOptimize(); bitmap.runOptimize();
} }
try {
bitmap.serialize( bitmap.serialize(
new DataOutputStream( new DataOutputStream(
new OutputStream() new OutputStream()
@ -180,38 +184,38 @@ public class WrappedRoaringBitmap implements MutableBitmap
@Override @Override
public String toString() public String toString()
{ {
return getClass().getSimpleName() + bitmap; return getClass().getSimpleName() + writer.getUnderlying();
} }
@Override @Override
public void remove(int entry) public void remove(int entry)
{ {
bitmap.remove(entry); writer.get().remove(entry);
} }
@Override @Override
public IntIterator iterator() public IntIterator iterator()
{ {
return bitmap.getIntIterator(); return writer.get().getIntIterator();
} }
@Override @Override
public boolean isEmpty() public boolean isEmpty()
{ {
return bitmap.isEmpty(); return writer.get().isEmpty();
} }
@Override @Override
public ImmutableBitmap intersection(ImmutableBitmap otherBitmap) public ImmutableBitmap intersection(ImmutableBitmap otherBitmap)
{ {
WrappedRoaringBitmap other = (WrappedRoaringBitmap) otherBitmap; WrappedRoaringBitmap other = (WrappedRoaringBitmap) otherBitmap;
MutableRoaringBitmap unwrappedOtherBitmap = other.bitmap; MutableRoaringBitmap unwrappedOtherBitmap = other.writer.get();
return new WrappedImmutableRoaringBitmap(MutableRoaringBitmap.and(bitmap, unwrappedOtherBitmap)); return new WrappedImmutableRoaringBitmap(MutableRoaringBitmap.and(writer.get(), unwrappedOtherBitmap));
} }
@Override @Override
public boolean get(int value) public boolean get(int value)
{ {
return bitmap.contains(value); return writer.get().contains(value);
} }
} }