test_dup_insert.c 7.29 KB
Newer Older
1 2 3
/* -*- mode: C; c-basic-offset: 4 -*- */
#ident "Copyright (c) 2007 Tokutek Inc.  All rights reserved."

Rich Prohaska's avatar
Rich Prohaska committed
4 5 6 7 8 9
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
#include <sys/stat.h>
10
#include <toku_portability.h>
Rich Prohaska's avatar
Rich Prohaska committed
11 12 13 14
#include <db.h>

#include "test.h"

15 16
static void
db_put (DB *db, int k, int v) {
17 18 19 20 21
    DB_TXN * const null_txn = 0;
    DBT key, val;
    int r = db->put(db, null_txn, dbt_init(&key, &k, sizeof k), dbt_init(&val, &v, sizeof v), DB_YESOVERWRITE);
    assert(r == 0);
}
Rich Prohaska's avatar
Rich Prohaska committed
22

23 24
static void
expect (DBC *cursor, int k, int v) {
Rich Prohaska's avatar
Rich Prohaska committed
25 26 27 28 29 30 31 32 33
    DBT key, val;
    int r = cursor->c_get(cursor, dbt_init_malloc(&key), dbt_init_malloc(&val), DB_NEXT);
    assert(r == 0);
    assert(key.size == sizeof k);
    int kk;
    memcpy(&kk, key.data, key.size);
    assert(val.size == sizeof v);
    int vv;
    memcpy(&vv, val.data, val.size);
34
    if (kk != k || vv != v) printf("expect key %u got %u - %u %u\n", (uint32_t)htonl(k), (uint32_t)htonl(kk), (uint32_t)htonl(v), (uint32_t)htonl(vv));
Rich Prohaska's avatar
Rich Prohaska committed
35 36 37 38 39 40 41
    assert(kk == k);
    assert(vv == v);

    free(key.data);
    free(val.data);
}

42 43 44 45
static int mycmp(const void *a, const void *b) {
    return memcmp(a, b, sizeof (int));
}

Rich Prohaska's avatar
Rich Prohaska committed
46
/* verify that key insertions are stored in insert order */
47 48
static void
test_insert (int n, int dup_mode) {
Rich Prohaska's avatar
Rich Prohaska committed
49 50 51 52 53
    if (verbose) printf("test_insert:%d %d\n", n, dup_mode);

    DB_ENV * const null_env = 0;
    DB *db;
    DB_TXN * const null_txn = 0;
54
    const char * const fname = ENVDIR "/" "test_insert.brt";
Rich Prohaska's avatar
Rich Prohaska committed
55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71
    int r;
    int i;

    unlink(fname);

    /* create the dup database file */
    r = db_create(&db, null_env, 0);
    assert(r == 0);
    r = db->set_flags(db, dup_mode);
    assert(r == 0);
    r = db->set_pagesize(db, 4096);
    assert(r == 0);
    r = db->open(db, null_txn, fname, "main", DB_BTREE, DB_CREATE, 0666);
    assert(r == 0);

    int values[n];
    for (i=0; i<n; i++)
72
        values[i] = htonl((i << 16) + (random() & 0xffff));
Rich Prohaska's avatar
Rich Prohaska committed
73 74 75 76
    int sortvalues[n];
    for (i=0; i<n; i++)
        sortvalues[i] = values[i];
    qsort(sortvalues, n, sizeof sortvalues[0], mycmp);
77

Rich Prohaska's avatar
Rich Prohaska committed
78 79 80 81 82 83
    /* insert n-1 unique keys {0, 1,  n-1} - {n/2} */
    for (i=0; i<n; i++) {
        if (i == n/2)
            continue;
        int k = htonl(i);
        int v = values[i];
84
        db_put(db, k, v);
Rich Prohaska's avatar
Rich Prohaska committed
85 86 87 88 89 90
    }

    /* insert n duplicates */
    for (i=0; i<n; i++) {
        int k = htonl(n/2);
        int v = values[i];
91
        db_put(db, k, v);
Rich Prohaska's avatar
Rich Prohaska committed
92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    } 

    /* verify lookups */
    for (i=0; i<n; i++) {
        int k = htonl(i);
        DBT key, val;
        r = db->get(db, null_txn, dbt_init(&key, &k, sizeof k), dbt_init_malloc(&val), 0);
        assert(r == 0);
        int vv;
        assert(val.size == sizeof vv);
        memcpy(&vv, val.data, val.size);
        if (i == n/2) {
            if (dup_mode & DB_DUPSORT)
                assert(vv == sortvalues[0]);
            else if (dup_mode & DB_DUP)
                assert(vv == values[0]);
            else
                assert(vv == values[n-1]);
        } else
            assert(vv == values[i]);
        free(val.data);
    }

    /* verify the sort order with a cursor */
    DBC *cursor;
    r = db->cursor(db, null_txn, &cursor, 0);
    assert(r == 0);

    for (i=0; i<n/2; i++)
        expect(cursor, htonl(i), values[i]);

    if (dup_mode & DB_DUPSORT) {
        for (i=0; i<n; i++) 
            expect(cursor, htonl(n/2), sortvalues[i]);
    } else if (dup_mode & DB_DUP) {
        for (i=0; i<n; i++)
            expect(cursor, htonl(n/2), values[i]);
    } else {
        expect(cursor, htonl(n/2), values[n-1]);
    }

    for (i=(n/2)+1; i<n; i++)
        expect(cursor, htonl(i), values[i]);

    r = cursor->c_close(cursor);
    assert(r == 0);

    r = db->close(db, 0);
    assert(r == 0);
}

