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 "locktree_unit_test.h"
40 
41 namespace toku {
42 
43 // test that ranges with infinite endpoints work
test_infinity(void)44 void locktree_unit_test::test_infinity(void) {
45     locktree lt;
46 
47     DICTIONARY_ID dict_id = { 1 };
48     lt.create(nullptr, dict_id, dbt_comparator);
49 
50     int r;
51     TXNID txnid_a = 1001;
52     TXNID txnid_b = 2001;
53     const DBT *zero = get_dbt(0);
54     const DBT *one = get_dbt(1);
55     const DBT *two = get_dbt(2);
56     const DBT *five = get_dbt(5);
57     const DBT min_int = min_dbt();
58     const DBT max_int = max_dbt();
59 
60     // txn A will lock -inf, 5.
61     r = lt.acquire_write_lock(txnid_a, toku_dbt_negative_infinity(), five, nullptr, false);
62     invariant(r == 0);
63     // txn B will fail to get any lock <= 5, even min_int
64     r = lt.acquire_write_lock(txnid_b, five, five, nullptr, false);
65     invariant(r == DB_LOCK_NOTGRANTED);
66     r = lt.acquire_write_lock(txnid_b, zero, one, nullptr, false);
67     invariant(r == DB_LOCK_NOTGRANTED);
68     r = lt.acquire_write_lock(txnid_b, &min_int, &min_int, nullptr, false);
69     invariant(r == DB_LOCK_NOTGRANTED);
70     r = lt.acquire_write_lock(txnid_b, toku_dbt_negative_infinity(), &min_int, nullptr, false);
71     invariant(r == DB_LOCK_NOTGRANTED);
72 
73     lt.remove_overlapping_locks_for_txnid(txnid_a, toku_dbt_negative_infinity(), five);
74 
75     // txn A will lock 1, +inf
76     r = lt.acquire_write_lock(txnid_a, one, toku_dbt_positive_infinity(), nullptr, false);
77     invariant(r == 0);
78     // txn B will fail to get any lock >= 1, even max_int
79     r = lt.acquire_write_lock(txnid_b, one, one, nullptr, false);
80     invariant(r == DB_LOCK_NOTGRANTED);
81     r = lt.acquire_write_lock(txnid_b, two, five, nullptr, false);
82     invariant(r == DB_LOCK_NOTGRANTED);
83     r = lt.acquire_write_lock(txnid_b, &max_int, &max_int, nullptr, false);
84     invariant(r == DB_LOCK_NOTGRANTED);
85     r = lt.acquire_write_lock(txnid_b, &max_int, toku_dbt_positive_infinity(), nullptr, false);
86     invariant(r == DB_LOCK_NOTGRANTED);
87 
88     lt.remove_overlapping_locks_for_txnid(txnid_a, toku_dbt_negative_infinity(), five);
89 
90     // txn A will lock -inf, +inf
91     r = lt.acquire_write_lock(txnid_a, toku_dbt_negative_infinity(), toku_dbt_positive_infinity(), nullptr, false);
92     invariant(r == 0);
93     // txn B will fail to get any lock
94     r = lt.acquire_write_lock(txnid_b, zero, one, nullptr, false);
95     invariant(r == DB_LOCK_NOTGRANTED);
96     r = lt.acquire_write_lock(txnid_b, two, five, nullptr, false);
97     invariant(r == DB_LOCK_NOTGRANTED);
98     r = lt.acquire_write_lock(txnid_b, &min_int, &min_int, nullptr, false);
99     invariant(r == DB_LOCK_NOTGRANTED);
100     r = lt.acquire_write_lock(txnid_b, &min_int, &max_int, nullptr, false);
101     invariant(r == DB_LOCK_NOTGRANTED);
102     r = lt.acquire_write_lock(txnid_b, &max_int, &max_int, nullptr, false);
103     invariant(r == DB_LOCK_NOTGRANTED);
104     r = lt.acquire_write_lock(txnid_b, toku_dbt_negative_infinity(), toku_dbt_negative_infinity(), nullptr, false);
105     invariant(r == DB_LOCK_NOTGRANTED);
106     r = lt.acquire_write_lock(txnid_b, toku_dbt_negative_infinity(), toku_dbt_positive_infinity(), nullptr, false);
107     invariant(r == DB_LOCK_NOTGRANTED);
108     r = lt.acquire_write_lock(txnid_b, toku_dbt_positive_infinity(), toku_dbt_positive_infinity(), nullptr, false);
109     invariant(r == DB_LOCK_NOTGRANTED);
110 
111     lt.remove_overlapping_locks_for_txnid(txnid_a, toku_dbt_negative_infinity(), toku_dbt_positive_infinity());
112 
113     lt.release_reference();
114     lt.destroy();
115 }
116 
117 } /* namespace toku */
118 
main(void)119 int main(void) {
120     toku::locktree_unit_test test;
121     test.test_infinity();
122     return 0;
123 }
124