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 /**
40 * Test that unique inserts work correctly. This exercises the rightmost leaf inject optimization.
41 */
42
43 #include <portability/toku_random.h>
44
45 #include "test.h"
46
47 static char random_buf[8];
48 static struct random_data random_data;
49
test_simple_unique_insert(DB_ENV * env)50 static void test_simple_unique_insert(DB_ENV *env) {
51 int r;
52 DB *db;
53 r = db_create(&db, env, 0); CKERR(r);
54 r = db->open(db, NULL, "db", NULL, DB_BTREE, DB_CREATE, 0644); CKERR(r);
55
56 DBT key1, key2, key3;
57 dbt_init(&key1, "a", sizeof("a"));
58 dbt_init(&key2, "b", sizeof("b"));
59 dbt_init(&key3, "c", sizeof("c"));
60 r = db->put(db, NULL, &key1, &key1, DB_NOOVERWRITE); CKERR(r);
61 r = db->put(db, NULL, &key1, &key1, DB_NOOVERWRITE); CKERR2(r, DB_KEYEXIST);
62 r = db->put(db, NULL, &key3, &key3, DB_NOOVERWRITE); CKERR(r);
63 r = db->put(db, NULL, &key3, &key3, DB_NOOVERWRITE); CKERR2(r, DB_KEYEXIST);
64 r = db->put(db, NULL, &key2, &key2, DB_NOOVERWRITE); CKERR(r);
65 r = db->put(db, NULL, &key2, &key2, DB_NOOVERWRITE); CKERR2(r, DB_KEYEXIST);
66 // sanity check
67 r = db->put(db, NULL, &key1, &key1, DB_NOOVERWRITE); CKERR2(r, DB_KEYEXIST);
68 r = db->put(db, NULL, &key1, &key3, DB_NOOVERWRITE); CKERR2(r, DB_KEYEXIST);
69
70 r = db->close(db, 0); CKERR(r);
71 r = env->dbremove(env, NULL, "db", NULL, 0); CKERR(r);
72 }
73
test_large_sequential_insert_unique(DB_ENV * env)74 static void test_large_sequential_insert_unique(DB_ENV *env) {
75 int r;
76 DB *db;
77 r = db_create(&db, env, 0); CKERR(r);
78
79 // very small nodes/basements to make a taller tree
80 r = db->set_pagesize(db, 8 * 1024); CKERR(r);
81 r = db->set_readpagesize(db, 2 * 1024); CKERR(r);
82 r = db->open(db, NULL, "db", NULL, DB_BTREE, DB_CREATE, 0644); CKERR(r);
83
84 const int val_size = 8;
85 char *XMALLOC_N(val_size, val_buf);
86 memset(val_buf, 'k', val_size);
87 DBT val;
88 dbt_init(&val, val_buf, val_size);
89
90 // grow a tree to about depth 3, taking sanity checks along the way
91 const int start_num_rows = (64 * 1024 * 1024) / val_size;
92 for (int i = 0; i < start_num_rows; i++) {
93 DBT key;
94 int k = toku_htonl(i);
95 dbt_init(&key, &k, sizeof(k));
96 r = db->put(db, NULL, &key, &val, DB_NOOVERWRITE); CKERR(r);
97 if (i % 50 == 0) {
98 // sanity check - should not be able to insert this key twice in a row
99 r = db->put(db, NULL, &key, &val, DB_NOOVERWRITE); CKERR2(r, DB_KEYEXIST);
100
101 // .. but re-inserting is okay, if we provisionally deleted the row
102 DB_TXN *txn;
103 r = env->txn_begin(env, NULL, &txn, 0); CKERR(r);
104 r = db->del(db, txn, &key, DB_DELETE_ANY); CKERR(r);
105 r = db->put(db, txn, &key, &val, DB_NOOVERWRITE); CKERR(r);
106 r = txn->commit(txn, 0); CKERR(r);
107
108 // re-inserting is also ok if we actually delete the row, for some key < k
109 if (i > 0) {
110 DBT other_key;
111 int other_k = toku_htonl(i - 10);
112 dbt_init(&other_key, &other_k, sizeof(other_k));
113 r = db->del(db, NULL, &other_key, DB_DELETE_ANY); CKERR(r);
114 r = db->put(db, NULL, &other_key, &val, DB_NOOVERWRITE); CKERR(r);
115 }
116 }
117 if (i > 0 && i % 250 == 0) {
118 // sanity check - unique checks on random keys we already inserted should
119 // fail (exercises middle-of-the-tree checks)
120 for (int check_i = 0; check_i < 4; check_i++) {
121 DBT rand_key;
122 int rand_k = toku_htonl(myrandom_r(&random_data) % i);
123 dbt_init(&rand_key, &rand_k, sizeof(rand_k));
124 r = db->put(db, NULL, &rand_key, &val, DB_NOOVERWRITE); CKERR2(r, DB_KEYEXIST);
125 }
126 }
127 }
128
129 toku_free(val_buf);
130 r = db->close(db, 0); CKERR(r);
131 r = env->dbremove(env, NULL, "db", NULL, 0); CKERR(r);
132 }
133
134
test_main(int argc,char * const argv[])135 int test_main(int argc, char * const argv[]) {
136 default_parse_args(argc, argv);
137
138 int r;
139 const int envflags = DB_INIT_MPOOL | DB_CREATE | DB_THREAD |
140 DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_TXN | DB_PRIVATE;
141
142 // startup
143 DB_ENV *env;
144 toku_os_recursive_delete(TOKU_TEST_FILENAME);
145 r = toku_os_mkdir(TOKU_TEST_FILENAME, 0755); CKERR(r);
146 r = db_env_create(&env, 0); CKERR(r);
147 r = env->open(env, TOKU_TEST_FILENAME, envflags, 0755);
148
149 r = myinitstate_r(random(), random_buf, 8, &random_data); CKERR(r);
150
151 test_simple_unique_insert(env);
152 test_large_sequential_insert_unique(env);
153
154 // cleanup
155 r = env->close(env, 0); CKERR(r);
156
157 return 0;
158 }
159
160