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 memcached_print_array(ptr
->prefix_key
),
96 (int) key_length
, key
,
98 no_reply
? " noreply" :"" );
103 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
104 "delete %.*s%.*s%s\r\n",
105 memcached_print_array(ptr
->prefix_key
),
106 (int)key_length
, key
, no_reply
? " noreply" :"");
109 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
|| send_length
< 0)
111 rc
= MEMCACHED_WRITE_FAILURE
;
115 if (ptr
->flags
.use_udp
&& ! to_write
)
117 if (send_length
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
118 return MEMCACHED_WRITE_FAILURE
;
119 if (send_length
+ instance
->write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
120 memcached_io_write(instance
, NULL
, 0, true);
123 rc
= memcached_do(instance
, buffer
, (size_t)send_length
, to_write
);
126 if (rc
!= MEMCACHED_SUCCESS
)
131 rc
= MEMCACHED_BUFFERED
;
135 rc
= memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
136 if (rc
== MEMCACHED_DELETED
)
137 rc
= MEMCACHED_SUCCESS
;
140 if (rc
== MEMCACHED_SUCCESS
&& ptr
->delete_trigger
)
141 ptr
->delete_trigger(ptr
, key
, key_length
);
144 LIBMEMCACHED_MEMCACHED_DELETE_END();
148 static inline memcached_return_t
binary_delete(memcached_st
*ptr
,
154 memcached_server_write_instance_st instance
;
155 protocol_binary_request_delete request
= {.bytes
= {0}};
157 instance
= memcached_server_instance_fetch(ptr
, server_key
);
159 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
160 if (ptr
->flags
.no_reply
)
161 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETEQ
;
163 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETE
;
164 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
+ memcached_array_size(ptr
->prefix_key
)));
165 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
166 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
+ memcached_array_size(ptr
->prefix_key
)));
168 if (ptr
->flags
.use_udp
&& ! flush
)
170 size_t cmd_size
= sizeof(request
.bytes
) + key_length
;
171 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
172 return MEMCACHED_WRITE_FAILURE
;
173 if (cmd_size
+ instance
->write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
174 memcached_io_write(instance
, NULL
, 0, true);
177 struct libmemcached_io_vector_st vector
[]=
179 { .length
= sizeof(request
.bytes
), .buffer
= request
.bytes
},
180 { .length
= memcached_array_size(ptr
->prefix_key
), .buffer
= memcached_array_string(ptr
->prefix_key
) },
181 { .length
= key_length
, .buffer
= key
},
184 memcached_return_t rc
= MEMCACHED_SUCCESS
;
186 if ((rc
= memcached_vdo(instance
, vector
, 3, flush
)) != MEMCACHED_SUCCESS
)
188 memcached_io_reset(instance
);
189 rc
= (rc
== MEMCACHED_SUCCESS
) ? MEMCACHED_WRITE_FAILURE
: rc
;
192 unlikely (ptr
->number_of_replicas
> 0)
194 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETEQ
;
196 for (uint32_t x
= 0; x
< ptr
->number_of_replicas
; ++x
)
198 memcached_server_write_instance_st replica
;
201 if (server_key
== memcached_server_count(ptr
))
204 replica
= memcached_server_instance_fetch(ptr
, server_key
);
206 if (memcached_vdo(replica
, vector
, 3, flush
) != MEMCACHED_SUCCESS
)
208 memcached_io_reset(replica
);
212 memcached_server_response_decrement(replica
);