1 #ifndef _NDB_BENCH_INLINE_STR_H_
2 #define _NDB_BENCH_INLINE_STR_H_
10 #include "../macros.h"
11 #include "serializer.h"
13 // equivalent to VARCHAR(N)
15 template <typename IntSizeType, unsigned int N>
16 class inline_str_base {
18 template <typename T, bool DoCompress> friend class serializer;
21 inline_str_base() : sz(0) {}
23 inline_str_base(const char *s)
28 inline_str_base(const char *s, size_t n)
33 inline_str_base(const std::string &s)
38 inline_str_base(const inline_str_base &that)
41 NDB_MEMCPY(&buf[0], &that.buf[0], sz);
45 operator=(const inline_str_base &that)
50 NDB_MEMCPY(&buf[0], &that.buf[0], sz);
68 str(bool zeropad = false) const
73 NDB_MEMCPY((char *) r.data(), &buf[0], sz);
76 return std::string(&buf[0], sz);
80 inline ALWAYS_INLINE const char *
86 inline ALWAYS_INLINE size_t
92 inline ALWAYS_INLINE void
99 assign(const char *s, size_t n)
102 NDB_MEMCPY(&buf[0], s, n);
106 inline ALWAYS_INLINE void
107 assign(const std::string &s)
109 assign(s.data(), s.size());
113 resize(size_t n, char c = 0)
117 NDB_MEMSET(&buf[sz], c, n - sz);
122 resize_junk(size_t n)
129 operator==(const inline_str_base &other) const
131 return memcmp(buf, other.buf, sz) == 0;
135 operator!=(const inline_str_base &other) const
137 return !operator==(other);
142 mutable char buf[N + 1];
145 template <typename IntSizeType, unsigned int N>
146 inline std::ostream &
147 operator<<(std::ostream &o, const inline_str_base<IntSizeType, N> &s)
149 o << std::string(s.data(), s.size());
153 template <unsigned int N>
154 class inline_str_8 : public inline_str_base<uint8_t, N> {
155 typedef inline_str_base<uint8_t, N> super_type;
157 inline_str_8() : super_type() {}
158 inline_str_8(const char *s) : super_type(s) {}
159 inline_str_8(const char *s, size_t n) : super_type(s, n) {}
160 inline_str_8(const std::string &s) : super_type(s) {}
163 template <unsigned int N>
164 class inline_str_16 : public inline_str_base<uint16_t, N> {
165 typedef inline_str_base<uint16_t, N> super_type;
167 inline_str_16() : super_type() {}
168 inline_str_16(const char *s) : super_type(s) {}
169 inline_str_16(const char *s, size_t n) : super_type(s, n) {}
170 inline_str_16(const std::string &s) : super_type(s) {}
173 // equiavlent to CHAR(N)
174 template <unsigned int N, char FillChar = ' '>
175 class inline_str_fixed {
177 template <typename T, bool DoCompress> friend class serializer;
181 NDB_MEMSET(&buf[0], FillChar, N);
184 inline_str_fixed(const char *s)
186 assign(s, strlen(s));
189 inline_str_fixed(const char *s, size_t n)
194 inline_str_fixed(const std::string &s)
196 assign(s.data(), s.size());
199 inline_str_fixed(const inline_str_fixed &that)
201 NDB_MEMCPY(&buf[0], &that.buf[0], N);
205 operator=(const inline_str_fixed &that)
209 NDB_MEMCPY(&buf[0], &that.buf[0], N);
213 inline ALWAYS_INLINE std::string
216 return std::string(&buf[0], N);
219 inline ALWAYS_INLINE const char *
225 inline ALWAYS_INLINE size_t
231 inline ALWAYS_INLINE void
232 assign(const char *s)
234 assign(s, strlen(s));
238 assign(const char *s, size_t n)
241 NDB_MEMCPY(&buf[0], s, n);
242 if ((N - n) > 0) // to suppress compiler warning
243 NDB_MEMSET(&buf[n], FillChar, N - n); // pad with spaces
246 inline ALWAYS_INLINE void
247 assign(const std::string &s)
249 assign(s.data(), s.size());
253 operator==(const inline_str_fixed &other) const
255 return memcmp(buf, other.buf, N) == 0;
259 operator!=(const inline_str_fixed &other) const
261 return !operator==(other);
268 template <unsigned int N, char FillChar>
269 inline std::ostream &
270 operator<<(std::ostream &o, const inline_str_fixed<N, FillChar> &s)
272 o << std::string(s.data(), s.size());
276 // serializer<T> specialization
277 template <typename IntSizeType, unsigned int N, bool Compress>
278 struct serializer< inline_str_base<IntSizeType, N>, Compress > {
279 typedef inline_str_base<IntSizeType, N> obj_type;
280 static inline uint8_t *
281 write(uint8_t *buf, const obj_type &obj)
283 buf = serializer<IntSizeType, Compress>::write(buf, &obj.sz);
284 NDB_MEMCPY(buf, &obj.buf[0], obj.sz);
288 static const uint8_t *
289 read(const uint8_t *buf, obj_type *obj)
291 buf = serializer<IntSizeType, Compress>::read(buf, &obj->sz);
292 NDB_MEMCPY(&obj->buf[0], buf, obj->sz);
293 return buf + obj->sz;
296 static const uint8_t *
297 failsafe_read(const uint8_t *buf, size_t nbytes, obj_type *obj)
299 const uint8_t * const hdrbuf =
300 serializer<IntSizeType, Compress>::failsafe_read(buf, nbytes, &obj->sz);
301 if (unlikely(!hdrbuf))
303 nbytes -= (hdrbuf - buf);
304 if (nbytes < obj->sz)
307 NDB_MEMCPY(&obj->buf[0], buf, obj->sz);
308 return buf + obj->sz;
312 nbytes(const obj_type *obj)
314 return serializer<IntSizeType, Compress>::nbytes(&obj->sz) + obj->sz;
318 skip(const uint8_t *stream, uint8_t *oldv)
321 const uint8_t * const body = serializer<IntSizeType, Compress>::read(stream, &sz);
322 const size_t totalsz = (body - stream) + sz;
324 NDB_MEMCPY(oldv, stream, totalsz);
329 failsafe_skip(const uint8_t *stream, size_t nbytes, uint8_t *oldv)
332 const uint8_t * const body =
333 serializer<IntSizeType, Compress>::failsafe_read(stream, nbytes, &sz);
336 nbytes -= (body - stream);
337 if (unlikely(nbytes < sz))
339 const size_t totalsz = (body - stream) + sz;
341 NDB_MEMCPY(oldv, stream, totalsz);
345 static inline constexpr size_t
348 return serializer<IntSizeType, Compress>::max_bytes() + N;
352 #endif /* _NDB_BENCH_INLINE_STR_H_ */