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 
42 /* Test to see if we can do logging and recovery. */
43 /* This is very specific to PerconaFT.  It won't work with Berkeley DB. */
44 /* This test_log10 inserts to a db, closes, reopens, and inserts more to db.  We want to make sure that the recovery of the buffers works. */
45 /* Lots of stuff gets inserted. */
46 
47 
48 #include <db.h>
49 #include <stdlib.h>
50 #include <sys/stat.h>
51 #include <sys/types.h>
52 #include <memory.h>
53 
54 // TOKU_TEST_FILENAME is defined in the Makefile
55 
56 struct in_db;
57 struct in_db {
58     long int r;
59     int i;
60     struct in_db *next;
61 } *items=0;
62 
63 int maxcount = 10000;
64 
insert_some(int outeri,bool close_env)65 static void insert_some (int outeri, bool close_env) {
66     uint32_t create_flag = outeri%2 ? DB_CREATE : 0; // Sometimes use DB_CREATE, sometimes don't.
67     int r;
68     DB_ENV *env;
69     DB *db;
70     DB_TXN *tid;
71     r=db_env_create(&env, 0); assert(r==0);
72     db_env_enable_engine_status(0);  // disable engine status on crash because test is expected to fail
73 
74     r=env->open(env, TOKU_TEST_FILENAME, DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_MPOOL|DB_INIT_TXN|DB_CREATE|DB_PRIVATE|create_flag, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
75     r=db_create(&db, env, 0); CKERR(r);
76     r=env->txn_begin(env, 0, &tid, 0); assert(r==0);
77     r=db->open(db, tid, "foo.db", 0, DB_BTREE, create_flag, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
78     r=tid->commit(tid, 0);    assert(r==0);
79 
80     r=env->txn_begin(env, 0, &tid, 0); assert(r==0);
81 
82     int i;
83     for (i=0; i<maxcount; i++) {
84 	char hello[30], there[30];
85 	DBT key,data;
86 	struct in_db *XMALLOC(newitem);
87 	newitem->r = random();
88 	newitem->i = i;
89 	newitem->next = items;
90 	items = newitem;
91 	snprintf(hello, sizeof(hello), "hello%ld.%d.%d", newitem->r, outeri, newitem->i);
92 	snprintf(there, sizeof(hello), "there%d", i);
93 	memset(&key, 0, sizeof(key));
94 	memset(&data, 0, sizeof(data));
95 	key.data  = hello; key.size=strlen(hello)+1;
96 	data.data = there; data.size=strlen(there)+1;
97 	r=db->put(db, tid, &key, &data, 0);  CKERR(r);
98     }
99     r=tid->commit(tid, 0);    assert(r==0);
100     r=db->close(db, 0);       assert(r==0);
101     if (close_env) {
102         r=env->close(env, 0);     assert(r==0);
103     }
104 }
105 
make_db(bool close_env)106 static void make_db (bool close_env) {
107     DB_ENV *env;
108     DB *db;
109     DB_TXN *tid;
110     int r;
111     int i;
112 
113     toku_os_recursive_delete(TOKU_TEST_FILENAME);
114     r=toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO);       assert(r==0);
115     r=db_env_create(&env, 0); assert(r==0);
116     db_env_enable_engine_status(0);  // disable engine status on crash because test is expected to fail
117 
118     r=env->open(env, TOKU_TEST_FILENAME, DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_MPOOL|DB_INIT_TXN|DB_CREATE|DB_PRIVATE, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
119     r=db_create(&db, env, 0); CKERR(r);
120     r=env->txn_begin(env, 0, &tid, 0); assert(r==0);
121     r=db->open(db, tid, "foo.db", 0, DB_BTREE, DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO); CKERR(r);
122     r=tid->commit(tid, 0);    assert(r==0);
123     r=db->close(db, 0);  CKERR(r);
124     if (close_env) {
125         r=env->close(env, 0); CKERR(r);
126     }
127 
128     for (i=0; i<10; i++)
129 	insert_some(i, close_env);
130 
131     while (items) {
132 	struct in_db *next=items->next;
133 	toku_free(items);
134 	items=next;
135     }
136 }
137 
138 int
test_main(int argc,char * const argv[])139 test_main (int argc, char *const argv[]) {
140     bool close_env = true;
141     for (int i=1; i<argc; i++) {
142         if (strcmp(argv[i], "--no-shutdown") == 0)
143             close_env = false;
144     }
145     make_db(close_env);
146     return 0;
147 }
148