Added srcdir includes so that the include files could be found in vpath builds.
[m6w6/libmemcached] / libmemcached / memcached_delete.c
1 #include "common.h"
2
3 memcached_return memcached_delete(memcached_st *ptr, const char *key, size_t key_length,
4 time_t expiration)
5 {
6 return memcached_delete_by_key(ptr, key, key_length,
7 key, key_length, expiration);
8 }
9
10 static inline memcached_return binary_delete(memcached_st *ptr,
11 unsigned int server_key,
12 const char *key,
13 size_t key_length,
14 int flush);
15
16 memcached_return memcached_delete_by_key(memcached_st *ptr,
17 const char *master_key, size_t master_key_length,
18 const char *key, size_t key_length,
19 time_t expiration)
20 {
21 char to_write;
22 size_t send_length;
23 memcached_return rc;
24 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
25 unsigned int server_key;
26
27 LIBMEMCACHED_MEMCACHED_DELETE_START();
28
29 rc= memcached_validate_key_length(key_length,
30 ptr->flags & MEM_BINARY_PROTOCOL);
31 unlikely (rc != MEMCACHED_SUCCESS)
32 return rc;
33
34 unlikely (ptr->hosts == NULL || ptr->number_of_hosts == 0)
35 return MEMCACHED_NO_SERVERS;
36
37 server_key= memcached_generate_hash(ptr, master_key, master_key_length);
38 to_write= (ptr->flags & MEM_BUFFER_REQUESTS) ? 0 : 1;
39 bool no_reply= (ptr->flags & MEM_NOREPLY);
40
41 if (ptr->flags & MEM_BINARY_PROTOCOL)
42 rc= binary_delete(ptr, server_key, key, key_length, to_write);
43 else
44 {
45 if (expiration)
46 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
47 "delete %s%.*s %u%s\r\n",
48 ptr->prefix_key,
49 (int)key_length, key,
50 (uint32_t)expiration, no_reply ? " noreply" :"" );
51 else
52 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
53 "delete %s%.*s%s\r\n",
54 ptr->prefix_key,
55 (int)key_length, key, no_reply ? " noreply" :"");
56
57 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
58 {
59 rc= MEMCACHED_WRITE_FAILURE;
60 goto error;
61 }
62
63 if (ptr->flags & MEM_USE_UDP && !to_write)
64 {
65 if (send_length > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
66 return MEMCACHED_WRITE_FAILURE;
67 if (send_length + ptr->hosts[server_key].write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
68 memcached_io_write(&ptr->hosts[server_key], NULL, 0, 1);
69 }
70
71 rc= memcached_do(&ptr->hosts[server_key], buffer, send_length, to_write);
72 }
73
74 if (rc != MEMCACHED_SUCCESS)
75 goto error;
76
77 if ((ptr->flags & MEM_BUFFER_REQUESTS))
78 rc= MEMCACHED_BUFFERED;
79 else if (!no_reply)
80 {
81 rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
82 if (rc == MEMCACHED_DELETED)
83 rc= MEMCACHED_SUCCESS;
84 }
85
86 if (rc == MEMCACHED_SUCCESS && ptr->delete_trigger)
87 ptr->delete_trigger(ptr, key, key_length);
88
89 error:
90 LIBMEMCACHED_MEMCACHED_DELETE_END();
91 return rc;
92 }
93
94 static inline memcached_return binary_delete(memcached_st *ptr,
95 unsigned int server_key,
96 const char *key,
97 size_t key_length,
98 int flush)
99 {
100 protocol_binary_request_delete request= {.bytes= {0}};
101
102 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
103 if (ptr->flags & MEM_NOREPLY)
104 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETEQ;
105 else
106 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETE;
107 request.message.header.request.keylen= htons((uint16_t)key_length);
108 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
109 request.message.header.request.bodylen= htonl(key_length);
110
111 if (ptr->flags & MEM_USE_UDP && !flush)
112 {
113 size_t cmd_size= sizeof(request.bytes) + key_length;
114 if (cmd_size > MAX_UDP_DATAGRAM_LENGTH - UDP_DATAGRAM_HEADER_LENGTH)
115 return MEMCACHED_WRITE_FAILURE;
116 if (cmd_size + ptr->hosts[server_key].write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
117 memcached_io_write(&ptr->hosts[server_key], NULL, 0, 1);
118 }
119
120 if ((memcached_do(&ptr->hosts[server_key], request.bytes,
121 sizeof(request.bytes), 0) != MEMCACHED_SUCCESS) ||
122 (memcached_io_write(&ptr->hosts[server_key], key,
123 key_length, flush) == -1))
124 {
125 memcached_io_reset(&ptr->hosts[server_key]);
126 return MEMCACHED_WRITE_FAILURE;
127 }
128
129 return MEMCACHED_SUCCESS;
130 }