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

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

Introduction

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

Prototype

public boolean put(T object) 

Source Link

Document

Puts an element into this BloomFilter .

Usage

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

/**
 * @param args/*from w w w .j a v a 2s.com*/
 */
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: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   www.j a v  a 2s  . c  o  m*/
        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:com.edduarte.vokter.diff.DifferenceMatcher.java

@Override
public Set<DifferenceMatcher.Result> call() {
    Stopwatch sw = Stopwatch.createStarted();

    Set<Result> matchedDiffs = new ConcurrentHashSet<>();

    DifferenceEvent lastAction = DifferenceEvent.nothing;
    BloomFilter<String> lastBloomFilter = null;
    for (Difference r : differences) {
        if (lastAction == DifferenceEvent.nothing || r.getAction() != lastAction) {
            // reset the bloom filter being used
            lastBloomFilter = BloomFilter.create((from, into) -> into.putUnencodedChars(from), 10);
            lastAction = r.getAction();//from  ww w  .j  a va  2 s  . c om
        }
        BloomFilter<String> bloomFilter = lastBloomFilter;
        bloomFilter.put(r.getOccurrenceText());

        // check if AT LEAST ONE of the keywords has ALL of its words
        // contained in the diff text
        keywords.parallelStream().unordered().filter(kw -> kw.textStream().allMatch(bloomFilter::mightContain))
                .map(kw -> new Pair<>(r, kw))
                .filter((pair) -> pair.b().textStream().anyMatch(pair.a().getOccurrenceText()::equals))
                .map((pair) -> {
                    Difference diff = pair.a();
                    Keyword keyword = pair.b();
                    DifferenceEvent i = diff.getAction();
                    if (i == DifferenceEvent.inserted && !ignoreAdded) {
                        return new Result(diff.getAction(), keyword, diff.getSnippet());

                    } else if (i == DifferenceEvent.deleted && !ignoreRemoved) {
                        return new Result(diff.getAction(), keyword, diff.getSnippet());
                    }
                    return null;
                }).filter(diff -> diff != null).forEach(matchedDiffs::add);
    }

    sw.stop();
    logger.info("Completed difference matching for keywords '{}' in {}", keywords.toString(), sw.toString());
    return matchedDiffs;
}

From source file:com.edduarte.argus.diff.DifferenceMatcher.java

@Override
public Set<DifferenceMatcher.Result> call() {
    Stopwatch sw = Stopwatch.createStarted();

    Set<Result> matchedDiffs = new ConcurrentHashSet<>();

    DifferenceAction lastAction = DifferenceAction.nothing;
    BloomFilter<String> lastBloomFilter = null;
    for (Difference r : differences) {
        if (lastAction == DifferenceAction.nothing || r.getAction() != lastAction) {
            // reset the bloom filter being used
            lastBloomFilter = BloomFilter.create((from, into) -> into.putUnencodedChars(from), 10);
            lastAction = r.getAction();// w w  w .  j a v a 2 s  .  c om
        }
        BloomFilter<String> bloomFilter = lastBloomFilter;
        bloomFilter.put(r.getOccurrenceText());

        // check if AT LEAST ONE of the keywords has ALL of its words
        // contained in the diff text
        keywords.parallelStream().unordered().filter(kw -> kw.textStream().allMatch(bloomFilter::mightContain))
                .map(kw -> new Pair<>(r, kw))
                .filter((pair) -> pair.b().textStream().anyMatch(pair.a().getOccurrenceText()::equals))
                .map((pair) -> {
                    Difference diff = pair.a();
                    Keyword keyword = pair.b();
                    DifferenceAction i = diff.getAction();
                    if (i == DifferenceAction.inserted && !ignoreAdded) {
                        return new Result(diff.getAction(), keyword, diff.getSnippet());

                    } else if (i == DifferenceAction.deleted && !ignoreRemoved) {
                        return new Result(diff.getAction(), keyword, diff.getSnippet());
                    }
                    return null;
                }).filter(diff -> diff != null).forEach(matchedDiffs::add);
    }

    sw.stop();
    logger.info("Completed difference matching for keywords '{}' in {}", keywords.toString(), sw.toString());
    return matchedDiffs;
}

From source file:graphene.ingest.batchoptimizers.BasicBatchOptimizer.java

/**
 * Checks to see if the relationship is unique, and if it is, adds it to the
 * bloom filter.//from w w w  .j a v  a2s  .c om
 * 
 * @param from
 * @param to
 * @param rel
 * @return
 */
public boolean is_unique_rel(BloomFilter<String> bf, Long from, String rel, Long to) {
    String key = "" + from + to + rel;
    if (bf.mightContain(key)) {
        return false;
    } else {
        bf.put(key);
        return true;
    }
}

From source file:etri.sdn.controller.module.routing.Route.java

/**
 * This method returns a bloom filter (set) that contains all node-port tuple 
 * inside the route. This bloom filter is used to test a link is within the 
 * route very fast.//  ww w.j a v a2  s . co m
 * 
 * @return BloomFilter<NodePortTuple>
 */
public BloomFilter<NodePortTuple> getBloomFilter() {
    BloomFilter<NodePortTuple> ret = BloomFilter.create(RouteFunnel.INSTANCE, 20);
    for (NodePortTuple npt : this.switchPorts) {
        ret.put(npt);
    }
    return ret;
}