10 #include <type_traits>
19 #include "masstree/config.h"
20 #include "masstree/string_slice.hh"
24 friend std::ostream &operator<<(std::ostream &o, const varkey &k);
26 inline varkey() : p(NULL), l(0) {}
27 inline varkey(const varkey &that) = default;
28 inline varkey(varkey &&that) = default;
29 inline varkey &operator=(const varkey &that) = default;
31 inline varkey(const uint8_t *p, size_t l)
36 explicit inline varkey(const std::string &s)
37 : p((const uint8_t *) s.data()), l(s.size())
41 explicit inline varkey(const char *s)
42 : p((const uint8_t *) s), l(strlen(s))
46 explicit inline varkey(const imstring &s)
47 : p(s.data()), l(s.size())
52 operator==(const varkey &that) const
54 if (size() != that.size())
56 return memcmp(data(), that.data(), size()) == 0;
60 operator!=(const varkey &that) const
62 return !operator==(that);
66 operator<(const varkey &that) const
68 int r = memcmp(data(), that.data(), std::min(size(), that.size()));
69 return r < 0 || (r == 0 && size() < that.size());
73 operator>=(const varkey &that) const
75 return !operator<(that);
79 operator<=(const varkey &that) const
81 int r = memcmp(data(), that.data(), std::min(size(), that.size()));
82 return r < 0 || (r == 0 && size() <= that.size());
86 operator>(const varkey &that) const
88 return !operator<=(that);
95 uint8_t *rp = (uint8_t *) &ret;
96 for (size_t i = 0; i < std::min(l, size_t(8)); i++)
98 return util::host_endian_trfm<uint64_t>()(ret);
102 inline uint64_t slice_at(int pos) const {
103 return string_slice<uint64_t>::make_comparable((const char*) p + pos, std::min(int(l - pos), 8));
111 return varkey(p + 8, l - 8);
115 shift_many(size_t n) const
117 INVARIANT(l >= 8 * n);
118 return varkey(p + 8 * n, l - 8 * n);
127 inline int length() const {
131 inline const uint8_t *
138 std::string str() const
140 return std::string((const char *) p, l);
144 str(std::string &buf) const
146 buf.assign((const char *) p, l);
151 inline operator lcdf::Str() const {
152 return lcdf::Str(p, l);
161 inline std::ostream &
162 operator<<(std::ostream &o, const varkey &k)
164 o << util::hexify(k.str());
168 template <bool is_signed, typename T>
169 struct signed_aware_trfm {};
171 template <typename T>
172 struct signed_aware_trfm<false, T> {
173 inline ALWAYS_INLINE T operator()(T t) const { return t; }
176 template <typename T>
177 struct signed_aware_trfm<true, T> {
178 typedef T signed_type;
180 typename std::enable_if<std::is_signed<T>::value, typename std::make_unsigned<T>::type>::type
182 inline ALWAYS_INLINE unsigned_type
183 operator()(signed_type s) const
185 const unsigned_type offset = -std::numeric_limits<signed_type>::min();
186 const unsigned_type converted = static_cast<unsigned_type>(s) + offset;
191 template <typename T,
192 typename EncodingTrfm = signed_aware_trfm<std::is_signed<T>::value, T>,
193 typename ByteTrfm = util::big_endian_trfm<T> >
194 class obj_varkey : public varkey {
198 typename std::enable_if<std::is_integral<T>::value, T>::type
201 inline obj_varkey() : varkey(), obj() {}
203 inline obj_varkey(integral_type t)
204 : varkey((const uint8_t *) &obj, sizeof(integral_type)),
205 obj(ByteTrfm()(EncodingTrfm()(t)))
213 typedef obj_varkey<uint8_t> u8_varkey;
214 typedef obj_varkey<int8_t> s8_varkey;
215 typedef obj_varkey<uint16_t> u16_varkey;
216 typedef obj_varkey<int16_t> s16_varkey;
217 typedef obj_varkey<uint32_t> u32_varkey;
218 typedef obj_varkey<int32_t> s32_varkey;
219 typedef obj_varkey<uint64_t> u64_varkey;
220 typedef obj_varkey<int64_t> s64_varkey;
222 #endif /* _NDB_VARKEY_H_ */