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