itzam_bool test_btree_thread(itzam_btree * btree, int maxkey, int test_size, int * completed)
{
    itzam_state state = ITZAM_FAILED;
    int32_t key;
    int i;

    if (btree != NULL)
    {
        for (i = 0; i < test_size; ++i)
        {
            key = (int32_t)random_int32((int32_t)maxkey);

            if (ITZAM_OKAY != itzam_btree_insert(btree,(const void *)&key))
            {
                // ignore error here, as the record may have been removed by another thread
                itzam_btree_remove(btree,(const void *)&key);
            }
        }

        state = ITZAM_OKAY;

        pthread_mutex_lock(&mutex);

        if (completed != NULL)
            (*completed)++;

        pthread_mutex_unlock(&mutex);
    }

    if (state == ITZAM_OKAY)
        return itzam_true;
    else
        return itzam_false;
}
Exemple #2
0
bool test_btree_insert() {
    itzam_btree  btree;
    itzam_state  state;
    record       rec;
    int n;
    char * filename      = "insert.itz";
    itzam_int test_size  = 10000000;
    int order            = 25;

    time_t start = time(NULL);

    memset(&rec.m_data,0,REC_SIZE * sizeof(uint32_t));
    rec.m_key = 0;

    // banner for this test
    printf("\nItzam/C B-Tree Test\nStraight Insertion Performance\n");
    printf("Please wait while I insert %d records of %d bytes...\n", (int)test_size, (int)(REC_SIZE * sizeof(uint32_t)));

    state = itzam_btree_create(&btree, filename, order, sizeof(record), compare_recs, error_handler);

    if (state != ITZAM_OKAY) {
        not_okay(state);
        return false;
    }

    for (n = 1; n <= test_size; ++ n)  {
        rec.m_key = n;
        rec.m_data[0] = n;
        rec.m_data[REC_SIZE - 1] = n;

        state = itzam_btree_insert(&btree,(const void *)&rec);

        if (state != ITZAM_OKAY) {
            not_okay(state);
            return false;
        }
    }

    time_t elapsed = time(NULL) - start;

    printf("      database  count: %d\n", (int)btree.m_header->m_count);
    printf("      database ticker: %d\n", (int)btree.m_header->m_ticker);
    printf("       total run time: %d seconds\n", (int)elapsed);
    printf("insertions per second: %f\n", ((double)test_size / (double)elapsed));

    // verify database after benchmark
    itzam_btree_cursor cursor;
    state = itzam_btree_cursor_create(&cursor, &btree);

    if (state == ITZAM_OKAY) {
        do {
            // get the key pointed to by the cursor
            state = itzam_btree_cursor_read(&cursor,(void *)&rec);

            if (state == ITZAM_OKAY) {
                if ((rec.m_key != rec.m_data[0]) || (rec.m_key != rec.m_data[REC_SIZE - 1])) {
                    printf("ERROR: record retrieved for %u does not match %u or %u\n",rec.m_key,rec.m_data[0],rec.m_data[REC_SIZE - 1]);
                    break;
                }
            }
            else
                not_okay(state);
        }
        while (itzam_btree_cursor_next(&cursor));

        state = itzam_btree_cursor_free(&cursor);

        if (state != ITZAM_OKAY)
            return false;
    }

    state = itzam_btree_close(&btree);

    if (state != ITZAM_OKAY) {
        not_okay(state);
        return false;
    }

    return true;
}
itzam_bool test_btree_befaft()
{
    itzam_bool result = false, bsuccess = false, asuccess = false;
    int n;
    const uint16_t order = 5; // small, so we get lots of tree changes
    time_t start;
    int32_t rec, before, after;
    
    char * filename = "relative.itz";
    
    itzam_btree btree;
    itzam_state state;
    
    // banner for this test
    printf("\nTesting Itzam/C b-tree indexes with new \"nearby\" search functions\n");
    
    // create an empty database file
    state = itzam_btree_create(&btree,
                               filename,
                               order,
                               sizeof(int32_t),
                               itzam_comparator_int32,
                               error_handler);
    
    if (state != ITZAM_OKAY)
    {
        printf("uable to create index file\n");
        return false;
    }
    
    // fill the btree with a known sequence
    int32_t largest = -1;
    int32_t x = -1;
    
    for (int n = 0; n < 100; ++n)
    {
        x += random_int32(3) + 1;
        largest = x;
        
        state = itzam_btree_insert(&btree, &x);
        
        if (state != ITZAM_OKAY)
        {
            printf("could not add %d to index file\n",n);
            return false;
        }
    }
    
    for (int n = -1; n < largest + 1; ++n)
    {
        before = -1;
        after = -1;
        
        // look before
        bsuccess = itzam_btree_find_before(&btree, &n, &before);
        
        // look after
        asuccess = itzam_btree_find_after(&btree, &n, &after);
        
        // print before (target) after
        printf("%8d (%s) <= %8d <= %8d (%s)\n", before, bsuccess ? "Y" : "N", n, after, asuccess ? "Y" : "N"); 
    }
    
    // done
    return itzam_true;
}
Exemple #4
0
bool test_btree_cursor(int testn) {
    bool result = false, bsuccess = false, asuccess = false;
    int n;
    const uint16_t order = 5; // small, so we get lots of tree changes
    time_t start;
    int32_t rec, before, after;
    
    char * filename = "relative.itz";
    
    itzam_btree btree;
    itzam_state state;

    if (testn < 0)
        testn = 50;
    
    // create an empty database file
    state = itzam_btree_create(&btree, filename, order, sizeof(int32_t), itzam_comparator_int32, error_handler);
    
    if (state != ITZAM_OKAY) {
        printf("uable to create index file\n");
        return false;
    }
    
    // fill the btree with a known sequence
    int32_t largest = -1;
    int32_t x = -1;
    
    for (int n = 0; n < testn; ++n) {
        x += random_int32(3) + 1;
        largest = x;
        
        state = itzam_btree_insert(&btree, &x);
        
        if (state != ITZAM_OKAY) {
            printf("could not add %d to index file\n",n);
            return false;
        }
    }
    
    itzam_btree_analyze(&btree, stdout);
    itzam_btree_dump_btree(&btree);

    itzam_btree_cursor cursor;

    // forward tests
    printf("\nforward tests\n");

    for (int target = 0; target < largest + 1; ++ target) {
        printf ("t = %3d: ", target);
        
        itzam_btree_cursor_create_at(&cursor, &btree, &target, false);
        
        do  {
            // get the key pointed to by the cursor
            state = itzam_btree_cursor_read(&cursor,(void *)&rec);
            
            if (state == ITZAM_OKAY) {
                printf("%d, ",rec);
                fflush(stdout);
            }
            else
                not_okay(state);
        }
        while (itzam_btree_cursor_next(&cursor));
        
        printf("\b\b\n");
        
        itzam_btree_cursor_free(&cursor);
    }

    // backward tests
    printf("\nbackward tests\n");

    for (int target = 0; target < largest + 1; ++target) {
        printf ("t = %3d: ", target);
        
        itzam_btree_cursor_create_at(&cursor, &btree, &target, false);
        
        do {
            // get the key pointed to by the cursor
            state = itzam_btree_cursor_read(&cursor,(void *)&rec);
            
            if (state == ITZAM_OKAY) {
                printf("%d, ",rec);
                fflush(stdout);
            }
            else
                not_okay(state);
        }
        while (itzam_btree_cursor_prev(&cursor));
        
        printf("\b\b\n");
        
        itzam_btree_cursor_free(&cursor);
    }
    
    // done
    return true;
}