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