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 <stdio.h>
41 
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <memory.h>
45 #include <sys/stat.h>
46 #include <db.h>
47 #include <memory.h>
48 
49 static void
test_789(void)50 test_789(void) {
51     int r;
52 
53     /* setup test directory */
54     toku_os_recursive_delete(TOKU_TEST_FILENAME);
55     toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO);
56 
57     /* setup environment */
58     DB_ENV *env;
59     {
60         r = db_env_create(&env, 0); assert(r == 0);
61         env->set_errfile(env, stdout);
62         r = env->open(env, TOKU_TEST_FILENAME, DB_INIT_MPOOL + DB_INIT_LOG + DB_INIT_LOCK + DB_INIT_TXN + DB_PRIVATE + DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO);
63         assert(r == 0);
64     }
65 
66     /* setup database */
67     DB *db;
68     {
69         DB_TXN *txn = 0;
70         r = env->txn_begin(env, 0, &txn, 0); assert(r == 0);
71 
72         r = db_create(&db, env, 0); assert(r == 0);
73         r = db->open(db, txn, "test.db", 0, DB_BTREE, DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO); assert(r == 0);
74 
75         r = txn->commit(txn, 0); assert(r == 0);
76     }
77 
78     /* insert, commit */
79     {
80         DB_TXN *txn_master;
81         r = env->txn_begin(env, 0, &txn_master, 0); assert(r == 0);
82         DB_TXN *txn;
83         r = env->txn_begin(env, txn_master, &txn, 0); assert(r == 0);
84         int i;
85         for (i=0; i<3; i++) {
86             int k = htonl(i);
87             int v = 0;
88             DBT key, val;
89             r = db->put(db, txn, dbt_init(&key, &k, sizeof k), dbt_init(&val, &v, sizeof v), 0);
90             assert(r == 0);
91         }
92         r = txn->commit(txn, 0); assert(r == 0);
93         r = txn_master->commit(txn_master, 0); assert(r == 0);
94     }
95 
96     /* update, rollback */
97     {
98         DB_TXN *txn_master;
99         r = env->txn_begin(env, 0, &txn_master, 0); assert(r == 0);
100         DB_TXN *txn;
101         r = env->txn_begin(env, txn_master, &txn, 0); assert(r == 0);
102         DBC *cursor;
103         r = db->cursor(db, txn, &cursor, 0); assert(r == 0);
104         DBT key, val;
105         r = cursor->c_get(cursor, dbt_init_malloc(&key), dbt_init_malloc(&val), DB_NEXT); assert(r == 0);
106         *(char*)val.data = 1;
107         r = db->put(db, txn, &key, &val, 0); assert(r == 0);
108         r = cursor->c_close(cursor); assert(r == 0);
109         toku_free(key.data); toku_free(val.data);
110         r = txn->commit(txn, 0); assert(r == 0);
111         r = txn_master->abort(txn_master); assert(r == 0);
112     }
113 
114     /* delete, rollback */
115     {
116         DB_TXN *txn_master;
117         r = env->txn_begin(env, 0, &txn_master, 0); assert(r == 0);
118         DB_TXN *txn;
119         r = env->txn_begin(env, txn_master, &txn, 0); assert(r == 0);
120         DBC *cursor;
121         r = db->cursor(db, txn, &cursor, 0); assert(r == 0);
122         DBT key, val;
123         r = cursor->c_get(cursor, dbt_init_malloc(&key), dbt_init_malloc(&val), DB_NEXT); assert(r == 0);
124         r = db->del(db, txn, &key, DB_DELETE_ANY); assert(r == 0);
125         r = cursor->c_close(cursor); assert(r == 0);
126         toku_free(key.data); toku_free(val.data);
127         r = txn->commit(txn, 0); assert(r == 0);
128         r = txn_master->abort(txn_master); assert(r == 0);
129     }
130 
131     /* update, commit */
132     {
133         DB_TXN *txn_master;
134         r = env->txn_begin(env, 0, &txn_master, 0); assert(r == 0);
135         DB_TXN *txn;
136         r = env->txn_begin(env, txn_master, &txn, 0); assert(r == 0);
137         DBC *cursor;
138         r = db->cursor(db, txn, &cursor, 0); assert(r == 0);
139         DBT key, val;
140         r = cursor->c_get(cursor, dbt_init_malloc(&key), dbt_init_malloc(&val), DB_NEXT); assert(r == 0);
141         *(char*)val.data = 2;
142         r = db->put(db, txn, &key, &val, 0); assert(r == 0);
143         r = cursor->c_close(cursor); assert(r == 0);
144         toku_free(key.data); toku_free(val.data);
145         r = txn->commit(txn, 0); assert(r == 0);
146         r = txn_master->commit(txn_master, 0); assert(r == 0);
147     }
148 
149     /* delete, commit */
150     {
151         DB_TXN *txn_master;
152         r = env->txn_begin(env, 0, &txn_master, 0); assert(r == 0);
153         DB_TXN *txn;
154         r = env->txn_begin(env, txn_master, &txn, 0); assert(r == 0);
155         DBC *cursor;
156         r = db->cursor(db, txn, &cursor, 0); assert(r == 0);
157         DBT key, val;
158         r = cursor->c_get(cursor, dbt_init_malloc(&key), dbt_init_malloc(&val), DB_NEXT); assert(r == 0);
159         r = db->del(db, txn, &key, DB_DELETE_ANY); assert(r == 0);
160         r = cursor->c_close(cursor); assert(r == 0);
161         toku_free(key.data); toku_free(val.data);
162         r = txn->commit(txn, 0); assert(r == 0);
163         r = txn_master->commit(txn_master, 0); assert(r == 0);
164     }
165 
166     /* close db */
167     r = db->close(db, 0); assert(r == 0);
168 
169     /* close env */
170     r = env->close(env, 0); assert(r == 0);
171 }
172 
173 int
test_main(int UU (argc),char UU (* const argv[]))174 test_main(int UU(argc), char UU(*const argv[])) {
175     test_789();
176     return 0;
177 }
178