Skip to content

Commit 5869a10

Browse files
committed
Updating the documentation with memory-file mapping examples.
1 parent df0b41b commit 5869a10

File tree

4 files changed

+106
-56
lines changed

4 files changed

+106
-56
lines changed

README.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,11 @@ Usage
8484

8585
See example.java.
8686

87+
88+
Please note that there are more examples in the "examples" folder (e.g.,
89+
for memory-file mapping).
90+
91+
8792
Maven central repository
8893
------------------------
8994

example.java

Lines changed: 73 additions & 53 deletions
Original file line numberDiff line numberDiff line change
@@ -1,62 +1,82 @@
11
import com.googlecode.javaewah.EWAHCompressedBitmap;
2+
23
import java.io.*;
4+
import java.nio.ByteBuffer;
35

46
/**
5-
* @author lemire
6-
*
7-
*/
8-
public class example {
9-
10-
/**
11-
* @param args arguments from the command line
12-
* @throws IOException if an IO error occurs
7+
* Simple illustrative example.
8+
*
9+
* @author Daniel Lemire
10+
*
1311
*/
14-
public static void main(final String[] args) throws Exception {
15-
EWAHCompressedBitmap ewahBitmap1 = EWAHCompressedBitmap.bitmapOf(0,2,55,64,1<<30);
16-
EWAHCompressedBitmap ewahBitmap2 = EWAHCompressedBitmap.bitmapOf(1,3,64,1<<30);
17-
System.out.println("bitmap 1: "+ewahBitmap1);
18-
System.out.println("bitmap 2: "+ewahBitmap2);
19-
// or
20-
EWAHCompressedBitmap orbitmap = ewahBitmap1.or(ewahBitmap2);
21-
System.out.println("bitmap 1 OR bitmap 2: "+orbitmap);
22-
System.out.println("memory usage: " + orbitmap.sizeInBytes() + " bytes");
23-
// and
24-
EWAHCompressedBitmap andbitmap = ewahBitmap1.and(ewahBitmap2);
25-
System.out.println("bitmap 1 AND bitmap 2: "+andbitmap);
26-
System.out.println("memory usage: " + andbitmap.sizeInBytes() + " bytes");
27-
// xor
28-
EWAHCompressedBitmap xorbitmap = ewahBitmap1.xor(ewahBitmap2);
29-
System.out.println("bitmap 1 XOR bitmap 2:"+xorbitmap);
30-
System.out.println("memory usage: " + xorbitmap.sizeInBytes() + " bytes");
31-
// fast aggregation over many bitmaps
32-
EWAHCompressedBitmap ewahBitmap3 = EWAHCompressedBitmap.bitmapOf(5,55,1<<30);
33-
EWAHCompressedBitmap ewahBitmap4 = EWAHCompressedBitmap.bitmapOf(4,66,1<<30);
34-
System.out.println("bitmap 3: "+ewahBitmap3);
35-
System.out.println("bitmap 4: "+ewahBitmap4);
36-
andbitmap = EWAHCompressedBitmap.and(ewahBitmap1,ewahBitmap2,
37-
ewahBitmap3,ewahBitmap4);
38-
System.out.println("b1 AND b2 AND b3 AND b4: "+andbitmap);
39-
// serialization
40-
ByteArrayOutputStream bos = new ByteArrayOutputStream();
41-
// Note: you could use a file output steam instead of ByteArrayOutputStream
42-
ObjectOutputStream oo = new ObjectOutputStream(bos);
43-
ewahBitmap1.writeExternal(oo);
44-
oo.close();
45-
ewahBitmap1 = null;
46-
ewahBitmap1 = new EWAHCompressedBitmap();
47-
ByteArrayInputStream bis = new ByteArrayInputStream(bos.toByteArray());
48-
ewahBitmap1.readExternal(new ObjectInputStream(bis));
49-
System.out.println("bitmap 1 (recovered) : "+ewahBitmap1);
50-
//
51-
// support for threshold function (new as of version 0.8.0):
52-
// mark as true a bit that occurs at least T times in the source
53-
// bitmaps
54-
//
55-
EWAHCompressedBitmap threshold2 = EWAHCompressedBitmap.threshold(2, ewahBitmap1,ewahBitmap2,
56-
ewahBitmap3,ewahBitmap4);
57-
System.out.println("threshold 2 : "+threshold2);
12+
public class example
13+
{
14+
15+
/**
16+
* @param args
17+
* arguments from the command line
18+
* @throws IOException
19+
* if an IO error occurs
20+
*/
21+
public static void main(final String[] args) throws Exception {
22+
EWAHCompressedBitmap ewahBitmap1 = EWAHCompressedBitmap.bitmapOf(0, 2, 55,
23+
64, 1 << 30);
24+
EWAHCompressedBitmap ewahBitmap2 = EWAHCompressedBitmap.bitmapOf(1, 3, 64,
25+
1 << 30);
26+
System.out.println("bitmap 1: " + ewahBitmap1);
27+
System.out.println("bitmap 2: " + ewahBitmap2);
28+
// or
29+
EWAHCompressedBitmap orbitmap = ewahBitmap1.or(ewahBitmap2);
30+
System.out.println("bitmap 1 OR bitmap 2: " + orbitmap);
31+
System.out.println("memory usage: " + orbitmap.sizeInBytes() + " bytes");
32+
// and
33+
EWAHCompressedBitmap andbitmap = ewahBitmap1.and(ewahBitmap2);
34+
System.out.println("bitmap 1 AND bitmap 2: " + andbitmap);
35+
System.out.println("memory usage: " + andbitmap.sizeInBytes() + " bytes");
36+
// xor
37+
EWAHCompressedBitmap xorbitmap = ewahBitmap1.xor(ewahBitmap2);
38+
System.out.println("bitmap 1 XOR bitmap 2:" + xorbitmap);
39+
System.out.println("memory usage: " + xorbitmap.sizeInBytes() + " bytes");
40+
// fast aggregation over many bitmaps
41+
EWAHCompressedBitmap ewahBitmap3 = EWAHCompressedBitmap.bitmapOf(5, 55,
42+
1 << 30);
43+
EWAHCompressedBitmap ewahBitmap4 = EWAHCompressedBitmap.bitmapOf(4, 66,
44+
1 << 30);
45+
System.out.println("bitmap 3: " + ewahBitmap3);
46+
System.out.println("bitmap 4: " + ewahBitmap4);
47+
andbitmap = EWAHCompressedBitmap.and(ewahBitmap1, ewahBitmap2, ewahBitmap3,
48+
ewahBitmap4);
49+
System.out.println("b1 AND b2 AND b3 AND b4: " + andbitmap);
50+
// serialization
51+
ByteArrayOutputStream bos = new ByteArrayOutputStream();
52+
// Note: you could use a file output steam instead of ByteArrayOutputStream
53+
ewahBitmap1.serialize(new DataOutputStream(bos));
54+
EWAHCompressedBitmap ewahBitmap1new = new EWAHCompressedBitmap();
55+
byte[] bout = bos.toByteArray();
56+
ewahBitmap1new.deserialize(new DataInputStream(new ByteArrayInputStream(
57+
bout)));
58+
System.out.println("bitmap 1 (recovered) : " + ewahBitmap1new);
59+
if (!ewahBitmap1.equals(ewahBitmap1new))
60+
throw new RuntimeException("Will not happen");
61+
//
62+
// we can use a ByteBuffer as backend for a bitmap
63+
// which allows memory-mapped bitmaps
64+
//
65+
ByteBuffer bb = ByteBuffer.wrap(bout);
66+
EWAHCompressedBitmap rmap = new EWAHCompressedBitmap(bb);
67+
System.out.println("bitmap 1 (mapped) : " + rmap);
5868

69+
if (!rmap.equals(ewahBitmap1))
70+
throw new RuntimeException("Will not happen");
71+
//
72+
// support for threshold function (new as of version 0.8.0):
73+
// mark as true a bit that occurs at least T times in the source
74+
// bitmaps
75+
//
76+
EWAHCompressedBitmap threshold2 = EWAHCompressedBitmap.threshold(2,
77+
ewahBitmap1, ewahBitmap2, ewahBitmap3, ewahBitmap4);
78+
System.out.println("threshold 2 : " + threshold2);
5979

60-
}
80+
}
6181

6282
}

examples/MemoryMappingExample.java

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
import com.googlecode.javaewah.EWAHCompressedBitmap;
2+
import java.io.*;
3+
import java.nio.*;
4+
import java.nio.channels.FileChannel;
5+
6+
7+
public class MemoryMappingExample {
8+
9+
public static void main(String[] args) throws IOException {
10+
File tmpfile = File.createTempFile("roaring", "bin");
11+
tmpfile.deleteOnExit();
12+
final FileOutputStream fos = new FileOutputStream(tmpfile);
13+
EWAHCompressedBitmap ewahBitmap = EWAHCompressedBitmap.bitmapOf(0, 2, 55,
14+
64, 1 << 30);
15+
System.out.println("Created the bitmap "+ewahBitmap);
16+
ewahBitmap.serialize(new DataOutputStream(fos));
17+
long totalcount = fos.getChannel().position();
18+
System.out.println("Serialized total count = "+totalcount+" bytes");
19+
fos.close();
20+
RandomAccessFile memoryMappedFile = new RandomAccessFile(tmpfile, "r");
21+
ByteBuffer bb = memoryMappedFile.getChannel().map(FileChannel.MapMode.READ_ONLY, 0, totalcount);
22+
EWAHCompressedBitmap mapped = new EWAHCompressedBitmap(bb);
23+
System.out.println("Mapped the bitmap "+mapped);
24+
memoryMappedFile.close();
25+
}
26+
}

src/main/java/com/googlecode/javaewah/RunningLengthWord.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -78,8 +78,7 @@ static void setNumberOfLiteralWords(final Buffer buffer, final int position, fin
7878
buffer.andWord(position, (number << (RUNNING_LENGTH_BITS + 1)) | RUNNING_LENGTH_PLUS_RUNNING_BIT);
7979
}
8080

81-
/**@Override
82-
public
81+
/**
8382
* Sets the running bit.
8483
*
8584
* @param b the new running bit
@@ -170,4 +169,4 @@ public RunningLengthWord clone() throws CloneNotSupportedException {
170169

171170
private static final long NOT_SHIFTED_LARGEST_RUNNING_LENGTH_COUNT = ~SHIFTED_LARGEST_RUNNING_LENGTH_COUNT;
172171

173-
}
172+
}

0 commit comments

Comments
 (0)