55a8b4ca17e9932040c9edac9ad97ab52d15f677
[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 *shell, const char *key, size_t key_length,
42 time_t expiration)
43 {
44 return memcached_delete_by_key(shell, key, key_length, key, key_length, expiration);
45 }
46
47 static inline memcached_return_t ascii_delete(memcached_instance_st* instance,
48 uint32_t ,
49 const char *key,
50 const size_t key_length,
51 const bool reply,
52 const bool is_buffering)
53 {
54 libmemcached_io_vector_st vector[]=
55 {
56 { NULL, 0 },
57 { memcached_literal_param("delete ") },
58 { memcached_array_string(instance->root->_namespace), memcached_array_size(instance->root->_namespace) },
59 { key, key_length },
60 { " noreply", reply ? 0 : memcached_literal_param_size(" noreply") },
61 { memcached_literal_param("\r\n") }
62 };
63
64 /* Send command header, only flush if we are NOT buffering */
65 return memcached_vdo(instance, vector, 6, is_buffering ? false : true);
66 }
67
68 static inline memcached_return_t binary_delete(memcached_instance_st* instance,
69 uint32_t server_key,
70 const char *key,
71 const size_t key_length,
72 const bool reply,
73 const bool is_buffering)
74 {
75 protocol_binary_request_delete request= {};
76
77 bool should_flush= is_buffering ? false : true;
78
79 initialize_binary_request(instance, request.message.header);
80
81 if (reply)
82 {
83 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETE;
84 }
85 else
86 {
87 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
88 }
89 request.message.header.request.keylen= htons(uint16_t(key_length + memcached_array_size(instance->root->_namespace)));
90 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
91 request.message.header.request.bodylen= htonl(uint32_t(key_length + memcached_array_size(instance->root->_namespace)));
92
93 libmemcached_io_vector_st vector[]=
94 {
95 { NULL, 0 },
96 { request.bytes, sizeof(request.bytes) },
97 { memcached_array_string(instance->root->_namespace), memcached_array_size(instance->root->_namespace) },
98 { key, key_length }
99 };
100
101 memcached_return_t rc;
102 if (memcached_fatal(rc= memcached_vdo(instance, vector, 4, should_flush)))
103 {
104 assert(memcached_last_error(instance->root) != MEMCACHED_SUCCESS);
105 memcached_io_reset(instance);
106 }
107
108 if (memcached_has_replicas(instance))
109 {
110 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
111
112 for (uint32_t x= 0; x < memcached_has_replicas(instance); ++x)
113 {
114 ++server_key;
115
116 if (server_key == memcached_server_count(instance->root))
117 {
118 server_key= 0;
119 }
120
121 memcached_instance_st* replica= memcached_instance_fetch(instance->root, server_key);
122
123 if (memcached_fatal(memcached_vdo(replica, vector, 4, should_flush)))
124 {
125 assert(memcached_last_error(instance->root) != MEMCACHED_SUCCESS);
126 memcached_io_reset(replica);
127 }
128 else
129 {
130 memcached_server_response_decrement(replica);
131 }
132 }
133 }
134
135 return rc;
136 }
137
138 memcached_return_t memcached_delete_by_key(memcached_st *shell,
139 const char *group_key, size_t group_key_length,
140 const char *key, size_t key_length,
141 time_t expiration)
142 {
143 Memcached* memc= memcached2Memcached(shell);
144 LIBMEMCACHED_MEMCACHED_DELETE_START();
145
146 memcached_return_t rc;
147 if (memcached_fatal(rc= initialize_query(memc, true)))
148 {
149 return rc;
150 }
151
152 if (memcached_fatal(rc= memcached_key_test(*memc, (const char **)&key, &key_length, 1)))
153 {
154 return memcached_last_error(memc);
155 }
156
157 if (expiration)
158 {
159 return memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
160 memcached_literal_param("Memcached server version does not allow expiration of deleted items"));
161 }
162
163 uint32_t server_key= memcached_generate_hash_with_redistribution(memc, group_key, group_key_length);
164 memcached_instance_st* instance= memcached_instance_fetch(memc, server_key);
165
166 bool is_buffering= memcached_is_buffering(instance->root);
167 bool is_replying= memcached_is_replying(instance->root);
168
169 // If a delete trigger exists, we need a response, so no buffering/noreply
170 if (memc->delete_trigger)
171 {
172 if (is_buffering)
173 {
174 return memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
175 memcached_literal_param("Delete triggers cannot be used if buffering is enabled"));
176 }
177
178 if (is_replying == false)
179 {
180 return memcached_set_error(*memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
181 memcached_literal_param("Delete triggers cannot be used if MEMCACHED_BEHAVIOR_NOREPLY is set"));
182 }
183 }
184
185 if (memcached_is_binary(memc))
186 {
187 rc= binary_delete(instance, server_key, key, key_length, is_replying, is_buffering);
188 }
189 else
190 {
191 rc= ascii_delete(instance, server_key, key, key_length, is_replying, is_buffering);
192 }
193
194 if (rc == MEMCACHED_SUCCESS)
195 {
196 if (is_buffering == true)
197 {
198 rc= MEMCACHED_BUFFERED;
199 }
200 else if (is_replying == false)
201 {
202 rc= MEMCACHED_SUCCESS;
203 }
204 else
205 {
206 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
207 rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
208 if (rc == MEMCACHED_DELETED)
209 {
210 rc= MEMCACHED_SUCCESS;
211 if (memc->delete_trigger)
212 {
213 memc->delete_trigger(memc, key, key_length);
214 }
215 }
216 }
217 }
218
219 LIBMEMCACHED_MEMCACHED_DELETE_END();
220 return rc;
221 }