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 | #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved." |
38 | |
39 | #pragma once |
40 | |
41 | #include <ft/comparator.h> |
42 | |
43 | namespace toku { |
44 | |
45 | // A keyrange has a left and right key as endpoints. |
46 | // |
47 | // When a keyrange is created it owns no memory, but when it copies |
48 | // or extends another keyrange, it copies memory as necessary. This |
49 | // means it is cheap in the common case. |
50 | |
51 | class keyrange { |
52 | public: |
53 | |
54 | // effect: constructor that borrows left and right key pointers. |
55 | // no memory is allocated or copied. |
56 | void create(const DBT *left_key, const DBT *right_key); |
57 | |
58 | // effect: constructor that allocates and copies another keyrange's points. |
59 | void create_copy(const keyrange &range); |
60 | |
61 | // effect: destroys the keyrange, freeing any allocated memory |
62 | void destroy(void); |
63 | |
64 | // effect: extends the keyrange by choosing the leftmost and rightmost |
65 | // endpoints from this range and the given range. |
66 | // replaced keys in this range are freed, new keys are copied. |
67 | void extend(const comparator &cmp, const keyrange &range); |
68 | |
69 | // returns: the amount of memory this keyrange takes. does not account |
70 | // for point optimizations or malloc overhead. |
71 | uint64_t get_memory_size(void) const; |
72 | |
73 | // returns: pointer to the left key of this range |
74 | const DBT *get_left_key(void) const; |
75 | |
76 | // returns: pointer to the right key of this range |
77 | const DBT *get_right_key(void) const; |
78 | |
79 | // two ranges are either equal, lt, gt, or overlapping |
80 | enum comparison { |
81 | EQUALS, |
82 | LESS_THAN, |
83 | GREATER_THAN, |
84 | OVERLAPS |
85 | }; |
86 | |
87 | // effect: compares this range to the given range |
88 | // returns: LESS_THAN if given range is strictly to the left |
89 | // GREATER_THAN if given range is strictly to the right |
90 | // EQUALS if given range has the same left and right endpoints |
91 | // OVERLAPS if at least one of the given range's endpoints falls |
92 | // between this range's endpoints |
93 | comparison compare(const comparator &cmp, const keyrange &range) const; |
94 | |
95 | // returns: true if the range and the given range are equal or overlapping |
96 | bool overlaps(const comparator &cmp, const keyrange &range) const; |
97 | |
98 | // returns: a keyrange representing -inf, +inf |
99 | static keyrange get_infinite_range(void); |
100 | |
101 | private: |
102 | // some keys should be copied, some keys should not be. |
103 | // |
104 | // to support both, we use two DBTs for copies and two pointers |
105 | // for temporaries. the access rule is: |
106 | // - if a pointer is non-null, then it reprsents the key. |
107 | // - otherwise the pointer is null, and the key is in the copy. |
108 | DBT m_left_key_copy; |
109 | DBT m_right_key_copy; |
110 | const DBT *m_left_key; |
111 | const DBT *m_right_key; |
112 | |
113 | // if this range is a point range, then m_left_key == m_right_key |
114 | // and the actual data is stored exactly once in m_left_key_copy. |
115 | bool m_point_range; |
116 | |
117 | // effect: initializes a keyrange to be empty |
118 | void init_empty(void); |
119 | |
120 | // effect: copies the given key once into the left key copy |
121 | // and sets the right key copy to share the left. |
122 | // rationale: optimization for point ranges to only do one malloc |
123 | void set_both_keys(const DBT *key); |
124 | |
125 | // effect: destroys the current left key. sets and copies the new one. |
126 | void replace_left_key(const DBT *key); |
127 | |
128 | // effect: destroys the current right key. sets and copies the new one. |
129 | void replace_right_key(const DBT *key); |
130 | }; |
131 | |
132 | } /* namespace toku */ |
133 | |