forked from kohler/masstree-beta
-
Notifications
You must be signed in to change notification settings - Fork 1
/
value_string.hh
193 lines (161 loc) · 6.39 KB
/
value_string.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
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
/* Masstree
* Eddie Kohler, Yandong Mao, Robert Morris
* Copyright (c) 2012-2014 President and Fellows of Harvard College
* Copyright (c) 2012-2014 Massachusetts Institute of Technology
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, subject to the conditions
* listed in the Masstree LICENSE file. These conditions include: you must
* preserve this copyright notice, and you cannot mention the copyright
* holders in advertising related to the Software without their permission.
* The Software is provided WITHOUT ANY WARRANTY, EXPRESS OR IMPLIED. This
* notice is a summary of the Masstree LICENSE file; the license in that file
* is legally binding.
*/
#ifndef VALUE_STRING_HH
#define VALUE_STRING_HH
#include "compiler.hh"
#include "json.hh"
class value_string {
public:
typedef unsigned index_type;
static const char *name() { return "String"; }
typedef lcdf::Str Str;
typedef lcdf::Json Json;
inline value_string();
inline kvtimestamp_t timestamp() const;
inline size_t size() const;
inline int ncol() const;
inline Str col(index_type idx) const;
template <typename ALLOC>
inline void deallocate(ALLOC& ti);
inline void deallocate_rcu(threadinfo& ti);
template <typename ALLOC>
value_string* update(const Json* first, const Json* last, kvtimestamp_t ts, ALLOC& ti) const;
static inline value_string* create(const Json* first, const Json* last, kvtimestamp_t ts, threadinfo& ti);
static inline value_string* create1(Str value, kvtimestamp_t ts, threadinfo& ti);
inline void deallocate_rcu_after_update(const Json* first, const Json* last, threadinfo& ti);
inline void deallocate_after_failed_update(const Json* first, const Json* last, threadinfo& ti);
template <typename PARSER>
static inline value_string* checkpoint_read(PARSER& par, kvtimestamp_t ts,
threadinfo& ti);
template <typename UNPARSER>
inline void checkpoint_write(UNPARSER& unpar) const;
void print(FILE* f, const char* prefix, int indent, Str key,
kvtimestamp_t initial_ts, const char* suffix = "") {
kvtimestamp_t adj_ts = timestamp_sub(ts_, initial_ts);
fprintf(f, "%s%*s%.*s = %.*s @" PRIKVTSPARTS "%s\n", prefix, indent, "",
key.len, key.s, std::min(40U, vallen_), s_,
KVTS_HIGHPART(adj_ts), KVTS_LOWPART(adj_ts), suffix);
}
static inline index_type make_index(unsigned offset, unsigned length);
static inline unsigned index_offset(index_type idx);
static inline unsigned index_length(index_type idx);
private:
kvtimestamp_t ts_;
unsigned vallen_;
char s_[0];
static inline unsigned index_last_offset(index_type idx);
static inline size_t shallow_size(int vallen);
inline size_t shallow_size() const;
};
inline value_string::index_type value_string::make_index(unsigned offset, unsigned length) {
return offset + (length << 16);
}
inline unsigned value_string::index_offset(index_type idx) {
return idx & 0xFFFF;
}
inline unsigned value_string::index_length(index_type idx) {
return idx >> 16;
}
inline value_string::value_string()
: ts_(0), vallen_(0) {
}
inline kvtimestamp_t value_string::timestamp() const {
return ts_;
}
inline size_t value_string::size() const {
return sizeof(value_string) + vallen_;
}
inline int value_string::ncol() const {
return 1;
}
inline unsigned value_string::index_last_offset(index_type idx) {
return index_offset(idx) + index_length(idx);
}
inline lcdf::Str value_string::col(index_type idx) const {
if (idx == 0)
return Str(s_, vallen_);
else {
unsigned off = std::min(vallen_, index_offset(idx));
return Str(s_ + off, std::min(vallen_ - off, index_length(idx)));
}
}
template <typename ALLOC>
inline void value_string::deallocate(ALLOC& ti) {
ti.deallocate(this, size(), memtag_value);
}
inline void value_string::deallocate_rcu(threadinfo& ti) {
ti.deallocate_rcu(this, size(), memtag_value);
}
inline size_t value_string::shallow_size(int vallen) {
return sizeof(value_string) + vallen;
}
inline size_t value_string::shallow_size() const {
return shallow_size(vallen_);
}
template <typename ALLOC>
value_string* value_string::update(const Json* first, const Json* last,
kvtimestamp_t ts, ALLOC& ti) const {
unsigned vallen = 0, cut = vallen_;
for (auto it = first; it != last; it += 2) {
unsigned idx = it[0].as_u(), length = it[1].as_s().length();
if (idx == 0)
cut = length;
vallen = std::max(vallen, index_offset(idx) + length);
}
vallen = std::max(vallen, cut);
value_string* row = (value_string*) ti.allocate(shallow_size(vallen), memtag_value);
row->ts_ = ts;
row->vallen_ = vallen;
memcpy(row->s_, s_, cut);
for (; first != last; first += 2) {
Str val = first[1].as_s();
memcpy(row->s_ + index_offset(first[0].as_u()), val.data(), val.length());
}
return row;
}
inline value_string* value_string::create(const Json* first, const Json* last,
kvtimestamp_t ts, threadinfo& ti) {
value_string empty;
return empty.update(first, last, ts, ti);
}
inline value_string* value_string::create1(Str value,
kvtimestamp_t ts,
threadinfo& ti) {
value_string* row = (value_string*) ti.allocate(shallow_size(value.length()), memtag_value);
row->ts_ = ts;
row->vallen_ = value.length();
memcpy(row->s_, value.data(), value.length());
return row;
}
inline void value_string::deallocate_rcu_after_update(const Json*, const Json*, threadinfo& ti) {
deallocate_rcu(ti);
}
inline void value_string::deallocate_after_failed_update(const Json*, const Json*, threadinfo& ti) {
deallocate(ti);
}
template <typename PARSER>
inline value_string* value_string::checkpoint_read(PARSER& par,
kvtimestamp_t ts,
threadinfo& ti) {
Str str;
par >> str;
return create1(str, ts, ti);
}
template <typename UNPARSER>
inline void value_string::checkpoint_write(UNPARSER& unpar) const {
unpar << Str(s_, vallen_);
}
#endif