1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202
|
/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
#ident "$Id$"
/*======
This file is part of PerconaFT.
Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
PerconaFT is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2,
as published by the Free Software Foundation.
PerconaFT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
----------------------------------------
PerconaFT is free software: you can redistribute it and/or modify
it under the terms of the GNU Affero General Public License, version 3,
as published by the Free Software Foundation.
PerconaFT is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU Affero General Public License for more details.
You should have received a copy of the GNU Affero General Public License
along with PerconaFT. If not, see <http://www.gnu.org/licenses/>.
======= */
#ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
#include "keyrange.h"
#include <util/dbt.h>
namespace toku {
// create a keyrange by borrowing the left and right dbt
// pointers. no memory is copied. no checks for infinity needed.
void keyrange::create(const DBT *left, const DBT *right) {
init_empty();
m_left_key = left;
m_right_key = right;
}
// destroy the key copies. if they were never set, then destroy does nothing.
void keyrange::destroy(void) {
toku_destroy_dbt(&m_left_key_copy);
toku_destroy_dbt(&m_right_key_copy);
}
// create a keyrange by copying the keys from the given range.
void keyrange::create_copy(const keyrange &range) {
// start with an initialized, empty range
init_empty();
// optimize the case where the left and right keys are the same.
// we'd like to only have one copy of the data.
if (toku_dbt_equals(range.get_left_key(), range.get_right_key())) {
set_both_keys(range.get_left_key());
} else {
// replace our empty left and right keys with
// copies of the range's left and right keys
replace_left_key(range.get_left_key());
replace_right_key(range.get_right_key());
}
}
// extend this keyrange by choosing the leftmost and rightmost
// endpoints between this range and the given. replaced keys
// in this range are freed and inherited keys are copied.
void keyrange::extend(const comparator &cmp, const keyrange &range) {
const DBT *range_left = range.get_left_key();
const DBT *range_right = range.get_right_key();
if (cmp(range_left, get_left_key()) < 0) {
replace_left_key(range_left);
}
if (cmp(range_right, get_right_key()) > 0) {
replace_right_key(range_right);
}
}
// how much memory does this keyrange take?
// - the size of the left and right keys
// --- ignore the fact that we may have optimized the point case.
// it complicates things for little gain.
// - the size of the keyrange class itself
uint64_t keyrange::get_memory_size(void) const {
const DBT *left_key = get_left_key();
const DBT *right_key = get_right_key();
return left_key->size + right_key->size + sizeof(keyrange);
}
// compare ranges.
keyrange::comparison keyrange::compare(const comparator &cmp, const keyrange &range) const {
if (cmp(get_right_key(), range.get_left_key()) < 0) {
return comparison::LESS_THAN;
} else if (cmp(get_left_key(), range.get_right_key()) > 0) {
return comparison::GREATER_THAN;
} else if (cmp(get_left_key(), range.get_left_key()) == 0 &&
cmp(get_right_key(), range.get_right_key()) == 0) {
return comparison::EQUALS;
} else {
return comparison::OVERLAPS;
}
}
bool keyrange::overlaps(const comparator &cmp, const keyrange &range) const {
// equality is a stronger form of overlapping.
// so two ranges "overlap" if they're either equal or just overlapping.
comparison c = compare(cmp, range);
return c == comparison::EQUALS || c == comparison::OVERLAPS;
}
keyrange keyrange::get_infinite_range(void) {
keyrange range;
range.create(toku_dbt_negative_infinity(), toku_dbt_positive_infinity());
return range;
}
void keyrange::init_empty(void) {
m_left_key = nullptr;
m_right_key = nullptr;
toku_init_dbt(&m_left_key_copy);
toku_init_dbt(&m_right_key_copy);
m_point_range = false;
}
const DBT *keyrange::get_left_key(void) const {
if (m_left_key) {
return m_left_key;
} else {
return &m_left_key_copy;
}
}
const DBT *keyrange::get_right_key(void) const {
if (m_right_key) {
return m_right_key;
} else {
return &m_right_key_copy;
}
}
// copy the given once and set both the left and right pointers.
// optimization for point ranges, so the left and right ranges
// are not copied twice.
void keyrange::set_both_keys(const DBT *key) {
if (toku_dbt_is_infinite(key)) {
m_left_key = key;
m_right_key = key;
} else {
toku_clone_dbt(&m_left_key_copy, *key);
toku_copyref_dbt(&m_right_key_copy, m_left_key_copy);
}
m_point_range = true;
}
// destroy the current left key. set and possibly copy the new one
void keyrange::replace_left_key(const DBT *key) {
// a little magic:
//
// if this is a point range, then the left and right keys share
// one copy of the data, and it lives in the left key copy. so
// if we're replacing the left key, move the real data to the
// right key copy instead of destroying it. now, the memory is
// owned by the right key and the left key may be replaced.
if (m_point_range) {
m_right_key_copy = m_left_key_copy;
} else {
toku_destroy_dbt(&m_left_key_copy);
}
if (toku_dbt_is_infinite(key)) {
m_left_key = key;
} else {
toku_clone_dbt(&m_left_key_copy, *key);
m_left_key = nullptr;
}
m_point_range = false;
}
// destroy the current right key. set and possibly copy the new one
void keyrange::replace_right_key(const DBT *key) {
toku_destroy_dbt(&m_right_key_copy);
if (toku_dbt_is_infinite(key)) {
m_right_key = key;
} else {
toku_clone_dbt(&m_right_key_copy, *key);
m_right_key = nullptr;
}
m_point_range = false;
}
} /* namespace toku */
|