Zero  0.1.0
btree_impl.h
Go to the documentation of this file.
1 /*
2  * (c) Copyright 2011-2014, Hewlett-Packard Development Company, LP
3  */
4 
5 #ifndef __BTREE_IMPL_H
6 #define __BTREE_IMPL_H
7 
8 #include "w_defines.h"
9 
10 #include "btree.h"
11 #include "btree_verify.h"
12 #include "w_okvl.h"
13 #include "xct.h"
14 
66 class btree_impl : public smlevel_0 {
67 public:
68 
69 #ifdef DOXYGEN_HIDE
70 #endif // DOXYGEN_HIDE
74 
90  (StoreID store,
91  const w_keystr_t& key,
92  bool& need_lock, slotid_t& slot, bool& found, bool& took_XN, bool& is_ghost, btree_page_h& leaf);
93 
103  static rc_t _ux_insert(
104  StoreID store,
105  const w_keystr_t& key,
106  const cvec_t& elem);
107 
109  static rc_t _ux_insert_core(
110  StoreID store,
111  const w_keystr_t& key,
112  const cvec_t& elem);
113 
116  (StoreID store,
117  const w_keystr_t& key, const cvec_t& el,
118  bool& need_lock, slotid_t& slot, bool& found, bool& alreay_took_XN,
119  bool& is_ghost, btree_page_h& leaf);
120 
130  static rc_t _ux_update(
131  StoreID store,
132  const w_keystr_t& key,
133  const cvec_t& elem);
134 
136  static rc_t _ux_update_core(
137  StoreID store,
138  const w_keystr_t& key,
139  const cvec_t& elem);
140 
142  static rc_t _ux_update_core_tail(
143  StoreID store,
144  const w_keystr_t& key, const cvec_t& elem,
145  bool& need_lock, slotid_t& slot, bool& found, bool& is_ghost,
146  btree_page_h& leaf);
147 
158  static rc_t _ux_put(
159  StoreID store,
160  const w_keystr_t& key,
161  const cvec_t& elem);
162 
165  static rc_t _ux_put_core(
166  StoreID store,
167  const w_keystr_t& key,
168  const cvec_t& elem);
169 
180  static rc_t _ux_overwrite(
181  StoreID store,
182  const w_keystr_t& key,
183  const char* el, smsize_t offset, smsize_t elen);
184 
186  static rc_t _ux_overwrite_core(
187  StoreID store,
188  const w_keystr_t& key,
189  const char* el, smsize_t offset, smsize_t elen);
190 
198  static rc_t _sx_reserve_ghost(
199  btree_page_h& leaf, const w_keystr_t& key, int elem_len);
200 
203  btree_page_h& leaf, const w_keystr_t& key, int elem_len);
204 
213  static rc_t _ux_remove(
214  StoreID store,
215  const w_keystr_t& key);
216 
218  static rc_t _ux_remove_core(StoreID store, const w_keystr_t& key);
219 
232  static rc_t _ux_undo_ghost_mark(
233  StoreID store,
234  const w_keystr_t& key);
235 
236 #ifdef DOXYGEN_HIDE
237 #endif // DOXYGEN_HIDE
241 
259  };
265  // 0 to nrecs-1 is child
266  };
267 
294  static rc_t _ux_traverse(
295  StoreID store,
296  const w_keystr_t& key,
297  traverse_mode_t traverse_mode,
298  latch_mode_t leaf_latch_mode,
299  btree_page_h& leaf,
300  bool allow_retry = true
301  );
302 
318  static rc_t _ux_traverse_recurse(
319  btree_page_h& start,
320  const w_keystr_t& key,
321  traverse_mode_t traverse_mode,
322  latch_mode_t leaf_latch_mode,
323  btree_page_h& leaf,
324  PageID& leaf_pid_causing_failed_upgrade
325  );
326 
338  static inline void _ux_traverse_search(btree_impl::traverse_mode_t traverse_mode,
339  btree_page_h* current,
340  const w_keystr_t& key,
341  bool& this_is_the_leaf_page, slot_follow_t& slot_to_follow);
342 
352  static rc_t _ux_traverse_try_eager_adopt(btree_page_h& current, PageID next_pid);
353 
363 
374  static rc_t _ux_lookup(
375  StoreID store,
376  const w_keystr_t& key,
377  bool& found,
378  void* el,
379  smsize_t& elen
380  );
381 
383  static rc_t _ux_lookup_core(
384  StoreID store,
385  const w_keystr_t& key,
386  bool& found,
387  void* el,
388  smsize_t& elen
389  );
390 
391 #ifdef DOXYGEN_HIDE
392 #endif // DOXYGEN_HIDE
396 
406  static rc_t _sx_norec_alloc(btree_page_h& page, PageID& new_page_id);
407 
415  static rc_t _ux_norec_alloc_core(btree_page_h& page, PageID& new_page_id);
416 
427  static rc_t _sx_adopt_foster_all(
428  btree_page_h& root, bool recursive = false);
429 
432  btree_page_h& parent, bool is_root, bool recursive);
433 
446  static rc_t _sx_adopt_foster(btree_page_h& parent, btree_page_h& child);
447 
453  static rc_t _ux_adopt_foster_core(btree_page_h& parent,
454  btree_page_h& child, const w_keystr_t& new_child_key);
455 
466  bool& pushedup);
467 
474  static rc_t _sx_adopt_foster_sweep(btree_page_h& parent_arg);
475 
483  static rc_t _sx_adopt_foster_sweep_approximate(btree_page_h& parent, PageID surely_need_child_pid);
484 
486  static void _ux_adopt_foster_apply_parent(btree_page_h& parent_arg,
487  PageID new_child_pid, lsn_t new_child_emlsn,
488  const w_keystr_t& new_child_key);
489 
491  static void _ux_adopt_foster_apply_child(btree_page_h& child);
492 
522  static rc_t _sx_split_foster(btree_page_h& page,
523  PageID& new_page_id, const w_keystr_t& triggering_key);
524 
533  const w_keystr_t& new_key);
534 
535 #ifdef DOXYGEN_HIDE
536 #endif // DOXYGEN_HIDE
540 
561  static rc_t _ux_lock_key(
562  const StoreID& store,
563  btree_page_h& leaf,
564  const w_keystr_t& key,
565  latch_mode_t latch_mode,
566  const okvl_mode& lock_mode,
567  bool check_only
568  );
569 
571  static rc_t _ux_lock_key(
572  const StoreID& store,
573  btree_page_h& leaf,
574  const void* keystr,
575  size_t keylen,
576  latch_mode_t latch_mode,
577  const okvl_mode& lock_mode,
578  bool check_only
579  );
580 
599  static rc_t _ux_lock_range(const StoreID& store,
600  btree_page_h& leaf,
601  const w_keystr_t& key,
602  slotid_t slot,
603  latch_mode_t latch_mode,
604  const okvl_mode& exact_hit_lock_mode,
605  const okvl_mode& miss_lock_mode,
606  bool check_only);
607 
609  static rc_t _ux_lock_range(const StoreID& store,
610  btree_page_h& leaf,
611  const void* keystr,
612  size_t keylen,
613  slotid_t slot,
614  latch_mode_t latch_mode,
615  const okvl_mode& exact_hit_lock_mode,
616  const okvl_mode& miss_lock_mode,
617  bool check_only);
618 
634 
635 #ifdef DOXYGEN_HIDE
636 #endif // DOXYGEN_HIDE
640 
645  static rc_t _ux_create_tree_core(const StoreID& stid, const PageID& root_pid);
646 
654  static rc_t _sx_shrink_tree(btree_page_h& root);
655 
660  static rc_t _ux_shrink_tree_core(btree_page_h& root);
661 
670  static rc_t _sx_grow_tree(btree_page_h& root);
671 
672 #ifdef DOXYGEN_HIDE
673 #endif // DOXYGEN_HIDE
677 
692  static rc_t _ux_verify_tree(
693  StoreID store, int hash_bits, bool& consistent);
694 
701  btree_page_h& parent, verification_context& context);
702 
709  static rc_t _ux_verify_feed_page(
710  btree_page_h& page, verification_context& context);
711 
723  static rc_t _ux_verify_volume(
724  int hash_bits, verify_volume_result& result);
725 
727  static void inquery_verify_init(StoreID store);
728 
730  static void inquery_verify_fact(btree_page_h& page);
731 
733  static void inquery_verify_expect(btree_page_h& page, slot_follow_t next_follow);
734 
735 #ifdef DOXYGEN_HIDE
736 #endif // DOXYGEN_HIDE
740 
756  static rc_t _sx_defrag_tree(
757  StoreID store,
758  uint16_t inpage_defrag_ghost_threshold = 10,
759  uint16_t inpage_defrag_usage_threshold = 50,
760  bool does_adopt = true,
761  bool does_merge = true);
762 
766  static rc_t _ux_defrag_tree_core(
767  StoreID store,
768  uint16_t inpage_defrag_ghost_threshold,
769  uint16_t inpage_defrag_usage_threshold,
770  bool does_adopt,
771  bool does_merge);
772 
784  static rc_t _sx_defrag_page(btree_page_h& page);
785 
791 
797 
798 #ifdef DOXYGEN_HIDE
799 #endif // DOXYGEN_HIDE
803  // see jira ticket:78 "Eager-Opportunistic Hybrid Latching" (originally trac ticket:80)
804  // these are used to help determine when we should do eager EX latching.
805  // these don't have to be exact or transactionally protected, so just static variables.
806  enum {
807  GAC_HASH_BITS = 16, // 64K
808  GAC_HASH_MOD = 65521 // some prime number smaller than 2^GAC_HASH_BITS
809  };
810 
815  static uint8_t s_ex_need_counts[1 << GAC_HASH_BITS];
816 
824 
831 
833  inline static uint32_t shpid2hash(PageID pid) {
834  return pid % GAC_HASH_MOD;
835  }
836 
839  return s_ex_need_mutex + shpid2hash(pid);
840  }
841 
843  inline static bool is_ex_recommended(PageID pid) {
844  uint32_t hash = shpid2hash(pid);
845  w_assert1(hash < (1 << GAC_HASH_BITS));
846  return (s_ex_need_counts[hash] > 30);
847  }
848 
850  inline static uint8_t get_expected_childrens(PageID pid) {
851  uint32_t hash = shpid2hash(pid);
852  w_assert1(hash < (1 << GAC_HASH_BITS));
853  return s_foster_children_counts[hash];
854  }
855 
857  inline static void increase_ex_need(PageID real_parent_pid) {
858  uint32_t hash = shpid2hash(real_parent_pid);
859  w_assert1(hash < (1 << GAC_HASH_BITS));
860  if (s_ex_need_counts[hash] < 255) {
861  ++s_ex_need_counts[hash];
862  }
863  }
864 
866  inline static void increase_forster_child(PageID new_foster_parent_pid) {
867  uint32_t hash = shpid2hash(new_foster_parent_pid);
868  w_assert1(hash < (1 << GAC_HASH_BITS));
869  ++s_foster_children_counts[hash];
870  if (s_foster_children_counts[hash] < 255) {
871  ++s_foster_children_counts[hash];
872  }
873  }
874 
876  inline static void clear_ex_need(PageID real_parent_pid) {
877  uint32_t hash = shpid2hash(real_parent_pid);
878  w_assert1(hash < (1 << GAC_HASH_BITS));
879  s_ex_need_counts[hash] = 0;
880  }
881 
883  inline static void clear_forster_child(PageID foster_parent_pid) {
884  uint32_t hash = shpid2hash(foster_parent_pid);
885  w_assert1(hash < (1 << GAC_HASH_BITS));
886  s_foster_children_counts[hash] = 0;
887  }
888 };
889 
890 #endif // __BTREE_IMPL_H
static rc_t _ux_lookup_core(StoreID store, const w_keystr_t &key, bool &found, void *el, smsize_t &elen)
Definition: btree_impl_search.cpp:40
static rc_t _ux_overwrite_core(StoreID store, const w_keystr_t &key, const char *el, smsize_t offset, smsize_t elen)
Definition: btree_impl.cpp:412
static rc_t _sx_grow_tree(btree_page_h &root)
On root page split, allocates a new child, shifts all entries of root to new child, and has the only entry in root (pid0) point to child. Tree grows by 1 level.
Definition: btree_impl_grow.cpp:109
static void increase_ex_need(PageID real_parent_pid)
Definition: btree_impl.h:857
static rc_t _ux_assure_fence_low_entry(btree_page_h &leaf)
Assures the given leaf page has an entry whose key is the low-fence.
Definition: btree_impl_lock.cpp:197
static rc_t _ux_insert_core(StoreID store, const w_keystr_t &key, const cvec_t &elem)
Definition: btree_impl.cpp:42
#define w_assert1(x)
Level 1 should not add significant extra time.
Definition: w_base.h:198
static queue_based_lock_t * mutex_for_high_contention(PageID pid)
Definition: btree_impl.h:838
uint32_t smsize_t
Definition: basics.h:41
static rc_t _ux_verify_tree(StoreID store, int hash_bits, bool &consistent)
Verifies the integrity of whole tree using the fence-key bitmap technique.
Definition: btree_impl_verify.cpp:28
static rc_t _ux_remove(StoreID store, const w_keystr_t &key)
Removes the specified key from B+Tree.
Definition: btree_impl.cpp:458
static uint8_t s_ex_need_counts[1<< GAC_HASH_BITS]
Definition: btree_impl.h:815
static rc_t _ux_update(StoreID store, const w_keystr_t &key, const cvec_t &elem)
This function finds the given key, updates the element if found. If needed, this method also splits t...
Definition: btree_impl.cpp:292
static rc_t _ux_adopt_foster_core(btree_page_h &parent, btree_page_h &child, const w_keystr_t &new_child_key)
Definition: btree_impl_split.cpp:256
Definition: btree_impl.h:808
Page handle for B-Tree data page.
Definition: btree_page_h.h:185
Definition: btree_verify.h:84
Definition: btree_impl.h:264
uint32_t StoreID
Definition: basics.h:47
static rc_t _ux_put_core(StoreID store, const w_keystr_t &key, const cvec_t &elem)
Definition: btree_impl.cpp:155
static bool is_ex_recommended(PageID pid)
Definition: btree_impl.h:843
static rc_t _ux_create_tree_core(const StoreID &stid, const PageID &root_pid)
Definition: btree_impl_grow.cpp:20
Key string class which can represent a few special values.
Definition: w_key.h:47
static rc_t _ux_verify_tree_recurse(btree_page_h &parent, verification_context &context)
Definition: btree_impl_verify.cpp:50
Definition: btree_impl.h:258
static void _ux_traverse_search(btree_impl::traverse_mode_t traverse_mode, btree_page_h *current, const w_keystr_t &key, bool &this_is_the_leaf_page, slot_follow_t &slot_to_follow)
Internal helper function to actually search for the correct slot and test fence assumptions.
Definition: btree_impl_search.cpp:270
static rc_t _ux_insert_core_tail(StoreID store, const w_keystr_t &key, const cvec_t &el, bool &need_lock, slotid_t &slot, bool &found, bool &alreay_took_XN, bool &is_ghost, btree_page_h &leaf)
Definition: btree_impl.cpp:207
static rc_t _sx_adopt_foster(btree_page_h &parent, btree_page_h &child)
Pushes up a foster pointer of child to the parent.
Definition: btree_impl_split.cpp:240
static rc_t _ux_traverse(StoreID store, const w_keystr_t &key, traverse_mode_t traverse_mode, latch_mode_t leaf_latch_mode, btree_page_h &leaf, bool allow_retry=true)
Traverse the btree starting at root node to find an appropriate leaf page.
Definition: btree_impl_search.cpp:92
The internal implementation class which actually implements the functions of btree_m.
Definition: btree_impl.h:66
static rc_t _sx_adopt_foster_all_core(btree_page_h &parent, bool is_root, bool recursive)
Definition: btree_impl_split.cpp:210
static void _ux_adopt_foster_apply_parent(btree_page_h &parent_arg, PageID new_child_pid, lsn_t new_child_emlsn, const w_keystr_t &new_child_key)
Definition: btree_impl_split.cpp:393
static rc_t _ux_traverse_try_opportunistic_adopt(btree_page_h &current, btree_page_h &next)
Definition: btree_impl_search.cpp:393
element_lock_mode
Lock mode for one OKVL component (key, partition, or gap).
Definition: w_okvl.h:107
static rc_t _ux_traverse_recurse(btree_page_h &start, const w_keystr_t &key, traverse_mode_t traverse_mode, latch_mode_t leaf_latch_mode, btree_page_h &leaf, PageID &leaf_pid_causing_failed_upgrade)
For internal recursion. Assuming start is non-leaf, check children recursively.
Definition: btree_impl_search.cpp:157
A constant vec_t (meaning things pointed to cannot be changed).
Definition: vec_t.h:95
static void clear_forster_child(PageID foster_parent_pid)
Definition: btree_impl.h:883
static rc_t _sx_split_if_needed(btree_page_h &page, const w_keystr_t &new_key)
Splits the given page if we need to do so for inserting the given key.
Definition: btree_impl_split.cpp:180
Definition: btree_impl.h:248
static void inquery_verify_expect(btree_page_h &page, slot_follow_t next_follow)
Definition: btree_impl_verify.cpp:234
static rc_t _sx_shrink_tree(btree_page_h &root)
Shrink the tree. Copy the child page over the root page so the tree is effectively one level shorter...
Definition: btree_impl_grow.cpp:43
uint32_t PageID
Definition: basics.h:45
static rc_t _sx_split_foster(btree_page_h &page, PageID &new_page_id, const w_keystr_t &triggering_key)
Splits a page, making the new page as foster-child.
Definition: btree_impl_split.cpp:88
static rc_t _sx_adopt_foster_sweep_approximate(btree_page_h &parent, PageID surely_need_child_pid)
Definition: btree_impl_split.cpp:320
static rc_t _sx_reserve_ghost(btree_page_h &leaf, const w_keystr_t &key, int elem_len)
Creates a ghost record for the key as a preparation for insert. Context: System transaction.
Definition: btree_impl.cpp:272
static void _ux_adopt_foster_apply_child(btree_page_h &child)
Definition: btree_impl_split.cpp:417
static rc_t _ux_update_core_tail(StoreID store, const w_keystr_t &key, const cvec_t &elem, bool &need_lock, slotid_t &slot, bool &found, bool &is_ghost, btree_page_h &leaf)
Definition: btree_impl.cpp:361
Log Sequence Number. See Log Sequence Numbers (LSN).
Definition: lsn.h:243
static rc_t _ux_lookup(StoreID store, const w_keystr_t &key, bool &found, void *el, smsize_t &elen)
Definition: btree_impl_search.cpp:27
static uint8_t s_foster_children_counts[1<< GAC_HASH_BITS]
Definition: btree_impl.h:830
static void clear_ex_need(PageID real_parent_pid)
Definition: btree_impl.h:876
static void increase_forster_child(PageID new_foster_parent_pid)
Definition: btree_impl.h:866
static rc_t _ux_verify_volume(int hash_bits, verify_volume_result &result)
Verifies consistency of all BTree indexes in the volume.
Definition: btree_impl_verify.cpp:354
static rc_t _ux_lock_key(const StoreID &store, btree_page_h &leaf, const w_keystr_t &key, latch_mode_t latch_mode, const okvl_mode &lock_mode, bool check_only)
Acquires a lock on the given leaf page, tentatively unlatching the page if needed.
Definition: btree_impl_lock.cpp:25
static rc_t _ux_put(StoreID store, const w_keystr_t &key, const cvec_t &elem)
This function finds the given key, updates the element if found and inserts it if not...
Definition: btree_impl.cpp:140
Return code for most functions and methods.
Definition: w_rc.h:87
static rc_t _ux_traverse_try_eager_adopt(btree_page_h &current, PageID next_pid)
Definition: btree_impl_search.cpp:362
static okvl_mode create_part_okvl(okvl_mode::element_lock_mode mode, const w_keystr_t &key)
Definition: btree_impl.cpp:580
static rc_t _ux_lock_range(const StoreID &store, btree_page_h &leaf, const w_keystr_t &key, slotid_t slot, latch_mode_t latch_mode, const okvl_mode &exact_hit_lock_mode, const okvl_mode &miss_lock_mode, bool check_only)
Definition: btree_impl_lock.cpp:131
static rc_t _ux_defrag_page_core(btree_page_h &p)
Definition: btree_impl_defrag.cpp:61
int16_t slotid_t
Definition: basics.h:53
An MCS queuing spinlock.
Definition: mcs_lock.h:61
static rc_t _sx_norec_alloc(btree_page_h &page, PageID &new_page_id)
Creates a new empty page as a foster-child of the given page.
Definition: btree_impl_split.cpp:26
static void inquery_verify_fact(btree_page_h &page)
Definition: btree_impl_verify.cpp:187
static rc_t _ux_insert(StoreID store, const w_keystr_t &key, const cvec_t &elem)
This function finds the leaf page to insert the given tuple, inserts it to the page and...
Definition: btree_impl.cpp:26
static rc_t _ux_get_page_and_status(StoreID store, const w_keystr_t &key, bool &need_lock, slotid_t &slot, bool &found, bool &took_XN, bool &is_ghost, btree_page_h &leaf)
Function finds the leaf page for a key, locks the key if it exists, and determines if it was found an...
Definition: btree_impl.cpp:174
The context object maintained throughout the verification.
Definition: btree_verify.h:18
Represents a lock mode of one key entry in the OKVL lock manager.
Definition: w_okvl.h:95
Definition: btree_impl.h:262
static rc_t _sx_defrag_page(btree_page_h &page)
Defrags the given page to remove holes and ghost records in the page.
Definition: btree_impl_defrag.cpp:53
Definition: btree_impl.h:807
static rc_t _ux_norec_alloc_core(btree_page_h &page, PageID &new_page_id)
Definition: btree_impl_split.cpp:34
static rc_t _ux_defrag_tree_core(StoreID store, uint16_t inpage_defrag_ghost_threshold, uint16_t inpage_defrag_usage_threshold, bool does_adopt, bool does_merge)
Definition: btree_impl_defrag.cpp:39
traverse_mode_t
3 modes of traverse().
Definition: btree_impl.h:243
static rc_t _ux_shrink_tree_core(btree_page_h &root)
Definition: btree_impl_grow.cpp:58
static rc_t _ux_overwrite(StoreID store, const w_keystr_t &key, const char *el, smsize_t offset, smsize_t elen)
This function finds the given key, updates the specific part of element if found. ...
Definition: btree_impl.cpp:398
static uint8_t get_expected_childrens(PageID pid)
Definition: btree_impl.h:850
Definition: btree_impl.h:263
static rc_t _ux_update_core(StoreID store, const w_keystr_t &key, const cvec_t &elem)
Definition: btree_impl.cpp:304
static rc_t _sx_opportunistic_adopt_foster(btree_page_h &parent, btree_page_h &child, bool &pushedup)
Pushes up a foster pointer of child to the parent IF we can get EX latches immediately.
Definition: btree_impl_split.cpp:289
static rc_t _sx_adopt_foster_all(btree_page_h &root, bool recursive=false)
Checks all direct children of parent and, if some child has foster, pushes them up to parent...
Definition: btree_impl_split.cpp:205
latch_mode_t
Definition: latch.h:79
static rc_t _sx_defrag_tree(StoreID store, uint16_t inpage_defrag_ghost_threshold=10, uint16_t inpage_defrag_usage_threshold=50, bool does_adopt=true, bool does_merge=true)
Checks the whole tree to opportunistically adopt, in-page defrag and rebalance.
Definition: btree_impl_defrag.cpp:22
static void inquery_verify_init(StoreID store)
Definition: btree_impl_verify.cpp:175
Definition: btree_impl.h:253
static rc_t _ux_verify_feed_page(btree_page_h &page, verification_context &context)
Definition: btree_impl_verify.cpp:86
static rc_t _ux_undo_ghost_mark(StoreID store, const w_keystr_t &key)
Reverses the ghost record of specified key to regular state.
Definition: btree_impl.cpp:531
static rc_t _sx_adopt_foster_sweep(btree_page_h &parent_arg)
Pushes up all foster-children of children to the parent.
Definition: btree_impl_split.cpp:363
static rc_t _ux_remove_core(StoreID store, const w_keystr_t &key)
Definition: btree_impl.cpp:471
static queue_based_lock_t s_ex_need_mutex[1<< GAC_HASH_BITS]
Definition: btree_impl.h:823
static rc_t _ux_reserve_ghost_core(btree_page_h &leaf, const w_keystr_t &key, int elem_len)
Definition: btree_impl.cpp:280
static uint32_t shpid2hash(PageID pid)
Definition: btree_impl.h:833
slot_follow_t
Definition: btree_impl.h:261