Deprecate MEMCACHED_NO_KEY_PROVIDED, and fixed key validation tests for the binary...
[awesomized/libmemcached] / libmemcached / memcached_delete.c
1 #include "common.h"
2
3 memcached_return memcached_delete(memcached_st *ptr, const char *key, size_t key_length,
4 time_t expiration)
5 {
6 return memcached_delete_by_key(ptr, key, key_length,
7 key, key_length, expiration);
8 }
9
10 static inline memcached_return binary_delete(memcached_st *ptr,
11 unsigned int server_key,
12 const char *key,
13 size_t key_length,
14 int flush);
15
16 memcached_return memcached_delete_by_key(memcached_st *ptr,
17 const char *master_key, size_t master_key_length,
18 const char *key, size_t key_length,
19 time_t expiration)
20 {
21 char to_write;
22 size_t send_length;
23 memcached_return rc;
24 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
25 unsigned int server_key;
26
27 LIBMEMCACHED_MEMCACHED_DELETE_START();
28
29 rc= memcached_validate_key_length(key_length,
30 ptr->flags & MEM_BINARY_PROTOCOL);
31 unlikely (rc != MEMCACHED_SUCCESS)
32 return rc;
33
34 unlikely (ptr->hosts == NULL || ptr->number_of_hosts == 0)
35 return MEMCACHED_NO_SERVERS;
36
37 server_key= memcached_generate_hash(ptr, master_key, master_key_length);
38 to_write= (ptr->flags & MEM_BUFFER_REQUESTS) ? 0 : 1;
39
40 if (ptr->flags & MEM_BINARY_PROTOCOL)
41 rc= binary_delete(ptr, server_key, key, key_length, to_write);
42 else
43 {
44 if (expiration)
45 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
46 "delete %s%.*s %u\r\n",
47 ptr->prefix_key,
48 (int)key_length, key,
49 (uint32_t)expiration);
50 else
51 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
52 "delete %s%.*s\r\n",
53 ptr->prefix_key,
54 (int)key_length, key);
55
56 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
57 {
58 rc= MEMCACHED_WRITE_FAILURE;
59 goto error;
60 }
61
62 rc= memcached_do(&ptr->hosts[server_key], buffer, send_length, to_write);
63 }
64
65 if (rc != MEMCACHED_SUCCESS)
66 goto error;
67
68 if ((ptr->flags & MEM_BUFFER_REQUESTS))
69 {
70 rc= MEMCACHED_BUFFERED;
71 }
72 else
73 {
74 rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
75 if (rc == MEMCACHED_DELETED)
76 rc= MEMCACHED_SUCCESS;
77 }
78
79 if (rc == MEMCACHED_SUCCESS && ptr->delete_trigger)
80 ptr->delete_trigger(ptr, key, key_length);
81
82 error:
83 LIBMEMCACHED_MEMCACHED_DELETE_END();
84 return rc;
85 }
86
87 static inline memcached_return binary_delete(memcached_st *ptr,
88 unsigned int server_key,
89 const char *key,
90 size_t key_length,
91 int flush)
92 {
93 protocol_binary_request_delete request= {.bytes= {0}};
94
95 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
96 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETE;
97 request.message.header.request.keylen= htons((uint16_t)key_length);
98 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
99 request.message.header.request.bodylen= htonl(key_length);
100
101 if ((memcached_do(&ptr->hosts[server_key], request.bytes,
102 sizeof(request.bytes), 0) != MEMCACHED_SUCCESS) ||
103 (memcached_io_write(&ptr->hosts[server_key], key,
104 key_length, flush) == -1))
105 {
106 memcached_io_reset(&ptr->hosts[server_key]);
107 return MEMCACHED_WRITE_FAILURE;
108 }
109
110 return MEMCACHED_SUCCESS;
111 }