Example #1
0
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);
}
Example #2
0
int scaling_bloom_check(scaling_bloom_t *bloom, const char *s, size_t len)
{
    int i;
    counting_bloom_t *cur_bloom;
    for (i = bloom->num_blooms - 1; i >= 0; i--) {
        cur_bloom = bloom->blooms[i];
        if (counting_bloom_check(cur_bloom, s, len)) {
            return 1;
        }
    }
    return 0;
}