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 // verify that a db->put with NOOVERWRITE grabs a write lock not a read lock.
42 // we use two transactions.  the first transaction tries to put with NOOVERWRITE
43 // and finds that the key already exists.  it now holds a write lock on the key.
44 // the second transaction trys to put the same key with NOOVERWRITE and gets
45 // LOCK_NOTGRANTED.  the second transaction can not put the key until the first
46 // transaction commits.
47 
test_main(int argc,char * const argv[])48 int test_main(int argc, char * const argv[]) {
49     int r;
50 
51     const char *env_dir = TOKU_TEST_FILENAME;
52     const char *db_filename = "replacetest";
53 
54     parse_args(argc, argv);
55 
56     char rm_cmd[strlen(env_dir) + strlen("rm -rf ") + 1];
57     snprintf(rm_cmd, sizeof(rm_cmd), "rm -rf %s", env_dir);
58     r = system(rm_cmd); assert_zero(r);
59 
60     r = toku_os_mkdir(env_dir, S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH); assert_zero(r);
61 
62     DB_ENV *env = NULL;
63     r = db_env_create(&env, 0); assert_zero(r);
64     int env_open_flags = DB_CREATE | DB_PRIVATE | DB_INIT_MPOOL | DB_INIT_TXN | DB_INIT_LOCK | DB_INIT_LOG;
65     r = env->open(env, env_dir, env_open_flags, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); assert_zero(r);
66 
67     // create the db
68     DB *db = NULL;
69     r = db_create(&db, env, 0); assert_zero(r);
70     DB_TXN *create_txn = NULL;
71     r = env->txn_begin(env, NULL, &create_txn, 0); assert_zero(r);
72     r = db->open(db, create_txn, db_filename, NULL, DB_BTREE, DB_CREATE, S_IRUSR | S_IWUSR | S_IRGRP | S_IROTH); assert_zero(r);
73     r = create_txn->commit(create_txn, 0); assert_zero(r);
74 
75     DB_TXN *write_txn = NULL;
76     r = env->txn_begin(env, NULL, &write_txn, 0); assert_zero(r);
77 
78     int k = htonl(42); int v = 42;
79     DBT key; dbt_init(&key, &k, sizeof k);
80     DBT val; dbt_init(&val, &v, sizeof v);
81     r = db->put(db, write_txn, &key, &val, DB_NOOVERWRITE); assert_zero(r);
82     r = write_txn->commit(write_txn, 0); assert_zero(r);
83 
84     DB_TXN *txn1 = NULL;
85     r = env->txn_begin(env, NULL, &txn1, 0); assert_zero(r);
86 
87     DB_TXN *txn2 = NULL;
88     r = env->txn_begin(env, NULL, &txn2, 0); assert_zero(r);
89 
90     r = db->put(db, txn1, &key, &val, DB_NOOVERWRITE); assert(r == DB_KEYEXIST);
91     r = db->put(db, txn2, &key, &val, DB_NOOVERWRITE); assert(r == DB_LOCK_NOTGRANTED);
92     r = db->put(db, txn1, &key, &val, 0); assert_zero(r);
93     r = db->put(db, txn2, &key, &val, 0); assert(r == DB_LOCK_NOTGRANTED);
94     r = txn1->commit(txn1, 0); assert_zero(r);
95     r = db->put(db, txn2, &key, &val, 0); assert_zero(r);
96     r = txn2->commit(txn2, 0); assert_zero(r);
97 
98     r = db->close(db, 0); assert_zero(r);
99 
100     r = env->close(env, 0); assert_zero(r);
101     return 0;
102 }
103