2 #include "memcached/protocol_binary.h"
4 memcached_return
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
binary_delete(memcached_st
*ptr
,
12 unsigned int server_key
,
17 memcached_return
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
,
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
& MEM_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
= (ptr
->flags
& MEM_BUFFER_REQUESTS
) ? 0 : 1;
40 bool no_reply
= (ptr
->flags
& MEM_NOREPLY
);
42 if (ptr
->flags
& MEM_BINARY_PROTOCOL
)
43 rc
= binary_delete(ptr
, server_key
, key
, key_length
, to_write
);
47 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
48 "delete %s%.*s %u%s\r\n",
51 (uint32_t)expiration
, no_reply
? " noreply" :"" );
53 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
54 "delete %s%.*s%s\r\n",
56 (int)key_length
, key
, no_reply
? " noreply" :"");
58 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
60 rc
= MEMCACHED_WRITE_FAILURE
;
64 if (ptr
->flags
& MEM_USE_UDP
&& !to_write
)
66 if (send_length
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
67 return MEMCACHED_WRITE_FAILURE
;
68 if (send_length
+ ptr
->hosts
[server_key
].write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
69 memcached_io_write(&ptr
->hosts
[server_key
], NULL
, 0, 1);
72 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, send_length
, to_write
);
75 if (rc
!= MEMCACHED_SUCCESS
)
78 if ((ptr
->flags
& MEM_BUFFER_REQUESTS
))
79 rc
= MEMCACHED_BUFFERED
;
82 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
83 if (rc
== MEMCACHED_DELETED
)
84 rc
= MEMCACHED_SUCCESS
;
87 if (rc
== MEMCACHED_SUCCESS
&& ptr
->delete_trigger
)
88 ptr
->delete_trigger(ptr
, key
, key_length
);
91 LIBMEMCACHED_MEMCACHED_DELETE_END();
95 static inline memcached_return
binary_delete(memcached_st
*ptr
,
96 unsigned int server_key
,
101 protocol_binary_request_delete request
= {.bytes
= {0}};
103 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
104 if (ptr
->flags
& MEM_NOREPLY
)
105 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETEQ
;
107 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETE
;
108 request
.message
.header
.request
.keylen
= htons((uint16_t)key_length
);
109 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
110 request
.message
.header
.request
.bodylen
= htonl(key_length
);
112 if (ptr
->flags
& MEM_USE_UDP
&& !flush
)
114 size_t cmd_size
= sizeof(request
.bytes
) + key_length
;
115 if (cmd_size
> MAX_UDP_DATAGRAM_LENGTH
- UDP_DATAGRAM_HEADER_LENGTH
)
116 return MEMCACHED_WRITE_FAILURE
;
117 if (cmd_size
+ ptr
->hosts
[server_key
].write_buffer_offset
> MAX_UDP_DATAGRAM_LENGTH
)
118 memcached_io_write(&ptr
->hosts
[server_key
], NULL
, 0, 1);
121 memcached_return rc
= MEMCACHED_SUCCESS
;
123 if ((memcached_do(&ptr
->hosts
[server_key
], request
.bytes
,
124 sizeof(request
.bytes
), 0) != MEMCACHED_SUCCESS
) ||
125 (memcached_io_write(&ptr
->hosts
[server_key
], key
,
126 key_length
, flush
) == -1))
128 memcached_io_reset(&ptr
->hosts
[server_key
]);
129 rc
= MEMCACHED_WRITE_FAILURE
;
132 unlikely (ptr
->number_of_replicas
> 0)
134 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_DELETEQ
;
136 for (uint32_t x
= 0; x
< ptr
->number_of_replicas
; ++x
)
139 if (server_key
== ptr
->number_of_hosts
)
142 memcached_server_st
* server
= &ptr
->hosts
[server_key
];
143 if ((memcached_do(server
, (const char*)request
.bytes
,
144 sizeof(request
.bytes
), 0) != MEMCACHED_SUCCESS
) ||
145 (memcached_io_write(server
, key
, key_length
, flush
) == -1))
146 memcached_io_reset(server
);