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 static const char *fname = TOKU_TEST_FILENAME;
42 
43 static TOKUTXN const null_txn = 0;
44 
45 static int
save_data(uint32_t UU (keylen),const void * UU (key),uint32_t vallen,const void * val,void * v,bool lock_only)46 save_data (uint32_t UU(keylen), const void *UU(key), uint32_t vallen, const void *val, void *v, bool lock_only) {
47     if (lock_only) return 0;
48     assert(key!=NULL);
49     void **CAST_FROM_VOIDP(vp, v);
50     *vp = toku_memdup(val, vallen);
51     return 0;
52 }
53 
54 
55 // Verify that different cursors return different data items when a DBT is initialized to all zeros (no flags)
56 // Note: The ft test used to implement DBTs with per-cursor allocated space, but there isn't any such thing any more
57 // so this test is a little bit obsolete.
test_multiple_ft_cursor_dbts(int n)58 static void test_multiple_ft_cursor_dbts(int n) {
59     if (verbose) printf("test_multiple_ft_cursors:%d\n", n);
60 
61     int r;
62     CACHETABLE ct;
63     FT_HANDLE ft;
64     FT_CURSOR cursors[n];
65 
66     unlink(fname);
67 
68     toku_cachetable_create(&ct, 0, ZERO_LSN, nullptr);
69 
70     r = toku_open_ft_handle(fname, 1, &ft, 1<<12, 1<<9, TOKU_DEFAULT_COMPRESSION_METHOD, ct, null_txn, toku_builtin_compare_fun);
71     assert(r==0);
72 
73     int i;
74     for (i=0; i<n; i++) {
75 	DBT kbt,vbt;
76 	char key[10],val[10];
77 	snprintf(key, sizeof key, "k%04d", i);
78 	snprintf(val, sizeof val, "v%04d", i);
79 	toku_ft_insert(ft,
80                        toku_fill_dbt(&kbt, key, 1+strlen(key)),
81                        toku_fill_dbt(&vbt, val, 1+strlen(val)),
82                        0);
83     }
84 
85     for (i=0; i<n; i++) {
86         r = toku_ft_cursor(ft, &cursors[i], NULL, false, false);
87         assert(r == 0);
88     }
89 
90     void *ptrs[n];
91     for (i=0; i<n; i++) {
92 	DBT kbt;
93 	char key[10];
94 	snprintf(key, sizeof key, "k%04d", i);
95 	r = toku_ft_cursor_get(cursors[i],
96 				toku_fill_dbt(&kbt, key, 1+strlen(key)),
97 				save_data,
98 				&ptrs[i],
99 				DB_SET);
100 	assert(r == 0);
101     }
102 
103     for (i=0; i<n; i++) {
104 	int j;
105 	for (j=i+1; j<n; j++) {
106 	    assert(strcmp((char*)ptrs[i],(char*)ptrs[j])!=0);
107 	}
108     }
109 
110     for (i=0; i<n; i++) {
111         toku_ft_cursor_close(cursors[i]);
112         assert(r == 0);
113 	toku_free(ptrs[i]);
114     }
115 
116     r = toku_close_ft_handle_nolsn(ft, 0);
117     assert(r==0);
118 
119     toku_cachetable_close(&ct);
120 }
121 
test_ft_cursor(void)122 static void test_ft_cursor(void) {
123     test_multiple_ft_cursor_dbts(1);
124     test_multiple_ft_cursor_dbts(2);
125     test_multiple_ft_cursor_dbts(3);
126 }
127 
128 
129 int
test_main(int argc,const char * argv[])130 test_main (int argc , const char *argv[]) {
131     default_parse_args(argc, argv);
132     test_ft_cursor();
133     if (verbose) printf("test ok\n");
134     return 0;
135 }
136