forked from fastio/1store
-
Notifications
You must be signed in to change notification settings - Fork 0
/
token.hh
68 lines (61 loc) · 2.09 KB
/
token.hh
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
#pragma once
#include "utils/managed_bytes.hh"
namespace redis {
class token {
public:
enum class kind {
before_all_keys,
key,
after_all_keys,
};
kind _kind;
// _data can be interpreted as a big endian binary fraction
// in the range [0.0, 1.0).
//
// So, [] == 0.0
// [0x00] == 0.0
// [0x80] == 0.5
// [0x00, 0x80] == 1/512
// [0xff, 0x80] == 1 - 1/512
managed_bytes _data;
token() : _kind(kind::before_all_keys) {
}
token(kind k, managed_bytes d) : _kind(std::move(k)), _data(std::move(d)) {
}
bool is_minimum() const {
return _kind == kind::before_all_keys;
}
bool is_maximum() const {
return _kind == kind::after_all_keys;
}
static token from_bytes(const bytes& key);
static token from_bytes(const bytes_view& key);
};
token midpoint_unsigned(const token& t1, const token& t2);
const token& minimum_token();
const token& maximum_token();
bool operator==(const token& t1, const token& t2);
bool operator<(const token& t1, const token& t2);
int tri_compare(const token& t1, const token& t2);
inline bool operator!=(const token& t1, const token& t2) { return std::rel_ops::operator!=(t1, t2); }
inline bool operator>(const token& t1, const token& t2) { return std::rel_ops::operator>(t1, t2); }
inline bool operator<=(const token& t1, const token& t2) { return std::rel_ops::operator<=(t1, t2); }
inline bool operator>=(const token& t1, const token& t2) { return std::rel_ops::operator>=(t1, t2); }
std::ostream& operator<<(std::ostream& out, const token& t);
}
namespace std {
template<> struct hash<redis::token> {
size_t operator()(const redis::token& t) const {
size_t ret = 0;
const auto& b = t._data;
if (b.size() <= sizeof(ret)) { // practically always
std::copy_n(b.data(), b.size(), reinterpret_cast<int8_t*>(&ret));
} else {
ret = hash_large_token(b);
}
return ret;
}
private:
size_t hash_large_token(const managed_bytes& b) const;
};
}