Example #1
0
END_TEST

START_TEST(sbf_add_filter)
{
    bloom_sbf_params params = SBF_DEFAULT_PARAMS;
    params.initial_capacity = 1e3;
    params.fp_probability = 1e-4;
    bloom_sbf sbf;
    int res = sbf_from_filters(&params, NULL, NULL, 0, NULL, &sbf);
    fail_unless(res == 0);

    // Check all the keys get added
    char buf[100];
    for (int i=0;i<2000;i++) {
        snprintf((char*)&buf, 100, "foobar%d", i);
        res = sbf_add(&sbf, (char*)&buf);
        fail_unless(res == 1);
    }

    fail_unless(sbf_size(&sbf) == 2000);
    fail_unless(sbf.num_filters == 2);
    fail_unless(sbf_total_capacity(&sbf) == 5*1e3);

    // Check all the keys exist
    for (int i=0;i<2000;i++) {
        snprintf((char*)&buf, 100, "foobar%d", i);
        res = sbf_contains(&sbf, (char*)&buf);
        fail_unless(res == 1);
    }
}
Example #2
0
/**
 * Gets the maximum capacity of the filter
 * @note Thread safe.
 * @arg filter The filter to check
 * @return The total capacity of the filter
 */
uint64_t bloomf_capacity(bloom_filter *filter) {
    if (filter->sbf) {
        return sbf_total_capacity((bloom_sbf*)filter->sbf);
    } else {
        return filter->filter_config.capacity;
    }
}
Example #3
0
END_TEST

START_TEST(test_sbf_close_does_flush)
{
    bloom_sbf_params params = SBF_DEFAULT_PARAMS;
    params.initial_capacity = 1e3;
    params.fp_probability = 1e-4;

    nextfile next;
    next.format = "/tmp/mmap_close.%d.data";
    next.num = 0;

    bloom_sbf sbf;
    int res = sbf_from_filters(&params, sbf_make_callback, &next, 0, NULL, &sbf);
    fail_unless(res == 0);

    char buf[100];
    for (int i=0;i<2000;i++) {
        snprintf((char*)&buf, 100, "foobar%d", i);
        sbf_add(&sbf, (char*)&buf);
    }
    fail_unless(sbf_close(&sbf) == 0);

    bloom_bitmap maps[2];
    bitmap_from_filename("/tmp/mmap_close.0.data", get_size("/tmp/mmap_close.0.data"), 1, 1, SHARED, (bloom_bitmap*)&maps);
    bitmap_from_filename("/tmp/mmap_close.1.data", get_size("/tmp/mmap_close.1.data"), 1, 1, SHARED, ((bloom_bitmap*)&maps)+1);

    bloom_bloomfilter filters[2];
    bf_from_bitmap((bloom_bitmap*)&maps, 1, 0, (bloom_bloomfilter*)&filters);
    bf_from_bitmap(((bloom_bitmap*)&maps)+1, 1, 0, ((bloom_bloomfilter*)&filters)+1);

    bloom_bloomfilter **filter_map = calloc(2, sizeof(bloom_bloomfilter*));
    filter_map[0] = (bloom_bloomfilter*)&filters;
    filter_map[1] = ((bloom_bloomfilter*)&filters)+1;

    res = sbf_from_filters(&params, sbf_make_callback, &next, 2, filter_map, &sbf);
    fail_unless(res == 0);

    fail_unless(sbf_size(&sbf) == 2000);
    fail_unless(sbf_total_capacity(&sbf) == 5*1e3);

    for (int i=0;i<2000;i++) {
        snprintf((char*)&buf, 100, "foobar%d", i);
        res = sbf_contains(&sbf, (char*)&buf);
        fail_unless(res == 1);
    }

    unlink("/tmp/mmap_close.0.data");
    unlink("/tmp/mmap_close.1.data");
}
Example #4
0
END_TEST

START_TEST(sbf_add_filter_2)
{
    bloom_sbf_params params = SBF_DEFAULT_PARAMS;
    params.initial_capacity = 1e3;
    params.fp_probability = 1e-5;
    bloom_sbf sbf;
    int res = sbf_from_filters(&params, NULL, NULL, 0, NULL, &sbf);
    fail_unless(res == 0);

    // Check all the keys get added
    char buf[100];
    for (int i=0;i<10000;i++) {
        snprintf((char*)&buf, 100, "foobar%d", i);
        res = sbf_add(&sbf, (char*)&buf);
        fail_unless(res == 1);
    }

    fail_unless(sbf_size(&sbf) == 10000);
    fail_unless(sbf.num_filters == 3);
    fail_unless(sbf_total_capacity(&sbf) == 21*1e3);

    // Check all the keys exist
    for (int i=0;i<10000;i++) {
        snprintf((char*)&buf, 100, "foobar%d", i);
        res = sbf_contains(&sbf, (char*)&buf);
        fail_unless(res == 1);
    }

    // Byte size should be greater than a static filter of the same config
    bloom_filter_params config_params = {0, 0, 21e3, 1e-4};
    bf_params_for_capacity(&config_params);
    uint64_t total_size = sbf_total_byte_size(&sbf);
    fail_unless(total_size > config_params.bytes);
    fail_unless(total_size < 2 * config_params.bytes);
}