1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6
7
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9
10 PerconaFT is free software: you can redistribute it and/or modify
11 it under the terms of the GNU General Public License, version 2,
12 as published by the Free Software Foundation.
13
14 PerconaFT is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
18
19 You should have received a copy of the GNU General Public License
20 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
21
22 ----------------------------------------
23
24 PerconaFT is free software: you can redistribute it and/or modify
25 it under the terms of the GNU Affero General Public License, version 3,
26 as published by the Free Software Foundation.
27
28 PerconaFT is distributed in the hope that it will be useful,
29 but WITHOUT ANY WARRANTY; without even the implied warranty of
30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
31 GNU Affero General Public License for more details.
32
33 You should have received a copy of the GNU Affero General Public License
34 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38
39 #include "test.h"
40 #include <db.h>
41 #include <sys/stat.h>
42
43 unsigned char N=5;
44
45 static int
fact(int n)46 fact(int n) {
47 if (n<=2) return n;
48 else return n*fact(n-1);
49 }
50
51 static void
swapc(unsigned char * a,unsigned char * b)52 swapc (unsigned char *a, unsigned char *b) {
53 unsigned char tmp=*a;
54 *a=*b;
55 *b=tmp;
56 }
57
58 DB_ENV *env;
59 DB *db;
60
61 static void
run(int choice)62 run (int choice) {
63 unsigned char v[N];
64 int i;
65 int r;
66 for (i=0; i<N; i++) {
67 v[i]=(unsigned char)(10*i);
68 }
69 for (i=0; i<N; i++) {
70 int nchoices=N-i;
71 swapc(&v[i], &v[i+choice%nchoices]);
72 choice=choice/nchoices;
73 }
74 if (0) {
75 for (i=0; i<N; i++) {
76 printf("%d ", v[i]);
77 }
78
79 printf("\n");
80 }
81 DB_TXN *txn;
82 {
83 r=env->txn_begin(env, 0, &txn, 0); CKERR(r);
84 for (i=0; i<N; i++) {
85 DBT kdbt,vdbt;
86 char key[2]={25,(char)v[i]};
87 char val=v[i];
88 //printf("put %d %d\n", key, val);
89 r=db->put(db, txn, dbt_init(&kdbt, &key, 2), dbt_init(&vdbt, &val, 1), 0); CKERR(r);
90 }
91 r=txn->commit(txn, DB_TXN_NOSYNC); CKERR(r);
92 }
93 {
94 r=env->txn_begin(env, 0, &txn, 0); CKERR(r);
95 DBC *c;
96 r=db->cursor(db, txn, &c, 0); CKERR(r);
97 DBT kdbt,vdbt;
98 memset(&kdbt, 0, sizeof(kdbt));
99 memset(&vdbt, 0, sizeof(vdbt));
100 i=0;
101 while (0==(r=c->c_get(c, &kdbt, &vdbt, DB_NEXT))) {
102 //printf("Got %d %d\n", *(unsigned char*)kdbt.data, *(unsigned char*)vdbt.data);
103 i++;
104 kdbt.data=0;
105 vdbt.data=0;
106 }
107 CKERR2(r, DB_NOTFOUND);
108 //printf("i=%d N=%d\n", i, N);
109 assert(i==N);
110 r=c->c_close(c); CKERR(r);
111 r=txn->commit(txn, DB_TXN_NOSYNC); CKERR(r);
112 }
113 {
114 r=env->txn_begin(env, 0, &txn, 0); CKERR(r);
115 DBC *c;
116 r=db->cursor(db, txn, &c, 0); CKERR(r);
117 DBT kdbt,vdbt;
118 memset(&kdbt, 0, sizeof(kdbt));
119 memset(&vdbt, 0, sizeof(vdbt));
120 i=0;
121 while (0==(r=(c->c_get(c, &kdbt, &vdbt, DB_FIRST)))) {
122 i++;
123 r = db->del(db, txn, &kdbt, DB_DELETE_ANY);
124 CKERR(r);
125 }
126 assert(r==DB_NOTFOUND);
127 r=c->c_close(c); CKERR(r);
128 r=txn->commit(txn, DB_TXN_NOSYNC); CKERR(r);
129 }
130 return;
131 #if 0
132 char v101=101, v102=102, v1=1, v2=2;
133 {
134 r=env->txn_begin(env, 0, &txn, 0); CKERR(r);
135 DBT k,v;
136 r=db->put(db, txn, dbt_init(&k, &v1, 1), dbt_init(&v, &v101, 1), 0); CKERR(r);
137 r=db->put(db, txn, dbt_init(&k, &v2, 1), dbt_init(&v, &v102, 1), 0); CKERR(r);
138 r=txn->commit(txn, 0); CKERR(r);
139 }
140 {
141 r=env->txn_begin(env, 0, &txn, 0); CKERR(r);
142 DBC *c;
143 r=db->cursor(db, txn, &c, 0); CKERR(r);
144 DBT k,v;
145 r=c->c_get(c, dbt_init_malloc(&k), dbt_init_malloc(&v), DB_FIRST); CKERR(r);
146 assert(*(char*)k.data==v1); assert(*(char*)v.data==v101);
147 r=c->c_get(c, dbt_init_malloc(&k), dbt_init_malloc(&v), DB_NEXT); CKERR(r);
148 assert(*(char*)k.data==v2); assert(*(char*)v.data==v102);
149 r=c->c_get(c, dbt_init_malloc(&k), dbt_init_malloc(&v), DB_NEXT); assert(r!=0);
150 r=c->c_close(c); CKERR(r);
151 r=txn->commit(txn, 0); CKERR(r);
152 }
153 #endif
154 }
155
156 int
test_main(int argc,char * const argv[])157 test_main(int argc, char *const argv[]) {
158 parse_args(argc, argv);
159
160 int r;
161 toku_os_recursive_delete(TOKU_TEST_FILENAME);
162 toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO);
163
164 DB_TXN *txn;
165 {
166 r = db_env_create(&env, 0); CKERR(r);
167 r = env->set_redzone(env, 0); CKERR(r);
168 r=env->open(env, TOKU_TEST_FILENAME, DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_MPOOL|DB_INIT_TXN|DB_CREATE|DB_PRIVATE, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
169 env->set_errfile(env, stderr);
170 r=env->txn_begin(env, 0, &txn, 0); CKERR(r);
171 r=db_create(&db, env, 0); CKERR(r);
172 r=db->open(db, txn, "foo.db", 0, DB_BTREE, DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
173 r=txn->commit(txn, 0); CKERR(r);
174 }
175 int i;
176 //printf("fact(%d)=%d\n", N, fact(N));
177 for (i=0; i<fact(N); i++) {
178 run(i);
179 }
180 {
181 r=db->close(db, 0); CKERR(r);
182 r=env->close(env, 0); CKERR(r);
183 }
184
185 return 0;
186 }
187