Updating to latest libtest.
[awesomized/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 memcached_server_write_instance_st instance;
62
63 LIBMEMCACHED_MEMCACHED_DELETE_START();
64
65 memcached_return_t rc;
66 if (memcached_failed(rc= initialize_query(ptr)))
67 {
68 return rc;
69 }
70
71 rc= memcached_validate_key_length(key_length,
72 ptr->flags.binary_protocol);
73
74 unlikely (memcached_failed(rc))
75 return rc;
76
77 unlikely (memcached_server_count(ptr) == 0)
78 return MEMCACHED_NO_SERVERS;
79
80 uint32_t 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->_namespace),
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->_namespace),
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_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
154 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
155 goto error;
156 }
157
158 if (ptr->flags.use_udp && ! to_write)
159 {
160 if (send_length > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
161 return MEMCACHED_WRITE_FAILURE;
162
163 if (send_length + instance->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
164 memcached_io_write(instance, NULL, 0, true);
165 }
166
167 rc= memcached_do(instance, buffer, (size_t)send_length, to_write);
168 }
169
170 if (rc != MEMCACHED_SUCCESS)
171 goto error;
172
173 if (! to_write)
174 {
175 rc= MEMCACHED_BUFFERED;
176 }
177 else if (!no_reply)
178 {
179 rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
180 if (rc == MEMCACHED_DELETED)
181 rc= MEMCACHED_SUCCESS;
182 }
183
184 if (rc == MEMCACHED_SUCCESS && ptr->delete_trigger)
185 ptr->delete_trigger(ptr, key, key_length);
186
187 error:
188 LIBMEMCACHED_MEMCACHED_DELETE_END();
189 return rc;
190 }
191
192 static inline memcached_return_t binary_delete(memcached_st *ptr,
193 uint32_t server_key,
194 const char *key,
195 size_t key_length,
196 bool flush)
197 {
198 memcached_server_write_instance_st instance;
199 protocol_binary_request_delete request= {};
200
201 instance= memcached_server_instance_fetch(ptr, server_key);
202
203 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
204 if (ptr->flags.no_reply)
205 {
206 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
207 }
208 else
209 {
210 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETE;
211 }
212 request.message.header.request.keylen= htons((uint16_t)(key_length + memcached_array_size(ptr->_namespace)));
213 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
214 request.message.header.request.bodylen= htonl((uint32_t)(key_length + memcached_array_size(ptr->_namespace)));
215
216 if (ptr->flags.use_udp && ! flush)
217 {
218 size_t cmd_size= sizeof(request.bytes) + key_length;
219 if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
220 return MEMCACHED_WRITE_FAILURE;
221
222 if (cmd_size + instance->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
223 memcached_io_write(instance, NULL, 0, true);
224 }
225
226 struct libmemcached_io_vector_st vector[]=
227 {
228 { sizeof(request.bytes), request.bytes},
229 { memcached_array_size(ptr->_namespace), memcached_array_string(ptr->_namespace) },
230 { key_length, key },
231 };
232
233 memcached_return_t rc= MEMCACHED_SUCCESS;
234
235 if ((rc= memcached_vdo(instance, vector, 3, flush)) != MEMCACHED_SUCCESS)
236 {
237 memcached_io_reset(instance);
238 rc= (rc == MEMCACHED_SUCCESS) ? MEMCACHED_WRITE_FAILURE : rc;
239 }
240
241 unlikely (ptr->number_of_replicas > 0)
242 {
243 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
244
245 for (uint32_t x= 0; x < ptr->number_of_replicas; ++x)
246 {
247 memcached_server_write_instance_st replica;
248
249 ++server_key;
250 if (server_key == memcached_server_count(ptr))
251 server_key= 0;
252
253 replica= memcached_server_instance_fetch(ptr, server_key);
254
255 if (memcached_vdo(replica, vector, 3, flush) != MEMCACHED_SUCCESS)
256 {
257 memcached_io_reset(replica);
258 }
259 else
260 {
261 memcached_server_response_decrement(replica);
262 }
263 }
264 }
265
266 return rc;
267 }