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

The following examples show how to use com.google.common.hash.BloomFilter#put() . 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: XpTrackerService.java    From runelite with BSD 2-Clause "Simplified" License 6 votes vote down vote up
private BloomFilter<String> createFilter()
{
	final BloomFilter<String> filter = BloomFilter.create(
		Funnels.stringFunnel(Charset.defaultCharset()),
		BLOOMFILTER_EXPECTED_INSERTIONS
	);

	synchronized (usernameUpdateQueue)
	{
		for (String toUpdate : usernameUpdateQueue)
		{
			filter.put(toUpdate);
		}
	}

	return filter;
}
 
Example 2
Source File: BloomFilterUnitTest.java    From tutorials with MIT License 6 votes vote down vote up
@Test
public void givenBloomFilter_whenAddNStringsToIt_thenShouldNotReturnAnyFalsePositive() {
    //when
    BloomFilter<Integer> filter = BloomFilter.create(
            Funnels.integerFunnel(),
            500,
            0.01);

    //when
    filter.put(1);
    filter.put(2);
    filter.put(3);

    //then
    // the probability that it returns true, but is actually false is 1%
    assertThat(filter.mightContain(1)).isTrue();
    assertThat(filter.mightContain(2)).isTrue();
    assertThat(filter.mightContain(3)).isTrue();

    assertThat(filter.mightContain(100)).isFalse();
}
 
Example 3
Source File: CompromisedPasswordClassifierTest.java    From quarantyne with Apache License 2.0 5 votes vote down vote up
@Test
public void testClassifier() {
  BloomFilter<String> bloom =
      BloomFilter.create(Funnels.stringFunnel(Charsets.UTF_8), 3);
  bloom.put("alpha");
  bloom.put("bravo");
  bloom.put("charlie");

  Supplier<Config> configSupplier = () -> Config.builder()
      .loginAction(new QIdentityAction("/login", "email", "password"))
      .registerAction(new QIdentityAction("/register", "email", "password"))
      .build();
  CompromisedPasswordClassifier classifier = new CompromisedPasswordClassifier(bloom, configSupplier);
  HttpRequest defaultRequest = TestHttpRequest.REQ();

  // null empty
  assertThat(classifier.classify(defaultRequest, null)).isEqualTo(Label.NONE);
  assertThat(classifier.classify(defaultRequest, TestHttpRequestBody.EMPTY)).isEqualTo(Label.NONE);

  // no key matches password
  assertThat(classifier.classify(defaultRequest,
      TestHttpRequestBody.make(new JsonObject().put("name", "john")))).isEqualTo(Label.NONE);

  // a key matches password but password is not in bloomf
  assertThat(classifier.classify(defaultRequest,
      TestHttpRequestBody.make(new JsonObject().put("password", "delta")))).isEqualTo(Label.NONE);

  // match
  HttpRequest requestOnPath = new TestHttpRequest.Builder().setPath("/login").build();
  assertThat(classifier.classify(requestOnPath,
      TestHttpRequestBody.make(new JsonObject().put("password", "bravo")))).isEqualTo(
          Label.COMPROMISED_PASSWORD);
}
 
Example 4
Source File: DisposableEmailClassifierTest.java    From quarantyne with Apache License 2.0 5 votes vote down vote up
@Test
public void testClassifier() {
  BloomFilter<String> bloom =
      BloomFilter.create(Funnels.stringFunnel(Charsets.UTF_8), 2);
  bloom.put("disposable.com");
  bloom.put("junk.com");

  Supplier<Config> configSupplier = () -> Config
      .builder()
      .emailParamKeys(Sets.newHashSet("email"))
      .registerAction(new QIdentityAction("/register", "email", "password"))
      .build();
  DisposableEmailClassifier classifier = new DisposableEmailClassifier(bloom, configSupplier);
  HttpRequest defaultRequest = TestHttpRequest.REQ();

  // null empty
  assertThat(classifier.classify(defaultRequest, null)).isEqualTo(Label.NONE);
  assertThat(classifier.classify(defaultRequest, TestHttpRequestBody.EMPTY)).isEqualTo(Label.NONE);

  // no key matches password
  assertThat(classifier.classify(defaultRequest,
      TestHttpRequestBody.make(new JsonObject().put("name", "john")))).isEqualTo(Label.NONE);

  // a key matches password but password is not in bloomf
  assertThat(classifier.classify(defaultRequest,
      TestHttpRequestBody.make(new JsonObject().put("email", "[email protected]")))).isEqualTo(Label.NONE);

  // match
  HttpRequest req = new TestHttpRequest.Builder().setPath("/register").build();
  assertThat(classifier.classify(req,
      TestHttpRequestBody.make(new JsonObject().put("email", "[email protected]")))).isEqualTo(
      Label.DISPOSABLE_EMAIL);
}
 
Example 5
Source File: ListsTest.java    From spring-boot-cookbook with Apache License 2.0 5 votes vote down vote up
/**
     * https://mp.weixin.qq.com/s/VGtH_DXI6paogOekrodixA
     */
    @Test
    public void testBloomFilterWithGuava() {
        StopWatch stopWatch = new StopWatch("guavaBloomFilterDemo");
        stopWatch.start("init");
        int num = 10000000;
        BloomFilter<Integer> bloomFilter = BloomFilter.create(Funnels.integerFunnel(), num, 0.01);
        for (int i = 0; i < num; i++) {
            bloomFilter.put(i);
        }
        stopWatch.stop();
        stopWatch.start("check-1");
        assertThat(bloomFilter.mightContain(1)).isTrue();
        stopWatch.stop();

        stopWatch.start("check-9999");
        assertThat(bloomFilter.mightContain(9999)).isTrue();
        stopWatch.stop();

        stopWatch.start("check-1234567");
        assertThat(bloomFilter.mightContain(1234567)).isTrue();
        stopWatch.stop();

        System.out.println(stopWatch.prettyPrint());

//        -----------------------------------------
//        ms     %     Task name
//        -----------------------------------------
//        04443  099%  init
//        00037  001%  check-1
//        00000  000%  check-9999
//        00000  000%  check-1234567

    }
 
Example 6
Source File: BloomFilterTest.java    From jdk-source-analysis with Apache License 2.0 5 votes vote down vote up
@Test
public void test() {
    BloomFilter<Integer> filter = BloomFilter.create(Funnels.integerFunnel(), 1500, 0.01);
    System.out.println(filter.mightContain(1));
    System.out.println(filter.mightContain(2));

    filter.put(1);
    filter.put(2);

    System.out.println(filter.mightContain(1));
    System.out.println(filter.mightContain(2));
}
 
Example 7
Source File: ClasspathPreloader.java    From activitystreams with Apache License 2.0 5 votes vote down vote up
private void load(
  Collection col, 
  Receiver<TypeValue> receiver, 
  BloomFilter<CharSequence> filter) {
  if (col != null && receiver != null)
    for (ASObject obj : col.items())
      if (obj.id() != null && (filter == null || filter.put(obj.id()))) {
        try {
          receiver.receive(obj);
        } catch (Throwable t) {}
      }
}
 
Example 8
Source File: TestBloomFiltersSpeed.java    From count-db with MIT License 5 votes vote down vote up
private static long putValues(BloomFilter<Long> bloomFilter2) {
    long start = System.currentTimeMillis();
    for (int i = 0; i < NUM_OF_VALUES; i++) {
        if (i % 3 == 0) {
            bloomFilter2.put((long) i);
        }
    }
    return System.currentTimeMillis() - start;
}
 
Example 9
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();
}