Skip to content

Instantly share code, notes, and snippets.

@gmucha
Created May 11, 2016 09:24
Show Gist options
  • Save gmucha/656b6015f712b14fba6c13efcbd7e316 to your computer and use it in GitHub Desktop.
Save gmucha/656b6015f712b14fba6c13efcbd7e316 to your computer and use it in GitHub Desktop.
import com.fasterxml.jackson.databind.util.ByteBufferBackedOutputStream;
import com.google.common.base.Stopwatch;
import com.google.common.hash.BloomFilter;
import com.google.common.hash.Funnel;
import com.google.common.hash.Funnels;
import com.google.common.io.ByteArrayDataOutput;
import com.google.common.io.ByteStreams;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.util.stream.IntStream;
import java.util.zip.GZIPInputStream;
import java.util.zip.GZIPOutputStream;
public class BloomExample {
public static final Funnel<CharSequence> FUNNEL = Funnels.unencodedCharsFunnel();
public static void main(String[] args) throws InterruptedException, IOException {
final double[] expectedfpp = {0};
IntStream.rangeClosed(1, 5).forEach(i -> {
BloomFilter<String> filter = BloomFilter.create(FUNNEL, 1_000_000, 0.00000001);
Stopwatch started = Stopwatch.createStarted();
IntStream.range(1, 4_000_000).forEach(ji -> {
filter.put("ITEM NUMBER " + Math.random());
});
// System.out.println(filter.mightContain("ITEM NUMBER 123456"));
System.out.println(started.stop());
expectedfpp[0] = filter.expectedFpp();
System.out.println(filter.expectedFpp());
ByteArrayDataOutput byteArrayDataOutput = ByteStreams.newDataOutput();
try {
ByteBuffer byteBuffer = ByteBuffer.allocate(60000000);
ByteBufferBackedOutputStream out = new ByteBufferBackedOutputStream(byteBuffer);
GZIPOutputStream gos = new GZIPOutputStream(out);
filter.writeTo(out);
gos.finish();
System.out.println(byteBuffer.position());
// BloomFilter.readFrom(new GZIPInputStream(new ByteArrayInputStream(byteBuffer.array())), FUNNEL);
} catch (IOException e) {
e.printStackTrace();
}
});
System.out.println(expectedfpp[0]);
}
}
class Beacon {
private final int id;
private final String name;
private final long value;
public int getId() {
return id;
}
public String getName() {
return name;
}
public long getValue() {
return value;
}
Beacon(int id, String name, long value) {
this.id = id;
this.name = name;
this.value = value;
}
// public Map<String, String> getExtensions() {
// return extensions;
// }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment