test938.c 4.41 KB
Newer Older
1
/* -*- mode: C; c-basic-offset: 4 -*- */
2
#include <toku_portability.h>
3 4 5 6
#include <db.h>
#include <sys/stat.h>
#include "test.h"

7
unsigned char N=5;
8 9 10

static int
fact(int n) {
11 12 13 14
    if (n<=2) return n;
    else return n*fact(n-1);
}

15 16
static void
swapc (unsigned char *a, unsigned char *b) {
17 18 19 20 21 22 23 24
    unsigned char tmp=*a;
    *a=*b;
    *b=tmp;
}

DB_ENV *env;
DB *db;

25 26
static void
run (int choice) {
27 28 29 30
    unsigned char v[N];
    int i;
    int r;
    for (i=0; i<N; i++) {
31
	v[i]=(unsigned char)(10*i);
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
    }
    for (i=0; i<N; i++) {
	int nchoices=N-i;
	swapc(&v[i], &v[i+choice%nchoices]);
	choice=choice/nchoices;
    }
    if (0) {
	for (i=0; i<N; i++) {
	    printf("%d ", v[i]);
	}

	printf("\n");
    }
    DB_TXN *txn;
    {
	r=env->txn_begin(env, 0, &txn, 0);                            CKERR(r);
48
	for (i=0; i<N; i++) {
49
	    DBT kdbt,vdbt;
50 51 52
	    char key=25;
	    char val=v[i];
	    //printf("put %d %d\n", key, val);
53 54 55 56 57 58 59 60 61
	    r=db->put(db, txn, dbt_init(&kdbt, &key, 1), dbt_init(&vdbt, &val, 1), DB_YESOVERWRITE); CKERR(r);
	}
	r=txn->commit(txn, DB_TXN_NOSYNC);                                        CKERR(r);
    }
    {
	r=env->txn_begin(env, 0, &txn, 0);                            CKERR(r);	
	DBC *c;
	r=db->cursor(db, txn, &c, 0);                                 CKERR(r);
	DBT kdbt,vdbt;
62 63
	memset(&kdbt, 0, sizeof(kdbt));
	memset(&vdbt, 0, sizeof(vdbt));
64 65
	i=0;
	while (0==(r=c->c_get(c, &kdbt, &vdbt, DB_NEXT))) {
66
	    //printf("Got %d %d\n", *(unsigned char*)kdbt.data, *(unsigned char*)vdbt.data);
67
	    i++;
68 69
	    kdbt.data=0;
	    vdbt.data=0;
70 71
	}
	CKERR2(r, DB_NOTFOUND);
72 73
	//printf("i=%d N=%d\n", i, N);
	assert(i==N);
74 75 76 77 78 79 80 81
	r=c->c_close(c);                                                          CKERR(r);
	r=txn->commit(txn, DB_TXN_NOSYNC);                                        CKERR(r);
    }
    {
	r=env->txn_begin(env, 0, &txn, 0);                            CKERR(r);
	DBC *c;
	r=db->cursor(db, txn, &c, 0);                                 CKERR(r);
	DBT kdbt,vdbt;
82 83
	memset(&kdbt, 0, sizeof(kdbt));
	memset(&vdbt, 0, sizeof(vdbt));
84 85 86 87 88 89 90 91 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
	i=0;
	while (0==(r=(c->c_get(c, &kdbt, &vdbt, DB_FIRST)))) {
	    i++;
	    r=c->c_del(c, 0);
	    CKERR(r);
	}
	assert(r==DB_NOTFOUND);
	r=c->c_close(c);                                                          CKERR(r);
	r=txn->commit(txn, DB_TXN_NOSYNC);                                        CKERR(r);
    }
    return;
#if 0
    char v101=101, v102=102, v1=1, v2=2;
    {
	r=env->txn_begin(env, 0, &txn, 0);                            CKERR(r);
	DBT k,v;
	r=db->put(db, txn, dbt_init(&k, &v1, 1), dbt_init(&v, &v101, 1), 0); CKERR(r);
	r=db->put(db, txn, dbt_init(&k, &v2, 1), dbt_init(&v, &v102, 1), 0); CKERR(r);
	r=txn->commit(txn, 0);                                        CKERR(r);
    }
    {
	r=env->txn_begin(env, 0, &txn, 0);                            CKERR(r);
	DBC *c;
	r=db->cursor(db, txn, &c, 0);                                 CKERR(r);
	DBT k,v;
	r=c->c_get(c, dbt_init_malloc(&k), dbt_init_malloc(&v), DB_FIRST); CKERR(r);
	assert(*(char*)k.data==v1); assert(*(char*)v.data==v101);
	r=c->c_get(c, dbt_init_malloc(&k), dbt_init_malloc(&v), DB_NEXT);  CKERR(r);
	assert(*(char*)k.data==v2); assert(*(char*)v.data==v102);
	r=c->c_get(c, dbt_init_malloc(&k), dbt_init_malloc(&v), DB_NEXT);  assert(r!=0);
	r=c->c_close(c);                                                   CKERR(r);
	r=txn->commit(txn, 0);                                             CKERR(r);
    }
#endif
}

Yoni Fogel's avatar
Yoni Fogel committed
120 121
int
test_main(int argc, const char *argv[]) {
122 123 124
    parse_args(argc, argv);

    system("rm -rf " ENVDIR);
125
    toku_os_mkdir(ENVDIR, S_IRWXU+S_IRWXG+S_IRWXO);
126 127 128 129 130 131

    int r;

    DB_TXN *txn;
    {
        r = db_env_create(&env, 0);                                   CKERR(r);
132
	r=env->open(env, ENVDIR, DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_MPOOL|DB_INIT_TXN|DB_CREATE|DB_PRIVATE, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
133 134 135 136
	env->set_errfile(env, stderr);
	r=env->txn_begin(env, 0, &txn, 0);                            CKERR(r);
	r=db_create(&db, env, 0);                                     CKERR(r);
	r=db->set_flags(db, DB_DUP|DB_DUPSORT);
137
	r=db->open(db, txn, "foo.db", 0, DB_BTREE, DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO);  CKERR(r);
138 139 140
	r=txn->commit(txn, 0);                                        CKERR(r);
    }
    int i;
141
    //printf("fact(%d)=%d\n", N, fact(N));
142 143 144 145 146 147 148 149 150 151
    for (i=0; i<fact(N); i++) {
	run(i);
    }
    {
	r=db->close(db, 0);                                           CKERR(r);
	r=env->close(env, 0);                                         CKERR(r);
    }

    return 0;
}