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
binary_delete(memcached_st
*ptr
,
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
,
60 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
62 memcached_server_write_instance_st instance
;
64 LIBMEMCACHED_MEMCACHED_DELETE_START();
66 memcached_return_t rc
;
67 if ((rc
= initialize_query(ptr
)) != MEMCACHED_SUCCESS
)
72 rc
= memcached_validate_key_length(key_length
,
73 ptr
->flags
.binary_protocol
);
74 unlikely (rc
!= MEMCACHED_SUCCESS
)
77 unlikely (memcached_server_count(ptr
) == 0)
78 return MEMCACHED_NO_SERVERS
;
80 server_key
= memcached_generate_hash_with_redistribution(ptr
, group_key
, group_key_length
);
81 instance
= memcached_server_instance_fetch(ptr
, server_key
);
83 to_write
= (ptr
->flags
.buffer_requests
) ? false : true;
85 bool no_reply
= (ptr
->flags
.no_reply
);
87 if (ptr
->flags
.binary_protocol
)
91 rc
= binary_delete(ptr
, server_key
, key
, key_length
, to_write
);
95 rc
= MEMCACHED_INVALID_ARGUMENTS
;
102 unlikely (expiration
)
104 if ((instance
->major_version
== 1 &&
105 instance
->minor_version
> 2) ||
106 instance
->major_version
> 1)
108 rc
= MEMCACHED_INVALID_ARGUMENTS
;
113 /* ensure that we are connected, otherwise we might bump the
114 * command counter before connection */
115 if ((rc
= memcached_connect(instance
)) != MEMCACHED_SUCCESS
)
117 WATCHPOINT_ERROR(rc
);
121 if (instance
->minor_version
== 0)
123 if (no_reply
|| ! to_write
)
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.
131 memcached_server_response_increment(instance
);
135 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
136 "delete %.*s%.*s %u%s\r\n",
137 memcached_print_array(ptr
->prefix_key
),
138 (int) key_length
, key
,
139 (uint32_t)expiration
,
140 no_reply
? " noreply" :"" );
145 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
146 "delete %.*s%.*s%s\r\n",
147 memcached_print_array(ptr
->prefix_key
),
148 (int)key_length
, key
, no_reply
? " noreply" :"");
151 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
|| send_length
< 0)
153 rc
= MEMCACHED_WRITE_FAILURE
;
157 if (ptr
->flags
.use_udp
&& ! to_write
)
159 if (send_length
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
160 return MEMCACHED_WRITE_FAILURE
;
162 if (send_length
+ instance
->write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
163 memcached_io_write(instance
, NULL
, 0, true);
166 rc
= memcached_do(instance
, buffer
, (size_t)send_length
, to_write
);
169 if (rc
!= MEMCACHED_SUCCESS
)
174 rc
= MEMCACHED_BUFFERED
;
178 rc
= memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
179 if (rc
== MEMCACHED_DELETED
)
180 rc
= MEMCACHED_SUCCESS
;
183 if (rc
== MEMCACHED_SUCCESS
&& ptr
->delete_trigger
)
184 ptr
->delete_trigger(ptr
, key
, key_length
);
187 LIBMEMCACHED_MEMCACHED_DELETE_END();
191 static inline memcached_return_t
binary_delete(memcached_st
*ptr
,
197 memcached_server_write_instance_st instance
;
198 protocol_binary_request_delete request
= {};
200 instance
= memcached_server_instance_fetch(ptr
, server_key
);
202 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
203 if (ptr
->flags
.no_reply
)
205 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETEQ
;
209 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETE
;
211 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
+ memcached_array_size(ptr
->prefix_key
)));
212 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
213 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
+ memcached_array_size(ptr
->prefix_key
)));
215 if (ptr
->flags
.use_udp
&& ! flush
)
217 size_t cmd_size
= sizeof(request
.bytes
) + key_length
;
218 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
219 return MEMCACHED_WRITE_FAILURE
;
221 if (cmd_size
+ instance
->write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
222 memcached_io_write(instance
, NULL
, 0, true);
225 struct libmemcached_io_vector_st vector
[]=
227 { sizeof(request
.bytes
), request
.bytes
},
228 { memcached_array_size(ptr
->prefix_key
), memcached_array_string(ptr
->prefix_key
) },
232 memcached_return_t rc
= MEMCACHED_SUCCESS
;
234 if ((rc
= memcached_vdo(instance
, vector
, 3, flush
)) != MEMCACHED_SUCCESS
)
236 memcached_io_reset(instance
);
237 rc
= (rc
== MEMCACHED_SUCCESS
) ? MEMCACHED_WRITE_FAILURE
: rc
;
240 unlikely (ptr
->number_of_replicas
> 0)
242 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETEQ
;
244 for (uint32_t x
= 0; x
< ptr
->number_of_replicas
; ++x
)
246 memcached_server_write_instance_st replica
;
249 if (server_key
== memcached_server_count(ptr
))
252 replica
= memcached_server_instance_fetch(ptr
, server_key
);
254 if (memcached_vdo(replica
, vector
, 3, flush
) != MEMCACHED_SUCCESS
)
256 memcached_io_reset(replica
);
260 memcached_server_response_decrement(replica
);