|
| 1 | +/* Masstree |
| 2 | + * Eddie Kohler, Yandong Mao, Robert Morris |
| 3 | + * Copyright (c) 2012-2014 President and Fellows of Harvard College |
| 4 | + * Copyright (c) 2012-2014 Massachusetts Institute of Technology |
| 5 | + * |
| 6 | + * Permission is hereby granted, free of charge, to any person obtaining a |
| 7 | + * copy of this software and associated documentation files (the "Software"), |
| 8 | + * to deal in the Software without restriction, subject to the conditions |
| 9 | + * listed in the Masstree LICENSE file. These conditions include: you must |
| 10 | + * preserve this copyright notice, and you cannot mention the copyright |
| 11 | + * holders in advertising related to the Software without their permission. |
| 12 | + * The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This |
| 13 | + * notice is a summary of the Masstree LICENSE file; the license in that file |
| 14 | + * is legally binding. |
| 15 | + */ |
| 16 | +#ifndef MASSTREE_ITERATOR_HH |
| 17 | +#define MASSTREE_ITERATOR_HH |
| 18 | +#include "masstree_scan.hh" |
| 19 | + |
| 20 | +namespace Masstree { |
| 21 | +template <typename P> |
| 22 | +class basic_table<P>::iterator |
| 23 | + : std::iterator<std::forward_iterator_tag, itvalue_type> { |
| 24 | + typedef typename P::ikey_type ikey_type; |
| 25 | + typedef typename node_type::key_type key_type; |
| 26 | + typedef typename node_type::leaf_type::leafvalue_type leafvalue_type; |
| 27 | + typedef typename node_type::nodeversion_type nodeversion_type; |
| 28 | + typedef typename leaf_type::permuter_type permuter_type; |
| 29 | + typedef typename leaf_type::bound_type bound_type; |
| 30 | + |
| 31 | + public: |
| 32 | + iterator(basic_table<P>* table, threadinfo* ti, Str firstkey = ""); |
| 33 | + static iterator make_end(basic_table<P>* table, threadinfo *ti); |
| 34 | + |
| 35 | + itvalue_type& operator*() { assert(!end_); return pair_; }; |
| 36 | + itvalue_type* operator->() { assert(!end_); return &pair_; }; |
| 37 | + bool operator==(const iterator& rhs) { return (end_ == rhs.end_) && (end_ || ka_.compare(rhs.ka_) == 0); }; |
| 38 | + bool operator!=(const iterator& rhs) { return !(*this == rhs); }; |
| 39 | + iterator operator++() { advance(); return *this; }; |
| 40 | + iterator operator++(int) { iterator it = *this; advance(); return it; }; |
| 41 | + |
| 42 | + private: |
| 43 | + basic_table<P>* table_; |
| 44 | + threadinfo* ti_; |
| 45 | + key_type ka_; |
| 46 | + value_type value_; |
| 47 | + itvalue_type pair_; |
| 48 | + bool end_; |
| 49 | + union { |
| 50 | + ikey_type x[(MASSTREE_MAXKEYLEN + sizeof(ikey_type) - 1)/sizeof(ikey_type)]; |
| 51 | + char s[MASSTREE_MAXKEYLEN]; |
| 52 | + } keybuf_; |
| 53 | + |
| 54 | + void advance(bool emit_equal = false); |
| 55 | + |
| 56 | + |
| 57 | + // Debugging support. |
| 58 | + int id_; |
| 59 | + static int count_; |
| 60 | + |
| 61 | + void dprintf(const char *format, ...) { |
| 62 | + va_list args; |
| 63 | + va_start(args, format); |
| 64 | + fprintf(stderr, "it%d: ", id_); |
| 65 | + vfprintf(stderr, format, args); |
| 66 | + va_end(args); |
| 67 | + } |
| 68 | +}; |
| 69 | + |
| 70 | +template <typename P> int basic_table<P>::iterator::count_ = 0; |
| 71 | + |
| 72 | +template <typename P> |
| 73 | +basic_table<P>::iterator::iterator(basic_table<P>* table, threadinfo* ti, Str firstkey) |
| 74 | + : table_(table), ti_(ti), end_(false), id_(count_++) { |
| 75 | + masstree_precondition(firstkey.len <= (int) sizeof(keybuf_)); |
| 76 | + memcpy(keybuf_.s, firstkey.s, firstkey.len); |
| 77 | + ka_ = key_type(keybuf_.s, firstkey.len); |
| 78 | + |
| 79 | + advance(true); |
| 80 | +}; |
| 81 | + |
| 82 | +template <typename P> |
| 83 | +typename basic_table<P>::iterator |
| 84 | +basic_table<P>::iterator::make_end(basic_table<P>* table, threadinfo *ti) { |
| 85 | + iterator it = iterator(table, ti); |
| 86 | + it.end_ = true; |
| 87 | + return it; |
| 88 | +} |
| 89 | + |
| 90 | +template <typename P> |
| 91 | +void |
| 92 | +basic_table<P>::iterator::advance(bool emit_equal) { |
| 93 | + int ki; |
| 94 | + bool try_next_index = true; |
| 95 | + leaf_type* n; |
| 96 | + nodeversion_type v; |
| 97 | + node_type* root; |
| 98 | + permuter_type perm; |
| 99 | + Str suffix; |
| 100 | + char suffixbuf[MASSTREE_MAXKEYLEN]; |
| 101 | + |
| 102 | + retry_root: |
| 103 | + ka_.unshift_all(); |
| 104 | + root = table_->root(); |
| 105 | + n = root->reach_leaf(ka_, v, *ti_); |
| 106 | + perm = n->permutation(); |
| 107 | + ki = bound_type::lower(ka_, *n).i; |
| 108 | + |
| 109 | + retry: |
| 110 | + if (v.deleted()) |
| 111 | + goto retry_root; |
| 112 | + |
| 113 | + int kp = (unsigned(ki) < unsigned(perm.size())) ? perm[ki] : -1; |
| 114 | + if (kp < 0) { |
| 115 | + n = n->safe_next(); |
| 116 | + if (!n) { |
| 117 | + if (root == table_->root()) { |
| 118 | + end_ = true; |
| 119 | + return; |
| 120 | + } |
| 121 | + |
| 122 | + ka_.unshift(); |
| 123 | + while (ka_.increment() && ka_.is_shifted()) |
| 124 | + ka_.unshift(); |
| 125 | + ka_.assign_store_ikey(ka_.ikey()); |
| 126 | + ka_.assign_store_length(ka_.ikey_size); |
| 127 | + goto retry_root; |
| 128 | + } |
| 129 | + perm = n->permutation(); |
| 130 | + v = n->stable(); |
| 131 | + ki = bound_type::lower(ka_, *n).i; |
| 132 | + goto retry; |
| 133 | + } |
| 134 | + |
| 135 | + int keylenx = n->keylenx_[kp]; |
| 136 | + ikey_type ikey = n->ikey0_[kp]; |
| 137 | + leafvalue_type entry = n->lv_[kp]; |
| 138 | + if (n->keylenx_has_ksuf(keylenx)) { |
| 139 | + suffix = n->ksuf(kp); |
| 140 | + memcpy(suffixbuf, suffix.s, suffix.len); |
| 141 | + suffix.s = suffixbuf; |
| 142 | + } |
| 143 | + |
| 144 | + if (n->has_changed(v)) |
| 145 | + goto retry_root; |
| 146 | + |
| 147 | + ka_.assign_store_ikey(ikey); |
| 148 | + if (n->keylenx_is_layer(keylenx)) { |
| 149 | + ka_.shift(); |
| 150 | + root = entry.layer(); |
| 151 | + n = root->reach_leaf(ka_, v, *ti_); |
| 152 | + perm = n->permutation(); |
| 153 | + ki = bound_type::lower(ka_, *n).i; |
| 154 | + goto retry; |
| 155 | + } |
| 156 | + |
| 157 | + // XXX This condition is suspect. |
| 158 | + if (!emit_equal && try_next_index && |
| 159 | + (!n->keylenx_has_ksuf(keylenx) || suffix.compare(ka_.suffix()) == 0)) { |
| 160 | + try_next_index = false; |
| 161 | + ki++; |
| 162 | + goto retry; |
| 163 | + } |
| 164 | + |
| 165 | + int keylen = keylenx; |
| 166 | + if (n->keylenx_has_ksuf(keylenx)) { |
| 167 | + keylen = ka_.assign_store_suffix(suffix); |
| 168 | + } |
| 169 | + ka_.assign_store_length(keylen); |
| 170 | + ka_.unshift_all(); |
| 171 | + pair_ = itvalue_type(ka_, entry.value()); |
| 172 | +} |
| 173 | + |
| 174 | +template <typename P> |
| 175 | +typename basic_table<P>::iterator |
| 176 | +basic_table<P>::begin(threadinfo& ti) { |
| 177 | + return iterator(this, &ti); |
| 178 | +} |
| 179 | + |
| 180 | +template <typename P> |
| 181 | +typename basic_table<P>::iterator |
| 182 | +basic_table<P>::end(threadinfo& ti) { |
| 183 | + return iterator::make_end(this, &ti); |
| 184 | +} |
| 185 | + |
| 186 | +template <typename P> |
| 187 | +typename basic_table<P>::iterator |
| 188 | +basic_table<P>::iterate_from(Str firstkey, threadinfo& ti) { |
| 189 | + return iterator(this, &ti, firstkey); |
| 190 | +} |
| 191 | +} |
| 192 | +#endif |
0 commit comments