-
Notifications
You must be signed in to change notification settings - Fork 1
/
BTree.impl.hpp
298 lines (256 loc) · 8.45 KB
/
BTree.impl.hpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
/*****************************************************************************/
/* Milliways - B+ trees and key-value store C++ library */
/* */
/* Copyright 2016 Marco Pantaleoni and J CUBE Inc. Tokyo, Japan. */
/* */
/* Author: Marco Pantaleoni <marco.pantaleoni@gmail.com> */
/* */
/* Licensed under the Apache License, Version 2.0 (the "License"); */
/* you may not use this file except in compliance with the License. */
/* You may obtain a copy of the License at */
/* */
/* http://www.apache.org/licenses/LICENSE-2.0 */
/* */
/* Unless required by applicable law or agreed to in writing, software */
/* distributed under the License is distributed on an "AS IS" BASIS, */
/* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. */
/* See the License for the specific language governing permissions and */
/* limitations under the License. */
/*****************************************************************************/
#ifndef MILLIWAYS_BTREE_H
#include "BTree.h"
#endif
#ifndef MILLIWAYS_BTREE_IMPL_H
//#define MILLIWAYS_BTREE_IMPL_H
namespace milliways {
/* ----------------------------------------------------------------- *
* BTree *
* ----------------------------------------------------------------- */
template < int B_, typename KeyTraits, typename TTraits, class Compare >
BTree<B_, KeyTraits, TTraits, Compare>::BTree() :
m_io(NULL), m_io_allocated(false)
{
typedef BTreeMemoryStorage<B_, KeyTraits, TTraits, Compare> btree_memst_t;
btree_memst_t* storage_ = new BTreeMemoryStorage<B_, KeyTraits, TTraits, Compare>();
storage(storage_);
m_io_allocated = true;
assert(m_io == storage_);
assert(m_io_allocated);
}
template < int B_, typename KeyTraits, typename TTraits, class Compare >
BTree<B_, KeyTraits, TTraits, Compare>::BTree(storage_type* storage_) :
m_io(NULL), m_io_allocated(false)
{
m_io_allocated = false;
storage(storage_);
assert(m_io == storage_);
assert(! m_io_allocated);
}
template < int B_, typename KeyTraits, typename TTraits, class Compare >
BTree<B_, KeyTraits, TTraits, Compare>::~BTree()
{
close();
if (m_io_allocated && m_io)
{
/* storage_type* old = m_io; */
storage(NULL);
assert(! m_io);
assert(! m_io_allocated);
// don't delete old - deleted by storage(NULL) !
} else if (m_io)
storage(NULL);
assert(! m_io);
assert(! m_io_allocated);
}
template < int B_, typename KeyTraits, typename TTraits, class Compare >
shptr<typename BTree<B_, KeyTraits, TTraits, Compare>::node_type> BTree<B_, KeyTraits, TTraits, Compare>::insert(const key_type& key_, const mapped_type& value_)
{
shptr<node_type> root_( root() );
assert(root_);
if (root_->full())
{
shptr<node_type> old_root( root_ );
shptr<node_type> new_root( node_alloc() );
new_root->leaf(false);
assert(new_root->n() == 0);
new_root->child(0) = old_root->id();
new_root->rank(old_root->rank() - 1);
root(new_root);
new_root->split_child(0);
root_ = new_root;
}
return root_->insert_non_full(key_, value_);
}
template < int B_, typename KeyTraits, typename TTraits, class Compare >
shptr<typename BTree<B_, KeyTraits, TTraits, Compare>::node_type> BTree<B_, KeyTraits, TTraits, Compare>::update(const key_type& key_, const mapped_type& value_)
{
lookup_type where;
shptr<node_type> root_( root() );
assert(root_);
if (! root_->search(where, key_))
return insert(key_, value_);
assert(where.found());
shptr<node_type> node_( where.node() );
assert(node_);
node_->value(where.pos()) = value_;
return node_;
}
template < int B_, typename KeyTraits, typename TTraits, class Compare >
bool BTree<B_, KeyTraits, TTraits, Compare>::search(lookup_type& res, const key_type& key_)
{
shptr<node_type> root_( root() );
assert(root_);
return root_->search(res, key_);
}
template < int B_, typename KeyTraits, typename TTraits, class Compare >
bool BTree<B_, KeyTraits, TTraits, Compare>::remove(lookup_type& res, const key_type& key_)
{
shptr<node_type> root_( root() );
assert(root_);
return root_->remove(res, key_);
}
/* -- BTree::iterator ---------------------------------------------- */
template < int B_, typename KeyTraits, typename TTraits, class Compare >
void BTree<B_, KeyTraits, TTraits, Compare>::iterator::update_current()
{
if (m_end || (! m_current.node()))
{
m_current.found(false).nodeReset().pos(-1);
m_end = true;
} else
{
assert(m_current.node());
assert(! m_end);
shptr<node_type> node( m_current.node() );
int pos = m_current.pos();
m_current.found(true).key(node->key(pos));
}
}
template < int B_, typename KeyTraits, typename TTraits, class Compare >
typename BTree<B_, KeyTraits, TTraits, Compare>::iterator& BTree<B_, KeyTraits, TTraits, Compare>::iterator::rewind(bool end_)
{
if (end_ || m_end || (! m_root))
{
m_current.found(false).nodeReset().pos(-1);
m_end = true;
} else
{
assert(m_root);
assert(! end_);
m_end = false;
m_first_node = down(m_root, /* right */ (m_forward ? false : true));
m_current.node(m_first_node).pos(m_forward ? 0 : (m_first_node->n() - 1));
assert(! m_end);
}
update_current();
return *this;
}
template < int B_, typename KeyTraits, typename TTraits, class Compare >
shptr<typename BTree<B_, KeyTraits, TTraits, Compare>::node_type> BTree<B_, KeyTraits, TTraits, Compare>::iterator::down(const shptr<node_type>& node, bool right)
{
shptr<node_type> nodePtr( node );
while (nodePtr && (! nodePtr->leaf()) && node_id_valid(nodePtr->child((right ? nodePtr->n() : 0))))
nodePtr = nodePtr->child_node((right ? nodePtr->n() : 0));
if (nodePtr->leaf())
return nodePtr;
return shptr<node_type>();
}
template < int B_, typename KeyTraits, typename TTraits, class Compare >
bool BTree<B_, KeyTraits, TTraits, Compare>::iterator::next()
{
if (! m_current.node()) {
update_current();
return false; /* stop iteration */
}
assert(m_current.node());
shptr<node_type> node( m_current.node() );
int pos = m_current.pos();
if (m_forward)
{
if (++pos >= node->n())
{
node = node->right();
pos = 0;
}
} else
{
if (--pos < 0)
{
node = node->left();
pos = node ? (node->n() - 1) : 0;
}
}
m_current.pos(pos).node(node);
update_current();
return true;
}
template < int B_, typename KeyTraits, typename TTraits, class Compare >
bool BTree<B_, KeyTraits, TTraits, Compare>::iterator::prev()
{
if (! m_current.node()) {
update_current();
return false; /* stop iteration */
}
assert(m_current.node());
shptr<node_type> node( m_current.node() );
int pos = m_current.pos();
if (m_forward)
{
if (--pos < 0)
{
node = node->left();
pos = node ? (node->n() - 1) : 0;
}
} else
{
if (++pos >= node->n())
{
node = node->right();
pos = 0;
}
}
m_current.pos(pos).node(node);
update_current();
return true;
}
/* ----------------------------------------------------------------- *
* BTreeStorage *
* ----------------------------------------------------------------- */
template < int B_, typename KeyTraits, typename TTraits, class Compare >
bool BTreeStorage<B_, KeyTraits, TTraits, Compare>::open()
{
if (isOpen())
return true;
bool created_ = false;
if (! openHelper(created_))
return false;
m_created = created_;
return postOpen();
}
template < int B_, typename KeyTraits, typename TTraits, class Compare >
bool BTreeStorage<B_, KeyTraits, TTraits, Compare>::close()
{
if (! isOpen())
return true;
if (! preClose())
return false;
return closeHelper();
}
template < int B_, typename KeyTraits, typename TTraits, class Compare >
bool BTreeStorage<B_, KeyTraits, TTraits, Compare>::postOpen()
{
if (! isOpen())
return false;
if (! created())
return header_read();
return true;
}
template < int B_, typename KeyTraits, typename TTraits, class Compare >
bool BTreeStorage<B_, KeyTraits, TTraits, Compare>::preClose()
{
if (! isOpen())
return false;
return header_write();
}
} /* end of namespace milliways */
#endif /* MILLIWAYS_BTREE_IMPL_H */