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