Example usage for com.google.common.hash BloomFilter create

List of usage examples for com.google.common.hash BloomFilter create

Introduction

In this page you can find the example usage for com.google.common.hash BloomFilter create.

Prototype

@CheckReturnValue
public static <T> BloomFilter<T> create(Funnel<? super T> funnel, long expectedInsertions, double fpp) 

Source Link

Document

Creates a BloomFilter BloomFilter with the expected number of insertions and expected false positive probability.

Usage

From source file:com.jordanwilliams.heftydb.test.performance.table.file.TableBloomFilterPerformance.java

public static void main(String[] args) throws Exception {
    TestFileHelper.createTestDirectory();
    KeyValueGenerator keyValueGenerator = new KeyValueGenerator();
    Value value = new Value(keyValueGenerator.testValue(100));

    System.out.println("Writing bloom filter");

    Paths paths = ConfigGenerator.testPaths();
    TableBloomFilterWriter filterWriter = TableBloomFilterWriter.open(1, paths, RECORD_COUNT);
    BloomFilter<Key> guavaFilter = BloomFilter.create(new Funnel<Key>() {
        @Override/*from   w  w w  . j  a v  a 2  s  . com*/
        public void funnel(Key key, PrimitiveSink primitiveSink) {
            primitiveSink.putBytes(key.data().array());
        }
    }, RECORD_COUNT, 0.01);

    for (int i = 0; i < RECORD_COUNT; i++) {
        value.data().rewind();
        filterWriter.write(new Key(ByteBuffers.fromString(i + ""), i));
        guavaFilter.put(new Key(ByteBuffers.fromString(i + ""), i));
    }

    filterWriter.finish();

    System.out.println("Reading bloom filter");

    TableBloomFilter tableBloomFilter = TableBloomFilter.read(1, paths);

    double hits = 0;
    double misses = 0;

    double ghits = 0;
    double gmisses = 0;

    for (int i = RECORD_COUNT * 2; i > RECORD_COUNT; i--) {
        if (tableBloomFilter.mightContain(new Key(ByteBuffers.fromString(i + ""), i))) {
            hits++;
        } else {
            misses++;
        }

        if (guavaFilter.mightContain(new Key(ByteBuffers.fromString(i + ""), i))) {
            ghits++;
        } else {
            gmisses++;
        }
    }

    System.out.println("False positive rate: " + hits / (hits + misses));
    System.out.println("Guava positive rate: " + ghits / (ghits + gmisses));

    TestFileHelper.cleanUpTestFiles();
}

From source file:org.apache.niolex.common.guava.GuavaHashing.java

/**
 * @param args/*  ww w  .j  a v  a2s  . c o m*/
 */
public static void main(String[] args) {
    // Common Hash
    HashFunction hf = Hashing.goodFastHash(32);
    HashCode code = hf.hashObject(new Person(1, "Jiyun", "Xie", 1984), PersonFunnel.INSTANCE);
    System.out.println("Code1 => " + code.asInt());
    code = hf.hashObject(new Person(1, "Jiyun", "Xie", 1985), PersonFunnel.INSTANCE);
    System.out.println("Code2 => " + code.asInt());
    // Consistent Hashing
    HashFunction hf2 = Hashing.goodFastHash(64);
    code = hf2.hashObject(new Person(1, "Jiyun", "Xie", 1984), PersonFunnel.INSTANCE);
    System.out.println("Code3 => " + code.asLong());
    long hash = code.asLong();
    int bucket = Hashing.consistentHash(code, 100);
    System.out.println("Bucket1 => " + bucket);
    bucket = Hashing.consistentHash(hash, 101);
    System.out.println("Bucket2 => " + bucket);
    for (int i = 0; i < 10; ++i) {
        System.out.println("HashTo5 => " + Hashing.consistentHash(i, 5));
        System.out.println("HashTo6 => " + Hashing.consistentHash(i, 6));
    }
    // BloomFilter
    BloomFilter<Person> friends = BloomFilter.create(PersonFunnel.INSTANCE, 500, 0.02);
    for (int i = 0; i < 500; ++i) {
        friends.put(new Person(i, "Jiyun", "Xie", 1984 + i));
    }
    int k = 0;
    for (int i = 0; i < 500; ++i) {
        if (!friends.mightContain(new Person(i, "Jiyun", "Xie", 1984 + i))) {
            System.out.println("Error1 => " + i);
            ++k;
        }
    }
    System.out.println("fnp => (should be 0)" + ((double) k / 500));
    k = 0;
    for (int i = 0; i < 1000; i += 2) {
        if (friends.mightContain(new Person(i, "Jiyun", "Xie", 1984 + i))) {
            //System.out.println("Error2 => " + i);
            ++k;
        }
    }
    System.out.println("fpp => " + ((double) k / 500));
}

