forked from membase/ep-engine
/
item.hh
286 lines (239 loc) · 6.86 KB
/
item.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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
#ifndef ITEM_HH
#define ITEM_HH
#include "config.h"
#include <string>
#include <string.h>
#include <stdio.h>
#include <memcached/engine.h>
#include "mutex.hh"
#include "locks.hh"
#include "atomic.hh"
/**
* A blob is a minimal sized storage for data up to 2^32 bytes long.
*/
class Blob {
public:
// Constructors.
/**
* Create a new Blob holding the given data.
*
* @param start the beginning of the data to copy into this blob
* @param len the amount of data to copy in
*
* @return the new Blob instance
*/
static Blob* New(const char *start, const size_t len) {
size_t total_len = len + sizeof(Blob);
Blob *t = new (::operator new(total_len)) Blob(start, len);
assert(t->length() == len);
return t;
}
/**
* Create a new Blob holding the contents of the given string.
*
* @param s the string whose contents go into the blob
*
* @return the new Blob instance
*/
static Blob* New(const std::string& s) {
return New(s.data(), s.length());
}
/**
* Create a new Blob pre-filled with the given character.
*
* @param len the size of the blob
* @param c the character to fill the blob with
*
* @return the new Blob instance
*/
static Blob* New(const size_t len, const char c) {
size_t total_len = len + sizeof(Blob);
Blob *t = new (::operator new(total_len)) Blob(c, len);
assert(t->length() == len);
return t;
}
// Actual accessorish things.
/**
* Get the pointer to the contents of this Blob.
*/
const char* getData() const {
return data;
}
/**
* Get the length of this Blob.
*/
size_t length() const {
return size;
}
/**
* Get a std::string representation of this blob.
*/
const std::string to_s() const {
return std::string(data, size);
}
// This is necessary for making C++ happy when I'm doing a
// placement new on fairly "normal" c++ heap allocations, just
// with variable-sized objects.
void operator delete(void* p) { ::operator delete(p); }
private:
explicit Blob(const char *start, const size_t len) : size(static_cast<uint32_t>(len)) {
std::memcpy(data, start, len);
}
explicit Blob(const char c, const size_t len) : size(static_cast<uint32_t>(len)) {
std::memset(data, c, len);
}
const uint32_t size;
char data[1];
DISALLOW_COPY_AND_ASSIGN(Blob);
};
typedef shared_ptr<const Blob> value_t;
/**
* The Item structure we use to pass information between the memcached
* core and the backend. Please note that the kvstore don't store these
* objects, so we do have an extra layer of memory copying :(
*/
class Item {
public:
Item(const void* k, const size_t nk, const size_t nb,
const int fl, const time_t exp, uint64_t theCas = 0,
int64_t i = -1, uint16_t vbid = 0) :
flags(fl), exptime(exp), cas(theCas), id(i), vbucketId(vbid)
{
key.assign(static_cast<const char*>(k), nk);
assert(id != 0);
setData(NULL, nb);
}
Item(const std::string &k, const int fl, const time_t exp,
const void *dta, const size_t nb, uint64_t theCas = 0,
int64_t i = -1, uint16_t vbid = 0) :
flags(fl), exptime(exp), cas(theCas), id(i), vbucketId(vbid)
{
key.assign(k);
assert(id != 0);
setData(static_cast<const char*>(dta), nb);
}
Item(const std::string &k, const int fl, const time_t exp,
value_t val, uint64_t theCas = 0, int64_t i = -1, uint16_t vbid = 0) :
flags(fl), exptime(exp), value(val), cas(theCas), id(i), vbucketId(vbid)
{
assert(id != 0);
key.assign(k);
}
Item(const void *k, uint16_t nk, const int fl, const time_t exp,
const void *dta, const size_t nb, uint64_t theCas = 0,
int64_t i = -1, uint16_t vbid = 0) :
flags(fl), exptime(exp), cas(theCas), id(i), vbucketId(vbid)
{
assert(id != 0);
key.assign(static_cast<const char*>(k), nk);
setData(static_cast<const char*>(dta), nb);
}
~Item() {}
const char *getData() const {
return value->getData();
}
value_t getValue() const {
return value;
}
const std::string &getKey() const {
return key;
}
int64_t getId() const {
return id;
}
int getNKey() const {
return static_cast<int>(key.length());
}
uint32_t getNBytes() const {
return static_cast<uint32_t>(value->length());
}
time_t getExptime() const {
return exptime;
}
int getFlags() const {
return flags;
}
uint64_t getCas() const {
return cas;
}
void setCas() {
cas = nextCas();
}
void setCas(uint64_t ncas) {
cas = ncas;
}
/**
* Append another item to this item
*
* @param item the item to append to this one
* @return true if success
*/
bool append(const Item &item);
/**
* Prepend another item to this item
*
* @param item the item to prepend to this one
* @return true if success
*/
bool prepend(const Item &item);
uint16_t getVBucketId(void) const {
return vbucketId;
}
void setVBucketId(uint16_t to) {
vbucketId = to;
}
/**
* Check if this item is expired or not.
*
* @param asOf the time to be compared with this item's expiry time
* @return true if this item's expiry time < asOf
*/
bool isExpired(time_t asOf) const {
if (getExptime() != 0 && getExptime() < asOf) {
return true;
}
return false;
}
private:
/**
* Set the item's data. This is only used by constructors, so we
* make it private.
*/
void setData(const char *dta, const size_t nb) {
Blob *data;
if (dta == NULL) {
data = Blob::New(nb, '\0');
} else {
data = Blob::New(dta, nb);
}
assert(data);
value.reset(data);
}
int flags;
time_t exptime;
std::string key;
value_t value;
uint64_t cas;
int64_t id;
uint16_t vbucketId;
static uint64_t nextCas(void) {
uint64_t ret;
ret = casCounter++;
if ((ret % casNotificationFrequency) == 0) {
casNotifier(ret);
}
return ret;
}
static void initializeCas(uint64_t initial, void (*notifier)(uint64_t current),
uint64_t frequency) {
casCounter = initial;
casNotifier = notifier;
casNotificationFrequency = frequency;
}
static uint64_t casNotificationFrequency;
static void (*casNotifier)(uint64_t);
static Atomic<uint64_t> casCounter;
DISALLOW_COPY_AND_ASSIGN(Item);
};
#endif