test_dupsort_delete.c 2.93 KB
Newer Older
1
/* -*- mode: C; c-basic-offset: 4 -*- */
Yoni Fogel's avatar
Yoni Fogel committed
2
#include <toku_portability.h>
3 4 5 6 7 8
#ident "Copyright (c) 2007 Tokutek Inc.  All rights reserved."

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
9
#include <memory.h>
10
#include <sys/stat.h>
11
#include <toku_portability.h>
12 13 14 15
#include <db.h>

#include "test.h"

16 17
static void
db_put (DB *db, int k, int v) {
18 19 20 21 22 23
    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);
}

24 25
static void
db_del (DB *db, int k) {
26 27 28 29 30 31
    DB_TXN *const null_txn = 0;
    DBT key;
    int r = db->del(db, null_txn, dbt_init(&key, &k, sizeof k), DB_DELETE_ANY);
    assert(r == 0);
}

32 33
static void
expect_cursor_get (DBC *cursor, int op, int expectr) {
34 35 36 37 38
    DBT key, val;
    int r = cursor->c_get(cursor, dbt_init_malloc(&key), dbt_init_malloc(&val), op);
    assert(r == expectr);
}

39 40 41 42
static int mycmp(const void *a, const void *b) {
    return memcmp(a, b, sizeof (int));
}

43 44
static void
test_dupsort_delete (int n) {
45 46 47 48 49
    if (verbose) printf("test_dupsort_delete:%d\n", n);

    DB_ENV * const null_env = 0;
    DB *db;
    DB_TXN * const null_txn = 0;
50
    const char * const fname = ENVDIR "/" "test_dupsort_delete.brt";
51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    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, DB_DUP + DB_DUPSORT); 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++)
        values[i] = htonl((i << 16) + (random() & 0xffff));
    int sortvalues[n];
    for (i=0; i<n; i++)
        sortvalues[i] = values[i];
    qsort(sortvalues, n, sizeof sortvalues[0], mycmp);

    for (i=0; i<n; i++) {
        db_put(db, htonl(n), values[i]);
    }

    /* 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, DB_DUP + DB_DUPSORT); 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);

    db_del(db, htonl(n));

    for (i=0; i<n; i++) {
        db_put(db, htonl(0), values[i]);
    }

    db_del(db, htonl(0));

    DBC *cursor;
    r = db->cursor(db, null_txn, &cursor, 0); assert(r == 0);

    /* verify all gone */
    expect_cursor_get(cursor, DB_NEXT, DB_NOTFOUND); 

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

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

Yoni Fogel's avatar
Yoni Fogel committed
100 101
int
test_main(int argc, const char *argv[]) {
102 103 104 105
    int i;

    parse_args(argc, argv);
  
106
    system("rm -rf " ENVDIR);
107
    toku_os_mkdir(ENVDIR, S_IRWXU+S_IRWXG+S_IRWXO);
108 109 110 111 112 113 114 115 116
    //   test_dupsort_delete(256); return 0;
    
    /* nodup tests */
    for (i = 1; i <= (1<<16); i *= 2) {
        test_dupsort_delete(i);
    }

    return 0;
}