Update all of the delete key logic (split it off for ASCII)
[m6w6/libmemcached] / libmemcached / delete.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
35 *
36 */
37
38 #include <libmemcached/common.h>
39 #include <libmemcached/memcached/protocol_binary.h>
40
41 memcached_return_t memcached_delete(memcached_st *ptr, const char *key, size_t key_length,
42 time_t expiration)
43 {
44 return memcached_delete_by_key(ptr, key, key_length,
45 key, key_length, expiration);
46 }
47
48 static inline memcached_return_t ascii_delete(memcached_st *ptr,
49 memcached_server_write_instance_st instance,
50 uint32_t ,
51 const char *key,
52 size_t key_length,
53 uint64_t expiration,
54 bool& reply,
55 bool& flush)
56 {
57 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
58 int send_length;
59
60 if (expiration)
61 {
62 if ((instance->major_version == 1 and
63 instance->minor_version > 2) or
64 instance->major_version > 1)
65 {
66 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
67 memcached_literal_param("Memcached server version does not allow expiration of deleted items"));
68 }
69 else
70 {
71 /* ensure that we are connected, otherwise we might bump the
72 * command counter before connection */
73 memcached_return_t rc;
74 if ((rc= memcached_connect(instance)) != MEMCACHED_SUCCESS)
75 {
76 WATCHPOINT_ERROR(rc);
77 return rc;
78 }
79
80 if (instance->minor_version == 0)
81 {
82 if (reply == false or flush == false)
83 {
84 /* We might get out of sync with the server if we send this command
85 * to a server newer than 1.2.x.. enable reply and buffered mode.
86 */
87 flush= true;
88 if (reply == false)
89 {
90 memcached_server_response_increment(instance);
91 }
92 reply= true;
93 }
94 }
95
96 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
97 "delete %.*s%.*s %u%s\r\n",
98 memcached_print_array(ptr->_namespace),
99 (int) key_length, key,
100 (uint32_t)expiration,
101 reply ? "" : " noreply");
102 }
103 }
104 else
105 {
106 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
107 "delete %.*s%.*s%s\r\n",
108 memcached_print_array(ptr->_namespace),
109 (int)key_length, key,
110 reply ? "" : " noreply");
111 }
112
113 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE || send_length < 0)
114 {
115 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
116 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
117 }
118
119 if (ptr->flags.use_udp and flush == false)
120 {
121 if (send_length > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
122 {
123 return MEMCACHED_WRITE_FAILURE;
124 }
125
126 if (send_length +instance->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
127 {
128 memcached_io_write(instance, NULL, 0, true);
129 }
130 }
131
132 return memcached_do(instance, buffer, (size_t)send_length, flush);
133 }
134
135 static inline memcached_return_t binary_delete(memcached_st *ptr,
136 memcached_server_write_instance_st instance,
137 uint32_t server_key,
138 const char *key,
139 size_t key_length,
140 time_t expiration,
141 bool& reply,
142 bool& flush)
143 {
144 protocol_binary_request_delete request= {};
145
146 // No expiration is supported in the binary protocol
147 if (expiration)
148 {
149 return MEMCACHED_INVALID_ARGUMENTS;
150 }
151
152 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
153 if (reply)
154 {
155 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETE;
156 }
157 else
158 {
159 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
160 }
161 request.message.header.request.keylen= htons((uint16_t)(key_length + memcached_array_size(ptr->_namespace)));
162 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
163 request.message.header.request.bodylen= htonl((uint32_t)(key_length + memcached_array_size(ptr->_namespace)));
164
165 if (ptr->flags.use_udp and flush == false)
166 {
167 size_t cmd_size= sizeof(request.bytes) + key_length;
168 if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
169 {
170 return MEMCACHED_WRITE_FAILURE;
171 }
172
173 if (cmd_size +instance->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
174 {
175 memcached_io_write(instance, NULL, 0, true);
176 }
177 }
178
179 struct libmemcached_io_vector_st vector[]=
180 {
181 { sizeof(request.bytes), request.bytes},
182 { memcached_array_size(ptr->_namespace), memcached_array_string(ptr->_namespace) },
183 { key_length, key },
184 };
185
186 memcached_return_t rc= MEMCACHED_SUCCESS;
187
188 if ((rc= memcached_vdo(instance, vector, 3, flush)) != MEMCACHED_SUCCESS)
189 {
190 memcached_io_reset(instance);
191 }
192
193 if (ptr->number_of_replicas > 0)
194 {
195 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
196
197 for (uint32_t x= 0; x < ptr->number_of_replicas; ++x)
198 {
199 memcached_server_write_instance_st replica;
200
201 ++server_key;
202 if (server_key == memcached_server_count(ptr))
203 server_key= 0;
204
205 replica= memcached_server_instance_fetch(ptr, server_key);
206
207 if (memcached_vdo(replica, vector, 3, flush) != MEMCACHED_SUCCESS)
208 {
209 memcached_io_reset(replica);
210 }
211 else
212 {
213 memcached_server_response_decrement(replica);
214 }
215 }
216 }
217
218 return rc;
219 }
220
221 memcached_return_t memcached_delete_by_key(memcached_st *ptr,
222 const char *group_key, size_t group_key_length,
223 const char *key, size_t key_length,
224 time_t expiration)
225 {
226 LIBMEMCACHED_MEMCACHED_DELETE_START();
227
228 memcached_return_t rc;
229 if (memcached_failed(rc= initialize_query(ptr)))
230 {
231 return rc;
232 }
233
234 rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol);
235 if (memcached_failed(rc))
236 {
237 return rc;
238 }
239
240 // If a delete trigger exists, we need a response, so no buffering/noreply
241 if (ptr->delete_trigger)
242 {
243 if (ptr->flags.buffer_requests)
244 {
245 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
246 memcached_literal_param("Delete triggers cannot be used if buffering is enabled"));
247 }
248
249 if (ptr->flags.no_reply)
250 {
251 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
252 memcached_literal_param("Delete triggers cannot be used if MEMCACHED_BEHAVIOR_NOREPLY is set"));
253 }
254 }
255
256
257 uint32_t server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length);
258 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, server_key);
259
260 bool to_write= (ptr->flags.buffer_requests) ? false : true;
261
262 // Invert the logic to make it simpler to read the code
263 bool reply= (ptr->flags.no_reply) ? false : true;
264
265 if (ptr->flags.binary_protocol)
266 {
267 rc= binary_delete(ptr, instance, server_key, key, key_length, expiration, reply, to_write);
268 }
269 else
270 {
271 rc= ascii_delete(ptr, instance, server_key, key, key_length, expiration, reply, to_write);
272 }
273
274 if (rc == MEMCACHED_SUCCESS)
275 {
276 if (to_write == false)
277 {
278 rc= MEMCACHED_BUFFERED;
279 }
280 else if (reply)
281 {
282 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
283 rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
284 if (rc == MEMCACHED_DELETED)
285 {
286 rc= MEMCACHED_SUCCESS;
287 }
288 }
289
290 if (rc == MEMCACHED_SUCCESS and ptr->delete_trigger)
291 {
292 ptr->delete_trigger(ptr, key, key_length);
293 }
294 }
295
296 LIBMEMCACHED_MEMCACHED_DELETE_END();
297 return rc;
298 }