test_db_delete.c 3.57 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
#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include <unistd.h>
#include <string.h>
9
#include <errno.h>
Rich Prohaska's avatar
Rich Prohaska committed
10
#include <sys/stat.h>
11
#include <toku_portability.h>
Rich Prohaska's avatar
Rich Prohaska committed
12 13 14 15 16 17
#include <db.h>

#include "test.h"



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

25 26
static void
expect_db_del (DB *db, int k, int flags, int expectr) {
Rich Prohaska's avatar
Rich Prohaska committed
27
    DBT key;
28
    int r = db->del(db, 0, dbt_init(&key, &k, sizeof k), flags);
Rich Prohaska's avatar
Rich Prohaska committed
29 30 31
    assert(r == expectr);
}

32 33
static void
expect_db_get (DB *db, int k, int expectr) {
Rich Prohaska's avatar
Rich Prohaska committed
34 35 36 37 38
    DBT key, val;
    int r = db->get(db, 0, dbt_init(&key, &k, sizeof k), dbt_init_malloc(&val), 0);
    assert(r == expectr);
}

39 40
static void
test_db_delete (int n, int dup_mode) {
41
    if (verbose) printf("test_db_delete:%d %d\n", n, dup_mode);
Rich Prohaska's avatar
Rich Prohaska committed
42 43 44 45

    DB_ENV * const null_env = 0;
    DB *db;
    DB_TXN * const null_txn = 0;
46
    const char * const fname = ENVDIR "/" "test.db.delete.brt";
Rich Prohaska's avatar
Rich Prohaska committed
47 48
    int r;

49
    system("rm -rf " ENVDIR);
50
    r=toku_os_mkdir(ENVDIR, S_IRWXU+S_IRWXG+S_IRWXO); assert(r==0);
Rich Prohaska's avatar
Rich Prohaska committed
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

    /* 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);

    /* insert n/2 <i, i> pairs */
    int i;
    for (i=0; i<n/2; i++) 
        db_put(db, htonl(i), 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, 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/2 <i, i> pairs */
    for (i=n/2; i<n; i++) 
        db_put(db, htonl(i), i);

    for (i=0; i<n; i++) {
84
        expect_db_del(db, htonl(i), 0, 0);
Rich Prohaska's avatar
Rich Prohaska committed
85 86 87 88

        expect_db_get(db, htonl(i), DB_NOTFOUND);
    }

89
    expect_db_del(db, htonl(n), 0, DB_NOTFOUND);
90
#if defined(USE_TDB)
91 92
    expect_db_del(db, htonl(n), DB_DELETE_ANY, 0);
#endif
93 94
#if defined(USE_BDB) && defined(DB_DELETE_ANY)
 #if DB_DELETE_ANY == 0
95
    expect_db_del(db, htonl(n), DB_DELETE_ANY, DB_NOTFOUND);
96
 #else
97
    expect_db_del(db, htonl(n), DB_DELETE_ANY, EINVAL);
98
 #endif
99 100 101 102 103 104
#endif

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

105 106
static void
test_db_get_datasize0 (void) {
107 108 109 110 111
    if (verbose) printf("test_db_get_datasize0\n");

    DB_ENV * const null_env = 0;
    DB *db;
    DB_TXN * const null_txn = 0;
112
    const char * const fname = ENVDIR "/" "test.db_delete.brt";
113 114
    int r;

115
    system("rm -rf " ENVDIR);
116
    r=toku_os_mkdir(ENVDIR, S_IRWXU+S_IRWXG+S_IRWXO); assert(r==0);
117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132

    /* create the dup database file */
    r = db_create(&db, null_env, 0);
    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 k = 0;
    db_put(db, k, 0);

    DBT key, val; 
    r = db->get(db, 0, dbt_init(&key, &k, sizeof k), dbt_init_malloc(&val), 0);
    assert(r == 0);
    free(val.data);
Rich Prohaska's avatar
Rich Prohaska committed
133 134 135 136 137

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

Yoni Fogel's avatar
Yoni Fogel committed
138 139
int
test_main(int argc, const char *argv[]) {
Rich Prohaska's avatar
Rich Prohaska committed
140 141
    parse_args(argc, argv);

142 143
    test_db_get_datasize0();

Rich Prohaska's avatar
Rich Prohaska committed
144 145 146 147 148 149 150 151 152
    test_db_delete(0, 0);

    int i;
    for (i = 1; i <= (1<<16); i *= 2) {
        test_db_delete(i, 0);
    }

    return 0;
}