Code for multi delete functions.
[awesomized/libmemcached] / lib / memcached_delete.c
1 #include "common.h"
2
3 memcached_return memcached_delete(memcached_st *ptr, 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 memcached_return memcached_delete_by_key(memcached_st *ptr,
11 char *master_key, size_t master_key_length,
12 char *key, size_t key_length,
13 time_t expiration)
14 {
15 char to_write;
16 size_t send_length;
17 memcached_return rc;
18 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
19 unsigned int server_key;
20
21 LIBMEMCACHED_MEMCACHED_DELETE_START();
22
23 if (key_length == 0)
24 return MEMCACHED_NO_KEY_PROVIDED;
25
26 if (ptr->hosts == NULL || ptr->number_of_hosts == 0)
27 return MEMCACHED_NO_SERVERS;
28
29 server_key= memcached_generate_hash(ptr, master_key, master_key_length);
30
31 if (expiration)
32 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
33 "delete %.*s %llu\r\n", (int)key_length, key,
34 (unsigned long long)expiration);
35 else
36 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
37 "delete %.*s\r\n", (int)key_length, key);
38
39 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
40 {
41 rc= MEMCACHED_WRITE_FAILURE;
42 goto error;
43 }
44
45 if ((ptr->flags & MEM_NO_BLOCK))
46 to_write= 0;
47 else
48 to_write= 1;
49
50 rc= memcached_do(ptr, server_key, buffer, send_length, to_write);
51 if (rc != MEMCACHED_SUCCESS)
52 goto error;
53
54 if ((ptr->flags & MEM_NO_BLOCK))
55 {
56 rc= MEMCACHED_SUCCESS;
57 }
58 else
59 {
60 rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL, server_key);
61 if (rc == MEMCACHED_DELETED)
62 rc= MEMCACHED_SUCCESS;
63 }
64
65 error:
66 LIBMEMCACHED_MEMCACHED_DELETE_END();
67 return rc;
68 }
69
70 memcached_return memcached_mdelete(memcached_st *ptr,
71 char **key, size_t *key_length,
72 unsigned int number_of_keys,
73 time_t expiration)
74 {
75 return memcached_mdelete_by_key(ptr, NULL, 0,
76 key, key_length,
77 number_of_keys, expiration);
78
79 }
80
81 memcached_return memcached_mdelete_by_key(memcached_st *ptr,
82 char *master_key, size_t master_key_length,
83 char **key, size_t *key_length,
84 unsigned int number_of_keys,
85 time_t expiration)
86 {
87 size_t send_length;
88 memcached_return rc= MEMCACHED_SUCCESS;
89 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
90 unsigned int master_server_key= 0;
91 unsigned int x;
92
93 LIBMEMCACHED_MEMCACHED_DELETE_START();
94
95 if (ptr->hosts == NULL || ptr->number_of_hosts == 0)
96 return MEMCACHED_NO_SERVERS;
97
98 if (master_key && master_key_length)
99 master_server_key= memcached_generate_hash(ptr, master_key, master_key_length);
100
101 for (x= 0; x < number_of_keys; x++)
102 {
103 unsigned int server_key;
104
105 if (master_key && master_key_length)
106 server_key= master_server_key;
107 else
108 server_key= memcached_generate_hash(ptr, key[x], key_length[x]);
109
110 if (expiration)
111 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
112 "delete %.*s %llu\r\n", (int)(key_length[x]), key[x],
113 (unsigned long long)expiration);
114 else
115 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
116 "delete %.*s\r\n", (int)(key_length[x]), key[x]);
117
118 (void)memcached_do(ptr, server_key, buffer, send_length, 0);
119 }
120
121 for (x= 0; x < ptr->number_of_hosts; x++)
122 {
123 if (memcached_server_response_count(ptr, x))
124 {
125 /* We need to do something about non-connnected hosts in the future */
126 if ((memcached_io_write(ptr, x, NULL, 0, 1)) == -1)
127 {
128 rc= MEMCACHED_SOME_ERRORS;
129 }
130 }
131 }
132
133 LIBMEMCACHED_MEMCACHED_DELETE_END();
134 return rc;
135 }