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