forked from kohler/masstree-beta
-
Notifications
You must be signed in to change notification settings - Fork 0
/
value_array.hh
147 lines (126 loc) · 5.16 KB
/
value_array.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
/* 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_ARRAY_HH
#define VALUE_ARRAY_HH
#include "compiler.hh"
#include "kvrow.hh"
class value_array : public row_base<short> {
public:
typedef short index_type;
static constexpr rowtype_id type_id = RowType_Array;
static const char *name() { return "Array"; }
inline value_array();
inline kvtimestamp_t timestamp() const;
inline int ncol() const;
inline Str col(int i) const;
void deallocate(threadinfo &ti);
void deallocate_rcu(threadinfo &ti);
template <typename CS>
value_array* update(const CS& changeset, kvtimestamp_t ts, threadinfo& ti) const;
template <typename CS>
static value_array* create(const CS& changeset, kvtimestamp_t ts, threadinfo& ti);
static inline value_array* create1(Str value, kvtimestamp_t ts, threadinfo& ti);
template <typename CS>
void deallocate_rcu_after_update(const CS& changeset, threadinfo& ti);
template <typename CS>
void deallocate_after_failed_update(const CS& changeset, threadinfo& ti);
static value_array* checkpoint_read(Str str, kvtimestamp_t ts,
threadinfo& ti);
void checkpoint_write(kvout* buf) 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 = ### @" PRIKVTSPARTS "%s\n", prefix, indent, "",
key.len, key.s, KVTS_HIGHPART(adj_ts), KVTS_LOWPART(adj_ts), suffix);
}
private:
kvtimestamp_t ts_;
short ncol_;
lcdf::inline_string* cols_[0];
static inline size_t shallow_size(int ncol);
inline size_t shallow_size() const;
static inline int count_columns(const change_t &c) {
// Changes are sorted by field! Cheers!
assert(c.size() && "Change can not be empty");
return c[c.size() - 1].c_fid + 1;
}
void update(const change_t &c, threadinfo &ti);
static value_array *make_sized_row(int ncol, kvtimestamp_t ts, threadinfo &ti);
};
inline value_array::value_array()
: ts_(0), ncol_(0) {
}
inline kvtimestamp_t value_array::timestamp() const {
return ts_;
}
inline int value_array::ncol() const {
return ncol_;
}
inline Str value_array::col(int i) const {
if (unsigned(i) < unsigned(ncol_))
return Str(cols_[i]->s, cols_[i]->len);
else
return Str();
}
inline size_t value_array::shallow_size(int ncol) {
return sizeof(value_array) + sizeof(lcdf::inline_string*) * ncol;
}
inline size_t value_array::shallow_size() const {
return shallow_size(ncol_);
}
template <typename CS>
value_array* value_array::update(const CS& changeset, kvtimestamp_t ts, threadinfo& ti) const {
precondition(ts >= ts_);
int ncol = std::max(int(ncol_), int(changeset.last_index()) + 1);
value_array* row = (value_array*) ti.allocate(shallow_size(ncol), memtag_row_array);
row->ts_ = ts;
row->ncol_ = ncol;
memcpy(row->cols_, cols_, ncol_ * sizeof(cols_[0]));
memset(row->cols_ + ncol_, 0, (ncol - ncol_) * sizeof(cols_[0]));
auto last = changeset.end();
for (auto it = changeset.begin(); it != last; ++it)
row->cols_[it->index()] = lcdf::inline_string::allocate(it->value(), ti);
return row;
}
template <typename CS>
value_array* value_array::create(const CS& changeset, kvtimestamp_t ts, threadinfo& ti) {
value_array empty;
return empty.update(changeset, ts, ti);
}
inline value_array* value_array::create1(Str value, kvtimestamp_t ts, threadinfo& ti) {
value_array* row = (value_array*) ti.allocate(shallow_size(1), memtag_row_array);
row->ts_ = ts;
row->ncol_ = 1;
row->cols_[0] = lcdf::inline_string::allocate(value, ti);
return row;
}
template <typename CS>
void value_array::deallocate_rcu_after_update(const CS& changeset, threadinfo& ti) {
auto last = changeset.end();
for (auto it = changeset.begin(); it != last && it->index() < ncol_; ++it)
if (cols_[it->index()])
cols_[it->index()]->deallocate_rcu(ti);
ti.deallocate_rcu(this, shallow_size(), memtag_row_array);
}
template <typename CS>
void value_array::deallocate_after_failed_update(const CS& changeset, threadinfo& ti) {
auto last = changeset.end();
for (auto it = changeset.begin(); it != last; ++it)
if (cols_[it->index()])
cols_[it->index()]->deallocate(ti);
ti.deallocate(this, shallow_size(), memtag_row_array);
}
#endif