Ejemplo n.º 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);
}
Ejemplo n.º 2
0
static ERL_NIF_TERM elibart_new(ErlNifEnv* env, int argc,
                                   const ERL_NIF_TERM argv[])
{
    art_tree* t = enif_alloc_resource(elibart_RESOURCE,
                                                    sizeof(art_tree));
    
    if (init_art_tree(t) != 0)
        return mk_error(env, "init_art_tree");
    else 
    {
        ERL_NIF_TERM res = enif_make_resource(env, t);
        enif_release_resource(t);
    
        return enif_make_tuple2(env, mk_atom(env, "ok"), res);
    }
}
Ejemplo n.º 3
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;
}
Ejemplo n.º 4
0
END_TEST

START_TEST(test_art_insert_search)
{
    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;
    while (fgets(buf, sizeof buf, f)) {
        len = strlen(buf);
        buf[len-1] = '\0';
        fail_unless(NULL ==
            art_insert(&t, buf, len, (void*)line));
        line++;
    }

    // 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';

        uintptr_t val = (uintptr_t)art_search(&t, buf, len);
	fail_unless(line == val, "Line: %d Val: %" PRIuPTR " Str: %s\n", line,
	    val, buf);
        line++;
    }

    // Check the minimum
    art_leaf *l = art_minimum(&t);
    fail_unless(l && strcmp((char*)l->key, "A") == 0);

    // Check the maximum
    l = art_maximum(&t);
    fail_unless(l && strcmp((char*)l->key, "zythum") == 0);

    res = destroy_art_tree(&t);
    fail_unless(res == 0);
}
Ejemplo n.º 5
0
END_TEST

START_TEST(test_art_insert)
{
    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;
    while (fgets(buf, sizeof buf, f)) {
        len = strlen(buf);
        buf[len-1] = '\0';
        fail_unless(NULL == art_insert(&t, buf, len, (void*)line));
        fail_unless(art_size(&t) == line);
        line++;
    }

    res = destroy_art_tree(&t);
    fail_unless(res == 0);
}