/** * Adds a new key to the bloom filter. * @arg filter The filter to add to * @arg key The key to add * @returns 1 if the key was added, 0 if present. Negative on failure. */ int bf_add(bloom_bloomfilter *filter, char* key) { // Allocate the hash space uint64_t *hashes = alloca(filter->header->k_num * sizeof(uint64_t)); // Compute the hashes bf_compute_hashes(filter->header->k_num, key, hashes); // Check if the item exists int res = bf_internal_contains(filter, hashes); if (res == 1) { return 0; // Key already present, do not add. } uint64_t m = filter->offset; uint64_t offset; uint64_t h; uint32_t i; uint64_t bit; for (i=0; i< filter->header->k_num; i++) { h = hashes[i]; // Get the hash value offset = 8*sizeof(bloom_filter_header) + i * m; // Get the partition offset bit = offset + (h % m); // Compute the bit offset bitmap_setbit(filter->map, bit); } filter->header->count += 1; return 1; }
END_TEST START_TEST(setbit_bitmap_anonymous_one_byte_aligned) { bloom_bitmap map; bitmap_from_file(-1, 4096, ANONYMOUS, &map); bitmap_setbit((&map), 8); fail_unless(map.mmap[1] == 128); }
END_TEST /* *#define BITMAP_SETBIT(map, idx, val) */ START_TEST(setbit_bitmap_anonymous_one_byte) { bloom_bitmap map; bitmap_from_file(-1, 4096, ANONYMOUS, &map); bitmap_setbit((&map), 1); fail_unless(map.mmap[0] == 64); }
END_TEST START_TEST(setbit_bitmap_anonymous_one) { bloom_bitmap map; bitmap_from_file(-1, 4096, ANONYMOUS, &map); for (int idx = 0; idx < 4096*8 ; idx++) { bitmap_setbit((&map), idx); } for (int idx = 0; idx < 4096; idx++) { fail_unless(map.mmap[idx] == 255); } }
END_TEST START_TEST(setbit_bitmap_file_one) { bloom_bitmap map; int res = bitmap_from_filename("/tmp/mmap_setbit_one", 4096, 1, 1, SHARED, &map); fail_unless(res == 0); for (int idx = 0; idx < 4096*8 ; idx++) { bitmap_setbit((&map), idx); } for (int idx = 0; idx < 4096; idx++) { fail_unless(map.mmap[idx] == 255); } unlink("/tmp/mmap_setbit_one"); }
END_TEST START_TEST(close_does_flush) { bloom_bitmap map; int res = bitmap_from_filename("/tmp/mmap_close_flush", 4096, 1, 1, SHARED, &map); fchmod(map.fileno, 0777); fail_unless(res == 0); for (int idx = 0; idx < 4096*8 ; idx++) { bitmap_setbit((&map), idx); } bitmap_close(&map); res = bitmap_from_filename("/tmp/mmap_close_flush", 4096, 0, 0, SHARED, &map); fail_unless(res == 0); for (int idx = 0; idx < 4096; idx++) { fail_unless(map.mmap[idx] == 255); } unlink("/tmp/mmap_close_flush"); }
END_TEST START_TEST(flush_does_write_persist) { bloom_bitmap map; int res = bitmap_from_filename("/tmp/persist_flush_write", 4096, 1, 1, PERSISTENT, &map); fchmod(map.fileno, 0777); fail_unless(res == 0); for (int idx = 0; idx < 4096*8 ; idx++) { bitmap_setbit((&map), idx); } bitmap_flush(&map); bloom_bitmap map2; res = bitmap_from_filename("/tmp/persist_flush_write", 4096, 0, 0, PERSISTENT, &map2); fail_unless(res == 0); for (int idx = 0; idx < 4096; idx++) { fail_unless(map2.mmap[idx] == 255); } unlink("/tmp/persist_flush_write"); }
int main(void) { bitmap_t bitmap = bitmap_allocate(6); if (bitmap == NULL) exit(EXIT_FAILURE); int i; for (i = 0; i < 3; i++) { bitmap_setbit(bitmap, i); } // Check up to 3 digits print_result(bitmap_alltrue(bitmap, 3)); // Check up all digits print_result(bitmap_alltrue(bitmap, 0)); bitmap_print(bitmap); bitmap_destroy(&bitmap); return (EXIT_SUCCESS); }
END_TEST /** * Test that flush does indeed write to disk */ START_TEST(flush_does_write) { bloom_bitmap map; int res = bitmap_from_filename("/tmp/mmap_flush_write", 4096, 1, 1, SHARED, &map); fchmod(map.fileno, 0777); fail_unless(res == 0); for (int idx = 0; idx < 4096*8 ; idx++) { bitmap_setbit((&map), idx); } bitmap_flush(&map); bloom_bitmap map2; res = bitmap_from_filename("/tmp/mmap_flush_write", 4096, 0, 0, SHARED, &map2); fail_unless(res == 0); for (int idx = 0; idx < 4096; idx++) { fail_unless(map2.mmap[idx] == 255); } unlink("/tmp/mmap_flush_write"); }