com.metamx.collections.bitmap.RoaringBitmapFactory Maven / Gradle / Ivy
/*
* Copyright 2011 - 2015 Metamarkets Group Inc.
*
* Licensed 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 com.metamx.collections.bitmap;
import com.google.common.base.Throwables;
import org.roaringbitmap.RoaringBitmap;
import org.roaringbitmap.buffer.BufferFastAggregation;
import org.roaringbitmap.buffer.ImmutableRoaringBitmap;
import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.nio.ByteBuffer;
import java.util.Iterator;
/**
* As the name suggests, this class instantiates bitmaps of the types
* WrappedRoaringBitmap and WrappedImmutableRoaringBitmap.
*/
public class RoaringBitmapFactory implements BitmapFactory
{
static final boolean DEFAULT_COMPRESS_RUN_ON_SERIALIZATION = false;
private static final ImmutableRoaringBitmap EMPTY_IMMUTABLE_BITMAP;
static {
try {
final RoaringBitmap roaringBitmap = new RoaringBitmap();
final ByteArrayOutputStream out = new ByteArrayOutputStream();
roaringBitmap.serialize(new DataOutputStream(out));
final byte[] bytes = out.toByteArray();
ByteBuffer buf = ByteBuffer.wrap(bytes);
EMPTY_IMMUTABLE_BITMAP = new ImmutableRoaringBitmap(buf);
}
catch (Exception e) {
throw Throwables.propagate(e);
}
}
private static final WrappedImmutableRoaringBitmap WRAPPED_IMMUTABLE_ROARING_BITMAP =
new WrappedImmutableRoaringBitmap(EMPTY_IMMUTABLE_BITMAP);
private final boolean compressRunOnSerialization;
public RoaringBitmapFactory()
{
this(DEFAULT_COMPRESS_RUN_ON_SERIALIZATION);
}
public RoaringBitmapFactory(boolean compressRunOnSerialization)
{
this.compressRunOnSerialization = compressRunOnSerialization;
}
@Override
public MutableBitmap makeEmptyMutableBitmap()
{
return new WrappedRoaringBitmap(compressRunOnSerialization);
}
@Override
public ImmutableBitmap makeEmptyImmutableBitmap()
{
return WRAPPED_IMMUTABLE_ROARING_BITMAP;
}
@Override
public ImmutableBitmap makeImmutableBitmap(MutableBitmap mutableBitmap)
{
if (!(mutableBitmap instanceof WrappedRoaringBitmap)) {
throw new IllegalStateException(String.format("Cannot convert [%s]", mutableBitmap.getClass()));
}
try {
return ((WrappedRoaringBitmap) mutableBitmap).toImmutableBitmap();
}
catch (Exception e) {
throw Throwables.propagate(e);
}
}
@Override
public ImmutableBitmap mapImmutableBitmap(ByteBuffer b)
{
return new WrappedImmutableRoaringBitmap(b);
}
@Override
public ImmutableBitmap union(Iterable b)
{
return new WrappedImmutableRoaringBitmap(ImmutableRoaringBitmap.or(unwrap(b).iterator()));
}
@Override
public ImmutableBitmap intersection(Iterable b)
{
return new WrappedImmutableRoaringBitmap(BufferFastAggregation.and(unwrap(b).iterator()));
}
@Override
public ImmutableBitmap complement(ImmutableBitmap b)
{
return new WrappedImmutableRoaringBitmap(
ImmutableRoaringBitmap.flip(
((WrappedImmutableRoaringBitmap) b).getBitmap(),
0,
b.size()
)
);
}
@Override
public ImmutableBitmap complement(
ImmutableBitmap b, int length
)
{
return new WrappedImmutableRoaringBitmap(
ImmutableRoaringBitmap.flip(
((WrappedImmutableRoaringBitmap) b).getBitmap(),
0,
length
)
);
}
private static Iterable unwrap(
final Iterable b
)
{
return new Iterable()
{
@Override
public Iterator iterator()
{
final Iterator i = b.iterator();
return new Iterator()
{
@Override
public void remove()
{
throw new UnsupportedOperationException();
}
@Override
public boolean hasNext()
{
return i.hasNext();
}
@Override
public ImmutableRoaringBitmap next()
{
WrappedImmutableRoaringBitmap wrappedBitmap = (WrappedImmutableRoaringBitmap) i.next();
if (wrappedBitmap == null) {
return EMPTY_IMMUTABLE_BITMAP;
}
return wrappedBitmap.getBitmap();
}
};
}
};
}
}