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 /* Try to exercise all the cases for the leafcommands in ft-ops.c
41 */
42
43
44 #include <db.h>
45 #include <sys/stat.h>
46
47 static DB_ENV *env;
48 static DB *db;
49 static DB_TXN *txn;
50
insert(int i,int j)51 static void insert (int i, int j) {
52 char hello[30], there[30];
53 DBT key,data;
54 if (verbose) printf("Insert %d\n", i);
55 snprintf(hello, sizeof(hello), "hello%d", i);
56 snprintf(there, sizeof(there), "there%d", j);
57 int r = db->put(db, txn,
58 dbt_init(&key, hello, strlen(hello)+1),
59 dbt_init(&data, there, strlen(there)+1),
60 0);
61 CKERR(r);
62 }
63
op_delete(int i)64 static void op_delete (int i) {
65 char hello[30];
66 DBT key;
67 if (verbose) printf("op_delete %d\n", i);
68 snprintf(hello, sizeof(hello), "hello%d", i);
69 int r = db->del(db, txn,
70 dbt_init(&key, hello, strlen(hello)+1),
71 DB_DELETE_ANY);
72 assert(r==0);
73 }
74
lookup(int i,int expect,int expectj)75 static void lookup (int i, int expect, int expectj) {
76 char hello[30], there[30];
77 DBT key,data;
78 snprintf(hello, sizeof(hello), "hello%d", i);
79 memset(&data, 0, sizeof(data));
80 if (verbose) printf("Looking up %d (expecting %s)\n", i, expect==0 ? "to find" : "not to find");
81 int r = db->get(db, txn,
82 dbt_init(&key, hello, strlen(hello)+1),
83 &data,
84 0);
85 assert(expect==r);
86 if (expect==0) {
87 CKERR(r);
88 snprintf(there, sizeof(there), "there%d", expectj);
89 assert(data.size==strlen(there)+1);
90 assert(strcmp((char*)data.data, there)==0);
91 }
92 }
93
94 static void
test_abort3(void)95 test_abort3 (void) {
96 int r;
97 toku_os_recursive_delete(TOKU_TEST_FILENAME);
98 r=toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO); assert(r==0);
99
100 r=db_env_create(&env, 0); assert(r==0);
101 env->set_errfile(env, stderr);
102 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);
103 r=db_create(&db, env, 0); CKERR(r);
104
105 r=env->txn_begin(env, 0, &txn, 0); assert(r==0);
106 r=db->open(db, txn, "foo.db", 0, DB_BTREE, DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
107 insert(0, 0);
108 r=txn->commit(txn, 0); assert(r==0);
109
110 r=env->txn_begin(env, 0, &txn, 0); CKERR(r);
111 op_delete(0);
112 op_delete(1);
113 r=txn->commit(txn, 0); CKERR(r);
114
115 r=env->txn_begin(env, 0, &txn, 0); CKERR(r);
116 lookup(1, DB_NOTFOUND, -1);
117 insert(2, 3);
118 r=txn->commit(txn, 0); CKERR(r);
119
120 r=env->txn_begin(env, 0, &txn, 0); CKERR(r);
121 insert(2, 4);
122 insert(2, 5);
123 lookup(2, 0, 5);
124 r=txn->commit(txn, 0); CKERR(r);
125
126 r=env->txn_begin(env, 0, &txn, 0); CKERR(r);
127 lookup(2, 0, 5);
128 r=txn->commit(txn, 0); CKERR(r);
129
130 r=env->txn_begin(env, 0, &txn, 0); CKERR(r);
131 insert(3, 0);
132 r=txn->commit(txn, 0); CKERR(r);
133
134 r=env->txn_begin(env, 0, &txn, 0); CKERR(r);
135 insert(3, 1);
136 lookup(3, 0, 1);
137 r=txn->abort(txn); CKERR(r);
138
139 r=env->txn_begin(env, 0, &txn, 0); CKERR(r);
140 lookup(3, 0, 0);
141 r=txn->commit(txn, 0); CKERR(r);
142
143 r=env->txn_begin(env, 0, &txn, 0); CKERR(r);
144 insert(4, 0);
145 r=txn->commit(txn, 0); CKERR(r);
146
147 r=env->txn_begin(env, 0, &txn, 0); CKERR(r);
148 op_delete(4);
149 lookup(4, DB_NOTFOUND, -1);
150 r=txn->abort(txn); CKERR(r);
151
152 r=env->txn_begin(env, 0, &txn, 0); CKERR(r);
153 lookup(4, 0, 0);
154 r=txn->commit(txn, 0); CKERR(r);
155
156
157 r=env->txn_begin(env, 0, &txn, 0); CKERR(r);
158 insert(5, 0);
159 r=txn->commit(txn, 0); CKERR(r);
160
161 r=env->txn_begin(env, 0, &txn, 0); CKERR(r);
162 insert(5, 1);
163 lookup(5, 0, 1);
164 op_delete(5);
165 lookup(5, DB_NOTFOUND, -1);
166 r=txn->abort(txn); CKERR(r);
167
168 r=env->txn_begin(env, 0, &txn, 0); CKERR(r);
169 lookup(5, 0, 0);
170 r=txn->commit(txn, 0); CKERR(r);
171
172 r=env->txn_begin(env, 0, &txn, 0); CKERR(r);
173 r=txn->commit(txn, 0); CKERR(r);
174
175 r=env->txn_begin(env, 0, &txn, 0); CKERR(r);
176 insert(6, 0);
177 lookup(6, 0, 0);
178 op_delete(6);
179 lookup(6, DB_NOTFOUND, -1);
180 r=txn->abort(txn); CKERR(r);
181
182 r=env->txn_begin(env, 0, &txn, 0); CKERR(r);
183 lookup(6, DB_NOTFOUND, -1);
184 r=txn->commit(txn, 0); CKERR(r);
185
186
187 r=db->close(db, 0); CKERR(r);
188 r=env->close(env, 0); CKERR(r);
189 }
190
191 int
test_main(int argc,char * const argv[])192 test_main (int argc, char *const argv[]) {
193 parse_args(argc, argv);
194 test_abort3();
195 return 0;
196 }
197