/* verify dup keys are buffered in order in non-leaf nodes */
144 145
static void
test_nonleaf_insert (int n, int dup_mode) {
Rich Prohaska's avatar
Rich Prohaska committed
146 147 148 149 150
    if (verbose) printf("test_nonleaf_insert:%d %d\n", n, dup_mode);

    DB_ENV * const null_env = 0;
    DB *db;
    DB_TXN * const null_txn = 0;
151
    const char * const fname = ENVDIR "/" "test_nonleaf_insert.brt";
Rich Prohaska's avatar
Rich Prohaska committed
152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    int r;
    int i;

    unlink(fname);

    /* create the dup database file */
    r = db_create(&db, null_env, 0);
    assert(r == 0);
    r = db->set_flags(db, dup_mode);
    assert(r == 0);
    r = db->set_pagesize(db, 4096);
    assert(r == 0);
    r = db->open(db, null_txn, fname, "main", DB_BTREE, DB_CREATE, 0666);
    assert(r == 0);

    int values[n];
    for (i=0; i<n; i++)
169
        values[i] = htonl((i << 16) + (random() & 0xffff));
Rich Prohaska's avatar
Rich Prohaska committed
170 171 172 173 174 175 176 177 178 179 180
    int sortvalues[n];
    for (i=0; i<n; i++)
        sortvalues[i] = values[i];
    qsort(sortvalues, n, sizeof sortvalues[0], mycmp);

    /* insert n-1 unique keys {0, 1,  n-1} - {n/2} */
    for (i=0; i<n; i++) {
        if (i == n/2)
            continue;
        int k = htonl(i);
        int v = values[i];
181
        db_put(db, k, v);
Rich Prohaska's avatar
Rich Prohaska committed
182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199
    }

    /* reopen the database to force nonleaf buffering */
    r = db->close(db, 0);
    assert(r == 0);
    r = db_create(&db, null_env, 0);
    assert(r == 0);
    r = db->set_flags(db, dup_mode);
    assert(r == 0);
    r = db->set_pagesize(db, 4096);
    assert(r == 0);
    r = db->open(db, null_txn, fname, "main", DB_BTREE, 0, 0666);
    assert(r == 0);

    /* insert n duplicates */
    for (i=0; i<n; i++) {
        int k = htonl(n/2);
        int v = values[i];
200
        db_put(db, k, v);
Rich Prohaska's avatar
Rich Prohaska committed
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256
    } 

   /* verify lookups */
    for (i=0; i<n; i++) {
        int k = htonl(i);
        DBT key, val;
        r = db->get(db, null_txn, dbt_init(&key, &k, sizeof k), dbt_init_malloc(&val), 0);
        assert(r == 0);
        int vv;
        assert(val.size == sizeof vv);
        memcpy(&vv, val.data, val.size);
        if (i == n/2) {
            if (dup_mode & DB_DUPSORT)
                assert(vv == sortvalues[0]);
            else if (dup_mode & DB_DUP)
                assert(vv == values[0]);
            else
                assert(vv == values[n-1]);
        } else
            assert(vv == values[i]);
        free(val.data);
    }

    /* verify the sort order with a cursor */
    DBC *cursor;
    r = db->cursor(db, null_txn, &cursor, 0);
    assert(r == 0);

    for (i=0; i<n/2; i++)
        expect(cursor, htonl(i), values[i]);

    if (dup_mode & DB_DUPSORT) {
        for (i=0; i<n; i++) 
            expect(cursor, htonl(n/2), sortvalues[i]);
    } else if (dup_mode & DB_DUP) {
        for (i=0; i<n; i++)
            expect(cursor, htonl(n/2), values[i]);
    } else {
        expect(cursor, htonl(n/2), values[n-1]);
    }

    for (i=(n/2)+1; i<n; i++)
        expect(cursor, htonl(i), values[i]);

    r = cursor->c_close(cursor);
    assert(r == 0);

    r = db->close(db, 0);
    assert(r == 0);
}

int main(int argc, const char *argv[]) {
    int i;

    parse_args(argc, argv);
  
257
    system("rm -rf " ENVDIR);
258
    toku_os_mkdir(ENVDIR, S_IRWXU+S_IRWXG+S_IRWXO);
Rich Prohaska's avatar
Rich Prohaska committed
259 260 261 262 263 264 265 266
    
    /* nodup tests */
    for (i = 1; i <= (1<<16); i *= 2) {
        test_insert(i, 0);
        test_nonleaf_insert(i, 0);
    }

    /* dup tests */
267 268 269 270 271 272 273
    if (IS_TDB) {
	//    printf("%s:%d:WARNING:tokudb does not support DB_DUP\n", __FILE__, __LINE__);
    } else {
	for (i = 1; i <= (1<<16); i *= 2) {
	    test_insert(i, DB_DUP);
	    test_nonleaf_insert(i, DB_DUP);
	}
Rich Prohaska's avatar
Rich Prohaska committed
274 275 276 277 278 279 280 281 282 283
    }

    /* dupsort tests */
    for (i = 1; i <= (1<<16); i *= 2) {
        test_insert(i, DB_DUP + DB_DUPSORT);
        test_nonleaf_insert(i, DB_DUP + DB_DUPSORT);
    }

    return 0;
}