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
,
12 unsigned int server_key
,
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
];
26 unsigned int server_key
;
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 (ptr
->hosts
== NULL
|| ptr
->number_of_hosts
== 0)
36 return MEMCACHED_NO_SERVERS
;
38 server_key
= memcached_generate_hash(ptr
, master_key
, master_key_length
);
39 to_write
= (uint8_t)((ptr
->flags
.buffer_requests
) ? 0 : 1);
40 bool no_reply
= (ptr
->flags
.no_reply
);
42 if (ptr
->flags
.binary_protocol
)
45 rc
= binary_delete(ptr
, server_key
, key
, key_length
, to_write
);
47 rc
= MEMCACHED_INVALID_ARGUMENTS
;
53 if ((ptr
->hosts
[server_key
].major_version
== 1 &&
54 ptr
->hosts
[server_key
].minor_version
> 2) ||
55 ptr
->hosts
[server_key
].major_version
> 1)
57 rc
= MEMCACHED_INVALID_ARGUMENTS
;
62 if (ptr
->hosts
[server_key
].minor_version
== 0)
64 if (no_reply
|| !to_write
)
66 /* We might get out of sync with the server if we
67 * send this command to a server newer than 1.2.x..
68 * disable no_reply and buffered mode.
72 memcached_server_response_increment(&ptr
->hosts
[server_key
]);
76 send_length
= (size_t) snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
77 "delete %s%.*s %u%s\r\n",
79 (int) key_length
, key
,
81 no_reply
? " noreply" :"" );
85 send_length
= (size_t) snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
86 "delete %s%.*s%s\r\n",
88 (int)key_length
, key
, no_reply
? " noreply" :"");
90 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
92 rc
= MEMCACHED_WRITE_FAILURE
;
96 if (ptr
->flags
.use_udp
&& !to_write
)
98 if (send_length
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
99 return MEMCACHED_WRITE_FAILURE
;
100 if (send_length
+ ptr
->hosts
[server_key
].write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
101 memcached_io_write(&ptr
->hosts
[server_key
], NULL
, 0, 1);
104 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, send_length
, to_write
);
107 if (rc
!= MEMCACHED_SUCCESS
)
111 rc
= MEMCACHED_BUFFERED
;
114 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
115 if (rc
== MEMCACHED_DELETED
)
116 rc
= MEMCACHED_SUCCESS
;
119 if (rc
== MEMCACHED_SUCCESS
&& ptr
->delete_trigger
)
120 ptr
->delete_trigger(ptr
, key
, key_length
);
123 LIBMEMCACHED_MEMCACHED_DELETE_END();
127 static inline memcached_return_t
binary_delete(memcached_st
*ptr
,
128 unsigned int server_key
,
133 protocol_binary_request_delete request
= {.bytes
= {0}};
135 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
136 if (ptr
->flags
.no_reply
)
137 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETEQ
;
139 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETE
;
140 request
.message
.header
.request
.keylen
= htons((uint16_t)key_length
);
141 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
142 request
.message
.header
.request
.bodylen
= htonl((uint32_t) key_length
);
144 if (ptr
->flags
.use_udp
&& !flush
)
146 size_t cmd_size
= sizeof(request
.bytes
) + key_length
;
147 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
148 return MEMCACHED_WRITE_FAILURE
;
149 if (cmd_size
+ ptr
->hosts
[server_key
].write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
150 memcached_io_write(&ptr
->hosts
[server_key
], NULL
, 0, 1);
153 memcached_return_t rc
= MEMCACHED_SUCCESS
;
155 if ((memcached_do(&ptr
->hosts
[server_key
], request
.bytes
,
156 sizeof(request
.bytes
), 0) != MEMCACHED_SUCCESS
) ||
157 (memcached_io_write(&ptr
->hosts
[server_key
], key
,
158 key_length
, (char) flush
) == -1))
160 memcached_io_reset(&ptr
->hosts
[server_key
]);
161 rc
= MEMCACHED_WRITE_FAILURE
;
164 unlikely (ptr
->number_of_replicas
> 0)
166 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETEQ
;
168 for (uint32_t x
= 0; x
< ptr
->number_of_replicas
; ++x
)
171 if (server_key
== ptr
->number_of_hosts
)
174 memcached_server_st
* server
= &ptr
->hosts
[server_key
];
175 if ((memcached_do(server
, (const char*)request
.bytes
,
176 sizeof(request
.bytes
), 0) != MEMCACHED_SUCCESS
) ||
177 (memcached_io_write(server
, key
, key_length
, (char) flush
) == -1))
178 memcached_io_reset(server
);
180 memcached_server_response_decrement(server
);