scaling_bloom_t *new_scaling_bloom(unsigned int capacity, double error_rate, const char *filename) { scaling_bloom_t *bloom; counting_bloom_t *cur_bloom; int fd; if ((fd = open(filename, O_RDWR | O_CREAT | O_TRUNC, (mode_t)0600)) < 0) { perror("Error, Opening File Failed"); fprintf(stderr, " %s \n", filename); return NULL; } bloom = scaling_bloom_init(capacity, error_rate, filename, fd); if (!(cur_bloom = new_counting_bloom_from_scale(bloom))) { fprintf(stderr, "Error, Could not create counting bloom\n"); free_scaling_bloom(bloom); return NULL; } cur_bloom->header->count = 0; cur_bloom->header->id = 0; bloom->header->mem_seqnum = 1; return bloom; }
int test_scaling_accuracy(const char *bloom_file, const char *words_file) { FILE *fp; char word[256]; scaling_bloom_t *bloom; int i; struct stats results = { 0 }; printf("\n* test scaling accuracy\n"); if ((fp = fopen(bloom_file, "r"))) { fclose(fp); remove(bloom_file); } if (!(bloom = new_scaling_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++) { if (i % 2 == 0) { chomp_line(word); scaling_bloom_add(bloom, word, strlen(word), i); } } fseek(fp, 0, SEEK_SET); for (i = 0; fgets(word, sizeof(word), fp); i++) { if (i % 2 == 1) { chomp_line(word); bloom_score(scaling_bloom_check(bloom, word, strlen(word)), 0, &results, word); } } fclose(fp); printf("Elements added: %6d" "\n" "Elements checked: %6d" "\n" "Total size: %d KiB" "\n\n", (i + 1) / 2, i / 2, (int) bloom->num_bytes / 1024); free_scaling_bloom(bloom); return print_results(&results); }
scaling_bloom_t *new_scaling_bloom_from_file(unsigned int capacity, double error_rate, const char *filename) { int fd; off_t size; uint64_t id; unsigned int count, offset; scaling_bloom_t *bloom; counting_bloom_t *cur_bloom; if ((fd = open(filename, O_RDWR, (mode_t)0600)) < 0) { fprintf(stderr, "Error, Could not open file %s with open: \n", filename); perror(""); 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 = scaling_bloom_init(capacity, error_rate, filename, fd); offset = sizeof(scaling_bloom_header_t); size -= offset; while (size) { id = ((counting_bloom_header_t *)(bloom->bitmap->array + offset))->id; count = ((counting_bloom_header_t *)(bloom->bitmap->array + offset))->count; cur_bloom = new_counting_bloom_from_scale(bloom, id, count); size -= cur_bloom->num_bytes; offset += cur_bloom->num_bytes; if (size < 0) { free_scaling_bloom(bloom); fprintf(stderr, "Error, Actual filesize and expected filesize are not equal\n"); return NULL; } } return bloom; }
scaling_bloom_t *scaling_bloom_init(unsigned int capacity, double error_rate, const char *filename, int fd) { scaling_bloom_t *bloom; if ((bloom = (scaling_bloom_t *)malloc(sizeof(scaling_bloom_t))) == NULL) { return NULL; } if ((bloom->bitmap = new_bitmap(fd, sizeof(scaling_bloom_header_t))) == NULL) { fprintf(stderr, "Error, Could not create bitmap with file\n"); free_scaling_bloom(bloom); return NULL; } bloom->header = (scaling_bloom_header_t *) bloom->bitmap->array; bloom->capacity = capacity; bloom->error_rate = error_rate; bloom->num_blooms = 0; bloom->num_bytes = sizeof(scaling_bloom_header_t); bloom->fd = fd; bloom->blooms = NULL; return bloom; }
scaling_bloom_t *new_scaling_bloom_from_file(unsigned int capacity, double error_rate, const char *filename) { int fd; off_t size; scaling_bloom_t *bloom; counting_bloom_t *cur_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 = scaling_bloom_init(capacity, error_rate, filename, fd); size -= sizeof(scaling_bloom_header_t); while (size) { cur_bloom = new_counting_bloom_from_scale(bloom); // leave count and id as they were set in the file size -= cur_bloom->num_bytes; if (size < 0) { free_scaling_bloom(bloom); fprintf(stderr, "Error, Actual filesize and expected filesize are not equal\n"); return NULL; } } return bloom; }
static void Dablooms_dealloc(Dablooms *self) { free_scaling_bloom(self->filter); self->ob_type->tp_free((PyObject *)self); }
void Dablooms_free(DabloomStruct* self) { free_scaling_bloom(self->filter); free(self); }