Skip to content

Commit

Permalink
GH-420 quads (#425)
Browse files Browse the repository at this point in the history
HDTq MSDL, DiffCat, GenDisk implementation and add tests
  • Loading branch information
ate47 authored Oct 3, 2023
1 parent c7fc174 commit 31e13e0
Show file tree
Hide file tree
Showing 81 changed files with 3,610 additions and 1,751 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ public interface AddSnapshotDeltaBitmap extends Bitmap, AutoCloseable {
* @return the roaring bitmap associated with this snapshot, can be null
* for empty value
*/
RoaringBitmap roaringBitmap();
RoaringBitmap64 roaringBitmap();
}

private class DeltaBitmap implements SimpleBitmap, AddSnapshotDeltaBitmap {
private boolean closed;
/**
* compressed memory bitmap storing the delta
*/
final RoaringBitmap snapshot = new RoaringBitmap();
final RoaringBitmap64 snapshot = new RoaringBitmap64();
/**
* next snapshot created after this one
*/
Expand All @@ -94,7 +94,7 @@ private class DeltaBitmap implements SimpleBitmap, AddSnapshotDeltaBitmap {
}

@Override
public RoaringBitmap roaringBitmap() {
public RoaringBitmap64 roaringBitmap() {
return snapshot;
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -162,12 +162,24 @@ public boolean access(long bitIndex) {

@Override
public long rank1(long pos) {
throw new NotImplementedException();
long c = 0;
for (int i = 0; i < pos; i++) {
if (access(i)) {
c++;
}
}
return c;
}

@Override
public long rank0(long pos) {
throw new NotImplementedException();
long c = 0;
for (int i = 0; i < pos; i++) {
if (!access(i)) {
c++;
}
}
return c;
}

@Override
Expand Down Expand Up @@ -264,7 +276,7 @@ public String getType() {
}

public long selectPrev1(long start) {
throw new NotImplementedException();
return select1(rank1(start));
}

public long getNumBits() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public class BitmapFactoryImpl extends BitmapFactory {
protected ModifiableBitmap doCreateModifiableBitmap(String type) {
return switch (Objects.requireNonNullElse(type, HDTVocabulary.BITMAP_TYPE_PLAIN)) {
case HDTVocabulary.BITMAP_TYPE_PLAIN -> Bitmap375Big.memory(0);
case HDTVocabulary.BITMAP_TYPE_ROARING -> new RoaringBitmap();
case HDTVocabulary.BITMAP_TYPE_ROARING -> new RoaringBitmap64();
default -> throw new IllegalArgumentException("Implementation not found for Bitmap with type " + type);
};
}
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,124 @@
package com.the_qa_company.qendpoint.core.compact.bitmap;

import com.the_qa_company.qendpoint.core.exceptions.NotImplementedException;
import com.the_qa_company.qendpoint.core.util.io.Closer;

import java.io.Closeable;
import java.io.IOException;

/**
* Bitmap to delete inside a graph, all the ids are mapped on a bitmap with the
* formula
*
* <pre>
* (id, graph) -> id * graphs + graph
* </pre>
*
* @author Antoine Willerval
*/
public class GraphDeleteBitmap implements SimpleModifiableBitmap, Closeable {
/**
* create empty graph delete bitmap
*
* @param graphs graphs count
* @param size triples count
* @return gdb
*/
public static GraphDeleteBitmap empty(long graphs, long size) {
return new GraphDeleteBitmap(EmptyBitmap.of(size * graphs), graphs);
}

/**
* create memory graph delete bitmap
*
* @param graphs graphs count
* @param size triples count
* @return gdb
*/
public static GraphDeleteBitmap memory(long graphs, long size) {
return new GraphDeleteBitmap(MultiRoaringBitmap.memory(size * graphs), graphs);
}

/**
* wrap a bitmap to create a {@link GraphDeleteBitmap}
*
* @param bitmap bitmap
* @param graphs graphs count
* @return bitmap if already instanceof graph delete bitmap and contains the
* right graphs number or wrap into GraphDeleteBitmap
*/
public static GraphDeleteBitmap wrap(Bitmap bitmap, long graphs) {
if (bitmap instanceof GraphDeleteBitmap gdb && gdb.graphs == graphs) {
// use directly the bitmap
return gdb;
}
return new GraphDeleteBitmap(bitmap, graphs);
}

private final Bitmap store;
private final long graphs;

private GraphDeleteBitmap(Bitmap store, long graphs) {
this.store = store;
this.graphs = graphs;
}

/**
* access a bit in a graph
*
* @param graph graph
* @param position position
* @return bit value
*/
public boolean access(long graph, long position) {
return access(position * graphs + graph);
}

/**
* set a bit in a graph
*
* @param graph graph
* @param position position
* @param value value
* @throws ClassCastException if the wrapped bitmap isn't a modifiable
* bitmap
*/
public void set(int graph, long position, boolean value) {
set(position * graphs + graph, value);
}

@Override
public boolean access(long position) {
return store.access(position);
}

@Override
public void set(long position, boolean value) {
((ModifiableBitmap) store).set(position, value);
}

@Override
public long getNumBits() {
return store.getNumBits();
}

@Override
public long getSizeBytes() {
return store.getSizeBytes();
}

@Override
public String getType() {
return store.getType();
}

@Override
public void append(boolean value) {
throw new NotImplementedException();
}

@Override
public void close() throws IOException {
Closer.closeSingle(store);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
package com.the_qa_company.qendpoint.core.compact.bitmap;

import com.the_qa_company.qendpoint.core.hdt.HDTVocabulary;
import com.the_qa_company.qendpoint.core.listener.ProgressListener;
import com.the_qa_company.qendpoint.core.util.io.CloseMappedByteBuffer;
import com.the_qa_company.qendpoint.core.util.io.IOUtil;
import org.roaringbitmap.buffer.ImmutableRoaringBitmap;

import java.io.Closeable;
import java.io.DataOutputStream;
import java.io.IOException;
import java.io.OutputStream;

/**
* Mapped {@link Bitmap} wrapper for the {@link ImmutableRoaringBitmap}, not
* compatible with {@link RoaringBitmap64}
*
* @author Antoine Willerval
*/
public class MappedRoaringBitmap implements SimpleBitmap, Closeable {
private final CloseMappedByteBuffer buffer;
private final ImmutableRoaringBitmap rbm;

public MappedRoaringBitmap(CloseMappedByteBuffer buffer) {
this.buffer = buffer;
this.rbm = new ImmutableRoaringBitmap(buffer.getInternalBuffer());
}

public ImmutableRoaringBitmap getHandle() {
return rbm;
}

@Override
public boolean access(long position) {
return rbm.contains((int) position);
}

@Override
public long getNumBits() {
return rbm.last();
}

@Override
public long getSizeBytes() {
return rbm.serializedSizeInBytes() + 8;
}

@Override
public void save(OutputStream output, ProgressListener listener) throws IOException {
long size = rbm.serializedSizeInBytes();
IOUtil.writeLong(output, size);
rbm.serialize(new DataOutputStream(output));
}

@Override
public String getType() {
return HDTVocabulary.BITMAP_TYPE_ROARING;
}

@Override
public long select1(long n) {
long position = n - 1;
if (position == -1)
return -1;
if (position < rbm.getLongCardinality()) {
return rbm.select((int) position);
} else {
return rbm.select((int) rbm.getLongCardinality() - 1) + 1;
}
}

@Override
public long rank1(long position) {
if (position >= 0)
return rbm.rankLong((int) position);
return 0;
}

@Override
public long countOnes() {
return rbm.getLongCardinality();
}

@Override
public long selectPrev1(long start) {
return select1(rank1(start));
}

@Override
public long selectNext1(long start) {
long pos = rank1(start - 1);
if (pos < rbm.getLongCardinality())
return select1(pos + 1);
return -1;
}

@Override
public void close() throws IOException {
buffer.close();
}
}
Loading

0 comments on commit 31e13e0

Please sign in to comment.