forked from kohler/masstree-beta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
value_string.hh
210 lines (179 loc) · 6.74 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
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
/* Masstree
* Eddie Kohler, Yandong Mao, Robert Morris
* Copyright (c) 2012-2013 President and Fellows of Harvard College
* Copyright (c) 2012-2013 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 "kvrow.hh"
struct valueindex_string {
short f_off;
short f_len;
valueindex_string() = default;
valueindex_string(short off, short len)
: f_off(off), f_len(len) {
}
friend bool operator==(const valueindex_string& a,
const valueindex_string& b) {
return a.f_off == b.f_off && a.f_len == b.f_len;
}
friend bool operator<(const valueindex_string& a,
const valueindex_string& b) {
return a.f_off < b.f_off;
}
};
template <>
struct valueindex<valueindex_string> {
static inline valueindex_string make_full() {
return valueindex_string(0, -1);
}
static inline valueindex_string make_fixed(int index, int width) {
return valueindex_string(index * width, width);
}
};
inline int KVR(kvin* kv, valueindex_string& field) {
int x = KVR(kv, field.f_off);
return x + KVR(kv, field.f_len);
}
inline int KVW(kvout* kv, valueindex_string field) {
int x = KVW(kv, field.f_off);
return x + KVW(kv, field.f_len);
}
class value_string : public row_base<valueindex_string> {
public:
typedef valueindex_string index_type;
static constexpr rowtype_id type_id = RowType_Str;
static const char *name() { return "String"; }
inline value_string();
inline kvtimestamp_t timestamp() const;
inline size_t size() const;
inline int ncol() const;
inline Str col(int i) const;
inline Str col(valueindex_string idx) const;
template <typename ALLOC>
inline void deallocate(ALLOC& ti);
inline void deallocate_rcu(threadinfo& ti);
template <typename CS, typename ALLOC>
value_string* update(const CS& changeset, kvtimestamp_t ts, ALLOC& ti) const;
template <typename CS>
static inline value_string* create(const CS& changeset, kvtimestamp_t ts, threadinfo& ti);
static inline value_string* create1(Str value, kvtimestamp_t ts, threadinfo& ti);
template <typename CS>
inline void deallocate_rcu_after_update(const CS& changeset, threadinfo& ti);
template <typename CS>
inline void deallocate_after_failed_update(const CS& changeset, threadinfo& ti);
static inline value_string* checkpoint_read(Str str, kvtimestamp_t ts,
threadinfo& ti);
inline void checkpoint_write(kvout* kv) 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(40, vallen_), s_,
KVTS_HIGHPART(adj_ts), KVTS_LOWPART(adj_ts), suffix);
}
private:
kvtimestamp_t ts_;
int vallen_;
char s_[0];
static inline size_t shallow_size(int vallen);
inline size_t shallow_size() const;
};
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 Str value_string::col(int i) const {
assert(i == 0);
(void) i;
return Str(s_, vallen_);
}
inline Str value_string::col(valueindex_string idx) const {
int len = idx.f_len == -1 ? vallen_ - idx.f_off : idx.f_len;
return Str(s_ + idx.f_off, len);
}
template <typename ALLOC>
inline void value_string::deallocate(ALLOC& ti) {
ti.deallocate(this, size(), memtag_row_str);
}
inline void value_string::deallocate_rcu(threadinfo& ti) {
ti.deallocate_rcu(this, size(), memtag_row_str);
}
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 CS, typename ALLOC>
value_string* value_string::update(const CS& changeset, kvtimestamp_t ts,
ALLOC& ti) const {
auto last = changeset.end();
int vallen = 0, cut = vallen_;
for (auto it = changeset.begin(); it != last; ++it) {
vallen = std::max(vallen, it->index().f_off + it->value_length());
if (it->index().f_len == -1)
cut = std::min(cut, int(it->index().f_off));
}
vallen = std::max(vallen, cut);
value_string* row = (value_string*) ti.allocate(shallow_size(vallen), memtag_row_str);
row->ts_ = ts;
row->vallen_ = vallen;
memcpy(row->s_, s_, cut);
for (auto it = changeset.begin(); it != last; ++it)
memcpy(row->s_ + it->index().f_off, it->value().data(),
it->value().length());
return row;
}
template <typename CS>
inline value_string* value_string::create(const CS& changeset,
kvtimestamp_t ts,
threadinfo& ti) {
value_string empty;
return empty.update(changeset, 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_row_str);
row->ts_ = ts;
row->vallen_ = value.length();
memcpy(row->s_, value.data(), value.length());
return row;
}
template <typename CS>
inline void value_string::deallocate_rcu_after_update(const CS&, threadinfo& ti) {
deallocate_rcu(ti);
}
template <typename CS>
inline void value_string::deallocate_after_failed_update(const CS&, threadinfo& ti) {
deallocate(ti);
}
inline value_string* value_string::checkpoint_read(Str str,
kvtimestamp_t ts,
threadinfo& ti) {
return create1(str, ts, ti);
}
inline void value_string::checkpoint_write(kvout* kv) const {
KVW(kv, Str(s_, vallen_));
}
#endif