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 #include <stdio.h>
41 
42 #include <stdlib.h>
43 #include <unistd.h>
44 #include <memory.h>
45 #include <sys/stat.h>
46 #include <db.h>
47 #include <memory.h>
48 
49 static void
testit(const int klen,const int vlen,const int n,const int lastvlen)50 testit (const int klen, const int vlen, const int n, const int lastvlen) {
51     if (verbose) printf("testit %d %d %d %d\n", klen, vlen, n, lastvlen);
52 
53     int r;
54 
55     // setup test directory
56     toku_os_recursive_delete(TOKU_TEST_FILENAME);
57     toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO);
58 
59     // setup environment
60     DB_ENV *env;
61     {
62         r = db_env_create(&env, 0); assert(r == 0);
63         env->set_errfile(env, stdout);
64         r = env->open(env, TOKU_TEST_FILENAME, DB_INIT_MPOOL + DB_PRIVATE + DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO);
65         assert(r == 0);
66     }
67 
68     // setup database
69     DB *db;
70     {
71         DB_TXN *txn = 0;
72         r = db_create(&db, env, 0); assert(r == 0);
73         r = db->open(db, txn, "test.db", 0, DB_BTREE, DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO); assert(r == 0);
74     }
75 
76     // insert to fill up a node
77     {
78         void *v = toku_malloc(vlen); assert(v); memset(v, 0, vlen);
79         DB_TXN *txn = 0;
80         int i;
81         for (i=0; i<n; i++) {
82             int k = htonl(i);
83             assert(sizeof k == klen);
84             DBT key, val;
85             r = db->put(db, txn, dbt_init(&key, &k, sizeof k), dbt_init(&val, v, vlen), 0);
86             assert(r == 0);
87         }
88         if (lastvlen > 0) {
89             int k = htonl(n);
90             DBT key, val;
91             r = db->put(db, txn, dbt_init(&key, &k, sizeof k), dbt_init(&val, v, lastvlen), 0);
92             assert(r == 0);
93         }
94         toku_free(v);
95     }
96 
97     // add another one to force a node split
98     {
99         void *v = toku_malloc(vlen); assert(v); memset(v, 0, vlen);
100         DB_TXN *txn = 0;
101         int k = htonl(n+1);
102         DBT key, val;
103         r = db->put(db, txn, dbt_init(&key, &k, sizeof k), dbt_init(&val, v, vlen), 0);
104         assert(r == 0);
105         toku_free(v);
106     }
107 
108     // close db
109     r = db->close(db, 0); assert(r == 0);
110 
111     // close env
112     r = env->close(env, 0); assert(r == 0);
113 }
114 
115 int
test_main(int argc,char * const argv[])116 test_main(int argc, char *const argv[]) {
117     parse_args(argc, argv);
118     const int meg = 1024*1024;
119     const int headeroverhead = 12*4;
120     const int numentries = 4;
121     const int klen = 4;
122     const int vlen = 4096;
123     const int leafoverhead = 1+8+4+4;
124     const int leafentrysize = leafoverhead+klen+vlen;
125     int n = (meg - headeroverhead - numentries) / leafentrysize;
126     int left = meg - headeroverhead - numentries - n*leafentrysize;
127     int lastvlen = left - leafoverhead - klen;
128     testit(klen, vlen, n, lastvlen-1);
129     testit(klen, vlen, n, lastvlen-0);
130     testit(klen, vlen, n, lastvlen+1);
131     return 0;
132 }
133