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 <sys/wait.h>
41 
42 const int envflags = DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_MPOOL|DB_INIT_TXN|DB_CREATE|DB_PRIVATE|DB_RECOVER;
43 
44 const int my_lg_max = 100;
45 
test_main(int UU (argc),char UU (* const argv[]))46 int test_main (int UU(argc), char UU(*const argv[])) {
47     int r;
48     pid_t pid;
49 
50     toku_os_recursive_delete(TOKU_TEST_FILENAME);
51     r = toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO);                                 CKERR(r);
52 
53     const int N = 5;
54 
55     if (0==(pid=fork())) {
56 	DB_ENV *env;
57 	r = db_env_create(&env, 0);                                                         CKERR(r);
58 	r = env->set_lg_max(env, my_lg_max);                                                CKERR(r);
59 	r = env->open(env, TOKU_TEST_FILENAME, envflags, S_IRWXU+S_IRWXG+S_IRWXO);                      CKERR(r);
60 	DB_TXN *txn;
61 	r = env->txn_begin(env, NULL, &txn, 0);                                             CKERR(r);
62 	DB *db;
63 	r = db_create(&db, env, 0);                                                         CKERR(r);
64 	r = db->open(db, txn, "test.db", 0, DB_BTREE, DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO);  CKERR(r);
65 	r = txn->commit(txn, 0);                                                            CKERR(r);
66 
67 	r = env->txn_begin(env, NULL, &txn, 0);                                             CKERR(r);
68 	r = env->txn_checkpoint(env, 0, 0, 0);                                              CKERR(r);
69 	for (int i=0; i<N; i++) {
70 	    DBT k,v;
71 	    r = db->put(db, txn, dbt_init(&k, &i, sizeof(i)), dbt_init(&v, &i, sizeof(i)), 0); CKERR(r);
72 	    r = env->txn_checkpoint(env, 0, 0, 0);                                             CKERR(r);
73 	}
74 	r = txn->commit(txn, 0);                                                            CKERR(r);
75 
76 	r = env->txn_begin(env, NULL, &txn, 0);                                             CKERR(r);
77 	r = env->txn_checkpoint(env, 0, 0, 0);                                              CKERR(r);
78 	for (int i=0; i<N; i+=2) {
79 	    DBT k;
80 	    r = db->del(db, txn, dbt_init(&k, &i, sizeof(i)), 0);                           CKERR(r);
81 	    r = env->txn_checkpoint(env, 0, 0, 0);                                          CKERR(r);
82 	}
83 	r = txn->commit(txn, 0);                                                            CKERR(r);
84 	exit(0);
85 
86     }
87     {
88 	int status;
89 	pid_t pid2 = wait(&status);
90 	assert(pid2==pid);
91 	assert(WIFEXITED(status) && WEXITSTATUS(status)==0);
92     }
93     // Now run recovery to see what happens.
94     DB_ENV *env;
95     r = db_env_create(&env, 0);                                                         CKERR(r);
96     r = env->open(env, TOKU_TEST_FILENAME, envflags, S_IRWXU+S_IRWXG+S_IRWXO);                      CKERR(r);
97     DB_TXN *txn;
98     r = env->txn_begin(env, NULL, &txn, 0);                                             CKERR(r);
99     DB *db;
100     r = db_create(&db, env, 0);                                                         CKERR(r);
101     r = db->open(db, txn, "test.db", 0, DB_BTREE, DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO);  CKERR(r);
102     for (int i=0; i<N; i++) {
103 	DBT k;
104 	DBT v;
105         dbt_init(&v, NULL, 0);
106 	r = db->get(db, txn, dbt_init(&k, &i, sizeof(i)), &v, 0);
107 	if (i%2==1) {
108 	    assert(r==0);
109 	    //printf("Got %d\n", *(int*)v.data);
110 	} else {
111 	    assert(r==DB_NOTFOUND);
112 	}
113     }
114     r = txn->commit(txn, 0);                                                            CKERR(r);
115     r = db->close(db, 0);                                                               CKERR(r);
116     r = env->close(env, 0);                                                             CKERR(r);
117 
118     //toku_os_recursive_delete(TOKU_TEST_FILENAME);
119 
120     return 0;
121 }
122