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 calls back into the update function
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 uint32_t four_byte_desc = 101;
48 uint64_t eight_byte_desc = 10101;
49 
50 
assert_desc_four(DB * db)51 static void assert_desc_four (DB* db) {
52     assert(db->descriptor->dbt.size == sizeof(four_byte_desc));
53     assert(*(uint32_t *)(db->descriptor->dbt.data) == four_byte_desc);
54 }
assert_desc_eight(DB * db)55 static void assert_desc_eight (DB* db) {
56     assert(db->descriptor->dbt.size == sizeof(eight_byte_desc));
57     assert(*(uint32_t *)(db->descriptor->dbt.data) == eight_byte_desc);
58 }
59 
checkpoint_callback_1(void * extra)60 static void checkpoint_callback_1(void * extra) {
61     assert(extra == NULL);
62     DB* db = NULL;
63 
64     DBT change_descriptor;
65     memset(&change_descriptor, 0, sizeof(change_descriptor));
66     change_descriptor.size = sizeof(eight_byte_desc);
67     change_descriptor.data = &eight_byte_desc;
68 
69     { int chk_r = db_create(&db, env, 0); CKERR(chk_r); }
70     { int chk_r = db->open(db, NULL, "foo.db", NULL, DB_BTREE, 0, 0666); CKERR(chk_r); }
71     assert_desc_four(db);
72     IN_TXN_ABORT(env, NULL, txn_change, 0, {
73             { int chk_r = db->change_descriptor(db, txn_change, &change_descriptor, 0); CKERR(chk_r); }
74             assert_desc_eight(db);
75         });
76     assert_desc_four(db);
77     { int chk_r = db->close(db,0); CKERR(chk_r); }
78 }
79 
setup(void)80 static void setup (void) {
81     toku_os_recursive_delete(TOKU_TEST_FILENAME);
82     { int chk_r = toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(chk_r); }
83     { int chk_r = db_env_create(&env, 0); CKERR(chk_r); }
84     db_env_set_checkpoint_callback(checkpoint_callback_1, NULL);
85     env->set_errfile(env, stderr);
86     { int chk_r = env->open(env, TOKU_TEST_FILENAME, envflags, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(chk_r); }
87 }
88 
cleanup(void)89 static void cleanup (void) {
90     { int chk_r = env->close(env, 0); CKERR(chk_r); }
91 }
92 
run_test(void)93 static void run_test(void) {
94     DB* db = NULL;
95 
96     DBT orig_desc;
97     memset(&orig_desc, 0, sizeof(orig_desc));
98     orig_desc.size = sizeof(four_byte_desc);
99     orig_desc.data = &four_byte_desc;
100     // verify we can only set a descriptor with version 1
101     IN_TXN_COMMIT(env, NULL, txn_create, 0, {
102             { int chk_r = db_create(&db, env, 0); CKERR(chk_r); }
103             assert(db->descriptor == NULL);
104             { int chk_r = db->open(db, txn_create, "foo.db", NULL, DB_BTREE, DB_CREATE, 0666); CKERR(chk_r); }
105             { int chk_r = db->change_descriptor(db, txn_create, &orig_desc, 0); CKERR(chk_r); }
106             assert_desc_four(db);
107         });
108     assert_desc_four(db);
109     { int chk_r = db->close(db,0); CKERR(chk_r); }
110 
111     { int chk_r = db_create(&db, env, 0); CKERR(chk_r); }
112     { int chk_r = db->open(db, NULL, "foo.db", NULL, DB_BTREE, 0, 0666); CKERR(chk_r); }
113     assert_desc_four(db);
114     { int chk_r = db->close(db,0); CKERR(chk_r); }
115 
116     { int chk_r = env->txn_checkpoint(env, 0, 0, 0); CKERR(chk_r); }
117 
118     { int chk_r = db_create(&db, env, 0); CKERR(chk_r); }
119     { int chk_r = db->open(db, NULL, "foo.db", NULL, DB_BTREE, 0, 0666); CKERR(chk_r); }
120     assert_desc_four(db);
121     { int chk_r = db->close(db,0); CKERR(chk_r); }
122 
123     db = NULL;
124 }
125 
test_main(int argc,char * const argv[])126 int test_main (int argc, char * const argv[]) {
127     parse_args(argc, argv);
128     setup();
129     run_test();
130     cleanup();
131     return 0;
132 }
133