Updating for 1.0.2 release
[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, key, key_length, expiration);
45 }
46
47 static inline memcached_return_t ascii_delete(memcached_st *ptr,
48 memcached_server_write_instance_st instance,
49 uint32_t ,
50 const char *key,
51 size_t key_length,
52 bool& reply,
53 bool& flush)
54 {
55 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
56 int send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
57 "delete %.*s%.*s%s\r\n",
58 memcached_print_array(ptr->_namespace),
59 (int)key_length, key,
60 reply ? "" : " noreply");
61
62 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE || send_length < 0)
63 {
64 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
65 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
66 }
67
68 if (ptr->flags.use_udp and flush == false)
69 {
70 if (send_length > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
71 {
72 return MEMCACHED_WRITE_FAILURE;
73 }
74
75 if (send_length +instance->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
76 {
77 memcached_io_write(instance, NULL, 0, true);
78 }
79 }
80
81 return memcached_do(instance, buffer, (size_t)send_length, flush);
82 }
83
84 static inline memcached_return_t binary_delete(memcached_st *ptr,
85 memcached_server_write_instance_st instance,
86 uint32_t server_key,
87 const char *key,
88 size_t key_length,
89 bool& reply,
90 bool& flush)
91 {
92 protocol_binary_request_delete request= {};
93
94 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
95 if (reply)
96 {
97 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETE;
98 }
99 else
100 {
101 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
102 }
103 request.message.header.request.keylen= htons((uint16_t)(key_length + memcached_array_size(ptr->_namespace)));
104 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
105 request.message.header.request.bodylen= htonl((uint32_t)(key_length + memcached_array_size(ptr->_namespace)));
106
107 if (ptr->flags.use_udp and flush == false)
108 {
109 size_t cmd_size= sizeof(request.bytes) + key_length;
110 if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
111 {
112 return MEMCACHED_WRITE_FAILURE;
113 }
114
115 if (cmd_size +instance->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
116 {
117 memcached_io_write(instance, NULL, 0, true);
118 }
119 }
120
121 struct libmemcached_io_vector_st vector[]=
122 {
123 { request.bytes, sizeof(request.bytes) },
124 { memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace) },
125 { key, key_length }
126 };
127
128 memcached_return_t rc= MEMCACHED_SUCCESS;
129
130 if ((rc= memcached_vdo(instance, vector, 3, flush)) != MEMCACHED_SUCCESS)
131 {
132 memcached_io_reset(instance);
133 }
134
135 if (ptr->number_of_replicas > 0)
136 {
137 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
138
139 for (uint32_t x= 0; x < ptr->number_of_replicas; ++x)
140 {
141 memcached_server_write_instance_st replica;
142
143 ++server_key;
144 if (server_key == memcached_server_count(ptr))
145 server_key= 0;
146
147 replica= memcached_server_instance_fetch(ptr, server_key);
148
149 if (memcached_vdo(replica, vector, 3, flush) != MEMCACHED_SUCCESS)
150 {
151 memcached_io_reset(replica);
152 }
153 else
154 {
155 memcached_server_response_decrement(replica);
156 }
157 }
158 }
159
160 return rc;
161 }
162
163 memcached_return_t memcached_delete_by_key(memcached_st *ptr,
164 const char *group_key, size_t group_key_length,
165 const char *key, size_t key_length,
166 time_t expiration)
167 {
168 LIBMEMCACHED_MEMCACHED_DELETE_START();
169
170 memcached_return_t rc;
171 if (memcached_failed(rc= initialize_query(ptr)))
172 {
173 return rc;
174 }
175
176 rc= memcached_validate_key_length(key_length, ptr->flags.binary_protocol);
177 if (memcached_failed(rc))
178 {
179 return rc;
180 }
181
182 if (expiration)
183 {
184 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
185 memcached_literal_param("Memcached server version does not allow expiration of deleted items"));
186 }
187
188 // If a delete trigger exists, we need a response, so no buffering/noreply
189 if (ptr->delete_trigger)
190 {
191 if (ptr->flags.buffer_requests)
192 {
193 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
194 memcached_literal_param("Delete triggers cannot be used if buffering is enabled"));
195 }
196
197 if (ptr->flags.no_reply)
198 {
199 return memcached_set_error(*ptr, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
200 memcached_literal_param("Delete triggers cannot be used if MEMCACHED_BEHAVIOR_NOREPLY is set"));
201 }
202 }
203
204
205 uint32_t server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length);
206 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, server_key);
207
208 bool to_write= (ptr->flags.buffer_requests) ? false : true;
209
210 // Invert the logic to make it simpler to read the code
211 bool reply= (ptr->flags.no_reply) ? false : true;
212
213 if (ptr->flags.binary_protocol)
214 {
215 rc= binary_delete(ptr, instance, server_key, key, key_length, reply, to_write);
216 }
217 else
218 {
219 rc= ascii_delete(ptr, instance, server_key, key, key_length, reply, to_write);
220 }
221
222 if (rc == MEMCACHED_SUCCESS)
223 {
224 if (to_write == false)
225 {
226 rc= MEMCACHED_BUFFERED;
227 }
228 else if (reply)
229 {
230 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
231 rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
232 if (rc == MEMCACHED_DELETED)
233 {
234 rc= MEMCACHED_SUCCESS;
235 }
236 }
237
238 if (rc == MEMCACHED_SUCCESS and ptr->delete_trigger)
239 {
240 ptr->delete_trigger(ptr, key, key_length);
241 }
242 }
243
244 LIBMEMCACHED_MEMCACHED_DELETE_END();
245 return rc;
246 }