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 
38    Licensed under the Apache License, Version 2.0 (the "License");
39    you may not use this file except in compliance with the License.
40    You may obtain a copy of the License at
41 
42        http://www.apache.org/licenses/LICENSE-2.0
43 
44    Unless required by applicable law or agreed to in writing, software
45    distributed under the License is distributed on an "AS IS" BASIS,
46    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
47    See the License for the specific language governing permissions and
48    limitations under the License.
49 ======= */
50 
51 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
52 
53 #include <toku_assert.h>
54 
create(const comparator * cmp)55 void concurrent_tree::create(const comparator *cmp) {
56     // start with an empty root node. we do this instead of
57     // setting m_root to null so there's always a root to lock
58     m_root.create_root(cmp);
59 }
60 
destroy(void)61 void concurrent_tree::destroy(void) {
62     m_root.destroy_root();
63 }
64 
is_empty(void)65 bool concurrent_tree::is_empty(void) {
66     return m_root.is_empty();
67 }
68 
get_insertion_memory_overhead(void)69 uint64_t concurrent_tree::get_insertion_memory_overhead(void) {
70     return sizeof(treenode);
71 }
72 
prepare(concurrent_tree * tree)73 void concurrent_tree::locked_keyrange::prepare(concurrent_tree *tree) {
74     // the first step in acquiring a locked keyrange is locking the root
75     treenode *const root = &tree->m_root;
76     m_tree = tree;
77     m_subtree = root;
78     m_range = keyrange::get_infinite_range();
79     root->mutex_lock();
80 }
81 
acquire(const keyrange & range)82 void concurrent_tree::locked_keyrange::acquire(const keyrange &range) {
83     treenode *const root = &m_tree->m_root;
84 
85     treenode *subtree;
86     if (root->is_empty() || root->range_overlaps(range)) {
87         subtree = root;
88     } else {
89         // we do not have a precomputed comparison hint, so pass null
90         const keyrange::comparison *cmp_hint = nullptr;
91         subtree = root->find_node_with_overlapping_child(range, cmp_hint);
92     }
93 
94     // subtree is locked. it will be unlocked when this is release()'d
95     invariant_notnull(subtree);
96     m_range = range;
97     m_subtree = subtree;
98 }
99 
release(void)100 void concurrent_tree::locked_keyrange::release(void) {
101     m_subtree->mutex_unlock();
102 }
103 
104 template <class F>
iterate(F * function) const105 void concurrent_tree::locked_keyrange::iterate(F *function) const {
106     // if the subtree is non-empty, traverse it by calling the given
107     // function on each range, txnid pair found that overlaps.
108     if (!m_subtree->is_empty()) {
109         m_subtree->traverse_overlaps(m_range, function);
110     }
111 }
112 
insert(const keyrange & range,TXNID txnid)113 void concurrent_tree::locked_keyrange::insert(const keyrange &range, TXNID txnid) {
114     // empty means no children, and only the root should ever be empty
115     if (m_subtree->is_empty()) {
116         m_subtree->set_range_and_txnid(range, txnid);
117     } else {
118         m_subtree->insert(range, txnid);
119     }
120 }
121 
remove(const keyrange & range)122 void concurrent_tree::locked_keyrange::remove(const keyrange &range) {
123     invariant(!m_subtree->is_empty());
124     treenode *new_subtree = m_subtree->remove(range);
125     // if removing range changed the root of the subtree,
126     // then the subtree must be the root of the entire tree.
127     if (new_subtree == nullptr) {
128         invariant(m_subtree->is_root());
129         invariant(m_subtree->is_empty());
130     }
131 }
132 
remove_all(void)133 void concurrent_tree::locked_keyrange::remove_all(void) {
134     m_subtree->recursive_remove();
135 }
136