Incomming fixes from Charles on the replication branch.
[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= (ptr->flags & MEM_BUFFER_REQUESTS) ? 0 : 1;
16 size_t send_length;
17 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
18 unsigned int server_key;
19 uint8_t replicas= 0;
20 memcached_return rc[MEMCACHED_MAX_REPLICAS];
21
22 unlikely (key_length == 0)
23 return MEMCACHED_NO_KEY_PROVIDED;
24
25 unlikely (ptr->hosts == NULL || ptr->number_of_hosts == 0)
26 return MEMCACHED_NO_SERVERS;
27
28 server_key= memcached_generate_hash(ptr, master_key, master_key_length);
29
30 if (expiration)
31 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
32 "delete %.*s %llu\r\n", (int)key_length, key,
33 (unsigned long long)expiration);
34 else
35 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
36 "delete %.*s\r\n", (int)key_length, key);
37
38 unlikely (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
39 return MEMCACHED_WRITE_FAILURE;
40
41 do
42 {
43 rc[replicas]= memcached_do(&ptr->hosts[server_key], buffer, send_length, to_write);
44 if (rc[replicas] != MEMCACHED_SUCCESS)
45 goto error;
46
47 if ((ptr->flags & MEM_BUFFER_REQUESTS))
48 {
49 rc[replicas]= MEMCACHED_BUFFERED;
50 }
51 else
52 {
53 char response_buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
54
55 rc[replicas]= memcached_response(&ptr->hosts[server_key], response_buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
56 }
57
58 /* On error we just jump to the next potential server */
59 error:
60 if (ptr->number_of_replicas > 1)
61 {
62 if (server_key == (ptr->number_of_hosts - 1))
63 server_key= 0;
64 else
65 server_key++;
66 }
67 } while ((++replicas) < ptr->number_of_replicas);
68
69 /* As long as one object gets stored, we count this as a success */
70 while (replicas--)
71 {
72 if (rc[replicas] == MEMCACHED_DELETED)
73 return MEMCACHED_SUCCESS;
74 else if (rc[replicas] == MEMCACHED_BUFFERED)
75 return MEMCACHED_BUFFERED;
76 }
77
78 return rc[0];
79 }