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 update broadcast does nothing if the table is empty
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 
update_fun(DB * UU (db),const DBT * UU (key),const DBT * UU (old_val),const DBT * UU (extra),void (* set_val)(const DBT * new_val,void * set_extra),void * set_extra)47 static int update_fun(DB *UU(db),
48                       const DBT *UU(key),
49                       const DBT *UU(old_val), const DBT *UU(extra),
50                       void (*set_val)(const DBT *new_val,
51                                          void *set_extra),
52                       void *set_extra) {
53   set_val(extra,set_extra);
54   return 0;
55 }
56 
setup(void)57 static void setup (void) {
58     toku_os_recursive_delete(TOKU_TEST_FILENAME);
59     { int chk_r = toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(chk_r); }
60     { int chk_r = db_env_create(&env, 0); CKERR(chk_r); }
61     env->set_errfile(env, stderr);
62     env->set_update(env, update_fun);
63     { int chk_r = env->open(env, TOKU_TEST_FILENAME, envflags, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(chk_r); }
64 }
65 
cleanup(void)66 static void cleanup (void) {
67     { int chk_r = env->close(env, 0); CKERR(chk_r); }
68 }
69 
do_updates(DB_TXN * txn,DB * db,uint32_t flags)70 static int do_updates(DB_TXN *txn, DB *db, uint32_t flags) {
71   DBT key, val;
72   uint32_t k = 101;
73   uint32_t v = 10101;
74   dbt_init(&key, &k, sizeof(k));
75   dbt_init(&val, &v, sizeof(v));
76 
77   int r = db->update(db, txn, &key, &val, flags); CKERR(r);
78     return r;
79 }
80 
run_test(bool prelock,bool commit)81 static void run_test(bool prelock, bool commit) {
82     DB *db;
83     uint32_t update_flags = 0;
84     setup();
85 
86     IN_TXN_COMMIT(env, NULL, txn_1, 0, {
87             { int chk_r = db_create(&db, env, 0); CKERR(chk_r); }
88             { int chk_r = db->open(db, txn_1, "foo.db", NULL, DB_BTREE, DB_CREATE, 0666); CKERR(chk_r); }
89         });
90     if (prelock) {
91         IN_TXN_COMMIT(env, NULL, txn_2, 0, {
92                 { int chk_r = db->pre_acquire_table_lock(db, txn_2); CKERR(chk_r); }
93         });
94     }
95 
96     if (commit) {
97         IN_TXN_COMMIT(env, NULL, txn_2, 0, {
98                 { int chk_r = do_updates(txn_2, db, update_flags); CKERR(chk_r); }
99         });
100         DBC *cursor = NULL;
101         DBT key, val;
102         memset(&key, 0, sizeof(key));
103         memset(&val, 0, sizeof(val));
104 
105         IN_TXN_COMMIT(env, NULL, txn_3, 0, {
106                 { int chk_r = db->cursor(db, txn_3, &cursor, 0); CKERR(chk_r); }
107                 { int chk_r = cursor->c_get(cursor, &key, &val, DB_NEXT); CKERR(chk_r); }
108             assert(key.size == sizeof(uint32_t));
109             assert(val.size == sizeof(uint32_t));
110             assert(*(uint32_t *)(key.data) == 101);
111             assert(*(uint32_t *)(val.data) == 10101);
112             { int chk_r = cursor->c_close(cursor); CKERR(chk_r); }
113         });
114     }
115     else {
116         IN_TXN_ABORT(env, NULL, txn_2, 0, {
117                 { int chk_r = do_updates(txn_2, db, update_flags); CKERR(chk_r); }
118         });
119         DBC *cursor = NULL;
120         DBT key, val;
121         memset(&key, 0, sizeof(key));
122         memset(&val, 0, sizeof(val));
123 
124         IN_TXN_COMMIT(env, NULL, txn_3, 0, {
125                 { int chk_r = db->cursor(db, txn_3, &cursor, 0); CKERR(chk_r); }
126                 { int chk_r = cursor->c_get(cursor, &key, &val, DB_NEXT); CKERR2(chk_r, DB_NOTFOUND); }
127                 { int chk_r = cursor->c_close(cursor); CKERR(chk_r); }
128         });
129     }
130     { int chk_r = db->close(db, 0); CKERR(chk_r); }
131     cleanup();
132 }
133 
test_main(int argc,char * const argv[])134 int test_main(int argc, char * const argv[]) {
135     parse_args(argc, argv);
136     run_test(true,true);
137     run_test(false,true);
138     run_test(true,false);
139     run_test(false,false);
140 
141     return 0;
142 }
143