2011-12-16 00:04:31 +05:30
|
|
|
// Copyright (C) 2011 Red Hat, Inc. All rights reserved.
|
2011-12-06 19:23:05 +05:30
|
|
|
//
|
2011-12-06 19:13:56 +05:30
|
|
|
// This file is part of the thin-provisioning-tools source.
|
|
|
|
//
|
|
|
|
// thin-provisioning-tools is free software: you can redistribute it
|
|
|
|
// and/or modify it under the terms of the GNU General Public License
|
|
|
|
// as published by the Free Software Foundation, either version 3 of
|
|
|
|
// the License, or (at your option) any later version.
|
|
|
|
//
|
|
|
|
// thin-provisioning-tools is distributed in the hope that it will be
|
|
|
|
// useful, but WITHOUT ANY WARRANTY; without even the implied warranty
|
|
|
|
// of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU General Public License along
|
|
|
|
// with thin-provisioning-tools. If not, see
|
|
|
|
// <http://www.gnu.org/licenses/>.
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
#include "btree.h"
|
2011-11-03 20:14:00 +05:30
|
|
|
|
2013-01-12 00:56:51 +05:30
|
|
|
#include "persistent-data/errors.h"
|
|
|
|
#include "persistent-data/checksum.h"
|
|
|
|
#include "persistent-data/transaction_manager.h"
|
2015-08-11 16:28:07 +05:30
|
|
|
#include "persistent-data/validators.h"
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2011-07-15 19:51:28 +05:30
|
|
|
#include <iostream>
|
2017-04-29 23:21:52 +05:30
|
|
|
#include <sstream>
|
2020-06-02 09:17:38 +05:30
|
|
|
#include <stack>
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2011-11-03 20:14:00 +05:30
|
|
|
namespace {
|
2013-05-28 18:23:23 +05:30
|
|
|
using namespace base;
|
|
|
|
using namespace persistent_data;
|
|
|
|
using namespace btree_detail;
|
|
|
|
using namespace std;
|
2020-06-02 09:17:38 +05:30
|
|
|
|
|
|
|
struct frame {
|
|
|
|
frame(block_address blocknr,
|
|
|
|
uint32_t level,
|
|
|
|
uint32_t nr_entries)
|
|
|
|
: blocknr_(blocknr),
|
|
|
|
level_(level),
|
|
|
|
nr_entries_(nr_entries),
|
|
|
|
current_child_(0) {
|
|
|
|
}
|
|
|
|
block_address blocknr_;
|
|
|
|
uint32_t level_;
|
|
|
|
uint32_t nr_entries_;
|
|
|
|
uint32_t current_child_;
|
|
|
|
};
|
|
|
|
|
|
|
|
// stack for postorder DFS traversal
|
|
|
|
// TODO: Refactor it into a spine-like class, e.g., btree_del_spine,
|
|
|
|
// "Spine" sounds better for btree operations.
|
|
|
|
struct btree_del_stack {
|
|
|
|
public:
|
|
|
|
btree_del_stack(transaction_manager &tm): tm_(tm) {
|
|
|
|
}
|
|
|
|
|
|
|
|
void push_frame(block_address blocknr,
|
|
|
|
uint32_t level,
|
|
|
|
uint32_t nr_entries) {
|
|
|
|
if (tm_.get_sm()->get_count(blocknr) > 1)
|
|
|
|
tm_.get_sm()->dec(blocknr);
|
|
|
|
else
|
|
|
|
spine_.push(frame(blocknr, level, nr_entries));
|
|
|
|
}
|
|
|
|
|
|
|
|
void pop_frame() {
|
|
|
|
tm_.get_sm()->dec(spine_.top().blocknr_);
|
|
|
|
spine_.pop();
|
|
|
|
}
|
|
|
|
|
|
|
|
frame &top_frame() {
|
|
|
|
return spine_.top();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool is_empty() {
|
|
|
|
return spine_.empty();
|
|
|
|
}
|
|
|
|
|
|
|
|
private:
|
|
|
|
transaction_manager &tm_;
|
|
|
|
std::stack<frame> spine_;
|
|
|
|
};
|
2011-11-03 20:14:00 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
namespace persistent_data {
|
2013-06-20 18:57:40 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
inline void
|
|
|
|
ro_spine::step(block_address b)
|
|
|
|
{
|
2014-08-26 15:44:49 +05:30
|
|
|
spine_.push_back(tm_.read_lock(b, validator_));
|
2013-05-28 18:23:23 +05:30
|
|
|
if (spine_.size() > 2)
|
|
|
|
spine_.pop_front();
|
|
|
|
}
|
2011-11-03 20:14:00 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
inline bool
|
|
|
|
shadow_spine::step(block_address b)
|
|
|
|
{
|
2014-08-26 15:44:49 +05:30
|
|
|
pair<write_ref, bool> p = tm_.shadow(b, validator_);
|
2013-05-28 18:23:23 +05:30
|
|
|
try {
|
|
|
|
step(p.first);
|
|
|
|
} catch (...) {
|
2014-08-26 15:44:49 +05:30
|
|
|
tm_.get_sm()->dec(p.first.get_location());
|
2013-05-28 18:23:23 +05:30
|
|
|
throw;
|
|
|
|
}
|
|
|
|
return p.second;
|
|
|
|
}
|
2011-11-03 20:14:00 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
node_ref<ValueTraits>::node_ref(block_address location, disk_node *raw)
|
|
|
|
: location_(location),
|
2014-06-06 19:35:41 +05:30
|
|
|
raw_(raw),
|
|
|
|
checked_(false)
|
2013-05-28 18:23:23 +05:30
|
|
|
{
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
uint32_t
|
|
|
|
node_ref<ValueTraits>::get_checksum() const
|
|
|
|
{
|
|
|
|
return to_cpu<uint32_t>(raw_->header.csum);
|
|
|
|
}
|
2011-09-16 14:36:37 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
block_address
|
|
|
|
node_ref<ValueTraits>::get_block_nr() const
|
|
|
|
{
|
|
|
|
return to_cpu<uint64_t>(raw_->header.blocknr);
|
|
|
|
}
|
2011-08-23 16:25:37 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
btree_detail::node_type
|
|
|
|
node_ref<ValueTraits>::get_type() const
|
|
|
|
{
|
|
|
|
uint32_t flags = to_cpu<uint32_t>(raw_->header.flags);
|
|
|
|
if (flags & INTERNAL_NODE) {
|
2016-02-26 23:00:45 +05:30
|
|
|
if (flags & LEAF_NODE) {
|
|
|
|
ostringstream out;
|
|
|
|
out << "btree node is both internal and leaf"
|
|
|
|
<< " (block " << location_ << ")";
|
|
|
|
throw runtime_error(out.str());
|
|
|
|
}
|
2013-05-28 18:23:23 +05:30
|
|
|
return INTERNAL;
|
|
|
|
|
|
|
|
} else if (flags & LEAF_NODE)
|
|
|
|
return LEAF;
|
2016-02-26 23:00:45 +05:30
|
|
|
else {
|
|
|
|
ostringstream out;
|
|
|
|
out << "unknown node type"
|
|
|
|
<< " (block " << location_ << ")";
|
|
|
|
throw runtime_error(out.str());
|
|
|
|
}
|
2013-05-28 18:23:23 +05:30
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
|
|
|
node_ref<ValueTraits>::set_type(node_type t)
|
|
|
|
{
|
|
|
|
uint32_t flags = to_cpu<uint32_t>(raw_->header.flags);
|
|
|
|
switch (t) {
|
|
|
|
case INTERNAL:
|
|
|
|
flags = INTERNAL_NODE;
|
|
|
|
break;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
case LEAF:
|
|
|
|
flags = LEAF_NODE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
raw_->header.flags = to_disk<le32>(flags);
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
unsigned
|
|
|
|
node_ref<ValueTraits>::get_nr_entries() const
|
|
|
|
{
|
|
|
|
return to_cpu<uint32_t>(raw_->header.nr_entries);
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
|
|
|
node_ref<ValueTraits>::set_nr_entries(unsigned n)
|
|
|
|
{
|
|
|
|
raw_->header.nr_entries = to_disk<le32>(n);
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
unsigned
|
|
|
|
node_ref<ValueTraits>::get_max_entries() const
|
|
|
|
{
|
|
|
|
return to_cpu<uint32_t>(raw_->header.max_entries);
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
|
|
|
node_ref<ValueTraits>::set_max_entries(unsigned n)
|
|
|
|
{
|
|
|
|
raw_->header.max_entries = to_disk<le32>(n);
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
|
|
|
node_ref<ValueTraits>::set_max_entries()
|
|
|
|
{
|
|
|
|
set_max_entries(calc_max_entries());
|
|
|
|
}
|
2011-08-23 16:25:37 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
size_t
|
|
|
|
node_ref<ValueTraits>::get_value_size() const
|
|
|
|
{
|
|
|
|
return to_cpu<uint32_t>(raw_->header.value_size);
|
|
|
|
}
|
2011-11-01 17:01:03 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
|
|
|
node_ref<ValueTraits>::set_value_size(size_t s)
|
|
|
|
{
|
|
|
|
raw_->header.value_size = to_disk<le32>(static_cast<uint32_t>(s));
|
|
|
|
}
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
uint64_t
|
|
|
|
node_ref<ValueTraits>::key_at(unsigned i) const
|
|
|
|
{
|
|
|
|
if (i >= get_nr_entries())
|
|
|
|
throw runtime_error("key index out of bounds");
|
|
|
|
return to_cpu<uint64_t>(raw_->keys[i]);
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
|
|
|
node_ref<ValueTraits>::set_key(unsigned i, uint64_t k)
|
|
|
|
{
|
|
|
|
raw_->keys[i] = to_disk<le64>(k);
|
|
|
|
}
|
2011-08-22 18:39:37 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
typename ValueTraits::value_type
|
|
|
|
node_ref<ValueTraits>::value_at(unsigned i) const
|
|
|
|
{
|
|
|
|
if (i >= get_nr_entries())
|
|
|
|
throw runtime_error("value index out of bounds");
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
// We have to copy because of alignment issues.
|
|
|
|
typename ValueTraits::disk_type d;
|
|
|
|
::memcpy(&d, value_ptr(i), sizeof(d));
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
typename ValueTraits::value_type v;
|
|
|
|
ValueTraits::unpack(d, v);
|
|
|
|
return v;
|
|
|
|
}
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
|
|
|
node_ref<ValueTraits>::set_value(unsigned i,
|
|
|
|
typename ValueTraits::value_type const &v)
|
|
|
|
{
|
|
|
|
typename ValueTraits::disk_type d;
|
|
|
|
ValueTraits::pack(v, d);
|
|
|
|
::memcpy(value_ptr(i), &d, sizeof(d));
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
|
|
|
node_ref<ValueTraits>::insert_at(unsigned i,
|
|
|
|
uint64_t key,
|
|
|
|
typename ValueTraits::value_type const &v)
|
|
|
|
{
|
|
|
|
unsigned n = get_nr_entries();
|
|
|
|
if ((n + 1) > get_max_entries())
|
|
|
|
throw runtime_error("too many entries");
|
|
|
|
|
|
|
|
set_nr_entries(n + 1);
|
|
|
|
::memmove(key_ptr(i + 1), key_ptr(i), sizeof(uint64_t) * (n - i));
|
|
|
|
::memmove(value_ptr(i + 1), value_ptr(i), sizeof(typename ValueTraits::disk_type) * (n - i));
|
|
|
|
overwrite_at(i, key, v);
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
|
|
|
node_ref<ValueTraits>::overwrite_at(unsigned i,
|
|
|
|
uint64_t key,
|
|
|
|
typename ValueTraits::value_type const &v)
|
|
|
|
{
|
|
|
|
set_key(i, key);
|
|
|
|
set_value(i, v);
|
|
|
|
}
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2020-06-04 14:01:24 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
|
|
|
node_ref<ValueTraits>::delete_at(unsigned i)
|
|
|
|
{
|
|
|
|
unsigned nr_entries = get_nr_entries();
|
|
|
|
if (i >= nr_entries)
|
|
|
|
throw runtime_error("key index out of bounds");
|
|
|
|
unsigned nr_to_copy = nr_entries - (i + 1);
|
|
|
|
|
|
|
|
if (nr_to_copy) {
|
|
|
|
::memmove(key_ptr(i), key_ptr(i + 1), sizeof(uint64_t) * nr_to_copy);
|
|
|
|
::memmove(value_ptr(i), value_ptr(i + 1), sizeof(typename ValueTraits::disk_type) * nr_to_copy);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_nr_entries(nr_entries - 1);
|
|
|
|
}
|
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
|
|
|
node_ref<ValueTraits>::copy_entries(node_ref const &rhs,
|
|
|
|
unsigned begin,
|
|
|
|
unsigned end)
|
|
|
|
{
|
|
|
|
unsigned count = end - begin;
|
|
|
|
unsigned n = get_nr_entries();
|
|
|
|
if ((n + count) > get_max_entries())
|
|
|
|
throw runtime_error("too many entries");
|
|
|
|
|
|
|
|
::memcpy(key_ptr(n), rhs.key_ptr(begin), sizeof(uint64_t) * count);
|
|
|
|
::memcpy(value_ptr(n), rhs.value_ptr(begin), sizeof(typename ValueTraits::disk_type) * count);
|
|
|
|
set_nr_entries(n + count);
|
|
|
|
}
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2020-06-04 14:01:24 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
|
|
|
node_ref<ValueTraits>::move_entries(node_ref<ValueTraits> &rhs,
|
|
|
|
int count)
|
|
|
|
{
|
|
|
|
if (!count)
|
|
|
|
return;
|
|
|
|
|
|
|
|
unsigned nr_left = get_nr_entries();
|
|
|
|
unsigned nr_right = rhs.get_nr_entries();
|
|
|
|
unsigned max_entries = get_max_entries();
|
|
|
|
|
2021-03-25 12:36:52 +05:30
|
|
|
if (nr_left - count > max_entries || nr_right + count > max_entries)
|
2020-06-04 14:01:24 +05:30
|
|
|
throw runtime_error("too many entries");
|
|
|
|
|
|
|
|
if (count > 0) {
|
|
|
|
rhs.shift_entries_right(count);
|
|
|
|
copy_entries_to_right(rhs, count);
|
|
|
|
} else {
|
|
|
|
copy_entries_to_left(rhs, -count);
|
|
|
|
rhs.shift_entries_left(-count);
|
|
|
|
}
|
|
|
|
|
|
|
|
set_nr_entries(nr_left - count);
|
|
|
|
rhs.set_nr_entries(nr_right + count);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
|
|
|
node_ref<ValueTraits>::copy_entries_to_left(node_ref const &rhs, unsigned count)
|
|
|
|
{
|
|
|
|
unsigned n = get_nr_entries();
|
|
|
|
if ((n + count) > get_max_entries())
|
|
|
|
throw runtime_error("too many entries");
|
|
|
|
|
|
|
|
::memcpy(key_ptr(n), rhs.key_ptr(0), sizeof(uint64_t) * count);
|
|
|
|
::memcpy(value_ptr(n), rhs.value_ptr(0), sizeof(typename ValueTraits::disk_type) * count);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
|
|
|
node_ref<ValueTraits>::copy_entries_to_right(node_ref &rhs, unsigned count) const
|
|
|
|
{
|
|
|
|
unsigned n = rhs.get_nr_entries();
|
|
|
|
if ((n + count) > get_max_entries())
|
|
|
|
throw runtime_error("too many entries");
|
|
|
|
|
|
|
|
unsigned nr_left = get_nr_entries();
|
|
|
|
::memcpy(rhs.key_ptr(0), key_ptr(nr_left - count), sizeof(uint64_t) * count);
|
|
|
|
::memcpy(rhs.value_ptr(0), value_ptr(nr_left - count), sizeof(typename ValueTraits::disk_type) * count);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
|
|
|
node_ref<ValueTraits>::shift_entries_left(unsigned shift)
|
|
|
|
{
|
|
|
|
unsigned n = get_nr_entries();
|
|
|
|
if (shift > n)
|
|
|
|
throw runtime_error("too many entries");
|
|
|
|
|
|
|
|
unsigned nr_shifted = n - shift;
|
|
|
|
::memmove(key_ptr(0), key_ptr(shift), sizeof(uint64_t) * nr_shifted);
|
|
|
|
::memmove(value_ptr(0), value_ptr(shift), sizeof(typename ValueTraits::disk_type) * nr_shifted);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
|
|
|
node_ref<ValueTraits>::shift_entries_right(unsigned shift)
|
|
|
|
{
|
|
|
|
unsigned n = get_nr_entries();
|
|
|
|
if (n + shift > get_max_entries())
|
|
|
|
throw runtime_error("too many entries");
|
|
|
|
|
|
|
|
::memmove(key_ptr(shift), key_ptr(0), sizeof(uint64_t) * n);
|
|
|
|
::memmove(value_ptr(shift), value_ptr(0), sizeof(typename ValueTraits::disk_type) * n);
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ValueTraits>
|
|
|
|
unsigned
|
|
|
|
node_ref<ValueTraits>::merge_threshold() const
|
|
|
|
{
|
|
|
|
return get_max_entries() / 3;
|
|
|
|
}
|
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
int
|
|
|
|
node_ref<ValueTraits>::bsearch(uint64_t key, int want_hi) const
|
|
|
|
{
|
|
|
|
int lo = -1, hi = get_nr_entries();
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
while(hi - lo > 1) {
|
|
|
|
int mid = lo + ((hi - lo) / 2);
|
|
|
|
uint64_t mid_key = key_at(mid);
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
if (mid_key == key)
|
|
|
|
return mid;
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
if (mid_key < key)
|
|
|
|
lo = mid;
|
|
|
|
else
|
|
|
|
hi = mid;
|
|
|
|
}
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
return want_hi ? hi : lo;
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
2013-06-25 18:18:02 +05:30
|
|
|
boost::optional<unsigned>
|
2013-05-28 18:23:23 +05:30
|
|
|
node_ref<ValueTraits>::exact_search(uint64_t key) const
|
|
|
|
{
|
|
|
|
int i = bsearch(key, 0);
|
|
|
|
if (i < 0 || static_cast<unsigned>(i) >= get_nr_entries())
|
2013-06-25 18:18:02 +05:30
|
|
|
return boost::optional<unsigned>();
|
2011-11-01 17:01:03 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
if (key != key_at(i))
|
2013-06-25 18:18:02 +05:30
|
|
|
return boost::optional<unsigned>();
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-06-25 18:18:02 +05:30
|
|
|
return boost::optional<unsigned>(i);
|
2013-05-28 18:23:23 +05:30
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
int
|
|
|
|
node_ref<ValueTraits>::lower_bound(uint64_t key) const
|
|
|
|
{
|
|
|
|
return bsearch(key, 0);
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
unsigned
|
|
|
|
node_ref<ValueTraits>::calc_max_entries(void)
|
|
|
|
{
|
|
|
|
uint32_t total;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
// key + value
|
|
|
|
size_t elt_size = sizeof(uint64_t) + sizeof(typename ValueTraits::disk_type);
|
|
|
|
total = (MD_BLOCK_SIZE - sizeof(struct node_header)) / elt_size;
|
|
|
|
return (total / 3) * 3; // rounds down
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
void *
|
|
|
|
node_ref<ValueTraits>::key_ptr(unsigned i) const
|
|
|
|
{
|
2014-06-06 19:35:41 +05:30
|
|
|
check_fits_within_block();
|
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
return raw_->keys + i;
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
void *
|
|
|
|
node_ref<ValueTraits>::value_ptr(unsigned i) const
|
|
|
|
{
|
2014-06-06 19:35:41 +05:30
|
|
|
check_fits_within_block();
|
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
void *value_base = &raw_->keys[to_cpu<uint32_t>(raw_->header.max_entries)];
|
|
|
|
return static_cast<unsigned char *>(value_base) +
|
|
|
|
sizeof(typename ValueTraits::disk_type) * i;
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ValueTraits>
|
|
|
|
template <typename RefCounter>
|
|
|
|
void
|
|
|
|
node_ref<ValueTraits>::inc_children(RefCounter &rc)
|
|
|
|
{
|
|
|
|
unsigned nr_entries = get_nr_entries();
|
|
|
|
for (unsigned i = 0; i < nr_entries; i++) {
|
|
|
|
typename ValueTraits::value_type v;
|
|
|
|
typename ValueTraits::disk_type d;
|
|
|
|
::memcpy(&d, value_ptr(i), sizeof(d));
|
|
|
|
ValueTraits::unpack(d, v);
|
|
|
|
rc.inc(v);
|
|
|
|
}
|
2011-07-22 20:39:56 +05:30
|
|
|
}
|
|
|
|
|
2020-06-02 09:17:38 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
template <typename RefCounter>
|
|
|
|
void
|
|
|
|
node_ref<ValueTraits>::dec_children(RefCounter &rc)
|
|
|
|
{
|
|
|
|
unsigned nr_entries = get_nr_entries();
|
|
|
|
for (unsigned i = 0; i < nr_entries; i++) {
|
|
|
|
typename ValueTraits::value_type v;
|
|
|
|
typename ValueTraits::disk_type d;
|
|
|
|
::memcpy(&d, value_ptr(i), sizeof(d));
|
|
|
|
ValueTraits::unpack(d, v);
|
|
|
|
rc.dec(v);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-05-26 16:36:34 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
bool
|
|
|
|
node_ref<ValueTraits>::value_sizes_match() const {
|
|
|
|
return sizeof(typename ValueTraits::disk_type) == get_value_size();
|
|
|
|
}
|
|
|
|
|
|
|
|
template <typename ValueTraits>
|
|
|
|
std::string
|
|
|
|
node_ref<ValueTraits>::value_mismatch_string() const {
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "value size mismatch: expected " << sizeof(typename ValueTraits::disk_type)
|
|
|
|
<< ", but got " << get_value_size()
|
2020-06-11 17:35:03 +05:30
|
|
|
<< " (block " << location_ << ")." << std::endl;
|
2015-05-26 16:36:34 +05:30
|
|
|
|
|
|
|
return out.str();
|
|
|
|
}
|
|
|
|
|
2014-06-06 19:35:41 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
|
|
|
node_ref<ValueTraits>::check_fits_within_block() const {
|
|
|
|
if (checked_)
|
|
|
|
return;
|
|
|
|
|
2015-05-26 16:36:34 +05:30
|
|
|
if (!value_sizes_match())
|
|
|
|
throw std::runtime_error(value_mismatch_string());
|
2014-06-06 19:35:41 +05:30
|
|
|
|
|
|
|
unsigned max = calc_max_entries();
|
|
|
|
|
|
|
|
if (max < get_nr_entries()) {
|
|
|
|
std::ostringstream out;
|
|
|
|
out << "Bad nr of elements: max per block = "
|
2016-02-26 23:00:45 +05:30
|
|
|
<< max << ", actual = " << get_nr_entries()
|
|
|
|
<< " (block " << location_ << ")" << std::endl;
|
2014-06-06 19:35:41 +05:30
|
|
|
throw std::runtime_error(out.str());
|
|
|
|
}
|
|
|
|
|
|
|
|
checked_ = true;
|
|
|
|
}
|
|
|
|
|
2013-06-20 18:57:40 +05:30
|
|
|
//--------------------------------
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
|
|
|
btree<Levels, ValueTraits>::
|
2014-08-26 15:44:49 +05:30
|
|
|
btree(transaction_manager &tm,
|
2013-05-28 18:23:23 +05:30
|
|
|
typename ValueTraits::ref_counter rc)
|
|
|
|
: tm_(tm),
|
|
|
|
destroy_(false),
|
2014-08-26 15:44:49 +05:30
|
|
|
internal_rc_(tm.get_sm()),
|
2013-05-28 18:23:23 +05:30
|
|
|
rc_(rc),
|
2015-08-11 16:28:07 +05:30
|
|
|
validator_(create_btree_node_validator())
|
2013-05-28 18:23:23 +05:30
|
|
|
{
|
|
|
|
using namespace btree_detail;
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2014-08-26 15:44:49 +05:30
|
|
|
write_ref root = tm_.new_block(validator_);
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2014-07-02 19:32:32 +05:30
|
|
|
if (Levels > 1) {
|
|
|
|
internal_node n = to_node<block_traits>(root);
|
|
|
|
n.set_type(btree_detail::LEAF);
|
|
|
|
n.set_nr_entries(0);
|
|
|
|
n.set_max_entries();
|
|
|
|
n.set_value_size(sizeof(typename block_traits::disk_type));
|
|
|
|
|
|
|
|
} else {
|
|
|
|
leaf_node n = to_node<ValueTraits>(root);
|
|
|
|
n.set_type(btree_detail::LEAF);
|
|
|
|
n.set_nr_entries(0);
|
|
|
|
n.set_max_entries();
|
|
|
|
n.set_value_size(sizeof(typename ValueTraits::disk_type));
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
root_ = root.get_location();
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
|
|
|
btree<Levels, ValueTraits>::
|
2014-08-26 15:44:49 +05:30
|
|
|
btree(transaction_manager &tm,
|
2013-05-28 18:23:23 +05:30
|
|
|
block_address root,
|
|
|
|
typename ValueTraits::ref_counter rc)
|
|
|
|
: tm_(tm),
|
|
|
|
destroy_(false),
|
|
|
|
root_(root),
|
2014-08-26 15:44:49 +05:30
|
|
|
internal_rc_(tm.get_sm()),
|
2013-05-28 18:23:23 +05:30
|
|
|
rc_(rc),
|
2015-08-11 16:28:07 +05:30
|
|
|
validator_(create_btree_node_validator())
|
2013-05-28 18:23:23 +05:30
|
|
|
{
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
|
|
|
btree<Levels, ValueTraits>::~btree()
|
|
|
|
{
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
namespace {
|
|
|
|
template <typename ValueTraits>
|
|
|
|
struct lower_bound_search {
|
2013-06-25 18:18:02 +05:30
|
|
|
static boost::optional<unsigned> search(btree_detail::node_ref<ValueTraits> n, uint64_t key) {
|
2015-05-26 18:39:29 +05:30
|
|
|
int i = n.lower_bound(key);
|
|
|
|
return (i < 0) ? boost::optional<unsigned>() : boost::optional<unsigned>(i);
|
2013-05-28 18:23:23 +05:30
|
|
|
}
|
|
|
|
};
|
2011-11-01 17:01:03 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
struct exact_search {
|
2013-06-25 18:18:02 +05:30
|
|
|
static boost::optional<unsigned> search(btree_detail::node_ref<ValueTraits> n, uint64_t key) {
|
2013-05-28 18:23:23 +05:30
|
|
|
return n.exact_search(key);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
}
|
2011-11-01 17:01:03 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
|
|
|
typename btree<Levels, ValueTraits>::maybe_value
|
|
|
|
btree<Levels, ValueTraits>::lookup(key const &key) const
|
|
|
|
{
|
|
|
|
using namespace btree_detail;
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
ro_spine spine(tm_, validator_);
|
|
|
|
block_address root = root_;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
for (unsigned level = 0; level < Levels - 1; ++level) {
|
2013-06-25 18:18:02 +05:30
|
|
|
boost::optional<block_address> mroot =
|
2013-06-20 18:57:40 +05:30
|
|
|
lookup_raw<block_traits, lower_bound_search<block_traits> >(spine, root, key[level]);
|
2013-05-28 18:23:23 +05:30
|
|
|
if (!mroot)
|
|
|
|
return maybe_value();
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
root = *mroot;
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
return lookup_raw<ValueTraits, exact_search<ValueTraits> >(spine, root, key[Levels - 1]);
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
|
|
|
typename btree<Levels, ValueTraits>::maybe_pair
|
|
|
|
btree<Levels, ValueTraits>::lookup_le(key const &key) const
|
|
|
|
{
|
|
|
|
using namespace btree_detail;
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
return maybe_pair();
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
|
|
|
typename btree<Levels, ValueTraits>::maybe_pair
|
|
|
|
btree<Levels, ValueTraits>::lookup_ge(key const &key) const
|
|
|
|
{
|
|
|
|
using namespace btree_detail;
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
return maybe_pair();
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
2016-08-10 21:10:48 +05:30
|
|
|
bool
|
2013-05-28 18:23:23 +05:30
|
|
|
btree<Levels, ValueTraits>::
|
|
|
|
insert(key const &key,
|
|
|
|
typename ValueTraits::value_type const &value)
|
|
|
|
{
|
|
|
|
using namespace btree_detail;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
block_address block = root_;
|
|
|
|
int index = 0; // FIXME: ???
|
|
|
|
shadow_spine spine(tm_, validator_);
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
for (unsigned level = 0; level < Levels - 1; ++level) {
|
2013-06-20 18:57:40 +05:30
|
|
|
bool need_insert = insert_location<block_traits>(spine, block, key[level], &index, internal_rc_);
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-06-20 18:57:40 +05:30
|
|
|
internal_node n = spine.template get_node<block_traits>();
|
2013-05-28 18:23:23 +05:30
|
|
|
if (need_insert) {
|
|
|
|
btree<Levels - 1, ValueTraits> new_tree(tm_, rc_);
|
|
|
|
n.insert_at(index, key[level], new_tree.get_root());
|
|
|
|
}
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
block = n.value_at(index);
|
|
|
|
}
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-06-19 21:50:12 +05:30
|
|
|
bool need_insert = insert_location<ValueTraits>(spine, block, key[Levels - 1], &index, rc_);
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
leaf_node n = spine.template get_node<ValueTraits>();
|
|
|
|
if (need_insert)
|
|
|
|
n.insert_at(index, key[Levels - 1], value);
|
2021-09-09 07:28:45 +05:30
|
|
|
else {
|
|
|
|
typename ValueTraits::value_type old_value = n.value_at(index);
|
|
|
|
if (value != old_value) {
|
|
|
|
// do decrement the old value if it already exists
|
|
|
|
rc_.dec(old_value);
|
|
|
|
|
|
|
|
n.set_value(index, value);
|
|
|
|
}
|
|
|
|
}
|
2013-06-19 21:49:24 +05:30
|
|
|
|
|
|
|
root_ = spine.get_root();
|
2016-08-10 21:10:48 +05:30
|
|
|
|
|
|
|
return need_insert;
|
2013-05-28 18:23:23 +05:30
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
|
|
|
block_address
|
|
|
|
btree<Levels, ValueTraits>::get_root() const
|
|
|
|
{
|
|
|
|
return root_;
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
|
|
|
void
|
|
|
|
btree<Levels, ValueTraits>::set_root(block_address root)
|
|
|
|
{
|
|
|
|
using namespace btree_detail;
|
|
|
|
root_ = root;
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
|
|
|
typename btree<Levels, ValueTraits>::ptr
|
|
|
|
btree<Levels, ValueTraits>::clone() const
|
|
|
|
{
|
2014-08-26 15:44:49 +05:30
|
|
|
tm_.get_sm()->inc(root_);
|
2013-05-28 18:23:23 +05:30
|
|
|
return ptr(new btree<Levels, ValueTraits>(tm_, root_, rc_));
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
|
|
|
void
|
|
|
|
btree<Levels, ValueTraits>::destroy()
|
|
|
|
{
|
|
|
|
using namespace btree_detail;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2020-06-02 09:17:38 +05:30
|
|
|
btree_del_stack s(tm_);
|
|
|
|
|
|
|
|
{
|
|
|
|
read_ref blk = tm_.read_lock(root_, validator_);
|
|
|
|
internal_node n = to_node<block_traits>(blk);
|
|
|
|
s.push_frame(root_, 0, n.get_nr_entries());
|
|
|
|
}
|
|
|
|
|
|
|
|
while (!s.is_empty()) {
|
|
|
|
frame &f = s.top_frame();
|
|
|
|
|
|
|
|
if (f.current_child_ >= f.nr_entries_) {
|
|
|
|
s.pop_frame();
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
// FIXME: Cache the read_ref object in the stack to avoid temporary objects?
|
|
|
|
read_ref current = tm_.read_lock(f.blocknr_, validator_);
|
|
|
|
internal_node n = to_node<block_traits>(current);
|
|
|
|
|
|
|
|
if (n.get_type() == INTERNAL) {
|
|
|
|
// TODO: test performance penalty of prefetching
|
|
|
|
//if (!f.current_child_)
|
|
|
|
// for (unsigned i = 0; i < n.get_nr_entries(); i++)
|
|
|
|
// tm_.prefetch(n.value_at(i));
|
|
|
|
|
|
|
|
block_address b = n.value_at(f.current_child_);
|
|
|
|
read_ref leaf = tm_.read_lock(b, validator_);
|
|
|
|
internal_node o = to_node<block_traits>(leaf);
|
|
|
|
s.push_frame(b, f.level_, o.get_nr_entries());
|
|
|
|
++f.current_child_;
|
|
|
|
// internal leaf
|
|
|
|
} else if (f.level_ < Levels - 1) {
|
|
|
|
block_address b = n.value_at(f.current_child_);
|
|
|
|
read_ref leaf = tm_.read_lock(b, validator_);
|
|
|
|
internal_node o = to_node<block_traits>(leaf);
|
|
|
|
s.push_frame(b, f.level_ + 1, o.get_nr_entries());
|
|
|
|
++f.current_child_;
|
|
|
|
} else {
|
|
|
|
leaf_node o = to_node<ValueTraits>(current);
|
|
|
|
o.dec_children(rc_); // FIXME: move this into pop_frame()
|
|
|
|
s.pop_frame();
|
|
|
|
}
|
|
|
|
}
|
2013-05-28 18:23:23 +05:30
|
|
|
}
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <unsigned Levels, typename _>
|
|
|
|
template <typename ValueTraits, typename Search>
|
2013-06-25 18:18:02 +05:30
|
|
|
boost::optional<typename ValueTraits::value_type>
|
2013-05-28 18:23:23 +05:30
|
|
|
btree<Levels, _>::
|
|
|
|
lookup_raw(ro_spine &spine, block_address block, uint64_t key) const
|
|
|
|
{
|
|
|
|
using namespace boost;
|
|
|
|
typedef typename ValueTraits::value_type leaf_type;
|
2011-11-01 17:01:03 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
for (;;) {
|
|
|
|
spine.step(block);
|
|
|
|
node_ref<ValueTraits> leaf = spine.template get_node<ValueTraits>();
|
2011-11-01 17:01:03 +05:30
|
|
|
|
2013-06-25 18:18:02 +05:30
|
|
|
boost::optional<unsigned> mi;
|
2013-05-28 18:23:23 +05:30
|
|
|
if (leaf.get_type() == btree_detail::LEAF) {
|
|
|
|
mi = Search::search(leaf, key);
|
|
|
|
if (!mi)
|
2013-06-25 18:18:02 +05:30
|
|
|
return boost::optional<leaf_type>();
|
|
|
|
return boost::optional<leaf_type>(leaf.value_at(*mi));
|
2011-11-01 17:01:03 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
}
|
|
|
|
|
2015-05-26 18:54:55 +05:30
|
|
|
{
|
|
|
|
int lb = leaf.lower_bound(key);
|
|
|
|
if (lb < 0)
|
|
|
|
return boost::optional<leaf_type>();
|
|
|
|
|
|
|
|
mi = lb;
|
|
|
|
}
|
2011-11-01 17:01:03 +05:30
|
|
|
|
2013-06-20 18:57:40 +05:30
|
|
|
node_ref<block_traits> internal = spine.template get_node<block_traits>();
|
2013-05-28 18:23:23 +05:30
|
|
|
block = internal.value_at(*mi);
|
|
|
|
}
|
2011-11-01 17:01:03 +05:30
|
|
|
}
|
|
|
|
|
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <unsigned Levels, typename _>
|
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
|
|
|
btree<Levels, _>::
|
|
|
|
split_node(btree_detail::shadow_spine &spine,
|
|
|
|
block_address parent_index,
|
|
|
|
uint64_t key,
|
|
|
|
bool top)
|
|
|
|
{
|
|
|
|
node_ref<ValueTraits> n = spine.template get_node<ValueTraits>();
|
|
|
|
if (n.get_nr_entries() == n.get_max_entries()) {
|
|
|
|
if (top)
|
|
|
|
split_beneath<ValueTraits>(spine, key);
|
|
|
|
else
|
|
|
|
split_sibling<ValueTraits>(spine, parent_index, key);
|
|
|
|
}
|
2011-07-15 19:51:28 +05:30
|
|
|
}
|
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <unsigned Levels, typename _>
|
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
|
|
|
btree<Levels, _>::
|
|
|
|
split_beneath(btree_detail::shadow_spine &spine,
|
|
|
|
uint64_t key)
|
|
|
|
{
|
|
|
|
using namespace btree_detail;
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
node_type type;
|
|
|
|
unsigned nr_left, nr_right;
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2014-08-26 15:44:49 +05:30
|
|
|
write_ref left = tm_.new_block(validator_);
|
2013-05-28 18:23:23 +05:30
|
|
|
node_ref<ValueTraits> l = to_node<ValueTraits>(left);
|
|
|
|
l.set_nr_entries(0);
|
|
|
|
l.set_max_entries();
|
|
|
|
l.set_value_size(sizeof(typename ValueTraits::disk_type));
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2014-08-26 15:44:49 +05:30
|
|
|
write_ref right = tm_.new_block(validator_);
|
2013-05-28 18:23:23 +05:30
|
|
|
node_ref<ValueTraits> r = to_node<ValueTraits>(right);
|
|
|
|
r.set_nr_entries(0);
|
|
|
|
r.set_max_entries();
|
|
|
|
r.set_value_size(sizeof(typename ValueTraits::disk_type));
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
{
|
|
|
|
node_ref<ValueTraits> p = spine.template get_node<ValueTraits>();
|
2011-11-01 17:01:03 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
if (p.get_value_size() != sizeof(typename ValueTraits::disk_type))
|
|
|
|
throw std::runtime_error("bad value_size");
|
2011-11-01 17:01:03 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
nr_left = p.get_nr_entries() / 2;
|
|
|
|
nr_right = p.get_nr_entries() - nr_left;
|
|
|
|
type = p.get_type();
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
l.set_type(type);
|
|
|
|
l.copy_entries(p, 0, nr_left);
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
r.set_type(type);
|
|
|
|
r.copy_entries(p, nr_left, nr_left + nr_right);
|
|
|
|
}
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
{
|
|
|
|
// The parent may have changed value type, so we re-get it.
|
2013-06-20 18:57:40 +05:30
|
|
|
internal_node p = spine.template get_node<block_traits>();
|
2013-05-28 18:23:23 +05:30
|
|
|
p.set_type(btree_detail::INTERNAL);
|
|
|
|
p.set_max_entries();
|
|
|
|
p.set_nr_entries(2);
|
2013-06-20 18:57:40 +05:30
|
|
|
p.set_value_size(sizeof(typename block_traits::disk_type));
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
p.overwrite_at(0, l.key_at(0), left.get_location());
|
|
|
|
p.overwrite_at(1, r.key_at(0), right.get_location());
|
|
|
|
}
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
if (key < r.key_at(0))
|
|
|
|
spine.step(left);
|
|
|
|
else
|
|
|
|
spine.step(right);
|
|
|
|
}
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <unsigned Levels, typename _>
|
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
|
|
|
btree<Levels, _>::
|
|
|
|
split_sibling(btree_detail::shadow_spine &spine,
|
|
|
|
block_address parent_index,
|
|
|
|
uint64_t key)
|
|
|
|
{
|
|
|
|
using namespace btree_detail;
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
node_ref<ValueTraits> l = spine.template get_node<ValueTraits>();
|
|
|
|
block_address left = spine.get_block();
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2014-08-26 15:44:49 +05:30
|
|
|
write_ref right = tm_.new_block(validator_);
|
2013-05-28 18:23:23 +05:30
|
|
|
node_ref<ValueTraits> r = to_node<ValueTraits>(right);
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
unsigned nr_left = l.get_nr_entries() / 2;
|
|
|
|
unsigned nr_right = l.get_nr_entries() - nr_left;
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
r.set_nr_entries(0);
|
|
|
|
r.set_max_entries();
|
|
|
|
r.set_type(l.get_type());
|
|
|
|
r.set_value_size(sizeof(typename ValueTraits::disk_type));
|
|
|
|
r.copy_entries(l, nr_left, nr_left + nr_right);
|
|
|
|
l.set_nr_entries(nr_left);
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
internal_node p = spine.get_parent();
|
|
|
|
p.overwrite_at(parent_index, l.key_at(0), left);
|
|
|
|
p.insert_at(parent_index + 1, r.key_at(0), right.get_location());
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-06-20 16:47:59 +05:30
|
|
|
if (key >= r.key_at(0)) {
|
|
|
|
spine.pop();
|
2013-05-28 18:23:23 +05:30
|
|
|
spine.step(right);
|
2013-06-20 16:47:59 +05:30
|
|
|
}
|
2013-05-28 18:23:23 +05:30
|
|
|
}
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
// Returns true if we need a new insertion, rather than overwrite.
|
|
|
|
template <unsigned Levels, typename _>
|
2013-06-19 21:50:12 +05:30
|
|
|
template <typename ValueTraits, typename RC>
|
2013-05-28 18:23:23 +05:30
|
|
|
bool
|
|
|
|
btree<Levels, _>::
|
|
|
|
insert_location(btree_detail::shadow_spine &spine,
|
|
|
|
block_address block,
|
|
|
|
uint64_t key,
|
2013-06-19 21:50:12 +05:30
|
|
|
int *index,
|
|
|
|
RC &leaf_rc)
|
2013-05-28 18:23:23 +05:30
|
|
|
{
|
|
|
|
using namespace btree_detail;
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
bool top = true; // this isn't the same as spine.has_parent()
|
|
|
|
int i = *index;
|
|
|
|
bool inc = false;
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
for (;;) {
|
|
|
|
inc = spine.step(block);
|
2013-06-20 18:56:17 +05:30
|
|
|
if (inc)
|
|
|
|
inc_children<ValueTraits>(spine, leaf_rc);
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
// patch up the parent to point to the new shadow
|
|
|
|
if (spine.has_parent()) {
|
|
|
|
internal_node p = spine.get_parent();
|
|
|
|
p.set_value(i, spine.get_block());
|
|
|
|
}
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-06-20 18:57:40 +05:30
|
|
|
internal_node internal = spine.template get_node<block_traits>();
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
// Split the node if we're full
|
|
|
|
if (internal.get_type() == INTERNAL)
|
2013-06-20 18:57:40 +05:30
|
|
|
split_node<block_traits>(spine, i, key, top);
|
2013-05-28 18:23:23 +05:30
|
|
|
else
|
|
|
|
split_node<ValueTraits>(spine, i, key, top);
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-06-20 18:57:40 +05:30
|
|
|
internal = spine.template get_node<block_traits>();
|
2013-05-28 18:23:23 +05:30
|
|
|
i = internal.lower_bound(key);
|
|
|
|
if (internal.get_type() == btree_detail::LEAF)
|
|
|
|
break;
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
if (i < 0) {
|
|
|
|
internal.set_key(0, key);
|
|
|
|
i = 0;
|
|
|
|
}
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
block = internal.value_at(i);
|
|
|
|
top = false;
|
|
|
|
}
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
node_ref<ValueTraits> leaf = spine.template get_node<ValueTraits>();
|
|
|
|
// FIXME: gross
|
|
|
|
if (i < 0 || leaf.key_at(i) != key)
|
|
|
|
i++;
|
|
|
|
|
|
|
|
*index = i;
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
return ((static_cast<unsigned>(i) >= leaf.get_nr_entries()) ||
|
|
|
|
(leaf.key_at(i) != key));
|
2011-07-15 19:51:28 +05:30
|
|
|
}
|
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
|
|
|
void
|
|
|
|
btree<Levels, ValueTraits>::visit_depth_first(visitor &v) const
|
|
|
|
{
|
|
|
|
node_location loc;
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
walk_tree(v, loc, root_);
|
|
|
|
v.visit_complete();
|
|
|
|
}
|
2013-05-01 20:46:23 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
|
|
|
void
|
|
|
|
btree<Levels, ValueTraits>::walk_tree(visitor &v,
|
|
|
|
node_location const &loc,
|
|
|
|
block_address b) const
|
|
|
|
{
|
|
|
|
try {
|
|
|
|
walk_tree_internal(v, loc, b);
|
2011-08-22 16:25:55 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
} catch (std::runtime_error const &e) {
|
|
|
|
switch (v.error_accessing_node(loc, b, e.what())) {
|
|
|
|
case visitor::EXCEPTION_HANDLED:
|
|
|
|
break;
|
2013-05-07 19:52:13 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
case visitor::RETHROW_EXCEPTION:
|
|
|
|
throw;
|
|
|
|
}
|
2013-05-07 19:52:13 +05:30
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
|
|
|
void
|
|
|
|
btree<Levels, ValueTraits>::walk_tree_internal(visitor &v,
|
|
|
|
node_location const &loc,
|
|
|
|
block_address b) const
|
|
|
|
{
|
|
|
|
using namespace btree_detail;
|
2011-08-23 16:25:37 +05:30
|
|
|
|
2014-08-26 15:44:49 +05:30
|
|
|
read_ref blk = tm_.read_lock(b, validator_);
|
2013-06-20 18:57:40 +05:30
|
|
|
internal_node o = to_node<block_traits>(blk);
|
2013-05-15 18:07:30 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
// FIXME: use a switch statement
|
|
|
|
if (o.get_type() == INTERNAL) {
|
2014-07-25 21:02:59 +05:30
|
|
|
if (v.visit_internal(loc, o)) {
|
|
|
|
for (unsigned i = 0; i < o.get_nr_entries(); i++)
|
2014-08-26 15:44:49 +05:30
|
|
|
tm_.prefetch(o.value_at(i));
|
2014-07-25 21:02:59 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
for (unsigned i = 0; i < o.get_nr_entries(); i++) {
|
|
|
|
node_location loc2(loc);
|
2013-05-01 20:46:23 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
loc2.inc_depth();
|
|
|
|
loc2.key = o.key_at(i);
|
2013-05-01 20:46:23 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
walk_tree(v, loc2, o.value_at(i));
|
|
|
|
}
|
2014-07-25 21:02:59 +05:30
|
|
|
}
|
2013-05-01 20:46:23 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
} else if (loc.path.size() < Levels - 1) {
|
|
|
|
if (v.visit_internal_leaf(loc, o))
|
|
|
|
for (unsigned i = 0; i < o.get_nr_entries(); i++) {
|
|
|
|
node_location loc2(loc);
|
2013-05-01 20:46:23 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
loc2.push_key(o.key_at(i));
|
2013-06-25 18:18:02 +05:30
|
|
|
loc2.key = boost::optional<uint64_t>();
|
2011-08-24 18:57:45 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
walk_tree(v, loc2, o.value_at(i));
|
|
|
|
}
|
2011-08-24 18:57:45 +05:30
|
|
|
|
2013-05-28 18:23:23 +05:30
|
|
|
} else {
|
|
|
|
leaf_node ov = to_node<ValueTraits>(blk);
|
|
|
|
v.visit_leaf(loc, ov);
|
|
|
|
}
|
2011-08-22 16:25:55 +05:30
|
|
|
}
|
2013-06-20 16:47:16 +05:30
|
|
|
|
|
|
|
|
|
|
|
template <unsigned Levels, typename _>
|
|
|
|
template <typename ValueTraits, typename RefCounter>
|
|
|
|
void
|
|
|
|
btree<Levels, _>::inc_children(btree_detail::shadow_spine &spine, RefCounter &leaf_rc)
|
|
|
|
{
|
2013-06-20 18:57:40 +05:30
|
|
|
node_ref<block_traits> nr = spine.template get_node<block_traits>();
|
2013-06-20 16:47:16 +05:30
|
|
|
if (nr.get_type() == INTERNAL)
|
|
|
|
nr.inc_children(internal_rc_);
|
|
|
|
else {
|
|
|
|
node_ref<ValueTraits> leaf = spine.template get_node<ValueTraits>();
|
|
|
|
leaf.inc_children(leaf_rc);
|
|
|
|
}
|
|
|
|
}
|
2011-08-22 16:25:55 +05:30
|
|
|
}
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
//----------------------------------------------------------------
|