1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
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.
38 #include <libmemcached/common.h>
39 #include <libmemcached/memcached/protocol_binary.h>
41 memcached_return_t
memcached_delete(memcached_st
*ptr
, const char *key
, size_t key_length
,
44 return memcached_delete_by_key(ptr
, key
, key_length
,
45 key
, key_length
, expiration
);
48 static inline memcached_return_t
ascii_delete(memcached_st
*ptr
,
49 memcached_server_write_instance_st instance
,
57 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
62 if ((instance
->major_version
== 1 and
63 instance
->minor_version
> 2) or
64 instance
->major_version
> 1)
66 return memcached_set_error(*ptr
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
,
67 memcached_literal_param("Memcached server version does not allow expiration of deleted items"));
71 /* ensure that we are connected, otherwise we might bump the
72 * command counter before connection */
73 memcached_return_t rc
;
74 if ((rc
= memcached_connect(instance
)) != MEMCACHED_SUCCESS
)
80 if (instance
->minor_version
== 0)
82 if (reply
== false or flush
== false)
84 /* We might get out of sync with the server if we send this command
85 * to a server newer than 1.2.x.. enable reply and buffered mode.
90 memcached_server_response_increment(instance
);
96 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
97 "delete %.*s%.*s %u%s\r\n",
98 memcached_print_array(ptr
->_namespace
),
99 (int) key_length
, key
,
100 (uint32_t)expiration
,
101 reply
? "" : " noreply");
106 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
107 "delete %.*s%.*s%s\r\n",
108 memcached_print_array(ptr
->_namespace
),
109 (int)key_length
, key
,
110 reply
? "" : " noreply");
113 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
|| send_length
< 0)
115 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
116 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
119 if (ptr
->flags
.use_udp
and flush
== false)
121 if (send_length
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
123 return MEMCACHED_WRITE_FAILURE
;
126 if (send_length
+instance
->write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
128 memcached_io_write(instance
, NULL
, 0, true);
132 return memcached_do(instance
, buffer
, (size_t)send_length
, flush
);
135 static inline memcached_return_t
binary_delete(memcached_st
*ptr
,
136 memcached_server_write_instance_st instance
,
144 protocol_binary_request_delete request
= {};
146 // No expiration is supported in the binary protocol
149 return MEMCACHED_INVALID_ARGUMENTS
;
152 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
155 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETE
;
159 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETEQ
;
161 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
+ memcached_array_size(ptr
->_namespace
)));
162 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
163 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
+ memcached_array_size(ptr
->_namespace
)));
165 if (ptr
->flags
.use_udp
and flush
== false)
167 size_t cmd_size
= sizeof(request
.bytes
) + key_length
;
168 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
170 return MEMCACHED_WRITE_FAILURE
;
173 if (cmd_size
+instance
->write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
175 memcached_io_write(instance
, NULL
, 0, true);
179 struct libmemcached_io_vector_st vector
[]=
181 { sizeof(request
.bytes
), request
.bytes
},
182 { memcached_array_size(ptr
->_namespace
), memcached_array_string(ptr
->_namespace
) },
186 memcached_return_t rc
= MEMCACHED_SUCCESS
;
188 if ((rc
= memcached_vdo(instance
, vector
, 3, flush
)) != MEMCACHED_SUCCESS
)
190 memcached_io_reset(instance
);
193 if (ptr
->number_of_replicas
> 0)
195 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETEQ
;
197 for (uint32_t x
= 0; x
< ptr
->number_of_replicas
; ++x
)
199 memcached_server_write_instance_st replica
;
202 if (server_key
== memcached_server_count(ptr
))
205 replica
= memcached_server_instance_fetch(ptr
, server_key
);
207 if (memcached_vdo(replica
, vector
, 3, flush
) != MEMCACHED_SUCCESS
)
209 memcached_io_reset(replica
);
213 memcached_server_response_decrement(replica
);
221 memcached_return_t
memcached_delete_by_key(memcached_st
*ptr
,
222 const char *group_key
, size_t group_key_length
,
223 const char *key
, size_t key_length
,
226 LIBMEMCACHED_MEMCACHED_DELETE_START();
228 memcached_return_t rc
;
229 if (memcached_failed(rc
= initialize_query(ptr
)))
234 rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
);
235 if (memcached_failed(rc
))
240 // If a delete trigger exists, we need a response, so no buffering/noreply
241 if (ptr
->delete_trigger
)
243 if (ptr
->flags
.buffer_requests
)
245 return memcached_set_error(*ptr
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
,
246 memcached_literal_param("Delete triggers cannot be used if buffering is enabled"));
249 if (ptr
->flags
.no_reply
)
251 return memcached_set_error(*ptr
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
,
252 memcached_literal_param("Delete triggers cannot be used if MEMCACHED_BEHAVIOR_NOREPLY is set"));
257 uint32_t server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
258 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, server_key
);
260 bool to_write
= (ptr
->flags
.buffer_requests
) ? false : true;
262 // Invert the logic to make it simpler to read the code
263 bool reply
= (ptr
->flags
.no_reply
) ? false : true;
265 if (ptr
->flags
.binary_protocol
)
267 rc
= binary_delete(ptr
, instance
, server_key
, key
, key_length
, expiration
, reply
, to_write
);
271 rc
= ascii_delete(ptr
, instance
, server_key
, key
, key_length
, expiration
, reply
, to_write
);
274 if (rc
== MEMCACHED_SUCCESS
)
276 if (to_write
== false)
278 rc
= MEMCACHED_BUFFERED
;
282 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
283 rc
= memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
284 if (rc
== MEMCACHED_DELETED
)
286 rc
= MEMCACHED_SUCCESS
;
290 if (rc
== MEMCACHED_SUCCESS
and ptr
->delete_trigger
)
292 ptr
->delete_trigger(ptr
, key
, key_length
);
296 LIBMEMCACHED_MEMCACHED_DELETE_END();