summaryrefslogtreecommitdiffstats
path: root/src/crimson/os/seastore/onode_manager/staged-fltree/tree.cc
blob: 2c8c216522652ea70424b18e09d0745531bb8e57 (plain)
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
// -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:nil -*-
// vim: ts=8 sw=2 smarttab

#include "tree.h"

#include "node.h"
#include "node_extent_manager.h"
#include "stages/key_layout.h"
#include "super.h"

namespace crimson::os::seastore::onode {

using btree_ertr = Btree::btree_ertr;
template <class ValueT=void>
using btree_future = Btree::btree_future<ValueT>;
using Cursor = Btree::Cursor;

Cursor::Cursor(Btree* p_tree, Ref<tree_cursor_t> _p_cursor)
  : p_tree(p_tree) {
  if (_p_cursor->is_end()) {
    // no need to hold the leaf node
  } else {
    p_cursor = _p_cursor;
  }
}
Cursor::Cursor(Btree* p_tree) : p_tree{p_tree} {}
Cursor::Cursor(const Cursor&) = default;
Cursor::Cursor(Cursor&&) noexcept = default;
Cursor& Cursor::operator=(const Cursor&) = default;
Cursor& Cursor::operator=(Cursor&&) = default;
Cursor::~Cursor() = default;

bool Cursor::is_end() const {
  if (p_cursor) {
    assert(!p_cursor->is_end());
    return false;
  } else {
    return true;
  }
}

ghobject_t Cursor::get_ghobj() const {
  return p_cursor->get_key_view().to_ghobj();
}

const onode_t* Cursor::value() const {
  return p_cursor->get_p_value();
}

bool Cursor::operator==(const Cursor& x) const {
  return p_cursor == x.p_cursor;
}

Cursor& Cursor::operator++() {
  // TODO
  return *this;
}

Cursor Cursor::operator++(int) {
  Cursor tmp = *this;
  ++*this;
  return tmp;
}

Cursor Cursor::make_end(Btree* p_tree) {
  return {p_tree};
}

Btree::Btree(NodeExtentManagerURef&& _nm)
  : nm{std::move(_nm)},
    root_tracker{RootNodeTracker::create(nm->is_read_isolated())} {}

Btree::~Btree() { assert(root_tracker->is_clean()); }

btree_future<> Btree::mkfs(Transaction& t) {
  return Node::mkfs(get_context(t), *root_tracker);
}

btree_future<Cursor> Btree::begin(Transaction& t) {
  return get_root(t).safe_then([this, &t](auto root) {
    return root->lookup_smallest(get_context(t));
  }).safe_then([this](auto cursor) {
    return Cursor{this, cursor};
  });
}

btree_future<Cursor> Btree::last(Transaction& t) {
  return get_root(t).safe_then([this, &t](auto root) {
    return root->lookup_largest(get_context(t));
  }).safe_then([this](auto cursor) {
    return Cursor(this, cursor);
  });
}

Cursor Btree::end() {
  return Cursor::make_end(this);
}

btree_future<bool>
Btree::contains(Transaction& t, const ghobject_t& obj) {
  return seastar::do_with(
    full_key_t<KeyT::HOBJ>(obj),
    [this, &t](auto& key) -> btree_future<bool> {
      return get_root(t).safe_then([this, &t, &key](auto root) {
        // TODO: improve lower_bound()
        return root->lower_bound(get_context(t), key);
      }).safe_then([](auto result) {
        return MatchKindBS::EQ == result.match();
      });
    }
  );
}

btree_future<Cursor>
Btree::find(Transaction& t, const ghobject_t& obj) {
  return seastar::do_with(
    full_key_t<KeyT::HOBJ>(obj),
    [this, &t](auto& key) -> btree_future<Cursor> {
      return get_root(t).safe_then([this, &t, &key](auto root) {
        // TODO: improve lower_bound()
        return root->lower_bound(get_context(t), key);
      }).safe_then([this](auto result) {
        if (result.match() == MatchKindBS::EQ) {
          return Cursor(this, result.p_cursor);
        } else {
          return Cursor::make_end(this);
        }
      });
    }
  );
}

btree_future<Cursor>
Btree::lower_bound(Transaction& t, const ghobject_t& obj) {
  return seastar::do_with(
    full_key_t<KeyT::HOBJ>(obj),
    [this, &t](auto& key) -> btree_future<Cursor> {
      return get_root(t).safe_then([this, &t, &key](auto root) {
        return root->lower_bound(get_context(t), key);
      }).safe_then([this](auto result) {
        return Cursor(this, result.p_cursor);
      });
    }
  );
}

btree_future<std::pair<Cursor, bool>>
Btree::insert(Transaction& t, const ghobject_t& obj, const onode_t& value) {
  return seastar::do_with(
    full_key_t<KeyT::HOBJ>(obj),
    [this, &t, &value](auto& key) -> btree_future<std::pair<Cursor, bool>> {
      return get_root(t).safe_then([this, &t, &key, &value](auto root) {
        return root->insert(get_context(t), key, value);
      }).safe_then([this](auto ret) {
        auto& [cursor, success] = ret;
        return std::make_pair(Cursor(this, cursor), success);
      });
    }
  );
}

btree_future<size_t> Btree::erase(Transaction& t, const ghobject_t& obj) {
  // TODO
  return btree_ertr::make_ready_future<size_t>(0u);
}

btree_future<Cursor> Btree::erase(Cursor& pos) {
  // TODO
  return btree_ertr::make_ready_future<Cursor>(
      Cursor::make_end(this));
}

btree_future<Cursor>
Btree::erase(Cursor& first, Cursor& last) {
  // TODO
  return btree_ertr::make_ready_future<Cursor>(
      Cursor::make_end(this));
}

btree_future<size_t> Btree::height(Transaction& t) {
  return get_root(t).safe_then([](auto root) {
    return size_t(root->level() + 1);
  });
}

btree_future<tree_stats_t> Btree::get_stats_slow(Transaction& t) {
  return get_root(t).safe_then([this, &t](auto root) {
    unsigned height = root->level() + 1;
    return root->get_tree_stats(get_context(t)
    ).safe_then([height](auto stats) {
      stats.height = height;
      return btree_ertr::make_ready_future<tree_stats_t>(stats);
    });
  });
}

std::ostream& Btree::dump(Transaction& t, std::ostream& os) {
  auto root = root_tracker->get_root(t);
  if (root) {
    root->dump(os);
  } else {
    os << "empty tree!";
  }
  return os;
}

std::ostream& Btree::print(std::ostream& os) const {
  return os << "BTree-" << *nm;
}

btree_future<Ref<Node>> Btree::get_root(Transaction& t) {
  auto root = root_tracker->get_root(t);
  if (root) {
    return btree_ertr::make_ready_future<Ref<Node>>(root);
  } else {
    return Node::load_root(get_context(t), *root_tracker);
  }
}

bool Btree::test_is_clean() const {
  return root_tracker->is_clean();
}

btree_future<> Btree::test_clone_from(
    Transaction& t, Transaction& t_from, Btree& from) {
  // Note: assume the tree to clone is tracked correctly in memory.
  // In some unit tests, parts of the tree are stubbed out that they
  // should not be loaded from NodeExtentManager.
  return from.get_root(t_from
  ).safe_then([this, &t](auto root_from) {
    return root_from->test_clone_root(get_context(t), *root_tracker);
  });
}

}