forked from efficient/cicada-exp-sigmod2017-silo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
varkey.h
222 lines (185 loc) · 4.48 KB
/
varkey.h
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
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
#ifndef _NDB_VARKEY_H_
#define _NDB_VARKEY_H_
#include <endian.h>
#include <stdint.h>
#include <string.h>
#include <iostream>
#include <string>
#include <type_traits>
#include <limits>
#include "imstring.h"
#include "macros.h"
#include "util.h"
#if NDB_MASSTREE
#include "prefetch.h"
#include "masstree/config.h"
#include "masstree/string_slice.hh"
#endif
class varkey {
friend std::ostream &operator<<(std::ostream &o, const varkey &k);
public:
inline varkey() : p(NULL), l(0) {}
inline varkey(const varkey &that) = default;
inline varkey(varkey &&that) = default;
inline varkey &operator=(const varkey &that) = default;
inline varkey(const uint8_t *p, size_t l)
: p(p), l(l)
{
}
explicit inline varkey(const std::string &s)
: p((const uint8_t *) s.data()), l(s.size())
{
}
explicit inline varkey(const char *s)
: p((const uint8_t *) s), l(strlen(s))
{
}
explicit inline varkey(const imstring &s)
: p(s.data()), l(s.size())
{
}
inline bool
operator==(const varkey &that) const
{
if (size() != that.size())
return false;
return memcmp(data(), that.data(), size()) == 0;
}
inline bool
operator!=(const varkey &that) const
{
return !operator==(that);
}
inline bool
operator<(const varkey &that) const
{
int r = memcmp(data(), that.data(), std::min(size(), that.size()));
return r < 0 || (r == 0 && size() < that.size());
}
inline bool
operator>=(const varkey &that) const
{
return !operator<(that);
}
inline bool
operator<=(const varkey &that) const
{
int r = memcmp(data(), that.data(), std::min(size(), that.size()));
return r < 0 || (r == 0 && size() <= that.size());
}
inline bool
operator>(const varkey &that) const
{
return !operator<=(that);
}
inline uint64_t
slice() const
{
uint64_t ret = 0;
uint8_t *rp = (uint8_t *) &ret;
for (size_t i = 0; i < std::min(l, size_t(8)); i++)
rp[i] = p[i];
return util::host_endian_trfm<uint64_t>()(ret);
}
#if NDB_MASSTREE
inline uint64_t slice_at(int pos) const {
return string_slice<uint64_t>::make_comparable((const char*) p + pos, std::min(int(l - pos), 8));
}
#endif
inline varkey
shift() const
{
INVARIANT(l >= 8);
return varkey(p + 8, l - 8);
}
inline varkey
shift_many(size_t n) const
{
INVARIANT(l >= 8 * n);
return varkey(p + 8 * n, l - 8 * n);
}
inline size_t
size() const
{
return l;
}
inline int length() const {
return l;
}
inline const uint8_t *
data() const
{
return p;
}
inline
std::string str() const
{
return std::string((const char *) p, l);
}
inline std::string &
str(std::string &buf) const
{
buf.assign((const char *) p, l);
return buf;
}
#if NDB_MASSTREE
inline operator lcdf::Str() const {
return lcdf::Str(p, l);
}
#endif
private:
const uint8_t *p;
size_t l;
};
inline std::ostream &
operator<<(std::ostream &o, const varkey &k)
{
o << util::hexify(k.str());
return o;
}
template <bool is_signed, typename T>
struct signed_aware_trfm {};
template <typename T>
struct signed_aware_trfm<false, T> {
inline ALWAYS_INLINE T operator()(T t) const { return t; }
};
template <typename T>
struct signed_aware_trfm<true, T> {
typedef T signed_type;
typedef
typename std::enable_if<std::is_signed<T>::value, typename std::make_unsigned<T>::type>::type
unsigned_type;
inline ALWAYS_INLINE unsigned_type
operator()(signed_type s) const
{
const unsigned_type offset = -std::numeric_limits<signed_type>::min();
const unsigned_type converted = static_cast<unsigned_type>(s) + offset;
return converted;
}
};
template <typename T,
typename EncodingTrfm = signed_aware_trfm<std::is_signed<T>::value, T>,
typename ByteTrfm = util::big_endian_trfm<T> >
class obj_varkey : public varkey {
public:
typedef
typename std::enable_if<std::is_integral<T>::value, T>::type
integral_type;
inline obj_varkey() : varkey(), obj() {}
inline obj_varkey(integral_type t)
: varkey((const uint8_t *) &obj, sizeof(integral_type)),
obj(ByteTrfm()(EncodingTrfm()(t)))
{
}
private:
integral_type obj;
};
typedef obj_varkey<uint8_t> u8_varkey;
typedef obj_varkey<int8_t> s8_varkey;
typedef obj_varkey<uint16_t> u16_varkey;
typedef obj_varkey<int16_t> s16_varkey;
typedef obj_varkey<uint32_t> u32_varkey;
typedef obj_varkey<int32_t> s32_varkey;
typedef obj_varkey<uint64_t> u64_varkey;
typedef obj_varkey<int64_t> s64_varkey;
#endif /* _NDB_VARKEY_H_ */