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 <db.h>
41 #include <sys/stat.h>
42 
43 unsigned char N=5;
44 
45 DB_ENV *env;
46 DB *db;
47 
48 static void
run(void)49 run (void) {
50     int r;
51     DB_TXN *txn, *txn2;
52     char v101=101, v102=102, v1=1, v2=1;
53     // Add (1,102) to the tree
54     // In one txn
55     //   add (1,101) to the tree
56     // In another concurrent txn
57     //   look up (1,102) and do  DB_NEXT
58     // That should be fine in PerconaFT.
59     // It fails before #938 is fixed.
60     // It also fails for BDB for other reasons (page-level locking vs. row-level locking)
61     {
62 	r=env->txn_begin(env, 0, &txn, 0);                            CKERR(r);
63 	char kk[2] = {v2, v102};
64 	DBT k,v;
65 	r=db->put(db, txn, dbt_init(&k, &kk, 2), dbt_init(&v, &v102, 1), 0); CKERR(r);
66 
67 	r=txn->commit(txn, 0);                                        CKERR(r);
68     }
69     {
70 	r=env->txn_begin(env, 0, &txn, 0);                            CKERR(r);
71 	r=env->txn_begin(env, 0, &txn2, 0);                           CKERR(r);
72 
73 	DBT k,v;
74 	{
75 	    char kk[2] = {v1, v101};
76 	    r=db->put(db, txn, dbt_init(&k, &kk, 2), dbt_init(&v, &v101, 1), 0); CKERR(r);
77 	}
78 
79 	DBC *c2;
80 	r=db->cursor(db, txn2, &c2, 0);                                CKERR(r);
81 
82 	{
83 	    char kk[2] = {v2, v102};
84 	    r=c2->c_get(c2, dbt_init(&k, &kk, 2), dbt_init(&v, &v102, 1), DB_SET); CKERR(r);
85 	}
86 	r=c2->c_get(c2, dbt_init_malloc(&k), dbt_init_malloc(&v), DB_NEXT);  assert(r==DB_NOTFOUND);
87 
88 	r=c2->c_close(c2);
89 	r=txn->commit(txn, 0);                                             CKERR(r);
90 	r=txn2->commit(txn2, 0);                                             CKERR(r);
91     }
92 }
93 
94 int
test_main(int argc,char * const argv[])95 test_main(int argc, char *const argv[]) {
96     parse_args(argc, argv);
97 
98     int r;
99     toku_os_recursive_delete(TOKU_TEST_FILENAME);
100     toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO);
101 
102     DB_TXN *txn;
103     {
104         r = db_env_create(&env, 0);                                   CKERR(r);
105 	r = env->set_redzone(env, 0);                                 CKERR(r);
106 	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);
107 	env->set_errfile(env, stderr);
108 	r=env->txn_begin(env, 0, &txn, 0);                            CKERR(r);
109 	r=db_create(&db, env, 0);                                     CKERR(r);
110 	r=db->open(db, txn, "foo.db", 0, DB_BTREE, DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO);  CKERR(r);
111 	r=txn->commit(txn, 0);                                        CKERR(r);
112     }
113     run();
114     {
115 	r=db->close(db, 0);                                           CKERR(r);
116 	r=env->close(env, 0);                                         CKERR(r);
117     }
118 
119     return 0;
120 }
121