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

#include <stdio.h>
#include <stdlib.h>
7

8 9 10
#include <unistd.h>
#include <errno.h>
#include <sys/stat.h>
11
#include <memory.h>
12 13
#include <db.h>

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

22 23
static void
test_cursor_current (void) {
Rich Prohaska's avatar
Rich Prohaska committed
24
    if (verbose) printf("test_cursor_current\n");
25 26

    DB_TXN * const null_txn = 0;
27
    const char * const fname = "test.cursor.current.brt";
28 29
    int r;

30 31 32 33 34 35
    DB_ENV *env;
    r = db_env_create(&env, 0); assert(r == 0);
    r = env->open(env, ENVDIR, DB_CREATE+DB_PRIVATE+DB_INIT_MPOOL, 0); assert(r == 0);

    DB *db;
    r = db_create(&db, env, 0); CKERR(r);
36
    db->set_errfile(db,0); // Turn off those annoying errors
37
    r = db->open(db, null_txn, fname, "main", DB_BTREE, DB_CREATE, 0666); CKERR(r);
Rich Prohaska's avatar
Rich Prohaska committed
38 39

    int k = 42, v = 42000;
40
    db_put(db, k, v);
Rich Prohaska's avatar
Rich Prohaska committed
41 42
    db_put(db, 43, 2000);
 
43 44
    DBC *cursor;

45
    r = db->cursor(db, null_txn, &cursor, 0); CKERR(r);
46 47 48

    DBT key, data; int kk, vv;

49 50 51
    r = cursor->c_del(cursor, 0);
    assert(r == EINVAL);

52 53 54 55
    r = cursor->c_get(cursor, dbt_init_malloc(&key), dbt_init_malloc(&data), DB_CURRENT);
    assert(r == EINVAL);

    r = cursor->c_get(cursor, dbt_init_malloc(&key), dbt_init_malloc(&data), DB_FIRST);
56
    CKERR(r);
57 58
    assert(key.size == sizeof kk);
    memcpy(&kk, key.data, sizeof kk);
Rich Prohaska's avatar
Rich Prohaska committed
59
    assert(kk == k);
60 61
    assert(data.size == sizeof vv);
    memcpy(&vv, data.data, data.size);
Rich Prohaska's avatar
Rich Prohaska committed
62
    assert(vv == v);
63
    toku_free(key.data); toku_free(data.data);
64 65

    r = cursor->c_get(cursor, dbt_init_malloc(&key), dbt_init_malloc(&data), DB_CURRENT);
66
    CKERR(r);
67 68
    assert(key.size == sizeof kk);
    memcpy(&kk, key.data, sizeof kk);
Rich Prohaska's avatar
Rich Prohaska committed
69
    assert(kk == k);
70 71
    assert(data.size == sizeof vv);
    memcpy(&vv, data.data, data.size);
Rich Prohaska's avatar
Rich Prohaska committed
72
    assert(vv == v);
73
    toku_free(key.data); toku_free(data.data);
74

Rich Prohaska's avatar
Rich Prohaska committed
75
    r = cursor->c_del(cursor, 0); 
76
    CKERR(r);
Rich Prohaska's avatar
Rich Prohaska committed
77 78

    r = cursor->c_get(cursor, dbt_init_malloc(&key), dbt_init_malloc(&data), DB_CURRENT);
79
    CKERR2(r,DB_KEYEMPTY);
Rich Prohaska's avatar
Rich Prohaska committed
80

81
    r = cursor->c_del(cursor, 0); 
82
    CKERR2(r,DB_KEYEMPTY);
83

84
    r = cursor->c_get(cursor, dbt_init_malloc(&key), dbt_init_malloc(&data), DB_CURRENT);
85
    CKERR2(r,DB_KEYEMPTY);
86

87
    r = cursor->c_close(cursor); CKERR(r);
88

89
    r = db->close(db, 0); CKERR(r);
90
    r = env->close(env, 0); CKERR(r);
91 92
}

93 94
static void
db_get (DB *db, int k, int UU(v), int expectr) {
Rich Prohaska's avatar
Rich Prohaska committed
95 96 97 98 99
    DBT key, val;
    int r = db->get(db, 0, dbt_init(&key, &k, sizeof k), dbt_init_malloc(&val), 0);
    assert(r == expectr);
}

100 101
static void
test_reopen (void) {
Rich Prohaska's avatar
Rich Prohaska committed
102 103 104
    if (verbose) printf("test_reopen\n");

    DB_TXN * const null_txn = 0;
105
    const char * const fname = "test.cursor.current.brt";
Rich Prohaska's avatar
Rich Prohaska committed
106 107
    int r;

108 109 110 111 112 113
    DB_ENV *env;
    r = db_env_create(&env, 0); assert(r == 0);
    r = env->open(env, ENVDIR, DB_CREATE+DB_PRIVATE+DB_INIT_MPOOL, 0); assert(r == 0);

    DB *db;
    r = db_create(&db, env, 0); CKERR(r);
114
    db->set_errfile(db,0); // Turn off those annoying errors
115
    r = db->open(db, null_txn, fname, "main", DB_BTREE, 0, 0666); CKERR(r);
Rich Prohaska's avatar
Rich Prohaska committed
116 117 118

    db_get(db, 1, 1, DB_NOTFOUND);

119
    r = db->close(db, 0); CKERR(r);
120
    r = env->close(env, 0); CKERR(r);
Rich Prohaska's avatar
Rich Prohaska committed
121 122
}

Yoni Fogel's avatar
Yoni Fogel committed
123
int
124
test_main(int argc, char *const argv[]) {
125 126
    parse_args(argc, argv);
  
127 128 129
    int r;
    r = system("rm -rf " ENVDIR);
    CKERR(r);
130
    toku_os_mkdir(ENVDIR, S_IRWXU+S_IRWXG+S_IRWXO);
131

Rich Prohaska's avatar
Rich Prohaska committed
132
    test_cursor_current();
Rich Prohaska's avatar
Rich Prohaska committed
133
    test_reopen();
134 135 136

    return 0;
}