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 <toku_assert.h> |
40 | |
41 | void concurrent_tree::create(const comparator *cmp) { |
42 | // start with an empty root node. we do this instead of |
43 | // setting m_root to null so there's always a root to lock |
44 | m_root.create_root(cmp); |
45 | } |
46 | |
47 | void concurrent_tree::destroy(void) { |
48 | m_root.destroy_root(); |
49 | } |
50 | |
51 | bool concurrent_tree::is_empty(void) { |
52 | return m_root.is_empty(); |
53 | } |
54 | |
55 | uint64_t concurrent_tree::get_insertion_memory_overhead(void) { |
56 | return sizeof(treenode); |
57 | } |
58 | |
59 | void concurrent_tree::locked_keyrange::prepare(concurrent_tree *tree) { |
60 | // the first step in acquiring a locked keyrange is locking the root |
61 | treenode *const root = &tree->m_root; |
62 | m_tree = tree; |
63 | m_subtree = root; |
64 | m_range = keyrange::get_infinite_range(); |
65 | root->mutex_lock(); |
66 | } |
67 | |
68 | void concurrent_tree::locked_keyrange::acquire(const keyrange &range) { |
69 | treenode *const root = &m_tree->m_root; |
70 | |
71 | treenode *subtree; |
72 | if (root->is_empty() || root->range_overlaps(range)) { |
73 | subtree = root; |
74 | } else { |
75 | // we do not have a precomputed comparison hint, so pass null |
76 | const keyrange::comparison *cmp_hint = nullptr; |
77 | subtree = root->find_node_with_overlapping_child(range, cmp_hint); |
78 | } |
79 | |
80 | // subtree is locked. it will be unlocked when this is release()'d |
81 | invariant_notnull(subtree); |
82 | m_range = range; |
83 | m_subtree = subtree; |
84 | } |
85 | |
86 | void concurrent_tree::locked_keyrange::release(void) { |
87 | m_subtree->mutex_unlock(); |
88 | } |
89 | |
90 | template <class F> |
91 | void concurrent_tree::locked_keyrange::iterate(F *function) const { |
92 | // if the subtree is non-empty, traverse it by calling the given |
93 | // function on each range, txnid pair found that overlaps. |
94 | if (!m_subtree->is_empty()) { |
95 | m_subtree->traverse_overlaps(m_range, function); |
96 | } |
97 | } |
98 | |
99 | void concurrent_tree::locked_keyrange::insert(const keyrange &range, TXNID txnid) { |
100 | // empty means no children, and only the root should ever be empty |
101 | if (m_subtree->is_empty()) { |
102 | m_subtree->set_range_and_txnid(range, txnid); |
103 | } else { |
104 | m_subtree->insert(range, txnid); |
105 | } |
106 | } |
107 | |
108 | void concurrent_tree::locked_keyrange::remove(const keyrange &range) { |
109 | invariant(!m_subtree->is_empty()); |
110 | treenode *new_subtree = m_subtree->remove(range); |
111 | // if removing range changed the root of the subtree, |
112 | // then the subtree must be the root of the entire tree. |
113 | if (new_subtree == nullptr) { |
114 | invariant(m_subtree->is_root()); |
115 | invariant(m_subtree->is_empty()); |
116 | } |
117 | } |
118 | |
119 | void concurrent_tree::locked_keyrange::remove_all(void) { |
120 | m_subtree->recursive_remove(); |
121 | } |
122 | |