Java Code Examples for com.google.common.hash.BloomFilter#mightContain()

The following examples show how to use com.google.common.hash.BloomFilter#mightContain() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: TestBloom.java    From bidder with Apache License 2.0 6 votes vote down vote up
/**
 * Test a valid bid response.
 * @throws Exception on networking errors.
 */
@Test 
public void testBloom() throws Exception {
 
 
   new Bloom("$test","data/c1x_cookies.csv");
   BloomFilter b = (BloomFilter)LookingGlass.get("$test");
   assertNotNull(b);
   
   boolean p = b.mightContain("842AAB10FBA04247B3A9CE00C9172350");
   
   BufferedReader br = new BufferedReader(new FileReader("data/c1x_cookies.csv"));
   String line = null;
   int nP = 0;
   int k = 0;
   while((line = br.readLine()) != null) {
   	p = b.mightContain(line);
   	if (p)
   		nP++;
   	k++;
   }
   assertTrue(k == nP);
}
 
Example 2
Source File: TestBloom.java    From XRTB with Apache License 2.0 6 votes vote down vote up
/**
 * Test a valid bid response.
 * @throws Exception on networking errors.
 */
@Test 
public void testBloom() throws Exception {
 
 
   new Bloom("$test","data/c1x_cookies.csv");
   BloomFilter b = (BloomFilter)LookingGlass.get("$test");
   assertNotNull(b);
   
   boolean p = b.mightContain("842AAB10FBA04247B3A9CE00C9172350");
   
   BufferedReader br = new BufferedReader(new FileReader("data/c1x_cookies.csv"));
   String line = null;
   int nP = 0;
   int k = 0;
   while((line = br.readLine()) != null) {
   	p = b.mightContain(line);
   	if (p)
   		nP++;
   	k++;
   }
   assertTrue(k == nP);
}
 
Example 3
Source File: ScalableBloomFilter.java    From nexus-public with Eclipse Public License 1.0 5 votes vote down vote up
/**
 * Determines whether across all filters there is a chance that this element has already been added.
 *
 * @param input - the element to check.
 * @return whether the element may exist in the filter.
 */
public boolean mightContain(final T input) {
  for (BloomFilter<T> filter : filters) {
    if (filter.mightContain(input)) {
      return true;
    }
  }
  return false;
}
 
Example 4
Source File: TestBloomFiltersSpeed.java    From count-db with MIT License 5 votes vote down vote up
private static long readValues(BloomFilter<Long> bloomFilter2) {
    long start = System.currentTimeMillis();
    for (int i = 0; i < NUM_OF_VALUES; i++) {
        if (i % 3 == 0) {
            bloomFilter2.mightContain((long) i);
        }
    }
    return System.currentTimeMillis() - start;
}
 
Example 5
Source File: TableBloomFilterPerformance.java    From heftydb with Apache License 2.0 4 votes vote down vote up
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
        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();
}