From source file:fr.tse.fi2.hpp.labs.queries.impl.lab5.BloomFilterGuava.java

public BloomFilterGuava(QueryProcessorMeasure measure) {
    super(measure);
    rec = BloomFilter.create(recFunnel, 1000, 0.001);
    // TODO Auto-generated constructor stub
}

From source file:fr.tse.fi2.hpp.labs.queries.impl.lab4.BloomGuava.java

public BloomGuava(QueryProcessorMeasure measure) {
    super(measure);
    rec = BloomFilter.create(recFunnel, 1000, 0.001);
    // TODO Auto-generated constructor stub
}

From source file:uk.co.smr.slf4j.logonce.strategy.BloomFilterStrategy.java

/**
 * @see BloomFilter//from w w  w.j  ava  2 s  . com
 * 
 * @param expectedInsertions the number of expected insertions to the constructed
 * @param fpp false positive probability must be positive, decimal fraction, eg. 0.03 for 3%
 */
public BloomFilterStrategy(int expectedInsertions, double fpp) {
    filter = BloomFilter.create(Funnels.stringFunnel(Charset.defaultCharset()), expectedInsertions, fpp);
}

From source file:uk.co.smr.slf4j.logonce.strategy.ThreadSafeBloomFilterStrategy.java

/**
 * @see BloomFilter/*w  ww .j ava2  s  .co m*/
 * 
 * @param expectedInsertions the number of expected insertions to the constructed
 * @param fpp false positive probability must be positive, decimal fraction, eg. 0.03 for 3%
 */
public ThreadSafeBloomFilterStrategy(int expectedInsertions, double fpp) {
    filter = BloomFilter.create(Funnels.stringFunnel(Charset.defaultCharset()), expectedInsertions, fpp);
}

From source file:cc.gospy.core.util.bloomfilter.ScalableBloomFilter.java

private BloomFilter<Task> grow() {
    this.activateBloomFilter = BloomFilter.create(Task.DIGEST, expectedInsertions, fpp);
    this.bloomFilters.add(activateBloomFilter);
    return activateBloomFilter;
}

From source file:com.spider.webmagic.scheduler.WechatDBDuplicateRemover.java

protected BloomFilter<CharSequence> rebuildBloomFilter() {
    counter = new AtomicInteger(0);
    return BloomFilter.create(Funnels.stringFunnel(Charset.defaultCharset()), expectedInsertions, fpp);
}

From source file:com.github.benmanes.caffeine.cache.simulator.membership.bloom.GuavaBloomFilter.java

private BloomFilter<Long> makeBloomFilter() {
    return BloomFilter.create(Funnels.longFunnel(), expectedInsertions, fpp);
}

From source file:com.conversantmedia.mapreduce.output.AbstractBloomFilter.java

/**
 * In order to support serialization the funnel must be serializable.
 * @param expectedInsertions   number of insertions expected
 * @param falsePossitiveRate   the desired false positive probability (must be positive and
 *        less than 1.0)/*from   www.j  a  va 2 s .com*/
 * @param funnel            the funnel of T's that the constructed {@code BloomFilter<T>} will use
 * @see com.google.common.hash.BloomFilter#create(Funnel, int, double)
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
public void init(final int expectedInsertions, final double falsePossitiveRate, final Funnel funnel) {
    assert funnel instanceof java.io.Serializable;
    filter = BloomFilter.create(funnel, expectedInsertions, falsePossitiveRate);
}