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