Merge in trunk.
[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 binary_delete(memcached_st *ptr,
49 uint32_t server_key,
50 const char *key,
51 size_t key_length,
52 bool flush);
53
54 memcached_return_t memcached_delete_by_key(memcached_st *ptr,
55 const char *group_key, size_t group_key_length,
56 const char *key, size_t key_length,
57 time_t expiration)
58 {
59 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
60 memcached_server_write_instance_st instance;
61
62 LIBMEMCACHED_MEMCACHED_DELETE_START();
63
64 memcached_return_t rc;
65 if (memcached_failed(rc= initialize_query(ptr)))
66 {
67 return rc;
68 }
69
70 rc= memcached_validate_key_length(key_length,
71 ptr->flags.binary_protocol);
72
73 unlikely (memcached_failed(rc))
74 return rc;
75
76 unlikely (memcached_server_count(ptr) == 0)
77 return MEMCACHED_NO_SERVERS;
78
79 uint32_t server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length);
80 instance= memcached_server_instance_fetch(ptr, server_key);
81
82 bool to_write= (ptr->flags.buffer_requests) ? false : true;
83
84 bool no_reply= (ptr->flags.no_reply);
85
86 if (ptr->flags.binary_protocol)
87 {
88 likely (! expiration)
89 {
90 rc= binary_delete(ptr, server_key, key, key_length, to_write);
91 }
92 else
93 {
94 rc= MEMCACHED_INVALID_ARGUMENTS;
95 }
96 }
97 else
98 {
99 int send_length;
100
101 unlikely (expiration)
102 {
103 if ((instance->major_version == 1 &&
104 instance->minor_version > 2) ||
105 instance->major_version > 1)
106 {
107 rc= MEMCACHED_INVALID_ARGUMENTS;
108 goto error;
109 }
110 else
111 {
112 /* ensure that we are connected, otherwise we might bump the
113 * command counter before connection */
114 if ((rc= memcached_connect(instance)) != MEMCACHED_SUCCESS)
115 {
116 WATCHPOINT_ERROR(rc);
117 return rc;
118 }
119
120 if (instance->minor_version == 0)
121 {
122 if (no_reply or to_write == false)
123 {
124 /* We might get out of sync with the server if we
125 * send this command to a server newer than 1.2.x..
126 * disable no_reply and buffered mode.
127 */
128 to_write= true;
129 if (no_reply)
130 memcached_server_response_increment(instance);
131 no_reply= false;
132 }
133 }
134 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
135 "delete %.*s%.*s %u%s\r\n",
136 memcached_print_array(ptr->_namespace),
137 (int) key_length, key,
138 (uint32_t)expiration,
139 no_reply ? " noreply" :"" );
140 }
141 }
142 else
143 {
144 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
145 "delete %.*s%.*s%s\r\n",
146 memcached_print_array(ptr->_namespace),
147 (int)key_length, key, no_reply ? " noreply" :"");
148 }
149
150 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE || send_length < 0)
151 {
152 rc= memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
153 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
154 goto error;
155 }
156
157 if (ptr->flags.use_udp and to_write == false)
158 {
159 if (send_length > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
160 return MEMCACHED_WRITE_FAILURE;
161
162 if (send_length + instance->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
163 {
164 memcached_io_write(instance, NULL, 0, true);
165 }
166 }
167
168 rc= memcached_do(instance, buffer, (size_t)send_length, to_write);
169 }
170
171 if (rc != MEMCACHED_SUCCESS)
172 {
173 goto error;
174 }
175
176 if (to_write == false)
177 {
178 rc= MEMCACHED_BUFFERED;
179 }
180 else if (no_reply == false)
181 {
182 rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
183 if (rc == MEMCACHED_DELETED)
184 {
185 rc= MEMCACHED_SUCCESS;
186 }
187 }
188
189 if (rc == MEMCACHED_SUCCESS and ptr->delete_trigger)
190 {
191 ptr->delete_trigger(ptr, key, key_length);
192 }
193
194 error:
195 LIBMEMCACHED_MEMCACHED_DELETE_END();
196 return rc;
197 }
198
199 static inline memcached_return_t binary_delete(memcached_st *ptr,
200 uint32_t server_key,
201 const char *key,
202 size_t key_length,
203 bool flush)
204 {
205 memcached_server_write_instance_st instance;
206 protocol_binary_request_delete request= {};
207
208 instance= memcached_server_instance_fetch(ptr, server_key);
209
210 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
211 if (ptr->flags.no_reply)
212 {
213 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
214 }
215 else
216 {
217 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETE;
218 }
219 request.message.header.request.keylen= htons((uint16_t)(key_length + memcached_array_size(ptr->_namespace)));
220 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
221 request.message.header.request.bodylen= htonl((uint32_t)(key_length + memcached_array_size(ptr->_namespace)));
222
223 if (ptr->flags.use_udp && ! flush)
224 {
225 size_t cmd_size= sizeof(request.bytes) + key_length;
226 if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
227 return MEMCACHED_WRITE_FAILURE;
228
229 if (cmd_size + instance->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
230 memcached_io_write(instance, NULL, 0, true);
231 }
232
233 struct libmemcached_io_vector_st vector[]=
234 {
235 { sizeof(request.bytes), request.bytes},
236 { memcached_array_size(ptr->_namespace), memcached_array_string(ptr->_namespace) },
237 { key_length, key },
238 };
239
240 memcached_return_t rc= MEMCACHED_SUCCESS;
241
242 if ((rc= memcached_vdo(instance, vector, 3, flush)) != MEMCACHED_SUCCESS)
243 {
244 memcached_io_reset(instance);
245 rc= (rc == MEMCACHED_SUCCESS) ? MEMCACHED_WRITE_FAILURE : rc;
246 }
247
248 unlikely (ptr->number_of_replicas > 0)
249 {
250 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
251
252 for (uint32_t x= 0; x < ptr->number_of_replicas; ++x)
253 {
254 memcached_server_write_instance_st replica;
255
256 ++server_key;
257 if (server_key == memcached_server_count(ptr))
258 server_key= 0;
259
260 replica= memcached_server_instance_fetch(ptr, server_key);
261
262 if (memcached_vdo(replica, vector, 3, flush) != MEMCACHED_SUCCESS)
263 {
264 memcached_io_reset(replica);
265 }
266 else
267 {
268 memcached_server_response_decrement(replica);
269 }
270 }
271 }
272
273 return rc;
274 }