Fix for solaris.
[m6w6/libmemcached] / libmemcached / delete.c
1 #include "common.h"
2 #include "memcached/protocol_binary.h"
3
4 memcached_return_t 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_t binary_delete(memcached_st *ptr,
12 unsigned int server_key,
13 const char *key,
14 size_t key_length,
15 uint8_t flush);
16
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,
20 time_t expiration)
21 {
22 uint8_t to_write;
23 size_t send_length;
24 memcached_return_t 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.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= (uint8_t)((ptr->flags.buffer_requests) ? 0 : 1);
40 bool no_reply= (ptr->flags.no_reply);
41
42 if (ptr->flags.binary_protocol)
43 {
44 likely (!expiration)
45 rc= binary_delete(ptr, server_key, key, key_length, to_write);
46 else
47 rc= MEMCACHED_INVALID_ARGUMENTS;
48 }
49 else
50 {
51 unlikely (expiration)
52 {
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)
56 {
57 rc= MEMCACHED_INVALID_ARGUMENTS;
58 goto error;
59 }
60 else
61 {
62 if (ptr->hosts[server_key].minor_version == 0)
63 {
64 if (no_reply || !to_write)
65 {
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.
69 */
70 to_write= 1;
71 if (no_reply)
72 memcached_server_response_increment(&ptr->hosts[server_key]);
73 no_reply= false;
74 }
75 }
76 send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
77 "delete %s%.*s %u%s\r\n",
78 ptr->prefix_key,
79 (int) key_length, key,
80 (uint32_t)expiration,
81 no_reply ? " noreply" :"" );
82 }
83 }
84 else
85 send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
86 "delete %s%.*s%s\r\n",
87 ptr->prefix_key,
88 (int)key_length, key, no_reply ? " noreply" :"");
89
90 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
91 {
92 rc= MEMCACHED_WRITE_FAILURE;
93 goto error;
94 }
95
96 if (ptr->flags.use_udp && !to_write)
97 {
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);
102 }
103
104 rc= memcached_do(&ptr->hosts[server_key], buffer, send_length, to_write);
105 }
106
107 if (rc != MEMCACHED_SUCCESS)
108 goto error;
109
110 if (!to_write)
111 rc= MEMCACHED_BUFFERED;
112 else if (!no_reply)
113 {
114 rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
115 if (rc == MEMCACHED_DELETED)
116 rc= MEMCACHED_SUCCESS;
117 }
118
119 if (rc == MEMCACHED_SUCCESS && ptr->delete_trigger)
120 ptr->delete_trigger(ptr, key, key_length);
121
122 error:
123 LIBMEMCACHED_MEMCACHED_DELETE_END();
124 return rc;
125 }
126
127 static inline memcached_return_t binary_delete(memcached_st *ptr,
128 unsigned int server_key,
129 const char *key,
130 size_t key_length,
131 uint8_t flush)
132 {
133 protocol_binary_request_delete request= {.bytes= {0}};
134
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;
138 else
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);
143
144 if (ptr->flags.use_udp && !flush)
145 {
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);
151 }
152
153 memcached_return_t rc= MEMCACHED_SUCCESS;
154
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))
159 {
160 memcached_io_reset(&ptr->hosts[server_key]);
161 rc= MEMCACHED_WRITE_FAILURE;
162 }
163
164 unlikely (ptr->number_of_replicas > 0)
165 {
166 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
167
168 for (uint32_t x= 0; x < ptr->number_of_replicas; ++x)
169 {
170 ++server_key;
171 if (server_key == ptr->number_of_hosts)
172 server_key= 0;
173
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);
179 else
180 memcached_server_response_decrement(server);
181 }
182 }
183
184 return rc;
185 }