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 // test that a transaction can update, then create a child which also updates
40 
41 #include "test.h"
42 
43 const int envflags = DB_INIT_MPOOL|DB_CREATE|DB_THREAD |DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_TXN|DB_PRIVATE;
44 
45 DB_ENV *env;
46 
47 const int to_update[] = { 0, 1, 1, 1, 0, 0, 1, 0, 1, 0 };
48 
_v(const unsigned int i)49 static inline unsigned int _v(const unsigned int i) { return 10 - i; }
_e(const unsigned int i)50 static inline unsigned int _e(const unsigned int i) { return i + 4; }
_u(const unsigned int v,const unsigned int e)51 static inline unsigned int _u(const unsigned int v, const unsigned int e) { return v * v * e; }
52 
update_fun(DB * UU (db),const DBT * key,const DBT * old_val,const DBT * extra,void (* set_val)(const DBT * new_val,void * set_extra),void * set_extra)53 static int update_fun(DB *UU(db),
54                       const DBT *key,
55                       const DBT *old_val, const DBT *extra,
56                       void (*set_val)(const DBT *new_val,
57                                       void *set_extra),
58                       void *set_extra) {
59     unsigned int *k, *ov, *e, v;
60     assert(key->size == sizeof(*k));
61     CAST_FROM_VOIDP(k, key->data);
62     assert(old_val->size == sizeof(*ov));
63     CAST_FROM_VOIDP(ov, old_val->data);
64     assert(extra->size == sizeof(*e));
65     CAST_FROM_VOIDP(e, extra->data);
66     v = _u(*ov, *e);
67 
68     {
69         DBT newval;
70         set_val(dbt_init(&newval, &v, sizeof(v)), set_extra);
71     }
72 
73     return 0;
74 }
75 
setup(void)76 static void setup (void) {
77     toku_os_recursive_delete(TOKU_TEST_FILENAME);
78     { int chk_r = toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(chk_r); }
79     { int chk_r = db_env_create(&env, 0); CKERR(chk_r); }
80     env->set_errfile(env, stderr);
81     env->set_update(env, update_fun);
82     { int chk_r = env->open(env, TOKU_TEST_FILENAME, envflags, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(chk_r); }
83 }
84 
cleanup(void)85 static void cleanup (void) {
86     { int chk_r = env->close(env, 0); CKERR(chk_r); }
87 }
88 
do_inserts(DB_TXN * txn,DB * db)89 static int do_inserts(DB_TXN *txn, DB *db) {
90     int r = 0;
91     DBT key, val;
92     unsigned int i, v;
93     DBT *keyp = dbt_init(&key, &i, sizeof(i));
94     DBT *valp = dbt_init(&val, &v, sizeof(v));
95     for (i = 0; i < (sizeof(to_update) / sizeof(to_update[0])); ++i) {
96         v = _v(i);
97         r = db->put(db, txn, keyp, valp, 0); CKERR(r);
98     }
99     return r;
100 }
101 
do_updates(DB_TXN * txn,DB * db)102 static int do_updates(DB_TXN *txn, DB *db) {
103     int r = 0;
104     DBT key, extra;
105     unsigned int i, e;
106     DBT *keyp = dbt_init(&key, &i, sizeof(i));
107     DBT *extrap = dbt_init(&extra, &e, sizeof(e));
108     for (i = 0; i < (sizeof(to_update) / sizeof(to_update[0])); ++i) {
109         if (to_update[i] == 1) {
110             e = _e(i);  // E I O
111             r = db->update(db, txn, keyp, extrap, 0); CKERR(r);
112         }
113     }
114     return r;
115 }
116 
do_verify_results(DB_TXN * txn,DB * db,bool updated_twice)117 static int do_verify_results(DB_TXN *txn, DB *db, bool updated_twice) {
118     int r = 0;
119     DBT key, val;
120     unsigned int i, *vp;
121     DBT *keyp = dbt_init(&key, &i, sizeof(i));
122     DBT *valp = dbt_init(&val, NULL, 0);
123     for (i = 0; i < (sizeof(to_update) / sizeof(to_update[0])); ++i) {
124         r = db->get(db, txn, keyp, valp, 0); CKERR(r);
125         assert(val.size == sizeof(*vp));
126         CAST_FROM_VOIDP(vp, val.data);
127         if (to_update[i]) {
128             if (updated_twice) {
129                 assert(*vp == _u(_u(_v(i), _e(i)), _e(i)));
130             } else {
131                 assert(*vp == _u(_v(i), _e(i)));
132             }
133         } else {
134             assert(*vp == _v(i));
135         }
136     }
137     return r;
138 }
139 
test_main(int argc,char * const argv[])140 int test_main (int argc, char * const argv[]) {
141     parse_args(argc, argv);
142     setup();
143 
144     DB *db;
145 
146     IN_TXN_COMMIT(env, NULL, txn_1, 0, {
147             { int chk_r = db_create(&db, env, 0); CKERR(chk_r); }
148             { int chk_r = db->open(db, txn_1, "foo.db", NULL, DB_BTREE, DB_CREATE, 0666); CKERR(chk_r); }
149 
150             { int chk_r = do_inserts(txn_1, db); CKERR(chk_r); }
151         });
152 
153     IN_TXN_COMMIT(env, NULL, txn_2, 0, {
154             { int chk_r = do_updates(txn_2, db); CKERR(chk_r); }
155             { int chk_r = do_verify_results(txn_2, db, false); CKERR(chk_r); }
156 
157             IN_TXN_COMMIT(env, txn_2, txn_21, 0, {
158                     { int chk_r = do_updates(txn_21, db); CKERR(chk_r); }
159                     { int chk_r = do_verify_results(txn_21, db, true); CKERR(chk_r); }
160                 });
161 
162             { int chk_r = do_verify_results(txn_2, db, true); CKERR(chk_r); }
163         });
164 
165     IN_TXN_COMMIT(env, NULL, txn_3, 0, {
166             { int chk_r = do_verify_results(txn_3, db, true); CKERR(chk_r); }
167         });
168 
169     { int chk_r = db->close(db, 0); CKERR(chk_r); }
170 
171     cleanup();
172 
173     return 0;
174 }
175