int test_counting_remove_reopen(const char *bloom_file, const char *words_file) { FILE *fp; char word[256]; counting_bloom_t *bloom; int i, key_removed; struct stats results = { 0 }; printf("\n* test counting remove & reopen\n"); if ((fp = fopen(bloom_file, "r"))) { fclose(fp); remove(bloom_file); } if (!(bloom = new_counting_bloom(CAPACITY, ERROR_RATE, bloom_file))) { fprintf(stderr, "ERROR: Could not create bloom filter\n"); return TEST_FAIL; } if (!(fp = fopen(words_file, "r"))) { fprintf(stderr, "ERROR: Could not open words file\n"); return TEST_FAIL; } for (i = 0; fgets(word, sizeof(word), fp) && (i < CAPACITY); i++) { chomp_line(word); counting_bloom_add(bloom, word, strlen(word)); } fseek(fp, 0, SEEK_SET); for (i = 0; fgets(word, sizeof(word), fp) && (i < CAPACITY); i++) { if (i % 5 == 0) { chomp_line(word); counting_bloom_remove(bloom, word, strlen(word)); } } free_counting_bloom(bloom); bloom = new_counting_bloom_from_file(CAPACITY, ERROR_RATE, bloom_file); fseek(fp, 0, SEEK_SET); for (i = 0; (fgets(word, sizeof(word), fp)) && (i < CAPACITY); i++) { chomp_line(word); key_removed = (i % 5 == 0); bloom_score(counting_bloom_check(bloom, word, strlen(word)), !key_removed, &results, word); } fclose(fp); printf("Elements added: %6d" "\n" "Elements removed: %6d" "\n" "Total size: %d KiB" "\n\n", i, i / 5, (int) bloom->num_bytes / 1024); free_counting_bloom(bloom); return print_results(&results); }
int free_scaling_bloom(scaling_bloom_t *bloom) { int i; for (i = bloom->num_blooms - 1; i >= 0; i--) { free_counting_bloom(*(bloom->blooms + i)); } free(bloom->blooms); free_bitmap(bloom->bitmap); free(bloom); return 0; }
counting_bloom_t *new_counting_bloom_from_file(unsigned int capacity, double error_rate, const char *filename) { int fd; off_t size; counting_bloom_t *bloom; if ((fd = open(filename, O_RDWR, (mode_t)0600)) < 0) { fprintf(stderr, "Error, Could not open file %s: %s\n", filename, strerror(errno)); return NULL; } if ((size = lseek(fd, 0, SEEK_END)) < 0) { perror("Error, calling lseek() to tell file size"); close(fd); return NULL; } if (size == 0) { fprintf(stderr, "Error, File size zero\n"); } bloom = counting_bloom_init(capacity, error_rate, 0); if (size != bloom->num_bytes) { free_counting_bloom(bloom); fprintf(stderr, "Error, Actual filesize and expected filesize are not equal\n"); return NULL; } if ((bloom->bitmap = new_bitmap(fd, size)) == NULL) { fprintf(stderr, "Error, Could not create bitmap with file\n"); free_counting_bloom(bloom); return NULL; } bloom->header = (counting_bloom_header_t *)(bloom->bitmap->array); return bloom; }