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/*======
5This file is part of PerconaFT.
6
7
8Copyright (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 <my_global.h>
40#include "ft/ft.h"
41#include "ft/ft-cachetable-wrappers.h"
42#include "ft/ft-internal.h"
43#include "ft/ft-flusher.h"
44#include "ft/serialize/ft_node-serialize.h"
45#include "ft/node.h"
46#include "ft/ule.h"
47
48// dummymsn needed to simulate msn because messages are injected at a lower level than toku_ft_root_put_msg()
49#define MIN_DUMMYMSN ((MSN) {(uint64_t)1 << 62})
50static MSN dummymsn;
51static int testsetup_initialized = 0;
52
53
54// Must be called before any other test_setup_xxx() functions are called.
55void
56toku_testsetup_initialize(void) {
57 if (testsetup_initialized == 0) {
58 testsetup_initialized = 1;
59 dummymsn = MIN_DUMMYMSN;
60 }
61}
62
63static MSN
64next_dummymsn(void) {
65 ++(dummymsn.msn);
66 return dummymsn;
67}
68
69
70bool ignore_if_was_already_open;
71int toku_testsetup_leaf(FT_HANDLE ft_handle, BLOCKNUM *blocknum, int n_children, char **keys, int *keylens) {
72 FTNODE node;
73 assert(testsetup_initialized);
74 toku_create_new_ftnode(ft_handle, &node, 0, n_children);
75 for (int i = 0; i < n_children; i++) {
76 BP_STATE(node, i) = PT_AVAIL;
77 }
78
79 DBT *XMALLOC_N(n_children - 1, pivotkeys);
80 for (int i = 0; i + 1 < n_children; i++) {
81 toku_memdup_dbt(&pivotkeys[i], keys[i], keylens[i]);
82 }
83 node->pivotkeys.create_from_dbts(pivotkeys, n_children - 1);
84 for (int i = 0; i + 1 < n_children; i++) {
85 toku_destroy_dbt(&pivotkeys[i]);
86 }
87 toku_free(pivotkeys);
88
89 *blocknum = node->blocknum;
90 toku_unpin_ftnode(ft_handle->ft, node);
91 return 0;
92}
93
94// Don't bother to clean up carefully if something goes wrong. (E.g., it's OK to have malloced stuff that hasn't been freed.)
95int toku_testsetup_nonleaf (FT_HANDLE ft_handle, int height, BLOCKNUM *blocknum, int n_children, BLOCKNUM *children, char **keys, int *keylens) {
96 FTNODE node;
97 assert(testsetup_initialized);
98 toku_create_new_ftnode(ft_handle, &node, height, n_children);
99 for (int i = 0; i < n_children; i++) {
100 BP_BLOCKNUM(node, i) = children[i];
101 BP_STATE(node,i) = PT_AVAIL;
102 }
103 DBT *XMALLOC_N(n_children - 1, pivotkeys);
104 for (int i = 0; i + 1 < n_children; i++) {
105 toku_memdup_dbt(&pivotkeys[i], keys[i], keylens[i]);
106 }
107 node->pivotkeys.create_from_dbts(pivotkeys, n_children - 1);
108 for (int i = 0; i + 1 < n_children; i++) {
109 toku_destroy_dbt(&pivotkeys[i]);
110 }
111 toku_free(pivotkeys);
112
113 *blocknum = node->blocknum;
114 toku_unpin_ftnode(ft_handle->ft, node);
115 return 0;
116}
117
118int toku_testsetup_root(FT_HANDLE ft_handle, BLOCKNUM blocknum) {
119 assert(testsetup_initialized);
120 ft_handle->ft->h->root_blocknum = blocknum;
121 return 0;
122}
123
124int toku_testsetup_get_sersize(FT_HANDLE ft_handle, BLOCKNUM diskoff) // Return the size on disk
125{
126 assert(testsetup_initialized);
127 void *node_v;
128 ftnode_fetch_extra bfe;
129 bfe.create_for_full_read(ft_handle->ft);
130 int r = toku_cachetable_get_and_pin(
131 ft_handle->ft->cf, diskoff,
132 toku_cachetable_hash(ft_handle->ft->cf, diskoff),
133 &node_v,
134 NULL,
135 get_write_callbacks_for_node(ft_handle->ft),
136 toku_ftnode_fetch_callback,
137 toku_ftnode_pf_req_callback,
138 toku_ftnode_pf_callback,
139 true,
140 &bfe
141 );
142 assert(r==0);
143 FTNODE CAST_FROM_VOIDP(node, node_v);
144 int size = toku_serialize_ftnode_size(node);
145 toku_unpin_ftnode(ft_handle->ft, node);
146 return size;
147}
148
149int toku_testsetup_insert_to_leaf (FT_HANDLE ft_handle, BLOCKNUM blocknum, const char *key, int keylen, const char *val, int vallen) {
150 void *node_v;
151 int r;
152
153 assert(testsetup_initialized);
154
155 ftnode_fetch_extra bfe;
156 bfe.create_for_full_read(ft_handle->ft);
157 r = toku_cachetable_get_and_pin(
158 ft_handle->ft->cf,
159 blocknum,
160 toku_cachetable_hash(ft_handle->ft->cf, blocknum),
161 &node_v,
162 NULL,
163 get_write_callbacks_for_node(ft_handle->ft),
164 toku_ftnode_fetch_callback,
165 toku_ftnode_pf_req_callback,
166 toku_ftnode_pf_callback,
167 true,
168 &bfe
169 );
170 if (r!=0) return r;
171 FTNODE CAST_FROM_VOIDP(node, node_v);
172 toku_verify_or_set_counts(node);
173 assert(node->height==0);
174
175 DBT kdbt, vdbt;
176 ft_msg msg(
177 toku_fill_dbt(&kdbt, key, keylen),
178 toku_fill_dbt(&vdbt, val, vallen),
179 FT_INSERT,
180 next_dummymsn(),
181 toku_xids_get_root_xids());
182
183 static size_t zero_flow_deltas[] = { 0, 0 };
184 txn_gc_info gc_info(nullptr, TXNID_NONE, TXNID_NONE, true);
185 toku_ftnode_put_msg(
186 ft_handle->ft->cmp,
187 ft_handle->ft->update_fun,
188 node,
189 -1,
190 msg,
191 true,
192 &gc_info,
193 zero_flow_deltas,
194 NULL,
195 NULL);
196
197 toku_verify_or_set_counts(node);
198
199 toku_unpin_ftnode(ft_handle->ft, node);
200 return 0;
201}
202
203static int
204testhelper_string_key_cmp(DB *UU(e), const DBT *a, const DBT *b)
205{
206 char *CAST_FROM_VOIDP(s, a->data), *CAST_FROM_VOIDP(t, b->data);
207 return strcmp(s, t);
208}
209
210
211void
212toku_pin_node_with_min_bfe(FTNODE* node, BLOCKNUM b, FT_HANDLE t)
213{
214 ftnode_fetch_extra bfe;
215 bfe.create_for_min_read(t->ft);
216 toku_pin_ftnode(
217 t->ft,
218 b,
219 toku_cachetable_hash(t->ft->cf, b),
220 &bfe,
221 PL_WRITE_EXPENSIVE,
222 node,
223 true
224 );
225}
226
227int toku_testsetup_insert_to_nonleaf (FT_HANDLE ft_handle, BLOCKNUM blocknum, enum ft_msg_type msgtype, const char *key, int keylen, const char *val, int vallen) {
228 void *node_v;
229 int r;
230
231 assert(testsetup_initialized);
232
233 ftnode_fetch_extra bfe;
234 bfe.create_for_full_read(ft_handle->ft);
235 r = toku_cachetable_get_and_pin(
236 ft_handle->ft->cf,
237 blocknum,
238 toku_cachetable_hash(ft_handle->ft->cf, blocknum),
239 &node_v,
240 NULL,
241 get_write_callbacks_for_node(ft_handle->ft),
242 toku_ftnode_fetch_callback,
243 toku_ftnode_pf_req_callback,
244 toku_ftnode_pf_callback,
245 true,
246 &bfe
247 );
248 if (r!=0) return r;
249 FTNODE CAST_FROM_VOIDP(node, node_v);
250 assert(node->height>0);
251
252 DBT k;
253 int childnum = toku_ftnode_which_child(node, toku_fill_dbt(&k, key, keylen), ft_handle->ft->cmp);
254
255 XIDS xids_0 = toku_xids_get_root_xids();
256 MSN msn = next_dummymsn();
257 toku::comparator cmp;
258 cmp.create(testhelper_string_key_cmp, nullptr);
259 toku_bnc_insert_msg(BNC(node, childnum), key, keylen, val, vallen, msgtype, msn, xids_0, true, cmp);
260 cmp.destroy();
261 // Hack to get the test working. The problem is that this test
262 // is directly queueing something in a FIFO instead of
263 // using ft APIs.
264 node->max_msn_applied_to_node_on_disk = msn;
265 node->dirty = 1;
266 // Also hack max_msn_in_ft
267 ft_handle->ft->h->max_msn_in_ft = msn;
268
269 toku_unpin_ftnode(ft_handle->ft, node);
270 return 0;
271}
272