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 
41 #include "toku_os.h"
42 #include "cachetable/checkpoint.h"
43 
44 
45 #define FILENAME "test0.ft"
46 
test_it(int N)47 static void test_it (int N) {
48     FT_HANDLE ft;
49     int r;
50     toku_os_recursive_delete(TOKU_TEST_FILENAME);
51     r = toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU);                                                                    CKERR(r);
52 
53     TOKULOGGER logger;
54     r = toku_logger_create(&logger);                                                                        CKERR(r);
55     r = toku_logger_open(TOKU_TEST_FILENAME, logger);                                                                  CKERR(r);
56 
57 
58     CACHETABLE ct;
59     toku_cachetable_create(&ct, 0, ZERO_LSN, logger);
60     toku_cachetable_set_env_dir(ct, TOKU_TEST_FILENAME);
61 
62     toku_logger_set_cachetable(logger, ct);
63 
64     r = toku_logger_open_rollback(logger, ct, true);                                                        CKERR(r);
65 
66     TOKUTXN txn;
67     r = toku_txn_begin_txn((DB_TXN*)NULL, (TOKUTXN)0, &txn, logger, TXN_SNAPSHOT_ROOT, false);                     CKERR(r);
68 
69     r = toku_open_ft_handle(FILENAME, 1, &ft, 1024, 256, TOKU_DEFAULT_COMPRESSION_METHOD, ct, txn, toku_builtin_compare_fun);                    CKERR(r);
70 
71     r = toku_txn_commit_txn(txn, false, NULL, NULL);                                 CKERR(r);
72     toku_txn_close_txn(txn);
73     CHECKPOINTER cp = toku_cachetable_get_checkpointer(ct);
74     r = toku_checkpoint(cp, logger, NULL, NULL, NULL, NULL, CLIENT_CHECKPOINT);                             CKERR(r);
75     r = toku_close_ft_handle_nolsn(ft, NULL);                                                                          CKERR(r);
76 
77     unsigned int rands[N];
78     for (int i=0; i<N; i++) {
79 	r = toku_txn_begin_txn((DB_TXN*)NULL, (TOKUTXN)0, &txn, logger, TXN_SNAPSHOT_ROOT, false);                 CKERR(r);
80 	r = toku_open_ft_handle(FILENAME, 0, &ft, 1024, 256, TOKU_DEFAULT_COMPRESSION_METHOD, ct, txn, toku_builtin_compare_fun);                CKERR(r);
81 	r = toku_txn_commit_txn(txn, false, NULL, NULL);                             CKERR(r);
82 	toku_txn_close_txn(txn);
83 
84 	r = toku_txn_begin_txn((DB_TXN*)NULL, (TOKUTXN)0, &txn, logger, TXN_SNAPSHOT_ROOT, false);                 CKERR(r);
85 	char key[100],val[300];
86 	DBT k, v;
87 	rands[i] = random();
88 	snprintf(key, sizeof(key), "key%x.%x", rands[i], i);
89 	memset(val, 'v', sizeof(val));
90 	val[sizeof(val)-1]=0;
91 	toku_ft_insert(ft, toku_fill_dbt(&k, key, 1+strlen(key)), toku_fill_dbt(&v, val, 1+strlen(val)), txn);
92 	r = toku_txn_commit_txn(txn, false, NULL, NULL);                                 CKERR(r);
93 	toku_txn_close_txn(txn);
94 
95 
96 	r = toku_checkpoint(cp, logger, NULL, NULL, NULL, NULL, CLIENT_CHECKPOINT);                             CKERR(r);
97 	r = toku_close_ft_handle_nolsn(ft, NULL);                                                                          CKERR(r);
98 
99 	if (verbose) printf("i=%d\n", i);
100     }
101     for (int i=0; i<N; i++) {
102 	r = toku_txn_begin_txn((DB_TXN*)NULL, (TOKUTXN)0, &txn, logger, TXN_SNAPSHOT_ROOT, false);                     CKERR(r);
103 	r = toku_open_ft_handle(FILENAME, 0, &ft, 1024, 256, TOKU_DEFAULT_COMPRESSION_METHOD, ct, txn, toku_builtin_compare_fun);                CKERR(r);
104 	r = toku_txn_commit_txn(txn, false, NULL, NULL);                                 CKERR(r);
105 	toku_txn_close_txn(txn);
106 
107 	r = toku_txn_begin_txn((DB_TXN*)NULL, (TOKUTXN)0, &txn, logger, TXN_SNAPSHOT_ROOT, false);                     CKERR(r);
108 	char key[100];
109 	DBT k;
110 	snprintf(key, sizeof(key), "key%x.%x", rands[i], i);
111 	toku_ft_delete(ft, toku_fill_dbt(&k, key, 1+strlen(key)), txn);
112 
113 	if (0) {
114 	bool is_empty;
115         is_empty = toku_ft_is_empty_fast(ft);
116 	assert(!is_empty);
117 	}
118 
119 	r = toku_txn_commit_txn(txn, false, NULL, NULL);                                 CKERR(r);
120 	toku_txn_close_txn(txn);
121 
122 	r = toku_checkpoint(cp, logger, NULL, NULL, NULL, NULL, CLIENT_CHECKPOINT);                             CKERR(r);
123 	r = toku_close_ft_handle_nolsn(ft, NULL);                                                                          CKERR(r);
124 
125 	if (verbose) printf("d=%d\n", i);
126     }
127     r = toku_txn_begin_txn((DB_TXN*)NULL, (TOKUTXN)0, &txn, logger, TXN_SNAPSHOT_ROOT, false);                        CKERR(r);
128     r = toku_open_ft_handle(FILENAME, 0, &ft, 1024, 256, TOKU_DEFAULT_COMPRESSION_METHOD, ct, txn, toku_builtin_compare_fun);                       CKERR(r);
129     r = toku_txn_commit_txn(txn, false, NULL, NULL);                                     CKERR(r);
130     toku_txn_close_txn(txn);
131 
132     if (0) {
133     bool is_empty;
134     is_empty = toku_ft_is_empty_fast(ft);
135     assert(is_empty);
136     }
137 
138     r = toku_checkpoint(cp, logger, NULL, NULL, NULL, NULL, CLIENT_CHECKPOINT);                                CKERR(r);
139     r = toku_close_ft_handle_nolsn(ft, NULL);                                                                             CKERR(r);
140 
141     r = toku_checkpoint(cp, logger, NULL, NULL, NULL, NULL, CLIENT_CHECKPOINT);                                CKERR(r);
142     toku_logger_close_rollback(logger);
143     r = toku_checkpoint(cp, logger, NULL, NULL, NULL, NULL, CLIENT_CHECKPOINT);                                CKERR(r);
144     toku_cachetable_close(&ct);
145     r = toku_logger_close(&logger);                                                        assert(r==0);
146 
147 }
148 
149 
test_main(int argc,const char * argv[])150 int test_main (int argc, const char *argv[]) {
151     default_parse_args(argc, argv);
152     for (int i=1; i<=64; i++) {
153 	test_it(i);
154     }
155     return 0;
156 }
157