2011-06-23 19:17:08 +05:30
|
|
|
#include "btree.h"
|
2011-06-27 15:15:30 +05:30
|
|
|
#include "transaction_manager.h"
|
|
|
|
|
2011-07-15 19:51:28 +05:30
|
|
|
#include <iostream>
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-07-15 19:51:28 +05:30
|
|
|
using namespace btree_detail;
|
2011-06-23 19:17:08 +05:30
|
|
|
using namespace persistent_data;
|
2011-07-15 19:51:28 +05:30
|
|
|
using namespace std;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
node_ref<ValueTraits>::node_ref(block_address location, disk_node *raw)
|
2011-08-22 18:39:37 +05:30
|
|
|
: location_(location),
|
|
|
|
raw_(raw)
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-08-23 16:25:37 +05:30
|
|
|
block_address
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::get_block_nr() const
|
2011-08-23 16:25:37 +05:30
|
|
|
{
|
|
|
|
return to_cpu<uint64_t>(raw_->header.blocknr);
|
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-07-15 19:51:28 +05:30
|
|
|
btree_detail::node_type
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::get_type() const
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
|
|
|
uint32_t flags = to_cpu<uint32_t>(raw_->header.flags);
|
|
|
|
if (flags & INTERNAL_NODE)
|
|
|
|
return INTERNAL;
|
|
|
|
else if (flags & LEAF_NODE)
|
|
|
|
return LEAF;
|
|
|
|
else
|
2011-09-02 19:35:26 +05:30
|
|
|
throw runtime_error("unknown node type");
|
2011-07-15 19:51:28 +05:30
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-07-15 19:51:28 +05:30
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::set_type(node_type t)
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
|
|
|
uint32_t flags = to_cpu<uint32_t>(raw_->header.flags);
|
|
|
|
switch (t) {
|
|
|
|
case INTERNAL:
|
|
|
|
flags |= INTERNAL_NODE;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case LEAF:
|
|
|
|
flags |= LEAF_NODE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
raw_->header.flags = to_disk<__le32>(flags);
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-07-15 19:51:28 +05:30
|
|
|
unsigned
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::get_nr_entries() const
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
|
|
|
return to_cpu<uint32_t>(raw_->header.nr_entries);
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-07-15 19:51:28 +05:30
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::set_nr_entries(unsigned n)
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
|
|
|
raw_->header.nr_entries = to_disk<__le32>(n);
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-07-15 19:51:28 +05:30
|
|
|
unsigned
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::get_max_entries() const
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
|
|
|
return to_cpu<uint32_t>(raw_->header.max_entries);
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-07-15 19:51:28 +05:30
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::set_max_entries(unsigned n)
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
|
|
|
raw_->header.max_entries = to_disk<__le32>(n);
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-07-15 19:51:28 +05:30
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::set_max_entries()
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
|
|
|
set_max_entries(calc_max_entries());
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-08-23 16:25:37 +05:30
|
|
|
size_t
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::get_value_size() const
|
2011-08-23 16:25:37 +05:30
|
|
|
{
|
|
|
|
return to_cpu<uint32_t>(raw_->header.value_size);
|
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-07-15 19:51:28 +05:30
|
|
|
uint64_t
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::key_at(unsigned i) const
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
2011-08-22 18:39:37 +05:30
|
|
|
if (i >= get_nr_entries())
|
|
|
|
throw runtime_error("key index out of bounds");
|
2011-07-15 19:51:28 +05:30
|
|
|
return to_cpu<uint64_t>(raw_->keys[i]);
|
|
|
|
}
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-07-15 19:51:28 +05:30
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::set_key(unsigned i, uint64_t k)
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
|
|
|
raw_->keys[i] = to_disk<__le64>(k);
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-07-15 19:51:28 +05:30
|
|
|
typename ValueTraits::value_type
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::value_at(unsigned i) const
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
2011-08-22 18:39:37 +05:30
|
|
|
if (i >= get_nr_entries())
|
|
|
|
throw runtime_error("value index out of bounds");
|
|
|
|
|
2011-07-15 19:51:28 +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
|
|
|
|
2011-07-15 19:51:28 +05:30
|
|
|
typename ValueTraits::value_type v;
|
|
|
|
ValueTraits::unpack(d, v);
|
|
|
|
return v;
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-07-15 19:51:28 +05:30
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::set_value(unsigned i,
|
|
|
|
typename ValueTraits::value_type const &v)
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
|
|
|
typename ValueTraits::disk_type d;
|
|
|
|
ValueTraits::pack(v, d);
|
|
|
|
::memcpy(value_ptr(i), &d, sizeof(d));
|
|
|
|
}
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-07-15 19:51:28 +05:30
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::insert_at(unsigned i,
|
|
|
|
uint64_t key,
|
|
|
|
typename ValueTraits::value_type const &v)
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
|
|
|
unsigned n = get_nr_entries();
|
|
|
|
if ((n + 1) > get_max_entries())
|
|
|
|
throw runtime_error("too many entries");
|
|
|
|
|
|
|
|
set_nr_entries(n + 1);
|
2011-08-22 18:39:37 +05:30
|
|
|
::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));
|
2011-07-15 19:51:28 +05:30
|
|
|
overwrite_at(i, key, v);
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-07-15 19:51:28 +05:30
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::overwrite_at(unsigned i,
|
|
|
|
uint64_t key,
|
|
|
|
typename ValueTraits::value_type const &v)
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
|
|
|
set_key(i, key);
|
|
|
|
set_value(i, v);
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-07-15 19:51:28 +05:30
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::copy_entries(node_ref const &rhs,
|
|
|
|
unsigned begin,
|
|
|
|
unsigned end)
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
|
|
|
unsigned count = end - begin;
|
|
|
|
unsigned n = get_nr_entries();
|
|
|
|
if ((n + count) > get_max_entries())
|
|
|
|
throw runtime_error("too many entries");
|
|
|
|
|
|
|
|
set_nr_entries(n + count);
|
2011-07-22 20:39:56 +05:30
|
|
|
::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);
|
2011-07-15 19:51:28 +05:30
|
|
|
}
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-07-15 19:51:28 +05:30
|
|
|
int
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::bsearch(uint64_t key, int want_hi) const
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
|
|
|
int lo = -1, hi = get_nr_entries();
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2011-07-15 19:51:28 +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
|
|
|
|
2011-07-15 19:51:28 +05:30
|
|
|
if (mid_key == key)
|
|
|
|
return mid;
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2011-07-15 19:51:28 +05:30
|
|
|
if (mid_key < key)
|
|
|
|
lo = mid;
|
|
|
|
else
|
|
|
|
hi = mid;
|
|
|
|
}
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2011-07-15 19:51:28 +05:30
|
|
|
return want_hi ? hi : lo;
|
|
|
|
}
|
2011-06-27 15:15:30 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-07-15 19:51:28 +05:30
|
|
|
optional<unsigned>
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::exact_search(uint64_t key) const
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
|
|
|
int i = bsearch(key, 0);
|
|
|
|
if (i < 0 || static_cast<unsigned>(i) >= get_nr_entries())
|
|
|
|
return optional<unsigned>();
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-07-15 19:51:28 +05:30
|
|
|
return optional<unsigned>(i);
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-07-15 19:51:28 +05:30
|
|
|
int
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::lower_bound(uint64_t key) const
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
|
|
|
return bsearch(key, 0);
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-07-15 19:51:28 +05:30
|
|
|
unsigned
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::calc_max_entries(void)
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
|
|
|
uint32_t total;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-07-15 19:51:28 +05:30
|
|
|
// key + value
|
|
|
|
size_t elt_size = sizeof(uint64_t) + sizeof(typename ValueTraits::disk_type);
|
2011-08-31 17:18:41 +05:30
|
|
|
total = (MD_BLOCK_SIZE - sizeof(struct node_header)) / elt_size;
|
2011-07-15 19:51:28 +05:30
|
|
|
return (total / 3) * 3; // rounds down
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-07-15 19:51:28 +05:30
|
|
|
void *
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::key_ptr(unsigned i) const
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
|
|
|
return raw_->keys + i;
|
|
|
|
}
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-07-15 19:51:28 +05:30
|
|
|
void *
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::value_ptr(unsigned i) const
|
2011-07-15 19:51:28 +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;
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <typename ValueTraits>
|
2011-07-22 20:39:56 +05:30
|
|
|
template <typename RefCounter>
|
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits>::inc_children(RefCounter &rc)
|
2011-07-22 20:39:56 +05:30
|
|
|
{
|
|
|
|
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-15 19:51:28 +05:30
|
|
|
//----------------------------------------------------------------
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
|
|
|
btree<Levels, ValueTraits>::
|
|
|
|
btree(typename transaction_manager::ptr tm,
|
2011-07-22 20:39:56 +05:30
|
|
|
typename ValueTraits::ref_counter rc)
|
2011-06-23 19:17:08 +05:30
|
|
|
: tm_(tm),
|
2011-07-22 20:39:56 +05:30
|
|
|
destroy_(false),
|
|
|
|
rc_(rc)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-06-27 15:15:30 +05:30
|
|
|
using namespace btree_detail;
|
|
|
|
|
|
|
|
write_ref root = tm_->new_block();
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
leaf_node n = to_node<ValueTraits>(root);
|
2011-07-15 19:51:28 +05:30
|
|
|
n.set_type(btree_detail::LEAF);
|
2011-06-23 19:17:08 +05:30
|
|
|
n.set_nr_entries(0);
|
2011-07-15 19:51:28 +05:30
|
|
|
n.set_max_entries();
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-06-27 15:15:30 +05:30
|
|
|
root_ = root.get_location();
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
|
|
|
btree<Levels, ValueTraits>::
|
|
|
|
btree(typename transaction_manager::ptr tm,
|
2011-07-22 20:39:56 +05:30
|
|
|
block_address root,
|
|
|
|
typename ValueTraits::ref_counter rc)
|
2011-06-23 19:17:08 +05:30
|
|
|
: tm_(tm),
|
|
|
|
destroy_(false),
|
2011-07-22 20:39:56 +05:30
|
|
|
root_(root),
|
|
|
|
rc_(rc)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
|
|
|
btree<Levels, ValueTraits>::~btree()
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
|
|
|
typename btree<Levels, ValueTraits>::maybe_value
|
|
|
|
btree<Levels, ValueTraits>::lookup(key const &key) const
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-06-27 15:15:30 +05:30
|
|
|
using namespace btree_detail;
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
ro_spine spine(tm_);
|
2011-06-23 19:17:08 +05:30
|
|
|
block_address root = root_;
|
|
|
|
|
|
|
|
for (unsigned level = 0; level < Levels - 1; ++level) {
|
2011-06-27 15:15:30 +05:30
|
|
|
optional<block_address> mroot =
|
2011-08-31 17:18:41 +05:30
|
|
|
lookup_raw<uint64_traits>(spine, root, key[level]);
|
2011-06-23 19:17:08 +05:30
|
|
|
if (!mroot)
|
|
|
|
return maybe_value();
|
|
|
|
|
|
|
|
root = *mroot;
|
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
return lookup_raw<ValueTraits>(spine, root, key[Levels - 1]);
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
|
|
|
typename btree<Levels, ValueTraits>::maybe_pair
|
|
|
|
btree<Levels, ValueTraits>::lookup_le(key const &key) const
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-06-27 15:15:30 +05:30
|
|
|
using namespace btree_detail;
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
return maybe_pair();
|
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
|
|
|
typename btree<Levels, ValueTraits>::maybe_pair
|
|
|
|
btree<Levels, ValueTraits>::lookup_ge(key const &key) const
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-06-27 15:15:30 +05:30
|
|
|
using namespace btree_detail;
|
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
return maybe_pair();
|
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
2011-06-23 19:17:08 +05:30
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
btree<Levels, ValueTraits>::
|
2011-07-15 19:51:28 +05:30
|
|
|
insert(key const &key,
|
|
|
|
typename ValueTraits::value_type const &value)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-06-27 15:15:30 +05:30
|
|
|
using namespace btree_detail;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
2011-07-15 19:51:28 +05:30
|
|
|
block_address block = root_;
|
|
|
|
int index = 0; // FIXME: ???
|
2011-08-31 17:18:41 +05:30
|
|
|
shadow_spine spine(tm_);
|
2011-07-15 19:51:28 +05:30
|
|
|
|
|
|
|
for (unsigned level = 0; level < Levels - 1; ++level) {
|
|
|
|
bool need_insert = insert_location<uint64_traits>(spine, block, key[level], &index);
|
|
|
|
|
2011-08-24 18:57:45 +05:30
|
|
|
internal_node n = spine.template get_node<uint64_traits>();
|
2011-07-15 19:51:28 +05:30
|
|
|
if (need_insert) {
|
2011-08-31 17:18:41 +05:30
|
|
|
btree<Levels - 1, ValueTraits> new_tree(tm_, rc_);
|
2011-07-15 19:51:28 +05:30
|
|
|
n.insert_at(index, key[level], new_tree.get_root());
|
|
|
|
}
|
|
|
|
|
|
|
|
block = n.value_at(index);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool need_insert = insert_location<ValueTraits>(spine, block, key[Levels - 1], &index);
|
|
|
|
|
2011-08-24 18:57:45 +05:30
|
|
|
leaf_node n = spine.template get_node<ValueTraits>();
|
2011-07-15 19:51:28 +05:30
|
|
|
if (need_insert)
|
|
|
|
n.insert_at(index, key[Levels - 1], value);
|
|
|
|
else
|
|
|
|
// FIXME: check if we're overwriting with the same value.
|
|
|
|
n.set_value(index, value);
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
2011-06-23 19:17:08 +05:30
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
btree<Levels, ValueTraits>::remove(key const &key)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-06-27 15:15:30 +05:30
|
|
|
using namespace btree_detail;
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
2011-06-23 19:17:08 +05:30
|
|
|
block_address
|
2011-08-31 17:18:41 +05:30
|
|
|
btree<Levels, ValueTraits>::get_root() const
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-07-22 20:39:56 +05:30
|
|
|
return root_;
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
2011-06-23 19:17:08 +05:30
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
btree<Levels, ValueTraits>::set_root(block_address root)
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-06-27 15:15:30 +05:30
|
|
|
using namespace btree_detail;
|
2011-07-22 20:39:56 +05:30
|
|
|
root_ = root;
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
|
|
|
typename btree<Levels, ValueTraits>::ptr
|
|
|
|
btree<Levels, ValueTraits>::clone() const
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-08-31 18:10:52 +05:30
|
|
|
tm_->get_sm()->inc(root_);
|
|
|
|
return ptr(new btree<Levels, ValueTraits>(tm_, root_, rc_));
|
2011-06-23 19:17:08 +05:30
|
|
|
}
|
|
|
|
|
2011-07-22 20:39:56 +05:30
|
|
|
#if 0
|
2011-08-31 17:18:41 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
2011-06-23 19:17:08 +05:30
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
btree<Levels, ValueTraits>::destroy()
|
2011-06-23 19:17:08 +05:30
|
|
|
{
|
2011-06-27 15:15:30 +05:30
|
|
|
using namespace btree_detail;
|
2011-06-23 19:17:08 +05:30
|
|
|
|
|
|
|
}
|
|
|
|
#endif
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <unsigned Levels, typename _>
|
2011-07-15 19:51:28 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
btree<Levels, _>::
|
|
|
|
split_node(btree_detail::shadow_spine &spine,
|
2011-07-15 19:51:28 +05:30
|
|
|
block_address parent_index,
|
|
|
|
uint64_t key,
|
|
|
|
bool top)
|
|
|
|
{
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits> n = spine.template get_node<ValueTraits>();
|
2011-07-15 19:51:28 +05:30
|
|
|
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-08-31 17:18:41 +05:30
|
|
|
template <unsigned Levels, typename _>
|
2011-07-15 19:51:28 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
btree<Levels, _>::
|
|
|
|
split_beneath(btree_detail::shadow_spine &spine,
|
2011-07-15 19:51:28 +05:30
|
|
|
uint64_t key)
|
|
|
|
{
|
|
|
|
using namespace btree_detail;
|
|
|
|
|
|
|
|
node_type type;
|
|
|
|
unsigned nr_left, nr_right;
|
|
|
|
|
2011-08-24 18:57:45 +05:30
|
|
|
write_ref left = tm_->new_block();
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits> l = to_node<ValueTraits>(left);
|
2011-07-15 19:51:28 +05:30
|
|
|
l.set_nr_entries(0);
|
|
|
|
l.set_max_entries();
|
|
|
|
|
2011-08-24 18:57:45 +05:30
|
|
|
write_ref right = tm_->new_block();
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits> r = to_node<ValueTraits>(right);
|
2011-07-15 19:51:28 +05:30
|
|
|
r.set_nr_entries(0);
|
|
|
|
r.set_max_entries();
|
|
|
|
|
|
|
|
{
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits> p = spine.template get_node<ValueTraits>();
|
2011-07-15 19:51:28 +05:30
|
|
|
nr_left = p.get_nr_entries() / 2;
|
|
|
|
nr_right = p.get_nr_entries() - nr_left;
|
|
|
|
type = p.get_type();
|
|
|
|
|
|
|
|
l.set_type(type);
|
|
|
|
l.copy_entries(p, 0, nr_left);
|
|
|
|
|
|
|
|
r.set_type(type);
|
|
|
|
r.copy_entries(p, nr_left, nr_left + nr_right);
|
|
|
|
}
|
|
|
|
|
|
|
|
{
|
|
|
|
// The parent may have changed value type, so we re-get it.
|
2011-08-24 18:57:45 +05:30
|
|
|
internal_node p = spine.template get_node<uint64_traits>();
|
2011-07-15 19:51:28 +05:30
|
|
|
p.set_type(btree_detail::INTERNAL);
|
|
|
|
p.set_nr_entries(2);
|
|
|
|
|
2011-08-24 18:57:45 +05:30
|
|
|
// FIXME: set the value_size
|
2011-07-15 19:51:28 +05:30
|
|
|
p.overwrite_at(0, l.key_at(0), left.get_location());
|
|
|
|
p.overwrite_at(1, r.key_at(0), right.get_location());
|
|
|
|
}
|
|
|
|
|
|
|
|
if (key < r.key_at(0))
|
|
|
|
spine.step(left);
|
|
|
|
else
|
|
|
|
spine.step(right);
|
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <unsigned Levels, typename _>
|
2011-07-15 19:51:28 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
btree<Levels, _>::
|
|
|
|
split_sibling(btree_detail::shadow_spine &spine,
|
2011-07-22 20:39:56 +05:30
|
|
|
block_address parent_index,
|
|
|
|
uint64_t key)
|
2011-07-15 19:51:28 +05:30
|
|
|
{
|
|
|
|
using namespace btree_detail;
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits> l = spine.template get_node<ValueTraits>();
|
2011-08-24 18:57:45 +05:30
|
|
|
block_address left = spine.get_block();
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2011-08-24 18:57:45 +05:30
|
|
|
write_ref right = tm_->new_block();
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits> r = to_node<ValueTraits>(right);
|
2011-07-15 19:51:28 +05:30
|
|
|
|
|
|
|
unsigned nr_left = l.get_nr_entries() / 2;
|
|
|
|
unsigned nr_right = l.get_nr_entries() - nr_left;
|
|
|
|
|
|
|
|
r.set_nr_entries(0);
|
|
|
|
r.set_type(l.get_type());
|
|
|
|
r.set_max_entries(l.get_max_entries());
|
|
|
|
r.copy_entries(l, nr_left, nr_left + nr_right);
|
|
|
|
l.set_nr_entries(nr_left);
|
|
|
|
|
2011-08-24 18:57:45 +05:30
|
|
|
internal_node p = spine.get_parent();
|
2011-07-15 19:51:28 +05:30
|
|
|
p.overwrite_at(parent_index, l.key_at(0), left);
|
|
|
|
p.insert_at(parent_index + 1, r.key_at(0), right.get_location());
|
|
|
|
|
|
|
|
spine.pop();
|
|
|
|
if (key < r.key_at(0))
|
|
|
|
spine.step(left);
|
|
|
|
else
|
|
|
|
spine.step(right);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns true if we need a new insertion, rather than overwrite.
|
2011-08-31 17:18:41 +05:30
|
|
|
template <unsigned Levels, typename _>
|
2011-07-15 19:51:28 +05:30
|
|
|
template <typename ValueTraits>
|
|
|
|
bool
|
2011-08-31 17:18:41 +05:30
|
|
|
btree<Levels, _>::
|
|
|
|
insert_location(btree_detail::shadow_spine &spine,
|
2011-07-15 19:51:28 +05:30
|
|
|
block_address block,
|
|
|
|
uint64_t key,
|
|
|
|
int *index)
|
|
|
|
{
|
|
|
|
using namespace btree_detail;
|
|
|
|
|
|
|
|
bool top = true; // this isn't the same as spine.has_parent()
|
|
|
|
int i = *index;
|
|
|
|
bool inc = false;
|
|
|
|
|
|
|
|
for (;;) {
|
|
|
|
inc = spine.step(block);
|
|
|
|
#if 0
|
|
|
|
if (inc)
|
|
|
|
inc_children<ValueTraits>();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// patch up the parent to point to the new shadow
|
|
|
|
if (spine.has_parent()) {
|
2011-08-24 18:57:45 +05:30
|
|
|
internal_node p = spine.get_parent();
|
2011-07-15 19:51:28 +05:30
|
|
|
p.set_value(i, spine.get_block());
|
|
|
|
}
|
|
|
|
|
2011-08-24 18:57:45 +05:30
|
|
|
internal_node internal = spine.template get_node<uint64_traits>();
|
2011-07-15 19:51:28 +05:30
|
|
|
|
|
|
|
// Split the node if we're full
|
|
|
|
if (internal.get_type() == INTERNAL)
|
|
|
|
split_node<uint64_traits>(spine, i, key, top);
|
|
|
|
else
|
|
|
|
split_node<ValueTraits>(spine, i, key, top);
|
|
|
|
|
|
|
|
internal = spine.template get_node<uint64_traits>();
|
|
|
|
i = internal.lower_bound(key);
|
|
|
|
if (internal.get_type() == btree_detail::LEAF)
|
|
|
|
break;
|
|
|
|
|
|
|
|
if (i < 0) {
|
|
|
|
internal.set_key(0, key);
|
|
|
|
i = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
block = internal.value_at(i);
|
|
|
|
top = false;
|
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
node_ref<ValueTraits> leaf = spine.template get_node<ValueTraits>();
|
2011-07-15 19:51:28 +05:30
|
|
|
// FIXME: gross
|
|
|
|
if (i < 0 || leaf.key_at(i) != key)
|
|
|
|
i++;
|
|
|
|
|
|
|
|
// do decrement the old value if it already exists
|
|
|
|
// FIXME: I'm not sure about this, I don't understand the |inc| reference
|
2011-08-22 18:39:37 +05:30
|
|
|
if (static_cast<unsigned>(i) < leaf.get_nr_entries() && leaf.key_at(i) == key && inc) {
|
2011-07-15 19:51:28 +05:30
|
|
|
// dec old entry
|
|
|
|
}
|
|
|
|
*index = i;
|
|
|
|
|
|
|
|
return ((static_cast<unsigned>(i) >= leaf.get_nr_entries()) ||
|
|
|
|
(leaf.key_at(i) != key));
|
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
2011-08-22 16:25:55 +05:30
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
btree<Levels, ValueTraits>::visit(typename visitor::ptr visitor) const
|
2011-08-22 16:25:55 +05:30
|
|
|
{
|
2011-09-02 15:56:42 +05:30
|
|
|
walk_tree(visitor, 0, boost::optional<uint64_t>(), root_);
|
2011-08-22 16:25:55 +05:30
|
|
|
}
|
|
|
|
|
2011-08-31 17:18:41 +05:30
|
|
|
template <unsigned Levels, typename ValueTraits>
|
2011-08-22 16:25:55 +05:30
|
|
|
void
|
2011-08-31 17:18:41 +05:30
|
|
|
btree<Levels, ValueTraits>::
|
2011-08-22 16:25:55 +05:30
|
|
|
walk_tree(typename visitor::ptr visitor,
|
2011-09-02 15:56:42 +05:30
|
|
|
unsigned level, boost::optional<uint64_t> key,
|
2011-08-25 20:57:58 +05:30
|
|
|
block_address b) const
|
2011-08-22 16:25:55 +05:30
|
|
|
{
|
|
|
|
using namespace btree_detail;
|
2011-08-23 16:25:37 +05:30
|
|
|
|
2011-08-26 15:43:13 +05:30
|
|
|
read_ref blk = tm_->read_lock(b);
|
2011-08-31 17:18:41 +05:30
|
|
|
internal_node o = to_node<uint64_traits>(blk);
|
2011-08-26 15:43:13 +05:30
|
|
|
if (o.get_type() == INTERNAL) {
|
2011-09-02 15:56:42 +05:30
|
|
|
if (visitor->visit_internal(level, key, o))
|
2011-08-24 18:57:45 +05:30
|
|
|
for (unsigned i = 0; i < o.get_nr_entries(); i++)
|
2011-09-02 15:56:42 +05:30
|
|
|
walk_tree(visitor, level, o.key_at(i), o.value_at(i));
|
2011-08-24 18:57:45 +05:30
|
|
|
|
2011-08-26 15:43:13 +05:30
|
|
|
} else if (level < Levels - 1) {
|
2011-09-02 15:56:42 +05:30
|
|
|
if (visitor->visit_internal_leaf(level, key, o))
|
2011-08-24 18:57:45 +05:30
|
|
|
for (unsigned i = 0; i < o.get_nr_entries(); i++)
|
2011-09-02 15:56:42 +05:30
|
|
|
walk_tree(visitor, level + 1, boost::optional<uint64_t>(), o.value_at(i));
|
2011-08-24 18:57:45 +05:30
|
|
|
|
2011-08-26 15:43:13 +05:30
|
|
|
} else {
|
2011-08-31 17:18:41 +05:30
|
|
|
leaf_node ov = to_node<ValueTraits>(blk);
|
2011-09-02 15:56:42 +05:30
|
|
|
visitor->visit_leaf(level, key, ov);
|
2011-08-22 16:25:55 +05:30
|
|
|
}
|
|
|
|
}
|
2011-07-15 19:51:28 +05:30
|
|
|
|
2011-06-23 19:17:08 +05:30
|
|
|
//----------------------------------------------------------------
|