Fix from Trond for deprecation
[m6w6/libmemcached] / libmemcached / memcached_delete.c
1 #include "common.h"
2 #include "memcached/protocol_binary.h"
3
4 memcached_return memcached_delete(memcached_st *ptr, const char *key, size_t key_length,
5 time_t expiration)
6 {
7 return memcached_delete_by_key(ptr, key, key_length,
8 key, key_length, expiration);
9 }
10
11 static inline memcached_return binary_delete(memcached_st *ptr,
12 unsigned int server_key,
13 const char *key,
14 size_t key_length,
15 int flush);
16
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,
20 time_t expiration)
21 {
22 char to_write;
23 size_t send_length;
24 memcached_return rc;
25 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
26 unsigned int server_key;
27
28 LIBMEMCACHED_MEMCACHED_DELETE_START();
29
30 rc= memcached_validate_key_length(key_length,
31 ptr->flags & MEM_BINARY_PROTOCOL);
32 unlikely (rc != MEMCACHED_SUCCESS)
33 return rc;
34
35 unlikely (ptr->hosts == NULL || ptr->number_of_hosts == 0)
36 return MEMCACHED_NO_SERVERS;
37
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);
41
42 if (ptr->flags & MEM_BINARY_PROTOCOL)
43 rc= binary_delete(ptr, server_key, key, key_length, to_write);
44 else
45 {
46 if (expiration)
47 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
48 "delete %s%.*s %u%s\r\n",
49 ptr->prefix_key,
50 (int)key_length, key,
51 (uint32_t)expiration, no_reply ? " noreply" :"" );
52 else
53 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
54 "delete %s%.*s%s\r\n",
55 ptr->prefix_key,
56 (int)key_length, key, no_reply ? " noreply" :"");
57
58 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
59 {
60 rc= MEMCACHED_WRITE_FAILURE;
61 goto error;
62 }
63
64 if (ptr->flags & MEM_USE_UDP && !to_write)
65 {
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);
70 }
71
72 rc= memcached_do(&ptr->hosts[server_key], buffer, send_length, to_write);
73 }
74
75 if (rc != MEMCACHED_SUCCESS)
76 goto error;
77
78 if ((ptr->flags & MEM_BUFFER_REQUESTS))
79 rc= MEMCACHED_BUFFERED;
80 else if (!no_reply)
81 {
82 rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
83 if (rc == MEMCACHED_DELETED)
84 rc= MEMCACHED_SUCCESS;
85 }
86
87 if (rc == MEMCACHED_SUCCESS && ptr->delete_trigger)
88 ptr->delete_trigger(ptr, key, key_length);
89
90 error:
91 LIBMEMCACHED_MEMCACHED_DELETE_END();
92 return rc;
93 }
94
95 static inline memcached_return binary_delete(memcached_st *ptr,
96 unsigned int server_key,
97 const char *key,
98 size_t key_length,
99 int flush)
100 {
101 protocol_binary_request_delete request= {.bytes= {0}};
102
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;
106 else
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);
111
112 if (ptr->flags & MEM_USE_UDP && !flush)
113 {
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);
119 }
120
121 memcached_return rc= MEMCACHED_SUCCESS;
122
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))
127 {
128 memcached_io_reset(&ptr->hosts[server_key]);
129 rc= MEMCACHED_WRITE_FAILURE;
130 }
131
132 unlikely (ptr->number_of_replicas > 0)
133 {
134 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
135
136 for (uint32_t x= 0; x < ptr->number_of_replicas; ++x)
137 {
138 ++server_key;
139 if (server_key == ptr->number_of_hosts)
140 server_key= 0;
141
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);
147 }
148 }
149
150 return rc;
151 }