From 7a003cd9d52bfdbc2891f81af2190a4bfb350c2c Mon Sep 17 00:00:00 2001 From: Yoni Fogel Date: Wed, 17 Apr 2013 00:01:00 -0400 Subject: [PATCH] refs #5139 separate omt-tmpl implementation (partially) git-svn-id: file:///svn/toku/tokudb@45984 c7de825b-a66e-492c-adef-691d508d4ae1 --- ft/omt-tmpl.cc | 938 +++++++++++++++++++++++++++++++++++++++++++++++++ ft/omt-tmpl.h | 895 +++++----------------------------------------- 2 files changed, 1030 insertions(+), 803 deletions(-) create mode 100644 ft/omt-tmpl.cc diff --git a/ft/omt-tmpl.cc b/ft/omt-tmpl.cc new file mode 100644 index 00000000000..9e614dffd2e --- /dev/null +++ b/ft/omt-tmpl.cc @@ -0,0 +1,938 @@ +/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ +// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4: +#ident "$Id$" +#ident "Copyright (c) 2007-2012 Tokutek Inc. All rights reserved." +#ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it." + +#ifndef OMT_TMPL_H +#include +#include +#include +#include +#include +#include +#include +#include +#include "omt-tmpl.h" +#include "fttypes.h" +#include "log-internal.h" +#endif + +namespace toku { + +template +void omt::create(void) { + this->create_internal(2); +} + +template +void omt::create_no_array(void) { + this->create_internal_no_array(0); +} + +template +void omt::create_from_sorted_array(const omtdata_t *const values, const uint32_t numvalues) { + this->create_internal(numvalues); + memcpy(this->d.a.values, values, numvalues * (sizeof values[0])); + this->d.a.num_values = numvalues; +} + +template +void omt::create_steal_sorted_array(omtdata_t **const values, const uint32_t numvalues, const uint32_t new_capacity) { + invariant_notnull(values); + this->create_internal_no_array(new_capacity); + this->d.a.num_values = numvalues; + this->d.a.values = *values; + *values = nullptr; +} + +template +int omt::split_at(omt *const newomt, const uint32_t idx) { + invariant_notnull(newomt); + if (idx > this->size()) { return EINVAL; } + this->convert_to_array(); + const uint32_t newsize = this->size() - idx; + newomt->create_from_sorted_array(&this->d.a.values[this->d.a.start_idx + idx], newsize); + this->d.a.num_values = idx; + this->maybe_resize_array(idx); + return 0; +} + +template +void omt::merge(omt *const leftomt, omt *const rightomt) { + invariant_notnull(leftomt); + invariant_notnull(rightomt); + const uint32_t leftsize = leftomt->size(); + const uint32_t rightsize = rightomt->size(); + const uint32_t newsize = leftsize + rightsize; + + if (leftomt->is_array) { + if (leftomt->capacity - (leftomt->d.a.start_idx + leftomt->d.a.num_values) >= rightsize) { + this->create_steal_sorted_array(&leftomt->d.a.values, leftomt->d.a.num_values, leftomt->capacity); + this->d.a.start_idx = leftomt->d.a.start_idx; + } else { + this->create_internal(newsize); + memcpy(&this->d.a.values[0], + &leftomt->d.a.values[leftomt->d.a.start_idx], + leftomt->d.a.num_values * (sizeof this->d.a.values[0])); + } + } else { + this->create_internal(newsize); + leftomt->fill_array_with_subtree_values(&this->d.a.values[0], leftomt->d.t.root); + } + leftomt->destroy(); + this->d.a.num_values = leftsize; + + if (rightomt->is_array) { + memcpy(&this->d.a.values[this->d.a.start_idx + this->d.a.num_values], + &rightomt->d.a.values[rightomt->d.a.start_idx], + rightomt->d.a.num_values * (sizeof this->d.a.values[0])); + } else { + rightomt->fill_array_with_subtree_values(&this->d.a.values[this->d.a.start_idx + this->d.a.num_values], + rightomt->d.t.root); + } + rightomt->destroy(); + this->d.a.num_values += rightsize; + invariant(this->size() == newsize); +} + +template +void omt::clone(const omt &src) { + this->create_internal(src.size()); + if (src.is_array) { + memcpy(&this->d.a.values[0], &src.d.a.values[src.d.a.start_idx], src.d.a.num_values * (sizeof this->d.a.values[0])); + } else { + src.fill_array_with_subtree_values(&this->d.a.values[0], src.d.t.root); + } + this->d.a.num_values = src.size(); +} + +template +void omt::clear(void) { + if (this->is_array) { + this->d.a.start_idx = 0; + this->d.a.num_values = 0; + } else { + this->d.t.root = NODE_NULL; + this->d.t.free_idx = 0; + } +} + +template +void omt::destroy(void) { + this->clear(); + this->capacity = 0; + if (this->is_array) { + if (this->d.a.values != nullptr) { + toku_free(this->d.a.values); + } + this->d.a.values = nullptr; + } else { + if (this->d.t.nodes != nullptr) { + toku_free(this->d.t.nodes); + } + this->d.t.nodes = nullptr; + } +} + +template +uint32_t omt::size(void) const { + if (this->is_array) { + return this->d.a.num_values; + } else { + return this->nweight(this->d.t.root); + } +} + + +template +template +int omt::insert(const omtdata_t &value, const omtcmp_t &v, uint32_t *const idx) { + int r; + uint32_t insert_idx; + + r = this->find_zero(v, nullptr, &insert_idx); + if (r==0) { + if (idx) *idx = insert_idx; + return DB_KEYEXIST; + } + if (r != DB_NOTFOUND) return r; + + if ((r = this->insert_at(value, insert_idx))) return r; + if (idx) *idx = insert_idx; + + return 0; +} + +template +int omt::insert_at(const omtdata_t &value, const uint32_t idx) { + if (idx > this->size()) { return EINVAL; } + this->maybe_resize_or_convert(this->size() + 1); + if (this->is_array && idx != this->d.a.num_values && + (idx != 0 || this->d.a.start_idx == 0)) { + this->convert_to_tree(); + } + if (this->is_array) { + if (idx == this->d.a.num_values) { + this->d.a.values[this->d.a.start_idx + this->d.a.num_values] = value; + } + else { + this->d.a.values[--this->d.a.start_idx] = value; + } + this->d.a.num_values++; + } + else { + node_idx *rebalance_idx = nullptr; + this->insert_internal(&this->d.t.root, value, idx, &rebalance_idx); + if (rebalance_idx != nullptr) { + this->rebalance(rebalance_idx); + } + } + return 0; +} + +template +int omt::set_at(const omtdata_t &value, const uint32_t idx) { + if (idx >= this->size()) { return EINVAL; } + if (this->is_array) { + this->set_at_internal_array(value, idx); + } else { + this->set_at_internal(this->d.t.root, value, idx); + } + return 0; +} + +template +int omt::delete_at(const uint32_t idx) { + if (idx >= this->size()) { return EINVAL; } + this->maybe_resize_or_convert(this->size() - 1); + if (this->is_array && idx != 0 && idx != this->d.a.num_values - 1) { + this->convert_to_tree(); + } + if (this->is_array) { + //Testing for 0 does not rule out it being the last entry. + //Test explicitly for num_values-1 + if (idx != this->d.a.num_values - 1) { + this->d.a.start_idx++; + } + this->d.a.num_values--; + } else { + node_idx *rebalance_idx = nullptr; + this->delete_internal(&this->d.t.root, idx, nullptr, &rebalance_idx); + if (rebalance_idx != nullptr) { + this->rebalance(rebalance_idx); + } + } + return 0; +} + +template +template +int omt::iterate(iterate_extra_t *const iterate_extra) const { + return this->iterate_on_range(0, this->size(), iterate_extra); +} + +template +template +int omt::iterate_on_range(const uint32_t left, const uint32_t right, iterate_extra_t *const iterate_extra) const { + if (right > this->size()) { return EINVAL; } + if (this->is_array) { + return this->iterate_internal_array(left, right, iterate_extra); + } + return this->iterate_internal(left, right, this->d.t.root, 0, iterate_extra); +} + +template +template +void omt::iterate_ptr(iterate_extra_t *const iterate_extra) { + if (this->is_array) { + this->iterate_ptr_internal_array(0, this->size(), iterate_extra); + } else { + this->iterate_ptr_internal(0, this->size(), this->d.t.root, 0, iterate_extra); + } +} + +template +int omt::fetch(const uint32_t idx, omtdataout_t *const value) const { + if (idx >= this->size()) { return EINVAL; } + if (this->is_array) { + this->fetch_internal_array(idx, value); + } else { + this->fetch_internal(this->d.t.root, idx, value); + } + return 0; +} + +template +template +int omt::find_zero(const omtcmp_t &extra, omtdataout_t *const value, uint32_t *const idxp) const { + uint32_t tmp_index; + uint32_t *const child_idxp = (idxp != nullptr) ? idxp : &tmp_index; + int r; + if (this->is_array) { + r = this->find_internal_zero_array(extra, value, child_idxp); + } + else { + r = this->find_internal_zero(this->d.t.root, extra, value, child_idxp); + } + return r; +} + +template +template +int omt::find(const omtcmp_t &extra, int direction, omtdataout_t *const value, uint32_t *const idxp) const { + uint32_t tmp_index; + uint32_t *const child_idxp = (idxp != nullptr) ? idxp : &tmp_index; + invariant(direction != 0); + if (direction < 0) { + if (this->is_array) { + return this->find_internal_minus_array(extra, value, child_idxp); + } else { + return this->find_internal_minus(this->d.t.root, extra, value, child_idxp); + } + } else { + if (this->is_array) { + return this->find_internal_plus_array(extra, value, child_idxp); + } else { + return this->find_internal_plus(this->d.t.root, extra, value, child_idxp); + } + } +} + +template +size_t omt::memory_size(void) { + if (this->is_array) { + return (sizeof *this) + this->capacity * (sizeof this->d.a.values[0]); + } + return (sizeof *this) + this->capacity * (sizeof this->d.t.nodes[0]); +} + + +template +void omt::create_internal_no_array(const uint32_t new_capacity) { + this->is_array = true; + this->capacity = new_capacity; + this->d.a.start_idx = 0; + this->d.a.num_values = 0; + this->d.a.values = nullptr; +} + +template +void omt::create_internal(const uint32_t new_capacity) { + this->create_internal_no_array(new_capacity); + XMALLOC_N(this->capacity, this->d.a.values); +} + +template +uint32_t omt::nweight(const node_idx idx) const { + if (idx == NODE_NULL) { + return 0; + } else { + return this->d.t.nodes[idx].weight; + } +} + +template +typename omt::node_idx omt::node_malloc(void) { + invariant(this->d.t.free_idx < this->capacity); + return this->d.t.free_idx++; +} + +template +void omt::node_free(const node_idx idx) { + invariant(idx < this->capacity); +} + +template +void omt::maybe_resize_array(const uint32_t n) { + const uint32_t new_size = n<=2 ? 4 : 2*n; + const uint32_t room = this->capacity - this->d.a.start_idx; + + if (room < n || this->capacity / 2 >= new_size) { + omtdata_t *XMALLOC_N(new_size, tmp_values); + memcpy(tmp_values, &this->d.a.values[this->d.a.start_idx], + this->d.a.num_values * (sizeof tmp_values[0])); + this->d.a.start_idx = 0; + this->capacity = new_size; + toku_free(this->d.a.values); + this->d.a.values = tmp_values; + } +} + +template +void omt::fill_array_with_subtree_values(omtdata_t *const array, const node_idx tree_idx) const { + if (tree_idx==NODE_NULL) return; + const omt_node &tree = this->d.t.nodes[tree_idx]; + this->fill_array_with_subtree_values(&array[0], tree.left); + array[this->nweight(tree.left)] = tree.value; + this->fill_array_with_subtree_values(&array[this->nweight(tree.left) + 1], tree.right); +} + +template +void omt::convert_to_array(void) { + if (!this->is_array) { + const uint32_t num_values = this->size(); + uint32_t new_size = 2*num_values; + new_size = new_size < 4 ? 4 : new_size; + + omtdata_t *XMALLOC_N(new_size, tmp_values); + this->fill_array_with_subtree_values(tmp_values, this->d.t.root); + toku_free(this->d.t.nodes); + this->is_array = true; + this->capacity = new_size; + this->d.a.num_values = num_values; + this->d.a.values = tmp_values; + this->d.a.start_idx = 0; + } +} + +template +void omt::rebuild_from_sorted_array(node_idx *const n_idxp, const omtdata_t *const values, const uint32_t numvalues) { + if (numvalues==0) { + *n_idxp = NODE_NULL; + } else { + const uint32_t halfway = numvalues/2; + const node_idx newidx = this->node_malloc(); + omt_node *const newnode = &this->d.t.nodes[newidx]; + newnode->weight = numvalues; + newnode->value = values[halfway]; + *n_idxp = newidx; // update everything before the recursive calls so the second call can be a tail call. + this->rebuild_from_sorted_array(&newnode->left, &values[0], halfway); + this->rebuild_from_sorted_array(&newnode->right, &values[halfway+1], numvalues - (halfway+1)); + } +} + +template +void omt::convert_to_tree(void) { + if (this->is_array) { + const uint32_t num_nodes = this->size(); + uint32_t new_size = num_nodes*2; + new_size = new_size < 4 ? 4 : new_size; + + omt_node *XMALLOC_N(new_size, new_nodes); + omtdata_t *const values = this->d.a.values; + omtdata_t *const tmp_values = &values[this->d.a.start_idx]; + this->is_array = false; + this->d.t.nodes = new_nodes; + this->capacity = new_size; + this->d.t.free_idx = 0; + this->d.t.root = NODE_NULL; + this->rebuild_from_sorted_array(&this->d.t.root, tmp_values, num_nodes); + toku_free(values); + } +} + +template +void omt::maybe_resize_or_convert(const uint32_t n) { + if (this->is_array) { + this->maybe_resize_array(n); + } else { + const uint32_t new_size = n<=2 ? 4 : 2*n; + const uint32_t num_nodes = this->nweight(this->d.t.root); + if ((this->capacity/2 >= new_size) || + (this->d.t.free_idx >= this->capacity && num_nodes < n) || + (this->capacityconvert_to_array(); + } + } +} + +template +bool omt::will_need_rebalance(const node_idx n_idx, const int leftmod, const int rightmod) const { + if (n_idx==NODE_NULL) { return false; } + const omt_node &n = this->d.t.nodes[n_idx]; + // one of the 1's is for the root. + // the other is to take ceil(n/2) + const uint32_t weight_left = this->nweight(n.left) + leftmod; + const uint32_t weight_right = this->nweight(n.right) + rightmod; + return ((1+weight_left < (1+1+weight_right)/2) + || + (1+weight_right < (1+1+weight_left)/2)); +} + +template +void omt::insert_internal(node_idx *const n_idxp, const omtdata_t &value, const uint32_t idx, node_idx **const rebalance_idx) { + if (*n_idxp == NODE_NULL) { + invariant_zero(idx); + const node_idx newidx = this->node_malloc(); + omt_node *const newnode = &this->d.t.nodes[newidx]; + newnode->weight = 1; + newnode->left = NODE_NULL; + newnode->right = NODE_NULL; + newnode->value = value; + *n_idxp = newidx; + } else { + const node_idx thisidx = *n_idxp; + omt_node *const n = &this->d.t.nodes[thisidx]; + n->weight++; + if (idx <= this->nweight(n->left)) { + if (*rebalance_idx == nullptr && this->will_need_rebalance(thisidx, 1, 0)) { + *rebalance_idx = n_idxp; + } + this->insert_internal(&n->left, value, idx, rebalance_idx); + } else { + if (*rebalance_idx == nullptr && this->will_need_rebalance(thisidx, 0, 1)) { + *rebalance_idx = n_idxp; + } + const uint32_t sub_index = idx - this->nweight(n->left) - 1; + this->insert_internal(&n->right, value, sub_index, rebalance_idx); + } + } +} + +template +void omt::set_at_internal_array(const omtdata_t &value, const uint32_t idx) { + this->d.a.values[this->d.a.start_idx + idx] = value; +} + +template +void omt::set_at_internal(const node_idx n_idx, const omtdata_t &value, const uint32_t idx) { + invariant(n_idx != NODE_NULL); + omt_node *const n = &this->d.t.nodes[n_idx]; + const uint32_t leftweight = this->nweight(n->left); + if (idx < leftweight) { + this->set_at_internal(n->left, value, idx); + } else if (idx == leftweight) { + n->value = value; + } else { + this->set_at_internal(n->right, value, idx - leftweight - 1); + } +} + +template +void omt::delete_internal(node_idx *const n_idxp, const uint32_t idx, omt_node *const copyn, node_idx **const rebalance_idx) { + invariant_notnull(n_idxp); + invariant_notnull(rebalance_idx); + invariant(*n_idxp != NODE_NULL); + omt_node *const n = &this->d.t.nodes[*n_idxp]; + const uint32_t leftweight = this->nweight(n->left); + if (idx < leftweight) { + n->weight--; + if (*rebalance_idx == nullptr && this->will_need_rebalance(*n_idxp, -1, 0)) { + *rebalance_idx = n_idxp; + } + this->delete_internal(&n->left, idx, copyn, rebalance_idx); + } else if (idx == leftweight) { + if (n->left == NODE_NULL) { + const uint32_t oldidx = *n_idxp; + *n_idxp = n->right; + if (copyn != nullptr) { + copyn->value = n->value; + } + this->node_free(oldidx); + } else if (n->right == NODE_NULL) { + const uint32_t oldidx = *n_idxp; + *n_idxp = n->left; + if (copyn != nullptr) { + copyn->value = n->value; + } + this->node_free(oldidx); + } else { + if (*rebalance_idx == nullptr && this->will_need_rebalance(*n_idxp, 0, -1)) { + *rebalance_idx = n_idxp; + } + // don't need to copy up value, it's only used by this + // next call, and when that gets to the bottom there + // won't be any more recursion + n->weight--; + this->delete_internal(&n->right, 0, n, rebalance_idx); + } + } else { + n->weight--; + if (*rebalance_idx == nullptr && this->will_need_rebalance(*n_idxp, 0, -1)) { + *rebalance_idx = n_idxp; + } + this->delete_internal(&n->right, idx - leftweight - 1, copyn, rebalance_idx); + } +} + +template +template +int omt::iterate_internal_array(const uint32_t left, const uint32_t right, + iterate_extra_t *const iterate_extra) const { + int r; + for (uint32_t i = left; i < right; ++i) { + r = f(this->d.a.values[this->d.a.start_idx + i], i, iterate_extra); + if (r != 0) { + return r; + } + } + return 0; +} + +template +template +void omt::iterate_ptr_internal(const uint32_t left, const uint32_t right, + const node_idx n_idx, const uint32_t idx, + iterate_extra_t *const iterate_extra) { + if (n_idx != NODE_NULL) { + omt_node *const n = &this->d.t.nodes[n_idx]; + const uint32_t idx_root = idx + this->nweight(n->left); + if (left < idx_root) { + this->iterate_ptr_internal(left, right, n->left, idx, iterate_extra); + } + if (left <= idx_root && idx_root < right) { + int r = f(&n->value, idx_root, iterate_extra); + lazy_assert_zero(r); + } + if (idx_root + 1 < right) { + this->iterate_ptr_internal(left, right, n->right, idx_root + 1, iterate_extra); + } + } +} + +template +template +void omt::iterate_ptr_internal_array(const uint32_t left, const uint32_t right, + iterate_extra_t *const iterate_extra) { + for (uint32_t i = left; i < right; ++i) { + int r = f(&this->d.a.values[this->d.a.start_idx + i], i, iterate_extra); + lazy_assert_zero(r); + } +} + +template +template +int omt::iterate_internal(const uint32_t left, const uint32_t right, + const node_idx n_idx, const uint32_t idx, + iterate_extra_t *const iterate_extra) const { + if (n_idx == NODE_NULL) { return 0; } + int r; + const omt_node &n = this->d.t.nodes[n_idx]; + const uint32_t idx_root = idx + this->nweight(n.left); + if (left < idx_root) { + r = this->iterate_internal(left, right, n.left, idx, iterate_extra); + if (r != 0) { return r; } + } + if (left <= idx_root && idx_root < right) { + r = f(n.value, idx_root, iterate_extra); + if (r != 0) { return r; } + } + if (idx_root + 1 < right) { + return this->iterate_internal(left, right, n.right, idx_root + 1, iterate_extra); + } + return 0; +} + +template +void omt::fetch_internal_array(const uint32_t i, omtdataout_t *value) const { + if (value != nullptr) { + copyout(value, &this->d.a.values[this->d.a.start_idx + i]); + } +} + +template +void omt::fetch_internal(const node_idx idx, const uint32_t i, omtdataout_t *value) const { + omt_node *const n = &this->d.t.nodes[idx]; + const uint32_t leftweight = this->nweight(n->left); + if (i < leftweight) { + this->fetch_internal(n->left, i, value); + } else if (i == leftweight) { + if (value != nullptr) { + copyout(value, n); + } + } else { + this->fetch_internal(n->right, i - leftweight - 1, value); + } +} + +template +void omt::fill_array_with_subtree_idxs(node_idx *const array, const node_idx tree_idx) const { + if (tree_idx != NODE_NULL) { + const omt_node &tree = this->d.t.nodes[tree_idx]; + this->fill_array_with_subtree_idxs(&array[0], tree.left); + array[this->nweight(tree.left)] = tree_idx; + this->fill_array_with_subtree_idxs(&array[this->nweight(tree.left) + 1], tree.right); + } +} + +template +void omt::rebuild_subtree_from_idxs(node_idx *const n_idxp, const node_idx *const idxs, const uint32_t numvalues) { + if (numvalues==0) { + *n_idxp = NODE_NULL; + } else { + uint32_t halfway = numvalues/2; + *n_idxp = idxs[halfway]; + //node_idx newidx = idxs[halfway]; + omt_node *const newnode = &this->d.t.nodes[*n_idxp]; + newnode->weight = numvalues; + // value is already in there. + this->rebuild_subtree_from_idxs(&newnode->left, &idxs[0], halfway); + this->rebuild_subtree_from_idxs(&newnode->right, &idxs[halfway+1], numvalues-(halfway+1)); + //n_idx = newidx; + } +} + +template +void omt::rebalance(node_idx *const n_idxp) { + node_idx idx = *n_idxp; + if (idx==this->d.t.root) { + //Try to convert to an array. + //If this fails, (malloc) nothing will have changed. + //In the failure case we continue on to the standard rebalance + //algorithm. + this->convert_to_array(); + } else { + const omt_node &n = this->d.t.nodes[idx]; + node_idx *tmp_array; + size_t mem_needed = n.weight * (sizeof tmp_array[0]); + size_t mem_free = (this->capacity - this->d.t.free_idx) * (sizeof this->d.t.nodes[0]); + bool malloced; + if (mem_needed<=mem_free) { + //There is sufficient free space at the end of the nodes array + //to hold enough node indexes to rebalance. + malloced = false; + tmp_array = reinterpret_cast(&this->d.t.nodes[this->d.t.free_idx]); + } + else { + malloced = true; + XMALLOC_N(n.weight, tmp_array); + } + this->fill_array_with_subtree_idxs(tmp_array, idx); + this->rebuild_subtree_from_idxs(n_idxp, tmp_array, n.weight); + if (malloced) toku_free(tmp_array); + } +} + +template +void omt::copyout(omtdata_t *const out, const omt_node *const n) { + *out = n->value; +} + +template +void omt::copyout(omtdata_t **const out, omt_node *const n) { + *out = &n->value; +} + +template +void omt::copyout(omtdata_t *const out, const omtdata_t *const stored_value_ptr) { + *out = *stored_value_ptr; +} + +template +void omt::copyout(omtdata_t **const out, omtdata_t *const stored_value_ptr) { + *out = stored_value_ptr; +} + +template +template +int omt::find_internal_zero_array(const omtcmp_t &extra, omtdataout_t *value, uint32_t *const idxp) const { + invariant_notnull(idxp); + uint32_t min = this->d.a.start_idx; + uint32_t limit = this->d.a.start_idx + this->d.a.num_values; + uint32_t best_pos = NODE_NULL; + uint32_t best_zero = NODE_NULL; + + while (min!=limit) { + uint32_t mid = (min + limit) / 2; + int hv = h(this->d.a.values[mid], extra); + if (hv<0) { + min = mid+1; + } + else if (hv>0) { + best_pos = mid; + limit = mid; + } + else { + best_zero = mid; + limit = mid; + } + } + if (best_zero!=NODE_NULL) { + //Found a zero + if (value != nullptr) { + copyout(value, &this->d.a.values[best_zero]); + } + *idxp = best_zero - this->d.a.start_idx; + return 0; + } + if (best_pos!=NODE_NULL) *idxp = best_pos - this->d.a.start_idx; + else *idxp = this->d.a.num_values; + return DB_NOTFOUND; +} + +template +template +int omt::find_internal_zero(const node_idx n_idx, const omtcmp_t &extra, omtdataout_t *value, uint32_t *const idxp) const { + invariant_notnull(idxp); + if (n_idx==NODE_NULL) { + *idxp = 0; + return DB_NOTFOUND; + } + omt_node *const n = &this->d.t.nodes[n_idx]; + int hv = h(n->value, extra); + if (hv<0) { + int r = this->find_internal_zero(n->right, extra, value, idxp); + *idxp += this->nweight(n->left)+1; + return r; + } else if (hv>0) { + return this->find_internal_zero(n->left, extra, value, idxp); + } else { + int r = this->find_internal_zero(n->left, extra, value, idxp); + if (r==DB_NOTFOUND) { + *idxp = this->nweight(n->left); + if (value != nullptr) { + copyout(value, n); + } + r = 0; + } + return r; + } +} + +template +template +int omt::find_internal_plus_array(const omtcmp_t &extra, omtdataout_t *value, uint32_t *const idxp) const { + invariant_notnull(idxp); + uint32_t min = this->d.a.start_idx; + uint32_t limit = this->d.a.start_idx + this->d.a.num_values; + uint32_t best = NODE_NULL; + + while (min != limit) { + const uint32_t mid = (min + limit) / 2; + const int hv = h(this->d.a.values[mid], extra); + if (hv > 0) { + best = mid; + limit = mid; + } else { + min = mid + 1; + } + } + if (best == NODE_NULL) { return DB_NOTFOUND; } + if (value != nullptr) { + copyout(value, &this->d.a.values[best]); + } + *idxp = best - this->d.a.start_idx; + return 0; +} + +template +template +int omt::find_internal_plus(const node_idx n_idx, const omtcmp_t &extra, omtdataout_t *value, uint32_t *const idxp) const { + invariant_notnull(idxp); + if (n_idx==NODE_NULL) { + return DB_NOTFOUND; + } + omt_node *const n = &this->d.t.nodes[n_idx]; + int hv = h(n->value, extra); + int r; + if (hv > 0) { + r = this->find_internal_plus(n->left, extra, value, idxp); + if (r == DB_NOTFOUND) { + *idxp = this->nweight(n->left); + if (value != nullptr) { + copyout(value, n); + } + r = 0; + } + } else { + r = this->find_internal_plus(n->right, extra, value, idxp); + if (r == 0) { + *idxp += this->nweight(n->left) + 1; + } + } + return r; +} + +template +template +int omt::find_internal_minus_array(const omtcmp_t &extra, omtdataout_t *value, uint32_t *const idxp) const { + invariant_notnull(idxp); + uint32_t min = this->d.a.start_idx; + uint32_t limit = this->d.a.start_idx + this->d.a.num_values; + uint32_t best = NODE_NULL; + + while (min != limit) { + const uint32_t mid = (min + limit) / 2; + const int hv = h(this->d.a.values[mid], extra); + if (hv < 0) { + best = mid; + min = mid + 1; + } else { + limit = mid; + } + } + if (best == NODE_NULL) { return DB_NOTFOUND; } + if (value != nullptr) { + copyout(value, &this->d.a.values[best]); + } + *idxp = best - this->d.a.start_idx; + return 0; +} + +template +template +int omt::find_internal_minus(const node_idx n_idx, const omtcmp_t &extra, omtdataout_t *value, uint32_t *const idxp) const { + invariant_notnull(idxp); + if (n_idx==NODE_NULL) { + return DB_NOTFOUND; + } + omt_node *const n = &this->d.t.nodes[n_idx]; + int hv = h(n->value, extra); + if (hv < 0) { + int r = this->find_internal_minus(n->right, extra, value, idxp); + if (r == 0) { + *idxp += this->nweight(n->left) + 1; + } else if (r == DB_NOTFOUND) { + *idxp = this->nweight(n->left); + if (value != nullptr) { + copyout(value, n); + } + r = 0; + } + return r; + } else { + return this->find_internal_minus(n->left, extra, value, idxp); + } +} + +template +int omt::deep_clone_iter(const omtdata_t &value, const uint32_t idx, omt *const dest) { +#ifndef __ICC + static_assert(std::is_pointer::value, "omtdata_t isn't a pointer, can't do deep clone"); +#endif + invariant_notnull(dest); + invariant(idx == dest->d.a.num_values); + invariant(idx < dest->capacity); + omtdata_t &destp = dest->d.a.values[dest->d.a.num_values++]; + XMEMDUP(destp, value); + return 0; +} + +template +int omt::free_items_iter(omtdata_t *value, const uint32_t UU(idx), void *const UU(unused)) { +#ifndef __ICC + static_assert(std::is_pointer::value, "omtdata_t isn't a pointer, can't do free items"); +#endif + invariant_notnull(*value); + toku_free(*value); + return 0; +} +template +void omt::free_items(void) { + this->iterate_ptr(nullptr); +} +template +void omt::deep_clone(const omt &src) { + this->create_internal(src.size()); + int r = src.iterate(this); + lazy_assert_zero(r); + this->d.a.num_values = src.size(); +} + +} // namespace toku diff --git a/ft/omt-tmpl.h b/ft/omt-tmpl.h index 416b339c963..f25f1f066c0 100644 --- a/ft/omt-tmpl.h +++ b/ft/omt-tmpl.h @@ -8,12 +8,8 @@ #ident "The technology is licensed by the Massachusetts Institute of Technology, Rutgers State University of New Jersey, and the Research Foundation of State University of New York at Stony Brook under United States of America Serial No. 11/760379 and to the patents and/or patent applications resulting from it." #include -#include -#include #include #include -#include -#include namespace toku { @@ -81,18 +77,14 @@ public: * Effect: Create an empty OMT. * Performance: constant time. */ - void create(void) - { - this->create_internal(2); - } + void create(void); /** - * + * Effect: Create an empty OMT with no internal allocated space. + * Performance: constant time. + * Rationale: In some cases we need a valid omt but don't want to malloc. */ - void create_no_array(void) - { - this->create_internal_no_array(0); - } + void create_no_array(void); /** * Effect: Create a OMT containing values. The number of values is in numvalues. @@ -106,12 +98,7 @@ public: * the structure is empty, we can batch insert them much faster. */ __attribute__((nonnull)) - void create_from_sorted_array(const omtdata_t *const values, const uint32_t numvalues) - { - this->create_internal(numvalues); - memcpy(this->d.a.values, values, numvalues * (sizeof values[0])); - this->d.a.num_values = numvalues; - } + void create_from_sorted_array(const omtdata_t *const values, const uint32_t numvalues); /** * Effect: Create an OMT containing values. The number of values is in numvalues. @@ -128,14 +115,7 @@ public: * and possibly a free (if the caller is done with the array). */ __attribute__((nonnull)) - void create_steal_sorted_array(omtdata_t **const values, const uint32_t numvalues, const uint32_t new_capacity) - { - invariant_notnull(values); - this->create_internal_no_array(new_capacity); - this->d.a.num_values = numvalues; - this->d.a.values = *values; - *values = nullptr; - } + void create_steal_sorted_array(omtdata_t **const values, const uint32_t numvalues, const uint32_t new_capacity); /** * Effect: Create a new OMT, storing it in *newomt. @@ -150,16 +130,7 @@ public: * are even, and other similar splitting criteria. It's easy to split evenly by calling size(), and dividing by two. */ __attribute__((nonnull)) - int split_at(omt *const newomt, const uint32_t idx) { - invariant_notnull(newomt); - if (idx > this->size()) { return EINVAL; } - this->convert_to_array(); - const uint32_t newsize = this->size() - idx; - newomt->create_from_sorted_array(&this->d.a.values[this->d.a.start_idx + idx], newsize); - this->d.a.num_values = idx; - this->maybe_resize_array(); - return 0; - } + int split_at(omt *const newomt, const uint32_t idx); /** * Effect: Appends leftomt and rightomt to produce a new omt. @@ -168,42 +139,7 @@ public: * Performance: time=O(n) is acceptable, but one can imagine implementations that are O(\log n) worst-case. */ __attribute__((nonnull)) - void merge(omt *const leftomt, omt *const rightomt) { - invariant_notnull(leftomt); - invariant_notnull(rightomt); - const uint32_t leftsize = leftomt->size(); - const uint32_t rightsize = rightomt->size(); - const uint32_t newsize = leftsize + rightsize; - - if (leftomt->is_array) { - if (leftomt->capacity - (leftomt->d.a.start_idx + leftomt->d.a.num_values) >= rightsize) { - this->create_steal_sorted_array(leftomt->d.a.values, leftomt->d.a.num_values, leftomt->capacity); - this->d.a.start_idx = leftomt->d.a.start_idx; - } else { - this->create_internal(newsize); - memcpy(&this->d.a.values[0], - &leftomt->d.a.values[leftomt->d.a.start_idx], - leftomt->d.a.num_values * (sizeof this->d.a.values[0])); - } - } else { - this->create_internal(newsize); - leftomt->fill_array_with_subtree_values(&this->d.a.values[0], leftomt->d.t.root); - } - leftomt->destroy(); - this->d.a.num_values = leftsize; - - if (rightomt->is_array) { - memcpy(&this->d.a.values[this->d.a.start_idx + this->d.a.num_values], - &rightomt->d.a.values[rightomt->d.a.start_idx], - rightomt->d.a.num_values * (sizeof this->d.a.values[0])); - } else { - rightomt->fill_array_with_subtree_values(&this->d.a.values[this->d.a.start_idx + this->d.a.num_values], - rightomt->d.t.root); - } - rightomt->destroy(); - this->d.a.num_values += rightsize; - invariant(this->size() == newsize); - } + void merge(omt *const leftomt, omt *const rightomt); /** * Effect: Creates a copy of an omt. @@ -211,46 +147,14 @@ public: * Each element is copied directly. If they are pointers, the underlying data is not duplicated. * Performance: O(n) or the running time of fill_array_with_subtree_values() */ - void clone(const omt &src) - { - this->create_internal(src.size()); - if (src.is_array) { - memcpy(&this->d.a.values[0], &src.d.a.values[src.d.a.start_idx], src.d.a.num_values * (sizeof this->d.a.values[0])); - } else { - src.fill_array_with_subtree_values(&this->d.a.values[0], src.d.t.root); - } - this->d.a.num_values = src.size(); - } - - /** - * Effect: Creates a copy of an omt. - * Creates this as the clone. - * Each element is assumed to be a pointer, and the underlying data is duplicated for the clone using toku_malloc. - * Performance: the running time of iterate() - */ - void deep_clone(const omt &src) - { - this->create_internal(src.size()); - int r = src.iterate(this); - lazy_assert_zero(r); - this->d.a.num_values = src.size(); - } + void clone(const omt &src); /** * Effect: Set the tree to be empty. * Note: Will not reallocate or resize any memory. * Performance: time=O(1) */ - void clear(void) - { - if (this->is_array) { - this->d.a.start_idx = 0; - this->d.a.num_values = 0; - } else { - this->d.t.root = NODE_NULL; - this->d.t.free_idx = 0; - } - } + void clear(void); /** * Effect: Destroy an OMT, freeing all its memory. @@ -259,37 +163,15 @@ public: * Rationale: Returns no values since free() cannot fail. * Rationale: Does not free the underlying pointers to reduce complexity. * Performance: time=O(1) - * */ - void destroy(void) - { - this->clear(); - this->capacity = 0; - if (this->is_array) { - if (this->d.a.values != nullptr) { - toku_free(this->d.a.values); - } - this->d.a.values = nullptr; - } else { - if (this->d.t.nodes != nullptr) { - toku_free(this->d.t.nodes); - } - this->d.t.nodes = nullptr; - } - } + void destroy(void); /** * Effect: return |this|. * Performance: time=O(1) */ - inline uint32_t size(void) const - { - if (this->is_array) { - return this->d.a.num_values; - } else { - return this->nweight(this->d.t.root); - } - } + uint32_t size(void) const; + /** * Effect: Insert value into the OMT. @@ -307,25 +189,8 @@ public: * Performance: time=O(\log N) amortized. * Rationale: Some future implementation may be O(\log N) worst-case time, but O(\log N) amortized is good enough for now. */ - template - int insert(const omtdata_t &value, const omtcmp_t &v, uint32_t *const idx) - { - int r; - uint32_t insert_idx; - - r = this->find_zero(v, nullptr, &insert_idx); - if (r==0) { - if (idx) *idx = insert_idx; - return DB_KEYEXIST; - } - if (r != DB_NOTFOUND) return r; - - if ((r = this->insert_at(value, insert_idx))) return r; - if (idx) *idx = insert_idx; - - return 0; - } + template + int insert(const omtdata_t &value, const omtcmp_t &v, uint32_t *const idx); /** * Effect: Increases indexes of all items at slot >= idx by 1. @@ -337,32 +202,7 @@ public: * Performance: time=O(\log N) amortized time. * Rationale: Some future implementation may be O(\log N) worst-case time, but O(\log N) amortized is good enough for now. */ - int insert_at(const omtdata_t &value, const uint32_t idx) - { - if (idx > this->size()) { return EINVAL; } - this->maybe_resize_or_convert(this->size() + 1); - if (this->is_array && idx != this->d.a.num_values && - (idx != 0 || this->d.a.start_idx == 0)) { - this->convert_to_tree(); - } - if (this->is_array) { - if (idx == this->d.a.num_values) { - this->d.a.values[this->d.a.start_idx + this->d.a.num_values] = value; - } - else { - this->d.a.values[--this->d.a.start_idx] = value; - } - this->d.a.num_values++; - } - else { - node_idx *rebalance_idx = nullptr; - this->insert_internal(&this->d.t.root, value, idx, &rebalance_idx); - if (rebalance_idx != nullptr) { - this->rebalance(rebalance_idx); - } - } - return 0; - } + int insert_at(const omtdata_t &value, const uint32_t idx); /** * Effect: Replaces the item at idx with value. @@ -374,16 +214,7 @@ public: * Rationale: The FT needs to be able to replace a value with another copy of the same value (allocated in a different location) * */ - int set_at(const omtdata_t &value, const uint32_t idx) - { - if (idx >= this->size()) { return EINVAL; } - if (this->is_array) { - this->set_at_internal_array(value, idx); - } else { - this->set_at_internal(this->d.t.root, value, idx); - } - return 0; - } + int set_at(const omtdata_t &value, const uint32_t idx); /** * Effect: Delete the item in slot idx. @@ -395,29 +226,7 @@ public: * Rationale: To delete an item, first find its index using find or find_zero, then delete it. * Performance: time=O(\log N) amortized. */ - int delete_at(const uint32_t idx) - { - if (idx >= this->size()) { return EINVAL; } - this->maybe_resize_or_convert(this->size() - 1); - if (this->is_array && idx != 0 && idx != this->d.a.num_values - 1) { - this->convert_to_tree(); - } - if (this->is_array) { - //Testing for 0 does not rule out it being the last entry. - //Test explicitly for num_values-1 - if (idx != this->d.a.num_values - 1) { - this->d.a.start_idx++; - } - this->d.a.num_values--; - } else { - node_idx *rebalance_idx = nullptr; - this->delete_internal(&this->d.t.root, idx, nullptr, &rebalance_idx); - if (rebalance_idx != nullptr) { - this->rebalance(rebalance_idx); - } - } - return 0; - } + int delete_at(const uint32_t idx); /** * Effect: Iterate over the values of the omt, from left to right, calling f on each value. @@ -436,9 +245,7 @@ public: */ template - int iterate(iterate_extra_t *const iterate_extra) const { - return this->iterate_on_range(0, this->size(), iterate_extra); - } + int iterate(iterate_extra_t *const iterate_extra) const; /** * Effect: Iterate over the values of the omt, from left to right, calling f on each value. @@ -460,13 +267,7 @@ public: */ template - int iterate_on_range(const uint32_t left, const uint32_t right, iterate_extra_t *const iterate_extra) const { - if (right > this->size()) { return EINVAL; } - if (this->is_array) { - return this->iterate_internal_array(left, right, iterate_extra); - } - return this->iterate_internal(left, right, this->d.t.root, 0, iterate_extra); - } + int iterate_on_range(const uint32_t left, const uint32_t right, iterate_extra_t *const iterate_extra) const; /** * Effect: Iterate over the values of the omt, from left to right, calling f on each value. @@ -482,24 +283,7 @@ public: */ template - void iterate_ptr(iterate_extra_t *const iterate_extra) { - if (this->is_array) { - this->iterate_ptr_internal_array(0, this->size(), iterate_extra); - } else { - this->iterate_ptr_internal(0, this->size(), this->d.t.root, 0, iterate_extra); - } - } - - /** - * Effect: Iterate over the values of the omt, from left to right, freeing each value with toku_free - * Requires: all items in OMT to have been malloced with toku_malloc - * Rational: This function was added due to a problem encountered in ft-ops.c. We needed to free the elements and then - * destroy the OMT. However, destroying the OMT requires invalidating cursors. This cannot be done if the values of the OMT - * have been already freed. So, this function is written to invalidate cursors and free items. - */ - void free_items(void) { - this->iterate_ptr(nullptr); - } + void iterate_ptr(iterate_extra_t *const iterate_extra); /** * Effect: Set *value=V_idx @@ -509,16 +293,8 @@ public: * On nonzero return, *value is unchanged * Performance: time=O(\log N) */ - int fetch(const uint32_t idx, omtdataout_t *const value) const - { - if (idx >= this->size()) { return EINVAL; } - if (this->is_array) { - this->fetch_internal_array(idx, value); - } else { - this->fetch_internal(this->d.t.root, idx, value); - } - return 0; - } + int fetch(const uint32_t idx, omtdataout_t *const value) const; + /** * Effect: Find the smallest i such that h(V_i, extra)>=0 @@ -540,23 +316,8 @@ public: */ template - int find_zero(const omtcmp_t &extra, omtdataout_t *const value, uint32_t *const idxp) const - { - uint32_t tmp_index; - uint32_t *const child_idxp = (idxp != nullptr) ? idxp : &tmp_index; - int r; - if (this->is_array) { - r = this->find_internal_zero_array(extra, value, child_idxp); - } - else { - r = this->find_internal_zero(this->d.t.root, extra, value, child_idxp); - } - return r; - } + int find_zero(const omtcmp_t &extra, omtdataout_t *const value, uint32_t *const idxp) const; - template - int find(const omtcmp_t &extra, int direction, omtdataout_t *const value, uint32_t *const idxp) const /** * Effect: * If direction >0 then find the smallest i such that h(V_i,extra)>0. @@ -617,34 +378,14 @@ public: * -...-0...0+...+ * AC B */ - { - uint32_t tmp_index; - uint32_t *const child_idxp = (idxp != nullptr) ? idxp : &tmp_index; - invariant(direction != 0); - if (direction < 0) { - if (this->is_array) { - return this->find_internal_minus_array(extra, value, child_idxp); - } else { - return this->find_internal_minus(this->d.t.root, extra, value, child_idxp); - } - } else { - if (this->is_array) { - return this->find_internal_plus_array(extra, value, child_idxp); - } else { - return this->find_internal_plus(this->d.t.root, extra, value, child_idxp); - } - } - } + template + int find(const omtcmp_t &extra, int direction, omtdataout_t *const value, uint32_t *const idxp) const; /** * Effect: Return the size (in bytes) of the omt, as it resides in main memory. If the data stored are pointers, don't include the size of what they all point to. */ - size_t memory_size(void) { - if (this->is_array) { - return (sizeof *this) + this->capacity * (sizeof this->d.a.values[0]); - } - return (sizeof *this) + this->capacity * (sizeof this->d.t.nodes[0]); - } + size_t memory_size(void); private: typedef uint32_t node_idx; @@ -679,591 +420,139 @@ private: } d; - void create_internal_no_array(const uint32_t new_capacity) { - this->is_array = true; - this->capacity = new_capacity; - this->d.a.start_idx = 0; - this->d.a.num_values = 0; - this->d.a.values = nullptr; - } + void create_internal_no_array(const uint32_t new_capacity); - void create_internal(const uint32_t new_capacity) { - this->create_internal_no_array(new_capacity); - XMALLOC_N(this->capacity, this->d.a.values); - } + void create_internal(const uint32_t new_capacity); - inline uint32_t nweight(const node_idx idx) const { - if (idx == NODE_NULL) { - return 0; - } else { - return this->d.t.nodes[idx].weight; - } - } + uint32_t nweight(const node_idx idx) const; - inline node_idx node_malloc(void) { - invariant(this->d.t.free_idx < this->capacity); - return this->d.t.free_idx++; - } + node_idx node_malloc(void); - inline void node_free(const node_idx idx) { - invariant(idx < this->capacity); - } + void node_free(const node_idx idx); - inline void maybe_resize_array(const uint32_t n) { - const uint32_t new_size = n<=2 ? 4 : 2*n; - const uint32_t room = this->capacity - this->d.a.start_idx; - - if (room < n || this->capacity / 2 >= new_size) { - omtdata_t *XMALLOC_N(new_size, tmp_values); - memcpy(tmp_values, &this->d.a.values[this->d.a.start_idx], - this->d.a.num_values * (sizeof tmp_values[0])); - this->d.a.start_idx = 0; - this->capacity = new_size; - toku_free(this->d.a.values); - this->d.a.values = tmp_values; - } - } + void maybe_resize_array(const uint32_t n); __attribute__((nonnull)) - inline void fill_array_with_subtree_values(omtdata_t *const array, const node_idx tree_idx) const { - if (tree_idx==NODE_NULL) return; - const omt_node &tree = this->d.t.nodes[tree_idx]; - this->fill_array_with_subtree_values(&array[0], tree.left); - array[this->nweight(tree.left)] = tree.value; - this->fill_array_with_subtree_values(&array[this->nweight(tree.left) + 1], tree.right); - } + void fill_array_with_subtree_values(omtdata_t *const array, const node_idx tree_idx) const; - inline void convert_to_array(void) { - if (!this->is_array) { - const uint32_t num_values = this->size(); - uint32_t new_size = 2*num_values; - new_size = new_size < 4 ? 4 : new_size; - - omtdata_t *XMALLOC_N(new_size, tmp_values); - this->fill_array_with_subtree_values(tmp_values, this->d.t.root); - toku_free(this->d.t.nodes); - this->is_array = true; - this->capacity = new_size; - this->d.a.num_values = num_values; - this->d.a.values = tmp_values; - this->d.a.start_idx = 0; - } - } + void convert_to_array(void); __attribute__((nonnull)) - inline void rebuild_from_sorted_array(node_idx *const n_idxp, const omtdata_t *const values, const uint32_t numvalues) { - if (numvalues==0) { - *n_idxp = NODE_NULL; - } else { - const uint32_t halfway = numvalues/2; - const node_idx newidx = this->node_malloc(); - omt_node *const newnode = &this->d.t.nodes[newidx]; - newnode->weight = numvalues; - newnode->value = values[halfway]; - *n_idxp = newidx; // update everything before the recursive calls so the second call can be a tail call. - this->rebuild_from_sorted_array(&newnode->left, &values[0], halfway); - this->rebuild_from_sorted_array(&newnode->right, &values[halfway+1], numvalues - (halfway+1)); - } - } + void rebuild_from_sorted_array(node_idx *const n_idxp, const omtdata_t *const values, const uint32_t numvalues); - inline void convert_to_tree(void) { - if (this->is_array) { - const uint32_t num_nodes = this->size(); - uint32_t new_size = num_nodes*2; - new_size = new_size < 4 ? 4 : new_size; + void convert_to_tree(void); - omt_node *XMALLOC_N(new_size, new_nodes); - omtdata_t *const values = this->d.a.values; - omtdata_t *const tmp_values = &values[this->d.a.start_idx]; - this->is_array = false; - this->d.t.nodes = new_nodes; - this->capacity = new_size; - this->d.t.free_idx = 0; - this->d.t.root = NODE_NULL; - this->rebuild_from_sorted_array(&this->d.t.root, tmp_values, num_nodes); - toku_free(values); - } - } + void maybe_resize_or_convert(const uint32_t n); - inline void maybe_resize_or_convert(const uint32_t n) { - if (this->is_array) { - this->maybe_resize_array(n); - } else { - const uint32_t new_size = n<=2 ? 4 : 2*n; - const uint32_t num_nodes = this->nweight(this->d.t.root); - if ((this->capacity/2 >= new_size) || - (this->d.t.free_idx >= this->capacity && num_nodes < n) || - (this->capacityconvert_to_array(); - } - } - } - - inline bool will_need_rebalance(const node_idx n_idx, const int leftmod, const int rightmod) const { - if (n_idx==NODE_NULL) { return false; } - const omt_node &n = this->d.t.nodes[n_idx]; - // one of the 1's is for the root. - // the other is to take ceil(n/2) - const uint32_t weight_left = this->nweight(n.left) + leftmod; - const uint32_t weight_right = this->nweight(n.right) + rightmod; - return ((1+weight_left < (1+1+weight_right)/2) - || - (1+weight_right < (1+1+weight_left)/2)); - } + bool will_need_rebalance(const node_idx n_idx, const int leftmod, const int rightmod) const; __attribute__((nonnull)) - inline void insert_internal(node_idx *const n_idxp, const omtdata_t &value, const uint32_t idx, node_idx **const rebalance_idx) { - if (*n_idxp == NODE_NULL) { - invariant_zero(idx); - const node_idx newidx = this->node_malloc(); - omt_node *const newnode = &this->d.t.nodes[newidx]; - newnode->weight = 1; - newnode->left = NODE_NULL; - newnode->right = NODE_NULL; - newnode->value = value; - *n_idxp = newidx; - } else { - const node_idx thisidx = *n_idxp; - omt_node *const n = &this->d.t.nodes[thisidx]; - n->weight++; - if (idx <= this->nweight(n->left)) { - if (*rebalance_idx == nullptr && this->will_need_rebalance(thisidx, 1, 0)) { - *rebalance_idx = n_idxp; - } - this->insert_internal(&n->left, value, idx, rebalance_idx); - } else { - if (*rebalance_idx == nullptr && this->will_need_rebalance(thisidx, 0, 1)) { - *rebalance_idx = n_idxp; - } - const uint32_t sub_index = idx - this->nweight(n->left) - 1; - this->insert_internal(&n->right, value, sub_index, rebalance_idx); - } - } - } + void insert_internal(node_idx *const n_idxp, const omtdata_t &value, const uint32_t idx, node_idx **const rebalance_idx); - inline void set_at_internal_array(const omtdata_t &value, const uint32_t idx) { - this->d.a.values[this->d.a.start_idx + idx] = value; - } + void set_at_internal_array(const omtdata_t &value, const uint32_t idx); - inline void set_at_internal(const node_idx n_idx, const omtdata_t &value, const uint32_t idx) { - invariant(n_idx != NODE_NULL); - omt_node *const n = &this->d.t.nodes[n_idx]; - const uint32_t leftweight = this->nweight(n->left); - if (idx < leftweight) { - this->set_at_internal(n->left, value, idx); - } else if (idx == leftweight) { - n->value = value; - } else { - this->set_at_internal(n->right, value, idx - leftweight - 1); - } - } + void set_at_internal(const node_idx n_idx, const omtdata_t &value, const uint32_t idx); - inline void delete_internal(node_idx *const n_idxp, const uint32_t idx, omt_node *const copyn, node_idx **const rebalance_idx) { - invariant_notnull(n_idxp); - invariant_notnull(rebalance_idx); - invariant(*n_idxp != NODE_NULL); - omt_node *const n = &this->d.t.nodes[*n_idxp]; - const uint32_t leftweight = this->nweight(n->left); - if (idx < leftweight) { - n->weight--; - if (*rebalance_idx == nullptr && this->will_need_rebalance(*n_idxp, -1, 0)) { - *rebalance_idx = n_idxp; - } - this->delete_internal(&n->left, idx, copyn, rebalance_idx); - } else if (idx == leftweight) { - if (n->left == NODE_NULL) { - const uint32_t oldidx = *n_idxp; - *n_idxp = n->right; - if (copyn != nullptr) { - copyn->value = n->value; - } - this->node_free(oldidx); - } else if (n->right == NODE_NULL) { - const uint32_t oldidx = *n_idxp; - *n_idxp = n->left; - if (copyn != nullptr) { - copyn->value = n->value; - } - this->node_free(oldidx); - } else { - if (*rebalance_idx == nullptr && this->will_need_rebalance(*n_idxp, 0, -1)) { - *rebalance_idx = n_idxp; - } - // don't need to copy up value, it's only used by this - // next call, and when that gets to the bottom there - // won't be any more recursion - n->weight--; - this->delete_internal(&n->right, 0, n, rebalance_idx); - } - } else { - n->weight--; - if (*rebalance_idx == nullptr && this->will_need_rebalance(*n_idxp, 0, -1)) { - *rebalance_idx = n_idxp; - } - this->delete_internal(&n->right, idx - leftweight - 1, copyn, rebalance_idx); - } - } + void delete_internal(node_idx *const n_idxp, const uint32_t idx, omt_node *const copyn, node_idx **const rebalance_idx); template - inline int iterate_internal_array(const uint32_t left, const uint32_t right, - iterate_extra_t *const iterate_extra) const { - int r; - for (uint32_t i = left; i < right; ++i) { - r = f(this->d.a.values[this->d.a.start_idx + i], i, iterate_extra); - if (r != 0) { - return r; - } - } - return 0; - } + int iterate_internal_array(const uint32_t left, const uint32_t right, + iterate_extra_t *const iterate_extra) const; template - inline void iterate_ptr_internal(const uint32_t left, const uint32_t right, + void iterate_ptr_internal(const uint32_t left, const uint32_t right, const node_idx n_idx, const uint32_t idx, - iterate_extra_t *const iterate_extra) { - if (n_idx != NODE_NULL) { - omt_node *const n = this->d.t.nodes[n_idx]; - const uint32_t idx_root = idx + this->nweight(n->left); - if (left < idx_root) { - this->iterate_ptr_internal(left, right, n->left, idx, iterate_extra); - } - if (left <= idx_root && idx_root < right) { - int r = f(&n->value, idx_root, iterate_extra); - lazy_assert_zero(r); - } - if (idx_root + 1 < right) { - this->iterate_ptr_internal(left, right, n->right, idx_root + 1, iterate_extra); - } - } - } + iterate_extra_t *const iterate_extra); template - inline void iterate_ptr_internal_array(const uint32_t left, const uint32_t right, - iterate_extra_t *const iterate_extra) { - for (uint32_t i = left; i < right; ++i) { - int r = f(&this->d.a.values[this->d.a.start_idx + i], i, iterate_extra); - lazy_assert_zero(r); - } - } + void iterate_ptr_internal_array(const uint32_t left, const uint32_t right, + iterate_extra_t *const iterate_extra); template - inline int iterate_internal(const uint32_t left, const uint32_t right, + int iterate_internal(const uint32_t left, const uint32_t right, const node_idx n_idx, const uint32_t idx, - iterate_extra_t *const iterate_extra) const { - if (n_idx == NODE_NULL) { return 0; } - int r; - const omt_node &n = this->d.t.nodes[n_idx]; - const uint32_t idx_root = idx + this->nweight(n.left); - if (left < idx_root) { - r = this->iterate_internal(left, right, n.left, idx, iterate_extra); - if (r != 0) { return r; } - } - if (left <= idx_root && idx_root < right) { - r = f(n.value, idx_root, iterate_extra); - if (r != 0) { return r; } - } - if (idx_root + 1 < right) { - return this->iterate_internal(left, right, n.right, idx_root + 1, iterate_extra); - } - return 0; - } + iterate_extra_t *const iterate_extra) const; - inline void fetch_internal_array(const uint32_t i, omtdataout_t *value) const { - if (value != nullptr) { - copyout(value, &this->d.a.values[this->d.a.start_idx + i]); - } - } + void fetch_internal_array(const uint32_t i, omtdataout_t *value) const; - inline void fetch_internal(const node_idx idx, const uint32_t i, omtdataout_t *value) const { - omt_node *const n = &this->d.t.nodes[idx]; - const uint32_t leftweight = this->nweight(n->left); - if (i < leftweight) { - this->fetch_internal(n->left, i, value); - } else if (i == leftweight) { - if (value != nullptr) { - copyout(value, n); - } - } else { - this->fetch_internal(n->right, i - leftweight - 1, value); - } - } + void fetch_internal(const node_idx idx, const uint32_t i, omtdataout_t *value) const; __attribute__((nonnull)) - inline void fill_array_with_subtree_idxs(node_idx *const array, const node_idx tree_idx) const { - if (tree_idx != NODE_NULL) { - const omt_node &tree = this->d.t.nodes[tree_idx]; - this->fill_array_with_subtree_idxs(&array[0], tree.left); - array[this->nweight(tree.left)] = tree_idx; - this->fill_array_with_subtree_idxs(&array[this->nweight(tree.left) + 1], tree.right); - } - } + void fill_array_with_subtree_idxs(node_idx *const array, const node_idx tree_idx) const; __attribute__((nonnull)) - inline void rebuild_subtree_from_idxs(node_idx *const n_idxp, const node_idx *const idxs, const uint32_t numvalues) { - if (numvalues==0) { - *n_idxp = NODE_NULL; - } else { - uint32_t halfway = numvalues/2; - *n_idxp = idxs[halfway]; - //node_idx newidx = idxs[halfway]; - omt_node *const newnode = &this->d.t.nodes[*n_idxp]; - newnode->weight = numvalues; - // value is already in there. - this->rebuild_subtree_from_idxs(&newnode->left, &idxs[0], halfway); - this->rebuild_subtree_from_idxs(&newnode->right, &idxs[halfway+1], numvalues-(halfway+1)); - //n_idx = newidx; - } - } + void rebuild_subtree_from_idxs(node_idx *const n_idxp, const node_idx *const idxs, const uint32_t numvalues); __attribute__((nonnull)) - inline void rebalance(node_idx *const n_idxp) { - node_idx idx = *n_idxp; - if (idx==this->d.t.root) { - //Try to convert to an array. - //If this fails, (malloc) nothing will have changed. - //In the failure case we continue on to the standard rebalance - //algorithm. - this->convert_to_array(); - } else { - const omt_node &n = this->d.t.nodes[idx]; - node_idx *tmp_array; - size_t mem_needed = n.weight * (sizeof tmp_array[0]); - size_t mem_free = (this->capacity - this->d.t.free_idx) * (sizeof this->d.t.nodes[0]); - bool malloced; - if (mem_needed<=mem_free) { - //There is sufficient free space at the end of the nodes array - //to hold enough node indexes to rebalance. - malloced = false; - tmp_array = reinterpret_cast(&this->d.t.nodes[this->d.t.free_idx]); - } - else { - malloced = true; - XMALLOC_N(n.weight, tmp_array); - } - this->fill_array_with_subtree_idxs(tmp_array, idx); - this->rebuild_subtree_from_idxs(n_idxp, tmp_array, n.weight); - if (malloced) toku_free(tmp_array); - } - } + void rebalance(node_idx *const n_idxp); __attribute__((nonnull)) - static inline void copyout(omtdata_t *const out, const omt_node *const n) { - *out = n->value; - } + static void copyout(omtdata_t *const out, const omt_node *const n); __attribute__((nonnull)) - static inline void copyout(omtdata_t **const out, omt_node *const n) { - *out = &n->value; - } + static void copyout(omtdata_t **const out, omt_node *const n); __attribute__((nonnull)) - static inline void copyout(omtdata_t *const out, const omtdata_t *const stored_value_ptr) { - *out = *stored_value_ptr; - } + static void copyout(omtdata_t *const out, const omtdata_t *const stored_value_ptr); __attribute__((nonnull)) - static inline void copyout(omtdata_t **const out, omtdata_t *const stored_value_ptr) { - *out = stored_value_ptr; - } + static void copyout(omtdata_t **const out, omtdata_t *const stored_value_ptr); template - inline int find_internal_zero_array(const omtcmp_t &extra, omtdataout_t *value, uint32_t *const idxp) const { - invariant_notnull(idxp); - uint32_t min = this->d.a.start_idx; - uint32_t limit = this->d.a.start_idx + this->d.a.num_values; - uint32_t best_pos = NODE_NULL; - uint32_t best_zero = NODE_NULL; - - while (min!=limit) { - uint32_t mid = (min + limit) / 2; - int hv = h(this->d.a.values[mid], extra); - if (hv<0) { - min = mid+1; - } - else if (hv>0) { - best_pos = mid; - limit = mid; - } - else { - best_zero = mid; - limit = mid; - } - } - if (best_zero!=NODE_NULL) { - //Found a zero - if (value != nullptr) { - copyout(value, &this->d.a.values[best_zero]); - } - *idxp = best_zero - this->d.a.start_idx; - return 0; - } - if (best_pos!=NODE_NULL) *idxp = best_pos - this->d.a.start_idx; - else *idxp = this->d.a.num_values; - return DB_NOTFOUND; - } + int find_internal_zero_array(const omtcmp_t &extra, omtdataout_t *value, uint32_t *const idxp) const; template - inline int find_internal_zero(const node_idx n_idx, const omtcmp_t &extra, omtdataout_t *value, uint32_t *const idxp) const { - invariant_notnull(idxp); - if (n_idx==NODE_NULL) { - *idxp = 0; - return DB_NOTFOUND; - } - omt_node *const n = &this->d.t.nodes[n_idx]; - int hv = h(n->value, extra); - if (hv<0) { - int r = this->find_internal_zero(n->right, extra, value, idxp); - *idxp += this->nweight(n->left)+1; - return r; - } else if (hv>0) { - return this->find_internal_zero(n->left, extra, value, idxp); - } else { - int r = this->find_internal_zero(n->left, extra, value, idxp); - if (r==DB_NOTFOUND) { - *idxp = this->nweight(n->left); - if (value != nullptr) { - copyout(value, n); - } - r = 0; - } - return r; - } - } + int find_internal_zero(const node_idx n_idx, const omtcmp_t &extra, omtdataout_t *value, uint32_t *const idxp) const; template - inline int find_internal_plus_array(const omtcmp_t &extra, omtdataout_t *value, uint32_t *const idxp) const { - invariant_notnull(idxp); - uint32_t min = this->d.a.start_idx; - uint32_t limit = this->d.a.start_idx + this->d.a.num_values; - uint32_t best = NODE_NULL; - - while (min != limit) { - const uint32_t mid = (min + limit) / 2; - const int hv = h(this->d.a.values[mid], extra); - if (hv > 0) { - best = mid; - limit = mid; - } else { - min = mid + 1; - } - } - if (best == NODE_NULL) { return DB_NOTFOUND; } - if (value != nullptr) { - copyout(value, &this->d.a.values[best]); - } - *idxp = best - this->d.a.start_idx; - return 0; - } + int find_internal_plus_array(const omtcmp_t &extra, omtdataout_t *value, uint32_t *const idxp) const; template - inline int find_internal_plus(const node_idx n_idx, const omtcmp_t &extra, omtdataout_t *value, uint32_t *const idxp) const { - invariant_notnull(idxp); - if (n_idx==NODE_NULL) { - return DB_NOTFOUND; - } - omt_node *const n = &this->d.t.nodes[n_idx]; - int hv = h(n->value, extra); - int r; - if (hv > 0) { - r = this->find_internal_plus(n->left, extra, value, idxp); - if (r == DB_NOTFOUND) { - *idxp = this->nweight(n->left); - if (value != nullptr) { - copyout(value, n); - } - r = 0; - } - } else { - r = this->find_internal_plus(n->right, extra, value, idxp); - if (r == 0) { - *idxp += this->nweight(n->left) + 1; - } - } - return r; - } + int find_internal_plus(const node_idx n_idx, const omtcmp_t &extra, omtdataout_t *value, uint32_t *const idxp) const; template - inline int find_internal_minus_array(const omtcmp_t &extra, omtdataout_t *value, uint32_t *const idxp) const { - invariant_notnull(idxp); - uint32_t min = this->d.a.start_idx; - uint32_t limit = this->d.a.start_idx + this->d.a.num_values; - uint32_t best = NODE_NULL; - - while (min != limit) { - const uint32_t mid = (min + limit) / 2; - const int hv = h(this->d.a.values[mid], extra); - if (hv < 0) { - best = mid; - min = mid + 1; - } else { - limit = mid; - } - } - if (best == NODE_NULL) { return DB_NOTFOUND; } - if (value != nullptr) { - copyout(value, &this->d.a.values[best]); - } - *idxp = best - this->d.a.start_idx; - return 0; - } + int find_internal_minus_array(const omtcmp_t &extra, omtdataout_t *value, uint32_t *const idxp) const; template - inline int find_internal_minus(const node_idx n_idx, const omtcmp_t &extra, omtdataout_t *value, uint32_t *const idxp) const { - invariant_notnull(idxp); - if (n_idx==NODE_NULL) { - return DB_NOTFOUND; - } - omt_node *const n = &this->d.t.nodes[n_idx]; - int hv = h(n->value, extra); - if (hv < 0) { - int r = this->find_internal_minus(n->right, extra, value, idxp); - if (r == 0) { - *idxp += this->nweight(n->left) + 1; - } else if (r == DB_NOTFOUND) { - *idxp = this->nweight(n->left); - if (value != nullptr) { - copyout(value, n); - } - r = 0; - } - return r; - } else { - return this->find_internal_minus(n->left, extra, value, idxp); - } - } + int find_internal_minus(const node_idx n_idx, const omtcmp_t &extra, omtdataout_t *value, uint32_t *const idxp) const; __attribute__((nonnull)) - static inline int deep_clone_iter(const omtdata_t &value, const uint32_t idx, omt *const dest) { -#ifndef __ICC - static_assert(std::is_pointer::value, "omtdata_t isn't a pointer, can't do deep clone"); -#endif - invariant_notnull(dest); - invariant(idx == dest->d.a.num_values); - invariant(idx < dest->capacity); - XMEMDUP(dest->d.a.values[dest->d.a.num_values++], value); - return 0; - } + static int deep_clone_iter(const omtdata_t &value, const uint32_t idx, omt *const dest); + + static int free_items_iter(omtdata_t *value, const uint32_t UU(idx), void *const UU(unused)); +public: + /** + * Effect: Iterate over the values of the omt, from left to right, freeing each value with toku_free + * Requires: all items in OMT to have been malloced with toku_malloc + * Rational: This function was added due to a problem encountered in ft-ops.c. We needed to free the elements and then + * destroy the OMT. However, destroying the OMT requires invalidating cursors. This cannot be done if the values of the OMT + * have been already freed. So, this function is written to invalidate cursors and free items. + */ + void free_items(void); + + /** + * Effect: Creates a copy of an omt. + * Creates this as the clone. + * Each element is assumed to be a pointer, and the underlying data is duplicated for the clone using toku_malloc. + * Performance: the running time of iterate() + */ + void deep_clone(const omt &src); - static inline int free_items_iter(omtdata_t *value, const uint32_t UU(idx), void *const UU(unused)) { -#ifndef __ICC - static_assert(std::is_pointer::value, "omtdata_t isn't a pointer, can't do free items"); -#endif - invariant_notnull(*value); - toku_free(*value); - return 0; - } }; -}; +} // namespace toku + +// include the implementation here +#include "omt-tmpl.cc" #endif /* #ifndef OMT_TMPL_H */