forked from scylladb/scylladb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
hashing.hh
160 lines (142 loc) · 4.81 KB
/
hashing.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
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
/*
* Copyright (C) 2015 ScyllaDB
*/
/*
* This file is part of Scylla.
*
* Scylla is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Scylla 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 Scylla. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <chrono>
#include <map>
#include <experimental/optional>
#include <seastar/core/byteorder.hh>
#include <seastar/core/sstring.hh>
#include "seastarx.hh"
//
// This hashing differs from std::hash<> in that it decouples knowledge about
// type structure from the way the hash value is calculated:
// * appending_hash<T> instantiation knows about what data should be included in the hash for type T.
// * Hasher object knows how to combine the data into the final hash.
//
// The appending_hash<T> should always feed some data into the hasher, regardless of the state the object is in,
// in order for the hash to be highly sensitive for value changes. For example, vector<optional<T>> should
// ideally feed different values for empty vector and a vector with a single empty optional.
//
// appending_hash<T> is machine-independent.
//
// The Hasher concept
struct Hasher {
void update(const char* ptr, size_t size);
};
template<typename T, typename Enable = void>
struct appending_hash;
template<typename Hasher, typename T, typename... Args>
inline
void feed_hash(Hasher& h, const T& value, Args&&... args) {
appending_hash<T>()(h, value, std::forward<Args>(args)...);
};
template<typename T>
struct appending_hash<T, std::enable_if_t<std::is_arithmetic<T>::value>> {
template<typename Hasher>
void operator()(Hasher& h, T value) const {
auto value_le = cpu_to_le(value);
h.update(reinterpret_cast<const char*>(&value_le), sizeof(T));
}
};
template<>
struct appending_hash<bool> {
template<typename Hasher>
void operator()(Hasher& h, bool value) const {
feed_hash(h, static_cast<uint8_t>(value));
}
};
template<typename T>
struct appending_hash<T, std::enable_if_t<std::is_enum<T>::value>> {
template<typename Hasher>
void operator()(Hasher& h, const T& value) const {
feed_hash(h, static_cast<std::underlying_type_t<T>>(value));
}
};
template<typename T>
struct appending_hash<std::experimental::optional<T>> {
template<typename Hasher>
void operator()(Hasher& h, const std::experimental::optional<T>& value) const {
if (value) {
feed_hash(h, true);
feed_hash(h, *value);
} else {
feed_hash(h, false);
}
}
};
template<size_t N>
struct appending_hash<char[N]> {
template<typename Hasher>
void operator()(Hasher& h, const char (&value) [N]) const {
feed_hash(h, N);
h.update(value, N);
}
};
template<typename T>
struct appending_hash<std::vector<T>> {
template<typename Hasher>
void operator()(Hasher& h, const std::vector<T>& value) const {
feed_hash(h, value.size());
for (auto&& v : value) {
appending_hash<T>()(h, v);
}
}
};
template<typename K, typename V>
struct appending_hash<std::map<K, V>> {
template<typename Hasher>
void operator()(Hasher& h, const std::map<K, V>& value) const {
feed_hash(h, value.size());
for (auto&& e : value) {
appending_hash<K>()(h, e.first);
appending_hash<V>()(h, e.second);
}
}
};
template<>
struct appending_hash<sstring> {
template<typename Hasher>
void operator()(Hasher& h, const sstring& v) const {
feed_hash(h, v.size());
h.update(reinterpret_cast<const char*>(v.cbegin()), v.size() * sizeof(sstring::value_type));
}
};
template<>
struct appending_hash<std::string> {
template<typename Hasher>
void operator()(Hasher& h, const std::string& v) const {
feed_hash(h, v.size());
h.update(reinterpret_cast<const char*>(v.data()), v.size() * sizeof(std::string::value_type));
}
};
template<typename T, typename R>
struct appending_hash<std::chrono::duration<T, R>> {
template<typename Hasher>
void operator()(Hasher& h, std::chrono::duration<T, R> v) const {
feed_hash(h, v.count());
}
};
template<typename Clock, typename Duration>
struct appending_hash<std::chrono::time_point<Clock, Duration>> {
template<typename Hasher>
void operator()(Hasher& h, std::chrono::time_point<Clock, Duration> v) const {
feed_hash(h, v.time_since_epoch().count());
}
};