5ec2fca31891cf3803d5856ebd7694de71f6ef29
[awesomized/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 unlikely (key_length == 0)
30 return MEMCACHED_NO_KEY_PROVIDED;
31
32 unlikely (ptr->hosts == NULL || ptr->number_of_hosts == 0)
33 return MEMCACHED_NO_SERVERS;
34
35 server_key= memcached_generate_hash(ptr, master_key, master_key_length);
36 to_write= (ptr->flags & MEM_BUFFER_REQUESTS) ? 0 : 1;
37
38 if (ptr->flags & MEM_BINARY_PROTOCOL)
39 rc= binary_delete(ptr, server_key, key, key_length, to_write);
40 else
41 {
42 if (expiration)
43 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
44 "delete %s%.*s %llu\r\n",
45 ptr->prefix_key,
46 (int)key_length, key,
47 (unsigned long long)expiration);
48 else
49 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
50 "delete %s%.*s\r\n",
51 ptr->prefix_key,
52 (int)key_length, key);
53
54 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
55 {
56 rc= MEMCACHED_WRITE_FAILURE;
57 goto error;
58 }
59
60 rc= memcached_do(&ptr->hosts[server_key], buffer, send_length, to_write);
61 }
62
63 if (rc != MEMCACHED_SUCCESS)
64 goto error;
65
66 if ((ptr->flags & MEM_BUFFER_REQUESTS))
67 {
68 rc= MEMCACHED_BUFFERED;
69 }
70 else
71 {
72 rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
73 if (rc == MEMCACHED_DELETED)
74 rc= MEMCACHED_SUCCESS;
75 }
76
77 if (rc == MEMCACHED_SUCCESS && ptr->delete_trigger)
78 ptr->delete_trigger(ptr, key, key_length);
79
80 error:
81 LIBMEMCACHED_MEMCACHED_DELETE_END();
82 return rc;
83 }
84
85 static inline memcached_return binary_delete(memcached_st *ptr,
86 unsigned int server_key,
87 const char *key,
88 size_t key_length,
89 int flush)
90 {
91 protocol_binary_request_delete request= {0};
92
93 request.message.header.request.magic= PROTOCOL_BINARY_REQ;
94 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_DELETE;
95 request.message.header.request.keylen= htons((uint16_t)key_length);
96 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
97 request.message.header.request.bodylen= htonl(key_length);
98
99 if ((memcached_do(&ptr->hosts[server_key], request.bytes,
100 sizeof(request.bytes), 0) != MEMCACHED_SUCCESS) ||
101 (memcached_io_write(&ptr->hosts[server_key], key,
102 key_length, flush) == -1))
103 {
104 memcached_io_reset(&ptr->hosts[server_key]);
105 return MEMCACHED_WRITE_FAILURE;
106 }
107
108 return MEMCACHED_SUCCESS;
109 }