Turns out the protocol docs were in error, and that the size for 1.2 was a
[awesomized/libmemcached] / lib / memcached_storage.c
1 /*
2 Memcached library
3
4 memcached_set()
5 memcached_replace()
6 memcached_add()
7
8 */
9
10 #include "common.h"
11 #include "memcached_io.h"
12
13 typedef enum {
14 SET_OP,
15 REPLACE_OP,
16 ADD_OP,
17 PREPEND_OP,
18 APPEND_OP,
19 CAS_OP,
20 } memcached_storage_action;
21
22 /* Inline this */
23 char *storage_op_string(memcached_storage_action verb)
24 {
25 switch (verb)
26 {
27 case SET_OP:
28 return "set";
29 case REPLACE_OP:
30 return "replace";
31 case ADD_OP:
32 return "add";
33 case PREPEND_OP:
34 return "prepend";
35 case APPEND_OP:
36 return "append";
37 case CAS_OP:
38 return "cas";
39 };
40
41 return SET_OP;
42 }
43
44 static inline memcached_return memcached_send(memcached_st *ptr,
45 char *master_key, size_t master_key_length,
46 char *key, size_t key_length,
47 char *value, size_t value_length,
48 time_t expiration,
49 uint32_t flags,
50 uint64_t cas,
51 memcached_storage_action verb)
52 {
53 char to_write;
54 size_t write_length;
55 ssize_t sent_length;
56 memcached_return rc;
57 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
58 unsigned int server_key;
59
60 WATCHPOINT_ASSERT(!(value == NULL && value_length > 0));
61 WATCHPOINT_ASSERT(!(value && value_length == 0));
62
63 if (key_length == 0)
64 return MEMCACHED_NO_KEY_PROVIDED;
65
66 if (ptr->number_of_hosts == 0)
67 return MEMCACHED_NO_SERVERS;
68
69 server_key= memcached_generate_hash(ptr, master_key, master_key_length);
70
71 if (cas)
72 write_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
73 "%s %.*s %u %llu %zu %llu\r\n", storage_op_string(verb),
74 (int)key_length, key, flags,
75 (unsigned long long)expiration, value_length,
76 (unsigned long long)cas);
77 else
78 write_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
79 "%s %.*s %u %llu %zu\r\n", storage_op_string(verb),
80 (int)key_length, key, flags,
81 (unsigned long long)expiration, value_length);
82
83 if (write_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
84 {
85 rc= MEMCACHED_WRITE_FAILURE;
86 goto error;
87 }
88
89 rc= memcached_do(ptr, server_key, buffer, write_length, 0);
90 if (rc != MEMCACHED_SUCCESS)
91 goto error;
92
93 if ((sent_length= memcached_io_write(ptr, server_key, value, value_length, 0)) == -1)
94 {
95 rc= MEMCACHED_WRITE_FAILURE;
96 goto error;
97 }
98
99 if ((ptr->flags & MEM_NO_BLOCK) && verb == SET_OP)
100 to_write= 0;
101 else
102 to_write= 1;
103
104 if ((sent_length= memcached_io_write(ptr, server_key, "\r\n", 2, to_write)) == -1)
105 {
106 rc= MEMCACHED_WRITE_FAILURE;
107 goto error;
108 }
109
110 if (to_write == 0)
111 {
112 rc= MEMCACHED_SUCCESS;
113 memcached_server_response_increment(ptr, server_key);
114 }
115 else
116 {
117 rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
118 }
119 if (rc == MEMCACHED_STORED)
120 return MEMCACHED_SUCCESS;
121 else
122 return rc;
123
124 error:
125 memcached_io_reset(ptr, server_key);
126
127 return rc;
128 }
129
130 memcached_return memcached_set(memcached_st *ptr, char *key, size_t key_length,
131 char *value, size_t value_length,
132 time_t expiration,
133 uint32_t flags)
134 {
135 memcached_return rc;
136 LIBMEMCACHED_MEMCACHED_SET_START();
137 rc= memcached_send(ptr, key, key_length,
138 key, key_length, value, value_length,
139 expiration, flags, 0, SET_OP);
140 LIBMEMCACHED_MEMCACHED_SET_END();
141 return rc;
142 }
143
144 memcached_return memcached_add(memcached_st *ptr,
145 char *key, size_t key_length,
146 char *value, size_t value_length,
147 time_t expiration,
148 uint32_t flags)
149 {
150 memcached_return rc;
151 LIBMEMCACHED_MEMCACHED_ADD_START();
152 rc= memcached_send(ptr, key, key_length,
153 key, key_length, value, value_length,
154 expiration, flags, 0, ADD_OP);
155 LIBMEMCACHED_MEMCACHED_ADD_END();
156 return rc;
157 }
158
159 memcached_return memcached_replace(memcached_st *ptr,
160 char *key, size_t key_length,
161 char *value, size_t value_length,
162 time_t expiration,
163 uint32_t flags)
164 {
165 memcached_return rc;
166 LIBMEMCACHED_MEMCACHED_REPLACE_START();
167 rc= memcached_send(ptr, key, key_length,
168 key, key_length, value, value_length,
169 expiration, flags, 0, REPLACE_OP);
170 LIBMEMCACHED_MEMCACHED_REPLACE_END();
171 return rc;
172 }
173
174 memcached_return memcached_prepend(memcached_st *ptr,
175 char *key, size_t key_length,
176 char *value, size_t value_length,
177 time_t expiration,
178 uint32_t flags)
179 {
180 memcached_return rc;
181 rc= memcached_send(ptr, key, key_length,
182 key, key_length, value, value_length,
183 expiration, flags, 0, PREPEND_OP);
184 return rc;
185 }
186
187 memcached_return memcached_append(memcached_st *ptr,
188 char *key, size_t key_length,
189 char *value, size_t value_length,
190 time_t expiration,
191 uint32_t flags)
192 {
193 memcached_return rc;
194 rc= memcached_send(ptr, key, key_length,
195 key, key_length, value, value_length,
196 expiration, flags, 0, APPEND_OP);
197 return rc;
198 }
199
200 memcached_return memcached_cas(memcached_st *ptr,
201 char *key, size_t key_length,
202 char *value, size_t value_length,
203 time_t expiration,
204 uint32_t flags,
205 uint64_t cas)
206 {
207 memcached_return rc;
208 rc= memcached_send(ptr, key, key_length,
209 key, key_length, value, value_length,
210 expiration, flags, cas, APPEND_OP);
211 return rc;
212 }
213
214 memcached_return memcached_set_by_key(memcached_st *ptr,
215 char *master_key, size_t master_key_length,
216 char *key, size_t key_length,
217 char *value, size_t value_length,
218 time_t expiration,
219 uint32_t flags)
220 {
221 memcached_return rc;
222 LIBMEMCACHED_MEMCACHED_SET_START();
223 rc= memcached_send(ptr, key, key_length,
224 key, key_length, value, value_length,
225 expiration, flags, 0, SET_OP);
226 LIBMEMCACHED_MEMCACHED_SET_END();
227 return rc;
228 }
229
230 memcached_return memcached_add_by_key(memcached_st *ptr,
231 char *master_key, size_t master_key_length,
232 char *key, size_t key_length,
233 char *value, size_t value_length,
234 time_t expiration,
235 uint32_t flags)
236 {
237 memcached_return rc;
238 LIBMEMCACHED_MEMCACHED_ADD_START();
239 rc= memcached_send(ptr, key, key_length,
240 key, key_length, value, value_length,
241 expiration, flags, 0, ADD_OP);
242 LIBMEMCACHED_MEMCACHED_ADD_END();
243 return rc;
244 }
245
246 memcached_return memcached_replace_by_key(memcached_st *ptr,
247 char *master_key, size_t master_key_length,
248 char *key, size_t key_length,
249 char *value, size_t value_length,
250 time_t expiration,
251 uint32_t flags)
252 {
253 memcached_return rc;
254 LIBMEMCACHED_MEMCACHED_REPLACE_START();
255 rc= memcached_send(ptr, key, key_length,
256 key, key_length, value, value_length,
257 expiration, flags, 0, REPLACE_OP);
258 LIBMEMCACHED_MEMCACHED_REPLACE_END();
259 return rc;
260 }
261
262 memcached_return memcached_prepend_by_key(memcached_st *ptr,
263 char *master_key, size_t master_key_length,
264 char *key, size_t key_length,
265 char *value, size_t value_length,
266 time_t expiration,
267 uint32_t flags)
268 {
269 memcached_return rc;
270 rc= memcached_send(ptr, key, key_length,
271 key, key_length, value, value_length,
272 expiration, flags, 0, PREPEND_OP);
273 return rc;
274 }
275
276 memcached_return memcached_append_by_key(memcached_st *ptr,
277 char *master_key, size_t master_key_length,
278 char *key, size_t key_length,
279 char *value, size_t value_length,
280 time_t expiration,
281 uint32_t flags)
282 {
283 memcached_return rc;
284 rc= memcached_send(ptr, key, key_length,
285 key, key_length, value, value_length,
286 expiration, flags, 0, APPEND_OP);
287 return rc;
288 }
289
290 memcached_return memcached_cas_by_key(memcached_st *ptr,
291 char *master_key, size_t master_key_length,
292 char *key, size_t key_length,
293 char *value, size_t value_length,
294 time_t expiration,
295 uint32_t flags,
296 uint64_t cas)
297 {
298 memcached_return rc;
299 rc= memcached_send(ptr, key, key_length,
300 key, key_length, value, value_length,
301 expiration, flags, cas, APPEND_OP);
302 return rc;
303 }