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
|| memcached_server_count(ptr
) == 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 /* ensure that we are connected, otherwise we might bump the
63 * command counter before connection */
64 if ((rc
= memcached_connect(&ptr
->hosts
[server_key
])) != MEMCACHED_SUCCESS
)
70 if (ptr
->hosts
[server_key
].minor_version
== 0)
72 if (no_reply
|| !to_write
)
74 /* We might get out of sync with the server if we
75 * send this command to a server newer than 1.2.x..
76 * disable no_reply and buffered mode.
80 memcached_server_response_increment(&ptr
->hosts
[server_key
]);
84 send_length
= (size_t) snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
85 "delete %s%.*s %u%s\r\n",
87 (int) key_length
, key
,
89 no_reply
? " noreply" :"" );
93 send_length
= (size_t) snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
94 "delete %s%.*s%s\r\n",
96 (int)key_length
, key
, no_reply
? " noreply" :"");
98 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
100 rc
= MEMCACHED_WRITE_FAILURE
;
104 if (ptr
->flags
.use_udp
&& !to_write
)
106 if (send_length
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
107 return MEMCACHED_WRITE_FAILURE
;
108 if (send_length
+ ptr
->hosts
[server_key
].write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
109 memcached_io_write(&ptr
->hosts
[server_key
], NULL
, 0, 1);
112 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, send_length
, to_write
);
115 if (rc
!= MEMCACHED_SUCCESS
)
119 rc
= MEMCACHED_BUFFERED
;
122 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
123 if (rc
== MEMCACHED_DELETED
)
124 rc
= MEMCACHED_SUCCESS
;
127 if (rc
== MEMCACHED_SUCCESS
&& ptr
->delete_trigger
)
128 ptr
->delete_trigger(ptr
, key
, key_length
);
131 LIBMEMCACHED_MEMCACHED_DELETE_END();
135 static inline memcached_return_t
binary_delete(memcached_st
*ptr
,
136 unsigned int server_key
,
141 protocol_binary_request_delete request
= {.bytes
= {0}};
143 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
144 if (ptr
->flags
.no_reply
)
145 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETEQ
;
147 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETE
;
148 request
.message
.header
.request
.keylen
= htons((uint16_t)key_length
);
149 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
150 request
.message
.header
.request
.bodylen
= htonl((uint32_t) key_length
);
152 if (ptr
->flags
.use_udp
&& !flush
)
154 size_t cmd_size
= sizeof(request
.bytes
) + key_length
;
155 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
156 return MEMCACHED_WRITE_FAILURE
;
157 if (cmd_size
+ ptr
->hosts
[server_key
].write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
158 memcached_io_write(&ptr
->hosts
[server_key
], NULL
, 0, 1);
161 memcached_return_t rc
= MEMCACHED_SUCCESS
;
163 if ((memcached_do(&ptr
->hosts
[server_key
], request
.bytes
,
164 sizeof(request
.bytes
), 0) != MEMCACHED_SUCCESS
) ||
165 (memcached_io_write(&ptr
->hosts
[server_key
], key
,
166 key_length
, (char) flush
) == -1))
168 memcached_io_reset(&ptr
->hosts
[server_key
]);
169 rc
= MEMCACHED_WRITE_FAILURE
;
172 unlikely (ptr
->number_of_replicas
> 0)
174 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETEQ
;
176 for (uint32_t x
= 0; x
< ptr
->number_of_replicas
; ++x
)
179 if (server_key
== memcached_server_count(ptr
))
182 memcached_server_st
* server
= &ptr
->hosts
[server_key
];
183 if ((memcached_do(server
, (const char*)request
.bytes
,
184 sizeof(request
.bytes
), 0) != MEMCACHED_SUCCESS
) ||
185 (memcached_io_write(server
, key
, key_length
, (char) flush
) == -1))
186 memcached_io_reset(server
);
188 memcached_server_response_decrement(server
);