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 <stdio.h>
42 #include <stdlib.h>
43 
44 #include <unistd.h>
45 #include <memory.h>
46 #include <sys/stat.h>
47 #include <db.h>
48 
49 
50 static void
expect_cursor_get(DBC * cursor,int k,int v,int op)51 expect_cursor_get (DBC *cursor, int k, int v, int op) {
52     int kk, vv;
53     DBT key, val;
54     int r = cursor->c_get(cursor, dbt_init_malloc(&key), dbt_init_malloc(&val), op);
55     assert(r == 0);
56     assert(key.size == sizeof kk); memcpy(&kk, key.data, key.size); assert(kk == k); toku_free(key.data);
57     assert(val.size == sizeof vv); memcpy(&vv, val.data, val.size); assert(vv == v); toku_free(val.data);
58 }
59 
60 static DBC *
new_cursor(DB * db,int k,int v,int op)61 new_cursor (DB *db, int k, int v, int op) {
62     DBC *cursor;
63     int r;
64     r = db->cursor(db, 0, &cursor, 0); assert(r == 0);
65     expect_cursor_get(cursor, k, v, op);
66     return cursor;
67 }
68 
69 static int
db_put(DB * db,int k,int v)70 db_put (DB *db, int k, int v) {
71     DBT key, val;
72     int r = db->put(db, 0, dbt_init(&key, &k, sizeof k), dbt_init(&val, &v, sizeof v), 0);
73     return r;
74 }
75 
76 /* use inserts and cursors to test the ft_nonleaf_expand function
77    insert keys 0 and n and set cursors to them
78    then insert keys 1 .. n-1.  this should cause leaf splits, new root nodes, nonleaf expands
79    and nonleaf splits as the tree grows.
80 
81    the reverse parameter controls where in insertions are made to test the <, =, >
82    cases in the ft_nonleaf_expand function */
83 
84 static void
test_cursor_nonleaf_expand(int n,int reverse)85 test_cursor_nonleaf_expand (int n, int reverse) {
86     if (verbose) printf("test_cursor_nonleaf_expand:%d %d\n", n, reverse);
87 
88     DB_TXN * const null_txn = 0;
89     const char * const fname = "test.insert.ft_handle";
90     int r;
91 
92     toku_os_recursive_delete(TOKU_TEST_FILENAME);
93     r=toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO); assert(r==0);
94 
95     /* create the dup database file */
96     DB_ENV *env;
97     r = db_env_create(&env, 0); assert(r == 0);
98     r = env->open(env, TOKU_TEST_FILENAME, DB_CREATE+DB_PRIVATE+DB_INIT_MPOOL, 0); assert(r == 0);
99 
100     DB *db;
101     r = db_create(&db, env, 0); assert(r == 0);
102     r = db->set_pagesize(db, 4096); assert(r == 0);
103     r = db->open(db, null_txn, fname, "main", DB_BTREE, DB_CREATE, 0666); assert(r == 0);
104 
105     r = db_put(db, htonl(0), 0); assert(r == 0);
106     DBC *cursor0 = new_cursor(db, htonl(0), 0, DB_FIRST); assert(cursor0);
107     r = db_put(db, htonl(n), n); assert(r == 0);
108     DBC *cursorn = new_cursor(db, htonl(n), n, DB_LAST); assert(cursorn);
109 
110     int i;
111     if (reverse) {
112         for (i=n-1; i > 0; i--) {
113             r = db_put(db, htonl(i), i); assert(r == 0);
114         }
115     } else {
116         for (i=1; i < n; i++) {
117             r = db_put(db, htonl(i), i); assert(r == 0);
118         }
119     }
120 
121     /* make sure the cursors did not move */
122     expect_cursor_get(cursor0, htonl(0), 0, DB_CURRENT);
123     expect_cursor_get(cursorn, htonl(n), n, DB_CURRENT);
124 
125     r = cursor0->c_close(cursor0); assert(r == 0);
126     r = cursorn->c_close(cursorn); assert(r == 0);
127     r = db->close(db, 0); assert(r == 0);
128     r = env->close(env, 0); assert(r == 0);
129 }
130 
131 int
test_main(int argc,char * const argv[])132 test_main(int argc, char *const argv[]) {
133     parse_args(argc, argv);
134 
135     int i;
136     for (i=1; i<=65536; i *= 2) {
137         test_cursor_nonleaf_expand(i, 0);
138         test_cursor_nonleaf_expand(i, 1);
139     }
140 
141     return 0;
142 }
143