Deprecate MEMCACHED_NO_KEY_PROVIDED, and fixed key validation tests for the binary...
[awesomized/libmemcached] / libmemcached / memcached_storage.c
1 /*
2 Memcached library
3
4 memcached_set()
5 memcached_replace()
6 memcached_add()
7
8 */
9 #include "common.h"
10 #include "memcached_io.h"
11
12 typedef enum {
13 SET_OP,
14 REPLACE_OP,
15 ADD_OP,
16 PREPEND_OP,
17 APPEND_OP,
18 CAS_OP,
19 } memcached_storage_action;
20
21 /* Inline this */
22 static char *storage_op_string(memcached_storage_action verb)
23 {
24 switch (verb)
25 {
26 case SET_OP:
27 return "set";
28 case REPLACE_OP:
29 return "replace";
30 case ADD_OP:
31 return "add";
32 case PREPEND_OP:
33 return "prepend";
34 case APPEND_OP:
35 return "append";
36 case CAS_OP:
37 return "cas";
38 default:
39 return "tosserror"; /* This is impossible, fixes issue for compiler warning in VisualStudio */
40 };
41
42 return SET_OP;
43 }
44
45 static memcached_return memcached_send_binary(memcached_server_st* server,
46 const char *key,
47 size_t key_length,
48 const char *value,
49 size_t value_length,
50 time_t expiration,
51 uint32_t flags,
52 uint64_t cas,
53 memcached_storage_action verb);
54
55 static inline memcached_return memcached_send(memcached_st *ptr,
56 const char *master_key, size_t master_key_length,
57 const char *key, size_t key_length,
58 const char *value, size_t value_length,
59 time_t expiration,
60 uint32_t flags,
61 uint64_t cas,
62 memcached_storage_action verb)
63 {
64 char to_write;
65 size_t write_length;
66 ssize_t sent_length;
67 memcached_return rc;
68 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
69 unsigned int server_key;
70
71 WATCHPOINT_ASSERT(!(value == NULL && value_length > 0));
72
73 rc= memcached_validate_key_length(key_length, ptr->flags & MEM_BINARY_PROTOCOL);
74 unlikely (rc != MEMCACHED_SUCCESS)
75 return rc;
76
77 unlikely (ptr->number_of_hosts == 0)
78 return MEMCACHED_NO_SERVERS;
79
80 if ((ptr->flags & MEM_VERIFY_KEY) && (memcachd_key_test((char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
81 return MEMCACHED_BAD_KEY_PROVIDED;
82
83 server_key= memcached_generate_hash(ptr, master_key, master_key_length);
84
85 if (ptr->flags & MEM_BINARY_PROTOCOL)
86 return memcached_send_binary(&ptr->hosts[server_key], key, key_length,
87 value, value_length, expiration,
88 flags, cas, verb);
89
90 if (cas)
91 write_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
92 "%s %s%.*s %u %llu %zu %llu\r\n", storage_op_string(verb),
93 ptr->prefix_key,
94 (int)key_length, key, flags,
95 (unsigned long long)expiration, value_length,
96 (unsigned long long)cas);
97 else
98 write_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
99 "%s %s%.*s %u %llu %zu\r\n", storage_op_string(verb),
100 ptr->prefix_key,
101 (int)key_length, key, flags,
102 (unsigned long long)expiration, value_length);
103
104 if (write_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
105 {
106 rc= MEMCACHED_WRITE_FAILURE;
107 goto error;
108 }
109
110 /* Send command header */
111 rc= memcached_do(&ptr->hosts[server_key], buffer, write_length, 0);
112 if (rc != MEMCACHED_SUCCESS)
113 goto error;
114
115 /* Send command body */
116 if ((sent_length= memcached_io_write(&ptr->hosts[server_key], value, value_length, 0)) == -1)
117 {
118 rc= MEMCACHED_WRITE_FAILURE;
119 goto error;
120 }
121
122 if ((ptr->flags & MEM_BUFFER_REQUESTS) && verb == SET_OP)
123 to_write= 0;
124 else
125 to_write= 1;
126
127 if ((sent_length= memcached_io_write(&ptr->hosts[server_key], "\r\n", 2, to_write)) == -1)
128 {
129 rc= MEMCACHED_WRITE_FAILURE;
130 goto error;
131 }
132
133 if (to_write == 0)
134 return MEMCACHED_BUFFERED;
135
136 rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
137
138 if (rc == MEMCACHED_STORED)
139 return MEMCACHED_SUCCESS;
140 else
141 return rc;
142
143 error:
144 memcached_io_reset(&ptr->hosts[server_key]);
145
146 return rc;
147 }
148
149
150 memcached_return memcached_set(memcached_st *ptr, const char *key, size_t key_length,
151 const char *value, size_t value_length,
152 time_t expiration,
153 uint32_t flags)
154 {
155 memcached_return rc;
156 LIBMEMCACHED_MEMCACHED_SET_START();
157 rc= memcached_send(ptr, key, key_length,
158 key, key_length, value, value_length,
159 expiration, flags, 0, SET_OP);
160 LIBMEMCACHED_MEMCACHED_SET_END();
161 return rc;
162 }
163
164 memcached_return memcached_add(memcached_st *ptr,
165 const char *key, size_t key_length,
166 const char *value, size_t value_length,
167 time_t expiration,
168 uint32_t flags)
169 {
170 memcached_return rc;
171 LIBMEMCACHED_MEMCACHED_ADD_START();
172 rc= memcached_send(ptr, key, key_length,
173 key, key_length, value, value_length,
174 expiration, flags, 0, ADD_OP);
175 LIBMEMCACHED_MEMCACHED_ADD_END();
176 return rc;
177 }
178
179 memcached_return memcached_replace(memcached_st *ptr,
180 const char *key, size_t key_length,
181 const char *value, size_t value_length,
182 time_t expiration,
183 uint32_t flags)
184 {
185 memcached_return rc;
186 LIBMEMCACHED_MEMCACHED_REPLACE_START();
187 rc= memcached_send(ptr, key, key_length,
188 key, key_length, value, value_length,
189 expiration, flags, 0, REPLACE_OP);
190 LIBMEMCACHED_MEMCACHED_REPLACE_END();
191 return rc;
192 }
193
194 memcached_return memcached_prepend(memcached_st *ptr,
195 const char *key, size_t key_length,
196 const char *value, size_t value_length,
197 time_t expiration,
198 uint32_t flags)
199 {
200 memcached_return rc;
201 rc= memcached_send(ptr, key, key_length,
202 key, key_length, value, value_length,
203 expiration, flags, 0, PREPEND_OP);
204 return rc;
205 }
206
207 memcached_return memcached_append(memcached_st *ptr,
208 const char *key, size_t key_length,
209 const char *value, size_t value_length,
210 time_t expiration,
211 uint32_t flags)
212 {
213 memcached_return rc;
214 rc= memcached_send(ptr, key, key_length,
215 key, key_length, value, value_length,
216 expiration, flags, 0, APPEND_OP);
217 return rc;
218 }
219
220 memcached_return memcached_cas(memcached_st *ptr,
221 const char *key, size_t key_length,
222 const char *value, size_t value_length,
223 time_t expiration,
224 uint32_t flags,
225 uint64_t cas)
226 {
227 memcached_return rc;
228 rc= memcached_send(ptr, key, key_length,
229 key, key_length, value, value_length,
230 expiration, flags, cas, CAS_OP);
231 return rc;
232 }
233
234 memcached_return memcached_set_by_key(memcached_st *ptr,
235 const char *master_key __attribute__((unused)),
236 size_t master_key_length __attribute__((unused)),
237 const char *key, size_t key_length,
238 const char *value, size_t value_length,
239 time_t expiration,
240 uint32_t flags)
241 {
242 memcached_return rc;
243 LIBMEMCACHED_MEMCACHED_SET_START();
244 rc= memcached_send(ptr, master_key, master_key_length,
245 key, key_length, value, value_length,
246 expiration, flags, 0, SET_OP);
247 LIBMEMCACHED_MEMCACHED_SET_END();
248 return rc;
249 }
250
251 memcached_return memcached_add_by_key(memcached_st *ptr,
252 const char *master_key, size_t master_key_length,
253 const char *key, size_t key_length,
254 const char *value, size_t value_length,
255 time_t expiration,
256 uint32_t flags)
257 {
258 memcached_return rc;
259 LIBMEMCACHED_MEMCACHED_ADD_START();
260 rc= memcached_send(ptr, master_key, master_key_length,
261 key, key_length, value, value_length,
262 expiration, flags, 0, ADD_OP);
263 LIBMEMCACHED_MEMCACHED_ADD_END();
264 return rc;
265 }
266
267 memcached_return memcached_replace_by_key(memcached_st *ptr,
268 const char *master_key, size_t master_key_length,
269 const char *key, size_t key_length,
270 const char *value, size_t value_length,
271 time_t expiration,
272 uint32_t flags)
273 {
274 memcached_return rc;
275 LIBMEMCACHED_MEMCACHED_REPLACE_START();
276 rc= memcached_send(ptr, master_key, master_key_length,
277 key, key_length, value, value_length,
278 expiration, flags, 0, REPLACE_OP);
279 LIBMEMCACHED_MEMCACHED_REPLACE_END();
280 return rc;
281 }
282
283 memcached_return memcached_prepend_by_key(memcached_st *ptr,
284 const char *master_key, size_t master_key_length,
285 const char *key, size_t key_length,
286 const char *value, size_t value_length,
287 time_t expiration,
288 uint32_t flags)
289 {
290 memcached_return rc;
291 rc= memcached_send(ptr, master_key, master_key_length,
292 key, key_length, value, value_length,
293 expiration, flags, 0, PREPEND_OP);
294 return rc;
295 }
296
297 memcached_return memcached_append_by_key(memcached_st *ptr,
298 const char *master_key, size_t master_key_length,
299 const char *key, size_t key_length,
300 const char *value, size_t value_length,
301 time_t expiration,
302 uint32_t flags)
303 {
304 memcached_return rc;
305 rc= memcached_send(ptr, master_key, master_key_length,
306 key, key_length, value, value_length,
307 expiration, flags, 0, APPEND_OP);
308 return rc;
309 }
310
311 memcached_return memcached_cas_by_key(memcached_st *ptr,
312 const char *master_key, size_t master_key_length,
313 const char *key, size_t key_length,
314 const char *value, size_t value_length,
315 time_t expiration,
316 uint32_t flags,
317 uint64_t cas)
318 {
319 memcached_return rc;
320 rc= memcached_send(ptr, master_key, master_key_length,
321 key, key_length, value, value_length,
322 expiration, flags, cas, CAS_OP);
323 return rc;
324 }
325
326 static memcached_return memcached_send_binary(memcached_server_st* server,
327 const char *key,
328 size_t key_length,
329 const char *value,
330 size_t value_length,
331 time_t expiration,
332 uint32_t flags,
333 uint64_t cas,
334 memcached_storage_action verb)
335 {
336 char flush;
337 protocol_binary_request_set request= {.bytes= {0}};
338 size_t send_length= sizeof(request.bytes);
339
340 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
341 switch (verb)
342 {
343 case SET_OP:
344 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_SET;
345 break;
346 case ADD_OP:
347 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_ADD;
348 break;
349 case REPLACE_OP:
350 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_REPLACE;
351 break;
352 case APPEND_OP:
353 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_APPEND;
354 break;
355 case PREPEND_OP:
356 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_PREPEND;
357 break;
358 case CAS_OP:
359 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_REPLACE;
360 break;
361 }
362
363 request.message.header.request.keylen= htons((uint16_t)key_length);
364 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
365 if (verb == APPEND_OP || verb == PREPEND_OP)
366 send_length -= 8; /* append & prepend does not contain extras! */
367 else
368 {
369 request.message.header.request.extlen= 8;
370 request.message.body.flags= htonl(flags);
371 request.message.body.expiration= htonl((uint32_t)expiration);
372 }
373
374 request.message.header.request.bodylen= htonl(key_length + value_length +
375 request.message.header.request.extlen);
376
377 if (cas)
378 request.message.header.request.cas= htonll(cas);
379
380 flush= ((server->root->flags & MEM_BUFFER_REQUESTS) && verb == SET_OP) ? 0 : 1;
381 /* write the header */
382 if ((memcached_do(server, (const char*)request.bytes, send_length, 0) != MEMCACHED_SUCCESS) ||
383 (memcached_io_write(server, key, key_length, 0) == -1) ||
384 (memcached_io_write(server, value, value_length, flush) == -1))
385 {
386 memcached_io_reset(server);
387 return MEMCACHED_WRITE_FAILURE;
388 }
389
390 if (flush == 0)
391 return MEMCACHED_BUFFERED;
392
393 return memcached_response(server, NULL, 0, NULL);
394 }
395