Fix problem with bumping the command counter _before_ connecting to the server
[awesomized/libmemcached] / libmemcached / delete.c
1 #include "common.h"
2 #include "memcached/protocol_binary.h"
3
4 memcached_return_t memcached_delete(memcached_st *ptr, const char *key, size_t key_length,
5 time_t expiration)
6 {
7 return memcached_delete_by_key(ptr, key, key_length,
8 key, key_length, expiration);
9 }
10
11 static inline memcached_return_t binary_delete(memcached_st *ptr,
12 unsigned int server_key,
13 const char *key,
14 size_t key_length,
15 uint8_t flush);
16
17 memcached_return_t memcached_delete_by_key(memcached_st *ptr,
18 const char *master_key, size_t master_key_length,
19 const char *key, size_t key_length,
20 time_t expiration)
21 {
22 uint8_t to_write;
23 size_t send_length;
24 memcached_return_t rc;
25 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
26 unsigned int server_key;
27
28 LIBMEMCACHED_MEMCACHED_DELETE_START();
29
30 rc= memcached_validate_key_length(key_length,
31 ptr->flags.binary_protocol);
32 unlikely (rc != MEMCACHED_SUCCESS)
33 return rc;
34
35 unlikely (ptr->hosts == NULL || ptr->number_of_hosts == 0)
36 return MEMCACHED_NO_SERVERS;
37
38 server_key= memcached_generate_hash(ptr, master_key, master_key_length);
39 to_write= (uint8_t)((ptr->flags.buffer_requests) ? 0 : 1);
40 bool no_reply= (ptr->flags.no_reply);
41
42 if (ptr->flags.binary_protocol)
43 {
44 likely (!expiration)
45 rc= binary_delete(ptr, server_key, key, key_length, to_write);
46 else
47 rc= MEMCACHED_INVALID_ARGUMENTS;
48 }
49 else
50 {
51 unlikely (expiration)
52 {
53 if ((ptr->hosts[server_key].major_version == 1 &&
54 ptr->hosts[server_key].minor_version > 2) ||
55 ptr->hosts[server_key].major_version > 1)
56 {
57 rc= MEMCACHED_INVALID_ARGUMENTS;
58 goto error;
59 }
60 else
61 {
62 /* ensure that we are connected, otherwise we might bump the
63 * command counter before connection */
64 if ((rc= memcached_connect(&ptr->hosts[server_key])) != MEMCACHED_SUCCESS)
65 {
66 WATCHPOINT_ERROR(rc);
67 return rc;
68 }
69
70 if (ptr->hosts[server_key].minor_version == 0)
71 {
72 if (no_reply || !to_write)
73 {
74 /* We might get out of sync with the server if we
75 * send this command to a server newer than 1.2.x..
76 * disable no_reply and buffered mode.
77 */
78 to_write= 1;
79 if (no_reply)
80 memcached_server_response_increment(&ptr->hosts[server_key]);
81 no_reply= false;
82 }
83 }
84 send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
85 "delete %s%.*s %u%s\r\n",
86 ptr->prefix_key,
87 (int) key_length, key,
88 (uint32_t)expiration,
89 no_reply ? " noreply" :"" );
90 }
91 }
92 else
93 send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
94 "delete %s%.*s%s\r\n",
95 ptr->prefix_key,
96 (int)key_length, key, no_reply ? " noreply" :"");
97
98 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
99 {
100 rc= MEMCACHED_WRITE_FAILURE;
101 goto error;
102 }
103
104 if (ptr->flags.use_udp && !to_write)
105 {
106 if (send_length > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
107 return MEMCACHED_WRITE_FAILURE;
108 if (send_length + ptr->hosts[server_key].write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
109 memcached_io_write(&ptr->hosts[server_key], NULL, 0, 1);
110 }
111
112 rc= memcached_do(&ptr->hosts[server_key], buffer, send_length, to_write);
113 }
114
115 if (rc != MEMCACHED_SUCCESS)
116 goto error;
117
118 if (!to_write)
119 rc= MEMCACHED_BUFFERED;
120 else if (!no_reply)
121 {
122 rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
123 if (rc == MEMCACHED_DELETED)
124 rc= MEMCACHED_SUCCESS;
125 }
126
127 if (rc == MEMCACHED_SUCCESS && ptr->delete_trigger)
128 ptr->delete_trigger(ptr, key, key_length);
129
130 error:
131 LIBMEMCACHED_MEMCACHED_DELETE_END();
132 return rc;
133 }
134
135 static inline memcached_return_t binary_delete(memcached_st *ptr,
136 unsigned int server_key,
137 const char *key,
138 size_t key_length,
139 uint8_t flush)
140 {
141 protocol_binary_request_delete request= {.bytes= {0}};
142
143 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
144 if (ptr->flags.no_reply)
145 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
146 else
147 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETE;
148 request.message.header.request.keylen= htons((uint16_t)key_length);
149 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
150 request.message.header.request.bodylen= htonl((uint32_t) key_length);
151
152 if (ptr->flags.use_udp && !flush)
153 {
154 size_t cmd_size= sizeof(request.bytes) + key_length;
155 if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
156 return MEMCACHED_WRITE_FAILURE;
157 if (cmd_size + ptr->hosts[server_key].write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
158 memcached_io_write(&ptr->hosts[server_key], NULL, 0, 1);
159 }
160
161 memcached_return_t rc= MEMCACHED_SUCCESS;
162
163 if ((memcached_do(&ptr->hosts[server_key], request.bytes,
164 sizeof(request.bytes), 0) != MEMCACHED_SUCCESS) ||
165 (memcached_io_write(&ptr->hosts[server_key], key,
166 key_length, (char) flush) == -1))
167 {
168 memcached_io_reset(&ptr->hosts[server_key]);
169 rc= MEMCACHED_WRITE_FAILURE;
170 }
171
172 unlikely (ptr->number_of_replicas > 0)
173 {
174 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
175
176 for (uint32_t x= 0; x < ptr->number_of_replicas; ++x)
177 {
178 ++server_key;
179 if (server_key == ptr->number_of_hosts)
180 server_key= 0;
181
182 memcached_server_st* server= &ptr->hosts[server_key];
183 if ((memcached_do(server, (const char*)request.bytes,
184 sizeof(request.bytes), 0) != MEMCACHED_SUCCESS) ||
185 (memcached_io_write(server, key, key_length, (char) flush) == -1))
186 memcached_io_reset(server);
187 else
188 memcached_server_response_decrement(server);
189 }
190 }
191
192 return rc;
193 }