memcached_behavior() causes commands to be buffered until they are flushed to
[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 to_write= (ptr->flags & MEM_BUFFER_REQUESTS) ? 0 : 1;
46
47 rc= memcached_do(ptr, server_key, buffer, send_length, to_write);
48 if (rc != MEMCACHED_SUCCESS)
49 goto error;
50
51 if ((ptr->flags & MEM_BUFFER_REQUESTS))
52 {
53 rc= MEMCACHED_BUFFERED;
54 }
55 else
56 {
57 rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL, server_key);
58 if (rc == MEMCACHED_DELETED)
59 rc= MEMCACHED_SUCCESS;
60 }
61
62 error:
63 LIBMEMCACHED_MEMCACHED_DELETE_END();
64 return rc;
65 }
66
67 memcached_return memcached_mdelete(memcached_st *ptr,
68 char **key, size_t *key_length,
69 unsigned int number_of_keys,
70 time_t expiration)
71 {
72 return memcached_mdelete_by_key(ptr, NULL, 0,
73 key, key_length,
74 number_of_keys, expiration);
75
76 }
77
78 memcached_return memcached_mdelete_by_key(memcached_st *ptr,
79 char *master_key, size_t master_key_length,
80 char **key, size_t *key_length,
81 unsigned int number_of_keys,
82 time_t expiration)
83 {
84 size_t send_length;
85 memcached_return rc= MEMCACHED_SUCCESS;
86 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
87 unsigned int master_server_key= 0;
88 unsigned int x;
89
90 LIBMEMCACHED_MEMCACHED_DELETE_START();
91
92 if (ptr->hosts == NULL || ptr->number_of_hosts == 0)
93 return MEMCACHED_NO_SERVERS;
94
95 if (master_key && master_key_length)
96 master_server_key= memcached_generate_hash(ptr, master_key, master_key_length);
97
98 for (x= 0; x < number_of_keys; x++)
99 {
100 unsigned int server_key;
101
102 if (master_key && master_key_length)
103 server_key= master_server_key;
104 else
105 server_key= memcached_generate_hash(ptr, key[x], key_length[x]);
106
107 if (expiration)
108 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
109 "delete %.*s %llu\r\n", (int)(key_length[x]), key[x],
110 (unsigned long long)expiration);
111 else
112 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
113 "delete %.*s\r\n", (int)(key_length[x]), key[x]);
114
115 (void)memcached_do(ptr, server_key, buffer, send_length, 0);
116 }
117
118 return MEMCACHED_BUFFERED;
119 }