-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathsplit_mc.c
80 lines (69 loc) · 2.24 KB
/
split_mc.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include "split_mc.h"
memcached_return split_mc_set(struct memcached_st *mc, char *key, size_t key_len, void *val,
size_t bytes, time_t expire, uint32_t flags)
{
char chunk_key[250];
int i;
memcached_return retval;
int r;
size_t chunk_bytes;
/* assert bytes > CHUNK_SIZE */
if (key_len > 200 || bytes > CHUNK_SIZE * 10) return MEMCACHED_KEY_TOO_BIG;
for (i=0; bytes>0; i++) {
r = snprintf(chunk_key, 250, "~%zu%s/%d", key_len, key, i);
if (r < 0) {
return MEMCACHED_BAD_KEY_PROVIDED;
} else if (r >= 250) {
return MEMCACHED_KEY_TOO_BIG;
}
chunk_bytes = bytes > CHUNK_SIZE ? CHUNK_SIZE : bytes;
retval = memcached_set(mc, chunk_key, r, val, chunk_bytes,
expire, flags);
if (retval != MEMCACHED_SUCCESS && retval != MEMCACHED_TIMEOUT) {
return retval;
}
val += CHUNK_SIZE;
bytes -= chunk_bytes;
}
sprintf(chunk_key, "%d", i); /* re-use chunk_key as value buffer */
return memcached_set(mc, key, key_len, chunk_key, strlen(chunk_key)+1,
expire, flags|FLAG_CHUNKED);
}
/* It's up to the caller to free returned *val !!! */
char* split_mc_get(struct memcached_st *mc, char *key, size_t key_len,
int nchunks, size_t *bytes)
{
int i;
char *c_val, *r, *v;
memcached_return rc;
uint32_t flags;
size_t length;
char chunk_key[250];
/* assert res && res.val && (res.flags & FLAG_CHUNKED) */
if (nchunks > 10 || key_len > 200)
return NULL;
r = (char*)malloc(sizeof(char) * CHUNK_SIZE * nchunks);
if (!r) return NULL;
for (i=0, v=r; i<nchunks; i++) {
snprintf(chunk_key, 250, "~%zu%s/%d", key_len, key, i);
c_val = memcached_get(mc, chunk_key, strlen(chunk_key),
&length, &flags, &rc);
if(rc != MEMCACHED_SUCCESS || !c_val || length > CHUNK_SIZE) {
if (c_val) free(c_val);
goto error;
}
memcpy(v, c_val, length);
free(c_val);
v += length;
}
*bytes = v - r;
return r;
error:
memcached_delete(mc, key, key_len, 0);
free(r);
return NULL;
}