Beispiel #1
0
END_TEST

START_TEST(test_art_insert_copy_delete)
{
    art_tree t;
    int res = init_art_tree(&t);
    fail_unless(res == 0);

    int len;
    char buf[512];
    FILE *f = fopen("tests/words.txt", "r");

    uintptr_t line = 1, nlines;
    while (fgets(buf, sizeof buf, f)) {
        len = strlen(buf);
        buf[len-1] = '\0';
        fail_unless(NULL ==
            art_insert(&t, buf, len, (void*)line));
        line++;
    }

    nlines = line - 1;

    // Create a new tree
    art_tree t2;
    fail_unless(art_copy(&t2, &t) == 0);

    // Destroy the original
    res = destroy_art_tree(&t);
    fail_unless(res == 0);

    // Seek back to the start
    fseek(f, 0, SEEK_SET);

    // Search for each line
    line = 1;
    while (fgets(buf, sizeof buf, f)) {
        len = strlen(buf);
        buf[len-1] = '\0';

        // Search first, ensure all entries still
        // visible
        uintptr_t val = (uintptr_t)art_search(&t2, buf, len);
        fail_unless(line == val, "Line: %d Val: %" PRIuPTR " Str: %s\n", line,
            val, buf);

        // Delete, should get lineno back
        val = (uintptr_t)art_delete(&t2, buf, len);
        fail_unless(line == val, "Line: %d Val: %" PRIuPTR " Str: %s\n", line,
            val, buf);

        // Check the size
        fail_unless(art_size(&t2) == nlines - line);
        line++;
    }

    res = destroy_art_tree(&t2);
    fail_unless(res == 0);
}
Beispiel #2
0
/**
 * Initializer
 * @arg config The configuration
 * @arg vacuum Should vacuuming be enabled. True unless in a
 * test or embedded environment using filtmgr_vacuum()
 * @arg mgr Output, resulting manager.
 * @return 0 on success.
 */
int init_filter_manager(bloom_config *config, int vacuum, bloom_filtmgr **mgr) {
    // Allocate a new object
    bloom_filtmgr *m = *mgr = calloc(1, sizeof(bloom_filtmgr));

    // Copy the config
    m->config = config;

    // Initialize the locks
    pthread_mutex_init(&m->write_lock, NULL);
    INIT_BLOOM_SPIN(&m->clients_lock);
    INIT_BLOOM_SPIN(&m->pending_lock);

    // Allocate storage for the art trees
    art_tree *trees = calloc(2, sizeof(art_tree));
    m->filter_map = trees;
    m->alt_filter_map = trees+1;

    // Allocate the initial art tree
    int res = init_art_tree(m->filter_map);
    if (res) {
        syslog(LOG_ERR, "Failed to allocate filter map!");
        free(m);
        return -1;
    }

    // Discover existing filters
    load_existing_filters(m);

    // Initialize the alternate map
    res = art_copy(m->alt_filter_map, m->filter_map);
    if (res) {
        syslog(LOG_ERR, "Failed to copy filter map to alternate!");
        destroy_filter_manager(m);
        return -1;
    }

    // Start the vacuum thread
    m->should_run = vacuum;
    if (vacuum && pthread_create(&m->vacuum_thread, NULL, filtmgr_thread_main, m)) {
        perror("Failed to start vacuum thread!");
        destroy_filter_manager(m);
        return 1;
    }

    // Done
    return 0;
}