]> git.proxmox.com Git - ceph.git/blob - ceph/src/rocksdb/utilities/transactions/lock/range/range_tree/lib/locktree/keyrange.h
update ceph source to reef 18.1.2
[ceph.git] / ceph / src / rocksdb / utilities / transactions / lock / range / range_tree / lib / locktree / keyrange.h
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 \
52 "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
53
54 #pragma once
55
56 #include "../ft/comparator.h"
57
58 namespace toku {
59
60 // A keyrange has a left and right key as endpoints.
61 //
62 // When a keyrange is created it owns no memory, but when it copies
63 // or extends another keyrange, it copies memory as necessary. This
64 // means it is cheap in the common case.
65
66 class keyrange {
67 public:
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 { EQUALS, LESS_THAN, GREATER_THAN, OVERLAPS };
95
96 // effect: compares this range to the given range
97 // returns: LESS_THAN if given range is strictly to the left
98 // GREATER_THAN if given range is strictly to the right
99 // EQUALS if given range has the same left and right endpoints
100 // OVERLAPS if at least one of the given range's endpoints falls
101 // between this range's endpoints
102 comparison compare(const comparator &cmp, const keyrange &range) const;
103
104 // returns: true if the range and the given range are equal or overlapping
105 bool overlaps(const comparator &cmp, const keyrange &range) const;
106
107 // returns: a keyrange representing -inf, +inf
108 static keyrange get_infinite_range(void);
109
110 private:
111 // some keys should be copied, some keys should not be.
112 //
113 // to support both, we use two DBTs for copies and two pointers
114 // for temporaries. the access rule is:
115 // - if a pointer is non-null, then it reprsents the key.
116 // - otherwise the pointer is null, and the key is in the copy.
117 DBT m_left_key_copy;
118 DBT m_right_key_copy;
119 const DBT *m_left_key;
120 const DBT *m_right_key;
121
122 // if this range is a point range, then m_left_key == m_right_key
123 // and the actual data is stored exactly once in m_left_key_copy.
124 bool m_point_range;
125
126 // effect: initializes a keyrange to be empty
127 void init_empty(void);
128
129 // effect: copies the given key once into the left key copy
130 // and sets the right key copy to share the left.
131 // rationale: optimization for point ranges to only do one malloc
132 void set_both_keys(const DBT *key);
133
134 // effect: destroys the current left key. sets and copies the new one.
135 void replace_left_key(const DBT *key);
136
137 // effect: destroys the current right key. sets and copies the new one.
138 void replace_right_key(const DBT *key);
139 };
140
141 } /* namespace toku */