2 #include "memcached/protocol_binary.h"
4 memcached_return_t
memcached_delete(memcached_st
*ptr
, const char *key
, size_t key_length
,
7 return memcached_delete_by_key(ptr
, key
, key_length
,
8 key
, key_length
, expiration
);
11 static inline memcached_return_t
binary_delete(memcached_st
*ptr
,
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
,
23 memcached_return_t rc
;
24 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
26 memcached_server_write_instance_st instance
;
28 LIBMEMCACHED_MEMCACHED_DELETE_START();
30 rc
= memcached_validate_key_length(key_length
,
31 ptr
->flags
.binary_protocol
);
32 unlikely (rc
!= MEMCACHED_SUCCESS
)
35 unlikely (memcached_server_count(ptr
) == 0)
36 return MEMCACHED_NO_SERVERS
;
38 server_key
= memcached_generate_hash_with_redistribution(ptr
, master_key
, master_key_length
);
39 instance
= memcached_server_instance_fetch(ptr
, server_key
);
41 to_write
= (ptr
->flags
.buffer_requests
) ? false : true;
43 bool no_reply
= (ptr
->flags
.no_reply
);
45 if (ptr
->flags
.binary_protocol
)
49 rc
= binary_delete(ptr
, server_key
, key
, key_length
, to_write
);
53 rc
= MEMCACHED_INVALID_ARGUMENTS
;
62 if ((instance
->major_version
== 1 &&
63 instance
->minor_version
> 2) ||
64 instance
->major_version
> 1)
66 rc
= MEMCACHED_INVALID_ARGUMENTS
;
71 /* ensure that we are connected, otherwise we might bump the
72 * command counter before connection */
73 if ((rc
= memcached_connect(instance
)) != MEMCACHED_SUCCESS
)
79 if (instance
->minor_version
== 0)
81 if (no_reply
|| ! to_write
)
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.
89 memcached_server_response_increment(instance
);
93 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
94 "delete %.*s%.*s %u%s\r\n",
95 (int)ptr
->prefix_key_length
,
97 (int) key_length
, key
,
99 no_reply
? " noreply" :"" );
104 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
105 "delete %.*s%.*s%s\r\n",
106 (int)ptr
->prefix_key_length
,
108 (int)key_length
, key
, no_reply
? " noreply" :"");
111 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
|| send_length
< 0)
113 rc
= MEMCACHED_WRITE_FAILURE
;
117 if (ptr
->flags
.use_udp
&& ! to_write
)
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);
125 rc
= memcached_do(instance
, buffer
, (size_t)send_length
, to_write
);
128 if (rc
!= MEMCACHED_SUCCESS
)
133 rc
= MEMCACHED_BUFFERED
;
137 rc
= memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
138 if (rc
== MEMCACHED_DELETED
)
139 rc
= MEMCACHED_SUCCESS
;
142 if (rc
== MEMCACHED_SUCCESS
&& ptr
->delete_trigger
)
143 ptr
->delete_trigger(ptr
, key
, key_length
);
146 LIBMEMCACHED_MEMCACHED_DELETE_END();
150 static inline memcached_return_t
binary_delete(memcached_st
*ptr
,
156 memcached_server_write_instance_st instance
;
157 protocol_binary_request_delete request
= {.bytes
= {0}};
159 instance
= memcached_server_instance_fetch(ptr
, server_key
);
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
;
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
));
170 if (ptr
->flags
.use_udp
&& ! flush
)
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);
179 struct libmemcached_io_vector_st vector
[]=
181 { .length
= sizeof(request
.bytes
), .buffer
= request
.bytes
},
182 { .length
= ptr
->prefix_key_length
, .buffer
= ptr
->prefix_key
},
183 { .length
= key_length
, .buffer
= key
},
186 memcached_return_t rc
= MEMCACHED_SUCCESS
;
188 if ((rc
= memcached_vdo(instance
, vector
, 3, flush
)) != MEMCACHED_SUCCESS
)
190 memcached_io_reset(instance
);
191 rc
= (rc
== MEMCACHED_SUCCESS
) ? MEMCACHED_WRITE_FAILURE
: rc
;
194 unlikely (ptr
->number_of_replicas
> 0)
196 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETEQ
;
198 for (uint32_t x
= 0; x
< ptr
->number_of_replicas
; ++x
)
200 memcached_server_write_instance_st replica
;
203 if (server_key
== memcached_server_count(ptr
))
206 replica
= memcached_server_instance_fetch(ptr
, server_key
);
208 if (memcached_vdo(replica
, vector
, 3, flush
) != MEMCACHED_SUCCESS
)
210 memcached_io_reset(replica
);
214 memcached_server_response_decrement(replica
);