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