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