1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */ 2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4: 3 #ident "$Id$" 4 /*====== 5 This file is part of PerconaFT. 6 7 8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved. 9 10 PerconaFT is free software: you can redistribute it and/or modify 11 it under the terms of the GNU General Public License, version 2, 12 as published by the Free Software Foundation. 13 14 PerconaFT is distributed in the hope that it will be useful, 15 but WITHOUT ANY WARRANTY; without even the implied warranty of 16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17 GNU General Public License for more details. 18 19 You should have received a copy of the GNU General Public License 20 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. 21 22 ---------------------------------------- 23 24 PerconaFT is free software: you can redistribute it and/or modify 25 it under the terms of the GNU Affero General Public License, version 3, 26 as published by the Free Software Foundation. 27 28 PerconaFT is distributed in the hope that it will be useful, 29 but WITHOUT ANY WARRANTY; without even the implied warranty of 30 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 31 GNU Affero General Public License for more details. 32 33 You should have received a copy of the GNU Affero General Public License 34 along with PerconaFT. If not, see <http://www.gnu.org/licenses/>. 35 36 ---------------------------------------- 37 38 Licensed under the Apache License, Version 2.0 (the "License"); 39 you may not use this file except in compliance with the License. 40 You may obtain a copy of the License at 41 42 http://www.apache.org/licenses/LICENSE-2.0 43 44 Unless required by applicable law or agreed to in writing, software 45 distributed under the License is distributed on an "AS IS" BASIS, 46 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 47 See the License for the specific language governing permissions and 48 limitations under the License. 49 ======= */ 50 51 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved." 52 53 #pragma once 54 55 #include <ft/comparator.h> 56 57 namespace toku { 58 59 // A keyrange has a left and right key as endpoints. 60 // 61 // When a keyrange is created it owns no memory, but when it copies 62 // or extends another keyrange, it copies memory as necessary. This 63 // means it is cheap in the common case. 64 65 class keyrange { 66 public: 67 68 // effect: constructor that borrows left and right key pointers. 69 // no memory is allocated or copied. 70 void create(const DBT *left_key, const DBT *right_key); 71 72 // effect: constructor that allocates and copies another keyrange's points. 73 void create_copy(const keyrange &range); 74 75 // effect: destroys the keyrange, freeing any allocated memory 76 void destroy(void); 77 78 // effect: extends the keyrange by choosing the leftmost and rightmost 79 // endpoints from this range and the given range. 80 // replaced keys in this range are freed, new keys are copied. 81 void extend(const comparator &cmp, const keyrange &range); 82 83 // returns: the amount of memory this keyrange takes. does not account 84 // for point optimizations or malloc overhead. 85 uint64_t get_memory_size(void) const; 86 87 // returns: pointer to the left key of this range 88 const DBT *get_left_key(void) const; 89 90 // returns: pointer to the right key of this range 91 const DBT *get_right_key(void) const; 92 93 // two ranges are either equal, lt, gt, or overlapping 94 enum comparison { 95 EQUALS, 96 LESS_THAN, 97 GREATER_THAN, 98 OVERLAPS 99 }; 100 101 // effect: compares this range to the given range 102 // returns: LESS_THAN if given range is strictly to the left 103 // GREATER_THAN if given range is strictly to the right 104 // EQUALS if given range has the same left and right endpoints 105 // OVERLAPS if at least one of the given range's endpoints falls 106 // between this range's endpoints 107 comparison compare(const comparator &cmp, const keyrange &range) const; 108 109 // returns: true if the range and the given range are equal or overlapping 110 bool overlaps(const comparator &cmp, const keyrange &range) const; 111 112 // returns: a keyrange representing -inf, +inf 113 static keyrange get_infinite_range(void); 114 115 private: 116 // some keys should be copied, some keys should not be. 117 // 118 // to support both, we use two DBTs for copies and two pointers 119 // for temporaries. the access rule is: 120 // - if a pointer is non-null, then it reprsents the key. 121 // - otherwise the pointer is null, and the key is in the copy. 122 DBT m_left_key_copy; 123 DBT m_right_key_copy; 124 const DBT *m_left_key; 125 const DBT *m_right_key; 126 127 // if this range is a point range, then m_left_key == m_right_key 128 // and the actual data is stored exactly once in m_left_key_copy. 129 bool m_point_range; 130 131 // effect: initializes a keyrange to be empty 132 void init_empty(void); 133 134 // effect: copies the given key once into the left key copy 135 // and sets the right key copy to share the left. 136 // rationale: optimization for point ranges to only do one malloc 137 void set_both_keys(const DBT *key); 138 139 // effect: destroys the current left key. sets and copies the new one. 140 void replace_left_key(const DBT *key); 141 142 // effect: destroys the current right key. sets and copies the new one. 143 void replace_right_key(const DBT *key); 144 }; 145 146 } /* namespace toku */ 147