ef1fa6da328e499e94d931e608f88db2ac6e5c81
[m6w6/libmemcached] / libmemcached / delete.c
1 #include "common.h"
2 #include "memcached/protocol_binary.h"
3
4 memcached_return_t memcached_delete(memcached_st *ptr, const char *key, size_t key_length,
5 time_t expiration)
6 {
7 return memcached_delete_by_key(ptr, key, key_length,
8 key, key_length, expiration);
9 }
10
11 static inline memcached_return_t binary_delete(memcached_st *ptr,
12 uint32_t server_key,
13 const char *key,
14 size_t key_length,
15 bool flush);
16
17 memcached_return_t memcached_delete_by_key(memcached_st *ptr,
18 const char *master_key, size_t master_key_length,
19 const char *key, size_t key_length,
20 time_t expiration)
21 {
22 bool to_write;
23 memcached_return_t rc;
24 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
25 uint32_t server_key;
26 memcached_server_write_instance_st instance;
27
28 LIBMEMCACHED_MEMCACHED_DELETE_START();
29
30 rc= memcached_validate_key_length(key_length,
31 ptr->flags.binary_protocol);
32 unlikely (rc != MEMCACHED_SUCCESS)
33 return rc;
34
35 unlikely (memcached_server_count(ptr) == 0)
36 return MEMCACHED_NO_SERVERS;
37
38 server_key= memcached_generate_hash_with_redistribution(ptr, master_key, master_key_length);
39 instance= memcached_server_instance_fetch(ptr, server_key);
40
41 to_write= (ptr->flags.buffer_requests) ? false : true;
42
43 bool no_reply= (ptr->flags.no_reply);
44
45 if (ptr->flags.binary_protocol)
46 {
47 likely (! expiration)
48 {
49 rc= binary_delete(ptr, server_key, key, key_length, to_write);
50 }
51 else
52 {
53 rc= MEMCACHED_INVALID_ARGUMENTS;
54 }
55 }
56 else
57 {
58 int send_length;
59
60 unlikely (expiration)
61 {
62 if ((instance->major_version == 1 &&
63 instance->minor_version > 2) ||
64 instance->major_version > 1)
65 {
66 rc= MEMCACHED_INVALID_ARGUMENTS;
67 goto error;
68 }
69 else
70 {
71 /* ensure that we are connected, otherwise we might bump the
72 * command counter before connection */
73 if ((rc= memcached_connect(instance)) != MEMCACHED_SUCCESS)
74 {
75 WATCHPOINT_ERROR(rc);
76 return rc;
77 }
78
79 if (instance->minor_version == 0)
80 {
81 if (no_reply || ! to_write)
82 {
83 /* We might get out of sync with the server if we
84 * send this command to a server newer than 1.2.x..
85 * disable no_reply and buffered mode.
86 */
87 to_write= true;
88 if (no_reply)
89 memcached_server_response_increment(instance);
90 no_reply= false;
91 }
92 }
93 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
94 "delete %.*s%.*s %u%s\r\n",
95 (int)ptr->prefix_key_length,
96 ptr->prefix_key,
97 (int) key_length, key,
98 (uint32_t)expiration,
99 no_reply ? " noreply" :"" );
100 }
101 }
102 else
103 {
104 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
105 "delete %.*s%.*s%s\r\n",
106 (int)ptr->prefix_key_length,
107 ptr->prefix_key,
108 (int)key_length, key, no_reply ? " noreply" :"");
109 }
110
111 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE || send_length < 0)
112 {
113 rc= MEMCACHED_WRITE_FAILURE;
114 goto error;
115 }
116
117 if (ptr->flags.use_udp && ! to_write)
118 {
119 if (send_length > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
120 return MEMCACHED_WRITE_FAILURE;
121 if (send_length + instance->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
122 memcached_io_write(instance, NULL, 0, true);
123 }
124
125 rc= memcached_do(instance, buffer, (size_t)send_length, to_write);
126 }
127
128 if (rc != MEMCACHED_SUCCESS)
129 goto error;
130
131 if (! to_write)
132 {
133 rc= MEMCACHED_BUFFERED;
134 }
135 else if (!no_reply)
136 {
137 rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
138 if (rc == MEMCACHED_DELETED)
139 rc= MEMCACHED_SUCCESS;
140 }
141
142 if (rc == MEMCACHED_SUCCESS && ptr->delete_trigger)
143 ptr->delete_trigger(ptr, key, key_length);
144
145 error:
146 LIBMEMCACHED_MEMCACHED_DELETE_END();
147 return rc;
148 }
149
150 static inline memcached_return_t binary_delete(memcached_st *ptr,
151 uint32_t server_key,
152 const char *key,
153 size_t key_length,
154 bool flush)
155 {
156 memcached_server_write_instance_st instance;
157 protocol_binary_request_delete request= {.bytes= {0}};
158
159 instance= memcached_server_instance_fetch(ptr, server_key);
160
161 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
162 if (ptr->flags.no_reply)
163 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
164 else
165 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETE;
166 request.message.header.request.keylen= htons((uint16_t)(key_length + ptr->prefix_key_length));
167 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
168 request.message.header.request.bodylen= htonl((uint32_t)(key_length + ptr->prefix_key_length));
169
170 if (ptr->flags.use_udp && ! flush)
171 {
172 size_t cmd_size= sizeof(request.bytes) + key_length;
173 if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
174 return MEMCACHED_WRITE_FAILURE;
175 if (cmd_size + instance->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
176 memcached_io_write(instance, NULL, 0, true);
177 }
178
179 struct libmemcached_io_vector_st vector[]=
180 {
181 { .length= sizeof(request.bytes), .buffer= request.bytes},
182 { .length= ptr->prefix_key_length, .buffer= ptr->prefix_key },
183 { .length= key_length, .buffer= key },
184 };
185
186 memcached_return_t rc= MEMCACHED_SUCCESS;
187
188 if ((rc= memcached_vdo(instance, vector, 3, flush)) != MEMCACHED_SUCCESS)
189 {
190 memcached_io_reset(instance);
191 rc= (rc == MEMCACHED_SUCCESS) ? MEMCACHED_WRITE_FAILURE : rc;
192 }
193
194 unlikely (ptr->number_of_replicas > 0)
195 {
196 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
197
198 for (uint32_t x= 0; x < ptr->number_of_replicas; ++x)
199 {
200 memcached_server_write_instance_st replica;
201
202 ++server_key;
203 if (server_key == memcached_server_count(ptr))
204 server_key= 0;
205
206 replica= memcached_server_instance_fetch(ptr, server_key);
207
208 if (memcached_vdo(replica, vector, 3, flush) != MEMCACHED_SUCCESS)
209 {
210 memcached_io_reset(replica);
211 }
212 else
213 {
214 memcached_server_response_decrement(replica);
215 }
216 }
217 }
218
219 return rc;
220 }