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
,
24 memcached_return_t rc
;
25 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
27 memcached_server_write_instance_st instance
;
29 LIBMEMCACHED_MEMCACHED_DELETE_START();
31 rc
= memcached_validate_key_length(key_length
,
32 ptr
->flags
.binary_protocol
);
33 unlikely (rc
!= MEMCACHED_SUCCESS
)
36 unlikely (memcached_server_count(ptr
) == 0)
37 return MEMCACHED_NO_SERVERS
;
39 server_key
= memcached_generate_hash_with_redistribution(ptr
, master_key
, master_key_length
);
40 instance
= memcached_server_instance_fetch(ptr
, server_key
);
42 to_write
= (ptr
->flags
.buffer_requests
) ? false : true;
44 bool no_reply
= (ptr
->flags
.no_reply
);
46 if (ptr
->flags
.binary_protocol
)
50 rc
= binary_delete(ptr
, server_key
, key
, key_length
, to_write
);
54 rc
= MEMCACHED_INVALID_ARGUMENTS
;
61 if ((instance
->major_version
== 1 &&
62 instance
->minor_version
> 2) ||
63 instance
->major_version
> 1)
65 rc
= MEMCACHED_INVALID_ARGUMENTS
;
70 /* ensure that we are connected, otherwise we might bump the
71 * command counter before connection */
72 if ((rc
= memcached_connect(instance
)) != MEMCACHED_SUCCESS
)
78 if (instance
->minor_version
== 0)
80 if (no_reply
|| ! to_write
)
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.
88 memcached_server_response_increment(instance
);
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
,
96 (int) key_length
, key
,
98 no_reply
? " noreply" :"" );
103 send_length
= (size_t) snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
104 "delete %.*s%.*s%s\r\n",
105 (int)ptr
->prefix_key_length
,
107 (int)key_length
, key
, no_reply
? " noreply" :"");
110 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
112 rc
= MEMCACHED_WRITE_FAILURE
;
116 if (ptr
->flags
.use_udp
&& ! to_write
)
118 if (send_length
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
119 return MEMCACHED_WRITE_FAILURE
;
120 if (send_length
+ instance
->write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
121 memcached_io_write(instance
, NULL
, 0, true);
124 rc
= memcached_do(instance
, buffer
, send_length
, to_write
);
127 if (rc
!= MEMCACHED_SUCCESS
)
132 rc
= MEMCACHED_BUFFERED
;
136 rc
= memcached_response(instance
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
137 if (rc
== MEMCACHED_DELETED
)
138 rc
= MEMCACHED_SUCCESS
;
141 if (rc
== MEMCACHED_SUCCESS
&& ptr
->delete_trigger
)
142 ptr
->delete_trigger(ptr
, key
, key_length
);
145 LIBMEMCACHED_MEMCACHED_DELETE_END();
149 static inline memcached_return_t
binary_delete(memcached_st
*ptr
,
155 memcached_server_write_instance_st instance
;
156 protocol_binary_request_delete request
= {.bytes
= {0}};
158 instance
= memcached_server_instance_fetch(ptr
, server_key
);
160 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
161 if (ptr
->flags
.no_reply
)
162 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETEQ
;
164 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETE
;
165 request
.message
.header
.request
.keylen
= htons((uint16_t)(key_length
+ ptr
->prefix_key_length
));
166 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
167 request
.message
.header
.request
.bodylen
= htonl((uint32_t)(key_length
+ ptr
->prefix_key_length
));
169 if (ptr
->flags
.use_udp
&& ! flush
)
171 size_t cmd_size
= sizeof(request
.bytes
) + key_length
;
172 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
173 return MEMCACHED_WRITE_FAILURE
;
174 if (cmd_size
+ instance
->write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
175 memcached_io_write(instance
, NULL
, 0, true);
178 struct __write_vector_st vector
[]=
180 { .length
= sizeof(request
.bytes
), .buffer
= request
.bytes
},
181 { .length
= ptr
->prefix_key_length
, .buffer
= ptr
->prefix_key
},
182 { .length
= key_length
, .buffer
= key
},
185 memcached_return_t rc
= MEMCACHED_SUCCESS
;
187 if ((rc
= memcached_vdo(instance
, vector
, 3, flush
)) != MEMCACHED_SUCCESS
)
189 memcached_io_reset(instance
);
190 rc
= (rc
== MEMCACHED_SUCCESS
) ? MEMCACHED_WRITE_FAILURE
: rc
;
193 unlikely (ptr
->number_of_replicas
> 0)
195 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETEQ
;
197 for (uint32_t x
= 0; x
< ptr
->number_of_replicas
; ++x
)
199 memcached_server_write_instance_st replica
;
202 if (server_key
== memcached_server_count(ptr
))
205 replica
= memcached_server_instance_fetch(ptr
, server_key
);
207 if (memcached_vdo(replica
, vector
, 3, flush
) != MEMCACHED_SUCCESS
)
209 memcached_io_reset(replica
);
213 memcached_server_response_decrement(replica
);