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 "toku_pthread.h"
41 #include <db.h>
42 #include <sys/stat.h>
43
44 static const char *envdir = TOKU_TEST_FILENAME;
45
46 DB_ENV *env;
47 int DISALLOW_PUTS=0;
48 int COMPRESS=0;
49 enum {MAX_NAME=128};
50 enum {NUM_DBS=1};
51 enum {NUM_KV_PAIRS=3};
52 struct kv_pair {
53 int64_t key;
54 int64_t val;
55 };
56 struct kv_pair kv_pairs[NUM_KV_PAIRS] = {{1,4},
57 {2,5},
58 {3,6}};
59
put_multiple_generate(DB * dest_db,DB * src_db,DBT_ARRAY * dest_keys,DBT_ARRAY * dest_vals,const DBT * src_key,const DBT * src_val)60 static int put_multiple_generate(DB *dest_db, DB *src_db, DBT_ARRAY *dest_keys, DBT_ARRAY *dest_vals, const DBT *src_key, const DBT *src_val) {
61 toku_dbt_array_resize(dest_keys, 1);
62 toku_dbt_array_resize(dest_vals, 1);
63 DBT *dest_key = &dest_keys->dbts[0];
64 DBT *dest_val = &dest_vals->dbts[0];
65 dest_key->flags = 0;
66 dest_val->flags = 0;
67
68 (void) src_db;
69
70 uint32_t which = *(uint32_t*)dest_db->app_private;
71 assert(which == 0);
72
73 dbt_init(dest_key, src_key->data, src_key->size);
74 dbt_init(dest_val, src_val->data, src_val->size);
75
76 // printf("dest_key.data = %d\n", *(int*)dest_key->data);
77 // printf("dest_val.data = %d\n", *(int*)dest_val->data);
78
79 return 0;
80 }
81
test_loader(DB ** dbs)82 static void test_loader(DB **dbs)
83 {
84 int r;
85 DB_TXN *txn;
86 DB_LOADER *loader;
87 uint32_t db_flags[NUM_DBS];
88 uint32_t dbt_flags[NUM_DBS];
89 for(int i=0;i<NUM_DBS;i++) {
90 db_flags[i] = DB_NOOVERWRITE;
91 dbt_flags[i] = 0;
92 }
93 uint32_t loader_flags = DISALLOW_PUTS | COMPRESS; // set with -p option
94
95 // create and initialize loader
96 r = env->txn_begin(env, NULL, &txn, 0);
97 CKERR(r);
98 r = env->create_loader(env, txn, &loader, dbs[0], NUM_DBS, dbs, db_flags, dbt_flags, loader_flags);
99 CKERR(r);
100 r = loader->set_error_callback(loader, NULL, NULL);
101 CKERR(r);
102 r = loader->set_poll_function(loader, NULL, NULL);
103 CKERR(r);
104
105 /* // using loader->put, put values into DB
106 DBT key, val;
107 for(int i=0;i<NUM_KV_PAIRS;i++) {
108 dbt_init(&key, &kv_pairs[i].key, sizeof(kv_pairs[i].key));
109 dbt_init(&val, &kv_pairs[i].val, sizeof(kv_pairs[i].val));
110 r = loader->put(loader, &key, &val);
111 if (DISALLOW_PUTS) {
112 CKERR2(r, EINVAL);
113 } else {
114 CKERR(r);
115 }
116 }
117 */
118 // close the loader
119 r = loader->close(loader);
120 CKERR(r);
121 r = txn->commit(txn, 0);
122 CKERR(r);
123
124 // verify the DBs
125 /*
126 DBC *cursor;
127 r = env->txn_begin(env, NULL, &txn, 0);
128 CKERR(r);
129
130 for(int j=0;j<NUM_DBS;j++) {
131 r = dbs[j]->cursor(dbs[j], txn, &cursor, 0);
132 CKERR(r);
133 for(int i=0;i<NUM_KV_PAIRS;i++) {
134 r = cursor->c_get(cursor, &key, &val, DB_NEXT);
135 if (DISALLOW_PUTS) {
136 CKERR2(r, DB_NOTFOUND);
137 } else {
138 if (r!=0) { fprintf(stderr, "r==%d, failure\n", r); }
139 CKERR(r);
140 assert(*(int64_t*)key.data == kv_pairs[i].key);
141 assert(*(int64_t*)val.data == kv_pairs[i].val);
142 }
143 }
144 cursor->c_close(cursor);
145 }
146 r = txn->commit(txn, 0);
147 CKERR(r);
148 */
149
150 printf("PASS\n");
151 }
152
run_test(void)153 static void run_test(void)
154 {
155 int r;
156 char rmcmd[32 + strlen(envdir)];
157 snprintf(rmcmd, sizeof rmcmd, "rm -rf %s", envdir);
158 r = system(rmcmd); CKERR(r);
159 r = toku_os_mkdir(envdir, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
160
161 r = db_env_create(&env, 0); CKERR(r);
162 r = env->set_default_bt_compare(env, int64_dbt_cmp); CKERR(r);
163 r = env->set_generate_row_callback_for_put(env, put_multiple_generate);
164 CKERR(r);
165 // int envflags = DB_INIT_LOCK | DB_INIT_MPOOL | DB_INIT_TXN | DB_CREATE | DB_PRIVATE | DB_INIT_LOG;
166 int envflags = DB_INIT_LOCK | DB_INIT_LOG | DB_INIT_MPOOL | DB_INIT_TXN | DB_CREATE | DB_PRIVATE | DB_INIT_LOG;
167 r = env->open(env, envdir, envflags, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
168 env->set_errfile(env, stderr);
169 //Disable auto-checkpointing
170 r = env->checkpointing_set_period(env, 0); CKERR(r);
171
172 DBT desc;
173 dbt_init(&desc, "foo", sizeof("foo"));
174 char name[MAX_NAME*2];
175
176 DB *dbs[NUM_DBS];
177 int idx[NUM_DBS];
178 for(int i=0;i<NUM_DBS;i++) {
179 idx[i] = i;
180 r = db_create(&dbs[i], env, 0); CKERR(r);
181 dbs[i]->app_private = &idx[i];
182 snprintf(name, sizeof(name), "db_%04x", i);
183 r = dbs[i]->open(dbs[i], NULL, name, NULL, DB_BTREE, DB_CREATE, 0666); CKERR(r);
184 IN_TXN_COMMIT(env, NULL, txn_desc, 0, {
185 { int chk_r = dbs[i]->change_descriptor(dbs[i], txn_desc, &desc, 0); CKERR(chk_r); }
186 });
187 }
188
189 // -------------------------- //
190 test_loader(dbs);
191 // -------------------------- //
192
193 for(int i=0;i<NUM_DBS;i++) {
194 dbs[i]->close(dbs[i], 0); CKERR(r);
195 dbs[i] = NULL;
196 }
197 r = env->close(env, 0); CKERR(r);
198 }
199
200 // ------------ infrastructure ----------
201 static void do_args(int argc, char * const argv[]);
202
test_main(int argc,char * const * argv)203 int test_main(int argc, char * const *argv) {
204 do_args(argc, argv);
205 run_test();
206 return 0;
207 }
208
do_args(int argc,char * const argv[])209 static void do_args(int argc, char * const argv[]) {
210 int resultcode;
211 char *cmd = argv[0];
212 argc--; argv++;
213 while (argc>0) {
214 if (strcmp(argv[0], "-v")==0) {
215 verbose++;
216 } else if (strcmp(argv[0],"-q")==0) {
217 verbose--;
218 if (verbose<0) verbose=0;
219 } else if (strcmp(argv[0], "-h")==0) {
220 resultcode=0;
221 do_usage:
222 fprintf(stderr, "Usage:\n%s\n", cmd);
223 exit(resultcode);
224 } else if (strcmp(argv[0], "-v")==0) {
225 verbose++;
226 } else if (strcmp(argv[0],"-q")==0) {
227 verbose--;
228 if (verbose<0) verbose=0;
229 } else if (strcmp(argv[0], "-z")==0) {
230 COMPRESS = LOADER_COMPRESS_INTERMEDIATES;
231 } else if (strcmp(argv[0], "-p")==0) {
232 DISALLOW_PUTS = LOADER_DISALLOW_PUTS;
233 } else if (strcmp(argv[0], "-e") == 0) {
234 argc--; argv++;
235 if (argc > 0)
236 envdir = argv[0];
237 } else {
238 fprintf(stderr, "Unknown arg: %s\n", argv[0]);
239 resultcode=1;
240 goto do_usage;
241 }
242 argc--;
243 argv++;
244 }
245 }
246