-
Notifications
You must be signed in to change notification settings - Fork 1
/
item.c
393 lines (360 loc) · 10.1 KB
/
item.c
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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
/*
* MemcacheDB - A distributed key-value storage system designed for persistent:
*
* http://memcachedb.googlecode.com
*
* The source code of Memcachedb is most based on Memcached:
*
* http://danga.com/memcached/
*
* Copyright 2008 Steve Chu. All rights reserved.
*
* Use and distribution licensed under the BSD license. See
* the LICENSE file for full text.
*
* Authors:
* Steve Chu <[email protected]>
*
*/
#include "memcachedb.h"
#include <stdio.h>
#include <errno.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <pthread.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdlib.h>
#define MAX_ITEM_FREELIST_LENGTH 4000
#define INIT_ITEM_FREELIST_LENGTH 500
static size_t item_make_header(const uint8_t nkey, const int flags, const int nbytes, char *suffix, uint8_t *nsuffix);
static item **freeitem;
static int freeitemtotal;
static int freeitemcurr;
void item_init(void) {
freeitemtotal = INIT_ITEM_FREELIST_LENGTH;
freeitemcurr = 0;
freeitem = (item **)malloc( sizeof(item *) * freeitemtotal );
if (freeitem == NULL) {
perror("malloc()");
}
return;
}
/*
* Returns a item buffer from the freelist, if any. Sholud call
* item_from_freelist for thread safty.
* */
item *do_item_from_freelist(void) {
item *s;
if (freeitemcurr > 0) {
s = freeitem[--freeitemcurr];
} else {
/* If malloc fails, let the logic fall through without spamming
* STDERR on the server. */
s = (item *)malloc( settings.item_buf_size );
if (s != NULL){
memset(s, 0, settings.item_buf_size);
}
}
return s;
}
/*
* Adds a item to the freelist. Should call
* item_add_to_freelist for thread safty.
*/
int do_item_add_to_freelist(item *it) {
if (freeitemcurr < freeitemtotal) {
freeitem[freeitemcurr++] = it;
return 0;
} else {
if (freeitemtotal >= MAX_ITEM_FREELIST_LENGTH){
return 1;
}
/* try to enlarge free item buffer array */
item **new_freeitem = (item **)realloc(freeitem, sizeof(item *) * freeitemtotal * 2);
if (new_freeitem) {
freeitemtotal *= 2;
freeitem = new_freeitem;
freeitem[freeitemcurr++] = it;
return 0;
}
}
return 1;
}
/**
* Generates the variable-sized part of the header for an object.
*
* key - The key
* nkey - The length of the key
* flags - key flags
* nbytes - Number of bytes to hold value and addition CRLF terminator
* suffix - Buffer for the "VALUE" line suffix (flags, size).
* nsuffix - The length of the suffix is stored here.
*
* Returns the total size of the header.
*/
static size_t item_make_header(const uint8_t nkey, const int flags, const int nbytes,
char *suffix, uint8_t *nsuffix) {
/* suffix is defined at 40 chars elsewhere.. */
*nsuffix = (uint8_t) snprintf(suffix, 40, " %d %d\r\n", flags, nbytes - 2);
return sizeof(item) + nkey + *nsuffix + nbytes;
}
/*
* alloc a item buffer, and init it.
*/
item *item_alloc1(char *key, const size_t nkey, const int flags, const int nbytes) {
uint8_t nsuffix;
item *it;
char suffix[40];
size_t ntotal = item_make_header(nkey + 1, flags, nbytes, suffix, &nsuffix);
if (ntotal > settings.item_buf_size){
it = (item *)malloc(ntotal);
if (it == NULL){
return NULL;
}
memset(it, 0, ntotal);
if (settings.verbose > 1) {
fprintf(stderr, "alloc a item buffer from malloc.\n");
}
}else{
it = item_from_freelist();
if (it == NULL){
return NULL;
}
if (settings.verbose > 1) {
fprintf(stderr, "alloc a item buffer from freelist.\n");
}
}
it->nkey = nkey;
it->nbytes = nbytes;
strcpy(ITEM_key(it), key);
memcpy(ITEM_suffix(it), suffix, (size_t)nsuffix);
it->nsuffix = nsuffix;
return it;
}
/*
* alloc a item buffer only.
*/
item *item_alloc2(size_t ntotal) {
item *it;
if (ntotal > settings.item_buf_size){
it = (item *)malloc(ntotal);
if (it == NULL){
return NULL;
}
memset(it, 0, ntotal);
if (settings.verbose > 1) {
fprintf(stderr, "alloc a item buffer from malloc.\n");
}
}else{
it = item_from_freelist();
if (it == NULL){
return NULL;
}
if (settings.verbose > 1) {
fprintf(stderr, "alloc a item buffer from freelist.\n");
}
}
return it;
}
/*
* free a item buffer. here 'it' must be a full item.
*/
int item_free(item *it) {
size_t ntotal = 0;
if (NULL == it)
return 0;
/* ntotal may be wrong, if 'it' is not a full item. */
ntotal = ITEM_ntotal(it);
if (ntotal > settings.item_buf_size){
if (settings.verbose > 1) {
fprintf(stderr, "ntotal: %"PRIuS", use free() directly.\n", ntotal);
}
free(it);
}else{
if (0 != item_add_to_freelist(it)) {
if (settings.verbose > 1) {
fprintf(stderr, "ntotal: %"PRIuS", add a item buffer to freelist fail, use free() directly.\n", ntotal);
}
free(it);
}else{
if (settings.verbose > 1) {
fprintf(stderr, "ntotal: %"PRIuS", add a item buffer to freelist.\n", ntotal);
}
}
}
return 0;
}
/* if return item is not NULL, free by caller */
item *item_get(char *key, size_t nkey){
item *it = NULL;
DBT dbkey, dbdata;
bool stop;
int ret;
/* first, alloc a fixed size */
it = item_alloc2(settings.item_buf_size);
if (it == 0) {
return NULL;
}
BDB_CLEANUP_DBT();
dbkey.data = key;
dbkey.size = nkey;
dbdata.ulen = settings.item_buf_size;
dbdata.data = it;
dbdata.flags = DB_DBT_USERMEM;
stop = false;
/* try to get a item from bdb */
while (!stop) {
switch (ret = dbp->get(dbp, NULL, &dbkey, &dbdata, 0)) {
case DB_BUFFER_SMALL: /* user mem small */
/* free the original smaller buffer */
item_free(it);
/* alloc the correct size */
it = item_alloc2(dbdata.size);
if (it == NULL) {
return NULL;
}
dbdata.ulen = dbdata.size;
dbdata.data = it;
break;
case 0: /* Success. */
stop = true;
break;
case DB_NOTFOUND:
stop = true;
item_free(it);
it = NULL;
break;
default:
/* TODO: may cause bug here, if return DB_BUFFER_SMALL then retun non-zero again
* here 'it' may not a full one. a item buffer larger than item_buf_size may be added to freelist */
stop = true;
item_free(it);
it = NULL;
if (settings.verbose > 1) {
fprintf(stderr, "dbp->get: %s\n", db_strerror(ret));
}
}
}
return it;
}
item *item_cget(DBC *cursorp, char *start, size_t nstart, u_int32_t flags){
item *it = NULL;
DBT dbkey, dbdata;
bool stop;
int ret;
/* first, alloc a fixed size */
it = item_alloc2(settings.item_buf_size);
if (it == 0) {
return NULL;
}
BDB_CLEANUP_DBT();
dbkey.data = start;
dbkey.size = nstart;
dbkey.dlen = 0;
dbkey.doff = 0;
dbkey.flags = DB_DBT_PARTIAL;
dbdata.ulen = settings.item_buf_size;
dbdata.data = it;
dbdata.flags = DB_DBT_USERMEM;
stop = false;
/* try to get a item from bdb */
while (!stop) {
switch (ret = cursorp->get(cursorp, &dbkey, &dbdata, flags)) {
case DB_BUFFER_SMALL: /* user mem small */
if (settings.verbose > 1) {
fprintf(stderr, "cursorp->get: %s\n", db_strerror(ret));
}
/* free the original smaller buffer */
item_free(it);
/* alloc the correct size */
it = item_alloc2(dbdata.size);
if (it == NULL) {
return NULL;
}
dbkey.data = start;
dbkey.size = nstart;
dbdata.ulen = dbdata.size;
dbdata.data = it;
break;
case 0: /* Success. */
stop = true;
break;
case DB_NOTFOUND:
stop = true;
item_free(it);
it = NULL;
break;
default:
/* TODO: may cause bug here, if return DB_BUFFER_SMALL then retun non-zero again
* here 'it' may not a full one. a item buffer larger than item_buf_size may be added to freelist */
stop = true;
item_free(it);
it = NULL;
if (settings.verbose > 1) {
fprintf(stderr, "cursorp->get: %s\n", db_strerror(ret));
}
}
}
return it;
}
/* 0 for Success
-1 for SERVER_ERROR
*/
int item_put(char *key, size_t nkey, item *it){
int ret;
DBT dbkey, dbdata;
BDB_CLEANUP_DBT();
dbkey.data = key;
dbkey.size = nkey;
dbdata.data = it;
dbdata.size = ITEM_ntotal(it);
ret = dbp->put(dbp, NULL, &dbkey, &dbdata, 0);
if (ret == 0) {
return 0;
} else {
if (settings.verbose > 1) {
fprintf(stderr, "dbp->put: %s\n", db_strerror(ret));
}
return -1;
}
}
/* 0 for Success
1 for NOT_FOUND
-1 for SERVER_ERROR
*/
int item_delete(char *key, size_t nkey){
int ret;
DBT dbkey;
memset(&dbkey, 0, sizeof(dbkey));
dbkey.data = key;
dbkey.size = nkey;
ret = dbp->del(dbp, NULL, &dbkey, 0);
if (ret == 0){
return 0;
}else if(ret == DB_NOTFOUND){
return 1;
}else{
if (settings.verbose > 1) {
fprintf(stderr, "dbp->del: %s\n", db_strerror(ret));
}
return -1;
}
}
/*
1 for exists
0 for non-exist
*/
int item_exists(char *key, size_t nkey){
int ret;
DBT dbkey;
memset(&dbkey, 0, sizeof(dbkey));
dbkey.data = key;
dbkey.size = nkey;
ret = dbp->exists(dbp, NULL, &dbkey, 0);
if (ret == 0){
return 1;
}
return 0;
}