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
*memc
, const char *key
, size_t key_length
,
44 return memcached_delete_by_key(memc
, key
, key_length
, key
, key_length
, expiration
);
47 static inline memcached_return_t
ascii_delete(memcached_server_write_instance_st instance
,
50 const size_t key_length
,
52 const bool is_buffering
)
54 libmemcached_io_vector_st vector
[]=
57 { memcached_literal_param("delete ") },
58 { memcached_array_string(instance
->root
->_namespace
), memcached_array_size(instance
->root
->_namespace
) },
60 { " noreply", reply
? 0 : memcached_literal_param_size(" noreply") },
61 { memcached_literal_param("\r\n") }
64 /* Send command header, only flush if we are NOT buffering */
65 return memcached_vdo(instance
, vector
, 6, is_buffering
? false : true);
68 static inline memcached_return_t
binary_delete(memcached_server_write_instance_st instance
,
71 const size_t key_length
,
73 const bool is_buffering
)
75 protocol_binary_request_delete request
= {};
77 bool should_flush
= is_buffering
? false : true;
79 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
82 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETE
;
86 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETEQ
;
88 request
.message
.header
.request
.keylen
= htons(uint16_t(key_length
+ memcached_array_size(instance
->root
->_namespace
)));
89 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
90 request
.message
.header
.request
.bodylen
= htonl(uint32_t(key_length
+ memcached_array_size(instance
->root
->_namespace
)));
92 libmemcached_io_vector_st vector
[]=
95 { request
.bytes
, sizeof(request
.bytes
) },
96 { memcached_array_string(instance
->root
->_namespace
), memcached_array_size(instance
->root
->_namespace
) },
100 memcached_return_t rc
;
101 if (memcached_fatal(rc
= memcached_vdo(instance
, vector
, 4, should_flush
)))
103 memcached_io_reset(instance
);
106 if (memcached_has_replicas(instance
))
108 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETEQ
;
110 for (uint32_t x
= 0; x
< memcached_has_replicas(instance
); ++x
)
114 if (server_key
== memcached_server_count(instance
->root
))
119 memcached_server_write_instance_st replica
= memcached_server_instance_fetch(instance
->root
, server_key
);
121 if (memcached_fatal(memcached_vdo(replica
, vector
, 4, should_flush
)))
123 memcached_io_reset(replica
);
127 memcached_server_response_decrement(replica
);
135 memcached_return_t
memcached_delete_by_key(memcached_st
*memc
,
136 const char *group_key
, size_t group_key_length
,
137 const char *key
, size_t key_length
,
140 LIBMEMCACHED_MEMCACHED_DELETE_START();
142 memcached_return_t rc
;
143 if (memcached_fatal(rc
= initialize_query(memc
, true)))
148 if (memcached_fatal(rc
= memcached_key_test(*memc
, (const char **)&key
, &key_length
, 1)))
150 return memcached_last_error(memc
);
155 return memcached_set_error(*memc
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
,
156 memcached_literal_param("Memcached server version does not allow expiration of deleted items"));
159 uint32_t server_key
= memcached_generate_hash_with_redistribution(memc
, group_key
, group_key_length
);
160 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(memc
, server_key
);
162 bool is_buffering
= memcached_is_buffering(instance
->root
);
163 bool is_replying
= memcached_is_replying(instance
->root
);
165 // If a delete trigger exists, we need a response, so no buffering/noreply
166 if (memc
->delete_trigger
)
170 return memcached_set_error(*memc
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
,
171 memcached_literal_param("Delete triggers cannot be used if buffering is enabled"));
174 if (is_replying
== false)
176 return memcached_set_error(*memc
, MEMCACHED_INVALID_ARGUMENTS
, MEMCACHED_AT
,
177 memcached_literal_param("Delete triggers cannot be used if MEMCACHED_BEHAVIOR_NOREPLY is set"));
181 if (memcached_is_binary(memc
))
183 rc
= binary_delete(instance
, server_key
, key
, key_length
, is_replying
, is_buffering
);
187 rc
= ascii_delete(instance
, server_key
, key
, key_length
, is_replying
, is_buffering
);
190 if (rc
== MEMCACHED_SUCCESS
)
192 if (is_buffering
== true)
194 rc
= MEMCACHED_BUFFERED
;
196 else if (is_replying
== false)
198 rc
= MEMCACHED_SUCCESS
;
202 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
203 rc
= memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
204 if (rc
== MEMCACHED_DELETED
)
206 rc
= MEMCACHED_SUCCESS
;
207 if (memc
->delete_trigger
)
209 memc
->delete_trigger(memc
, key
, key_length
);
215 LIBMEMCACHED_MEMCACHED_DELETE_END();
216 return memcached_last_error(memc
);