forked from kohler/masstree-beta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
masstree_remove.hh
260 lines (231 loc) · 7.13 KB
/
masstree_remove.hh
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
/* Masstree
* Eddie Kohler, Yandong Mao, Robert Morris
* Copyright (c) 2012-2013 President and Fellows of Harvard College
* Copyright (c) 2012-2013 Massachusetts Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, subject to the conditions
* listed in the Masstree LICENSE file. These conditions include: you must
* preserve this copyright notice, and you cannot mention the copyright
* holders in advertising related to the Software without their permission.
* The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
* notice is a summary of the Masstree LICENSE file; the license in that file
* is legally binding.
*/
#ifndef MASSTREE_REMOVE_HH
#define MASSTREE_REMOVE_HH
#include "masstree_tcursor.hh"
#include "btree_leaflink.hh"
namespace Masstree {
template <typename P>
bool tcursor<P>::gc_layer(threadinfo *ti)
{
find_locked(ti);
precondition(!n_->deleted() && !n_->deleted_layer());
// find_locked might return early if another gc_layer attempt has
// succeeded at removing multiple tree layers. So check that the whole
// key has been consumed
if (ka_.has_suffix())
return false;
// find the slot for the child tree
// ka_ is a multiple of ikey_size bytes long. We are looking for the entry
// for the next tree layer, which has keylenx_ corresponding to ikey_size+1.
// So if has_value(), then we found an entry for the same ikey, but with
// length ikey_size; we need to adjust ki_.
ki_ += has_value();
if (ki_ >= n_->size())
return false;
permuter_type perm(n_->permutation_);
kp_ = perm[ki_];
if (n_->ikey0_[kp_] != ka_.ikey() || !n_->is_stable_node(kp_))
return false;
// remove redundant internode layers
node_type *layer;
while (1) {
layer = n_->lv_[kp_].node();
if (layer->has_split())
n_->lv_[kp_] = layer = layer->unsplit_ancestor();
if (layer->isleaf())
break;
internode_type *in = static_cast<internode_type *>(layer);
if (in->size() > 0 && !in->has_split())
return false;
in->lock(*in, ti->lock_fence(tc_internode_lock));
if (in->has_split() && !in->parent())
in->mark_root();
if (in->size() > 0 || in->has_split()) {
in->unlock();
return false;
}
node_type *child = in->child_[0];
child->set_parent(0);
n_->lv_[kp_] = child;
in->mark_split();
in->set_parent(child); // ensure concurrent reader finds true root
// NB: now p->parent() might weirdly be a LEAF!
in->unlock();
in->deallocate_rcu(ti);
}
// we are left with a leaf child
leaf_type *lf = static_cast<leaf_type *>(layer);
if (lf->size() > 0 && !lf->has_split())
return false;
lf->lock(*lf, ti->lock_fence(tc_leaf_lock));
if (lf->has_split() && !lf->parent())
lf->mark_root();
if (lf->size() > 0 || lf->has_split()) {
lf->unlock();
return false;
}
// child is an empty leaf: kill it
invariant(!lf->prev_ && !lf->next_.ptr);
invariant(!lf->deleted());
invariant(!lf->deleted_layer());
if (circular_int<kvtimestamp_t>::less(n_->node_ts_, lf->node_ts_))
n_->node_ts_ = lf->node_ts_;
lf->mark_deleted_layer(); // NB DO NOT mark as deleted (see above)
lf->unlock();
lf->deallocate_rcu(ti);
return true;
}
template <typename P>
struct gc_layer_rcu_callback : public rcu_callback {
basic_table<P> *tablep_;
int len_;
char s_[0];
void operator()(threadinfo *ti);
size_t size() const {
return len_ + sizeof(*this);
}
static void make(basic_table<P> &table, Str prefix, threadinfo *ti);
};
template <typename P>
void gc_layer_rcu_callback<P>::operator()(threadinfo *ti)
{
tcursor<P> lp(*tablep_, s_, len_);
bool do_remove = lp.gc_layer(ti);
if (!do_remove || !lp.finish_remove(ti))
lp.n_->unlock();
ti->deallocate(this, size(), ta_rcu);
}
template <typename P>
void gc_layer_rcu_callback<P>::make(basic_table<P> &table, Str prefix,
threadinfo *ti)
{
size_t sz = prefix.len + sizeof(gc_layer_rcu_callback);
void *data = ti->allocate(sz, ta_rcu);
gc_layer_rcu_callback *cb = new(data) gc_layer_rcu_callback;
cb->tablep_ = &table;
cb->len_ = prefix.len;
memcpy(cb->s_, prefix.s, cb->len_);
ti->rcu_register(cb);
}
template <typename P>
bool tcursor<P>::finish_remove(threadinfo *ti)
{
permuter_type perm(n_->permutation_);
perm.remove(ki_);
n_->permutation_ = perm.value();
++n_->nremoved_;
if (perm.size())
return false;
else
return remove_leaf(n_, *tablep_, ka_.prefix_string(), ti);
}
template <typename P>
bool tcursor<P>::remove_leaf(leaf_type *leaf, basic_table<P> &table,
Str prefix, threadinfo *ti)
{
if (!leaf->prev_) {
if (!leaf->next_.ptr && !prefix.empty())
gc_layer_rcu_callback<P>::make(table, prefix, ti);
return false;
}
// mark leaf deleted, RCU-free
leaf->mark_deleted();
leaf->deallocate_rcu(ti);
// Ensure node that becomes responsible for our keys has its node_ts_ kept
// up to date
while (1) {
leaf_type *prev = leaf->prev_;
kvtimestamp_t prev_ts = prev->node_ts_;
while (circular_int<kvtimestamp_t>::less(prev_ts, leaf->node_ts_)
&& !bool_cmpxchg(&prev->node_ts_, prev_ts, leaf->node_ts_))
prev_ts = prev->node_ts_;
fence();
if (prev == leaf->prev_)
break;
}
// Unlink leaf from doubly-linked leaf list
btree_leaflink<leaf_type>::unlink(leaf);
// Remove leaf from tree. This is simple unless the leaf is the first
// child of its parent, in which case we need to traverse up until we find
// its key.
node_type *n = leaf;
ikey_type ikey = leaf->ikey_bound(), reshape_ikey = 0;
bool reshaping = false;
while (1) {
internode_type *p = n->locked_parent(ti);
invariant(p);
n->unlock();
int kp = internode_type::bound_type::upper(ikey, *p);
invariant(kp == 0 || key_compare(ikey, *p, kp - 1) == 0);
if (kp > 0) {
p->mark_insert();
if (!reshaping) {
p->shift_down(kp - 1, kp, p->nkeys_ - kp);
--p->nkeys_;
} else
p->ikey0_[kp - 1] = reshape_ikey;
if (kp > 1 || p->child_[0]) {
if (p->size() == 0)
collapse(p, ikey, table, prefix, ti);
else
p->unlock();
break;
}
}
if (!reshaping) {
if (p->size() == 0) {
p->mark_deleted();
p->deallocate_rcu(ti);
} else {
reshaping = true;
reshape_ikey = p->ikey0_[0];
p->child_[0] = 0;
}
}
n = p;
}
return true;
}
template <typename P>
void tcursor<P>::collapse(internode_type *p, ikey_type ikey,
basic_table<P> &table, Str prefix, threadinfo *ti)
{
precondition(p && p->locked());
while (1) {
internode_type *gp = p->locked_parent(ti);
if (!gp) {
if (!prefix.empty())
gc_layer_rcu_callback<P>::make(table, prefix, ti);
p->unlock();
break;
}
int kp = key_upper_bound(ikey, *gp);
invariant(gp->child_[kp] == p);
gp->child_[kp] = p->child_[0];
p->child_[0]->set_parent(gp);
p->mark_deleted();
p->unlock();
p->deallocate_rcu(ti);
p = gp;
if (p->size() != 0) {
p->unlock();
break;
}
}
}
} // namespace Masstree
#endif