/
item.hh
371 lines (309 loc) · 8.6 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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* -*- 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"
#include "objectregistry.hh"
#include "stats.hh"
/**
* A blob is a minimal sized storage for data up to 2^32 bytes long.
*/
class Blob : public RCValue {
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
*
* @return the new Blob instance
*/
static Blob* New(const size_t len) {
size_t total_len = len + sizeof(Blob);
Blob *t = new (::operator new(total_len)) Blob(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 value.
*/
size_t length() const {
return size;
}
/**
* Get the size of this Blob instance.
*/
size_t getSize() const {
return size + sizeof(Blob);
}
/**
* 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); }
~Blob() {
ObjectRegistry::onDeleteBlob(this);
}
private:
explicit Blob(const char *start, const size_t len) :
size(static_cast<uint32_t>(len))
{
std::memcpy(data, start, len);
ObjectRegistry::onCreateBlob(this);
}
explicit Blob(const size_t len) :
size(static_cast<uint32_t>(len))
{
#ifdef VALGRIND
memset(data, 0, len);
#endif
ObjectRegistry::onCreateBlob(this);
}
const uint32_t size;
char data[1];
DISALLOW_COPY_AND_ASSIGN(Blob);
};
typedef SingleThreadedRCPtr<Blob> value_t;
const uint64_t DEFAULT_REV_SEQ_NUM = 1;
/**
* The ItemMetaData structure is used to pass meata data information of
* an Item.
*/
class ItemMetaData {
public:
ItemMetaData() :
cas(0), seqno(DEFAULT_REV_SEQ_NUM), flags(0), exptime(0) {
}
ItemMetaData(uint64_t c, uint32_t s, uint32_t f, time_t e) :
cas(c), seqno(s == 0 ? DEFAULT_REV_SEQ_NUM : s), flags(f), exptime(e) {
}
uint64_t cas;
uint64_t seqno;
uint32_t flags;
time_t exptime;
};
/**
* 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 uint32_t fl, const time_t exp, uint64_t theCas = 0,
int64_t i = -1, uint16_t vbid = 0) :
metaData(theCas, 1, fl, exp), id(i), vbucketId(vbid)
{
key.assign(static_cast<const char*>(k), nk);
assert(id != 0);
setData(NULL, nb);
ObjectRegistry::onCreateItem(this);
}
Item(const std::string &k, const uint32_t fl, const time_t exp,
const void *dta, const size_t nb, uint64_t theCas = 0,
int64_t i = -1, uint16_t vbid = 0) :
metaData(theCas, 1, fl, exp), id(i), vbucketId(vbid)
{
key.assign(k);
assert(id != 0);
setData(static_cast<const char*>(dta), nb);
ObjectRegistry::onCreateItem(this);
}
Item(const std::string &k, const uint32_t fl, const time_t exp,
const value_t &val, uint64_t theCas = 0, int64_t i = -1, uint16_t vbid = 0,
uint64_t sno = 1) :
metaData(theCas, sno, fl, exp), value(val), id(i), vbucketId(vbid)
{
assert(id != 0);
key.assign(k);
ObjectRegistry::onCreateItem(this);
}
Item(const void *k, uint16_t nk, const uint32_t fl, const time_t exp,
const void *dta, const size_t nb, uint64_t theCas = 0,
int64_t i = -1, uint16_t vbid = 0, uint64_t sno = 1) :
metaData(theCas, sno, fl, exp), id(i), vbucketId(vbid)
{
assert(id != 0);
key.assign(static_cast<const char*>(k), nk);
setData(static_cast<const char*>(dta), nb);
ObjectRegistry::onCreateItem(this);
}
~Item() {
ObjectRegistry::onDeleteItem(this);
}
const char *getData() const {
return value.get() ? value->getData() : NULL;
}
const value_t &getValue() const {
return value;
}
const std::string &getKey() const {
return key;
}
int64_t getId() const {
return id;
}
void setId(int64_t to) {
id = to;
}
int getNKey() const {
return static_cast<int>(key.length());
}
uint32_t getNBytes() const {
return value.get() ? static_cast<uint32_t>(value->length()) : 0;
}
size_t getValMemSize() const {
return value.get() ? value->getSize() : 0;
}
time_t getExptime() const {
return metaData.exptime;
}
uint32_t getFlags() const {
return metaData.flags;
}
uint64_t getCas() const {
return metaData.cas;
}
void setCas() {
metaData.cas = nextCas();
}
void setCas(uint64_t ncas) {
metaData.cas = ncas;
}
void setValue(const value_t &v) {
value.reset(v);
}
void setFlags(uint32_t f) {
metaData.flags = f;
}
void setExpTime(time_t exp_time) {
metaData.exptime = exp_time;
}
/**
* 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 (metaData.exptime != 0 && metaData.exptime < asOf) {
return true;
}
return false;
}
size_t size() {
return sizeof(Item) + key.size() + getValMemSize();
}
uint64_t getSeqno() const {
return metaData.seqno;
}
void setSeqno(uint64_t to) {
if (to == 0) {
to = DEFAULT_REV_SEQ_NUM;
}
metaData.seqno = to;
}
static uint32_t getNMetaBytes() {
return metaDataSize;
}
const ItemMetaData& getMetaData() const {
return metaData;
}
static uint64_t nextCas(void) {
uint64_t ret = gethrtime();
if ((ret & 1000) == 0) {
// we don't have a good enough resolution on the clock
ret |= casCounter++;
if (casCounter > 1000) {
casCounter = 1;
}
}
return ret;
}
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);
} else {
data = Blob::New(dta, nb);
}
assert(data);
value.reset(data);
}
ItemMetaData metaData;
value_t value;
std::string key;
int64_t id;
uint16_t vbucketId;
static Atomic<uint64_t> casCounter;
static const uint32_t metaDataSize;
DISALLOW_COPY_AND_ASSIGN(Item);
};
#endif