merge in brian's change to "Update for async delete"
[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 char to_write;
7 size_t send_length, sent_length;
8 memcached_return rc;
9 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
10 unsigned int server_key;
11
12 LIBMEMCACHED_MEMCACHED_DELETE_START();
13
14 server_key= memcached_generate_hash(ptr, key, key_length);
15
16 if ((rc= memcached_connect(ptr, server_key)) != MEMCACHED_SUCCESS)
17 return rc;
18
19
20 if (expiration)
21 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
22 "delete %.*s %llu\r\n", (int)key_length, key,
23 (unsigned long long)expiration);
24 else
25 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
26 "delete %.*s\r\n", (int)key_length, key);
27
28 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
29 {
30 rc= MEMCACHED_WRITE_FAILURE;
31 goto error;
32 }
33
34 if ((ptr->flags & MEM_NO_BLOCK))
35 to_write= 0;
36 else
37 to_write= 1;
38
39 if ((sent_length= memcached_io_write(ptr, server_key, buffer, send_length, to_write)) == -1)
40 {
41 memcached_quit_server(ptr, server_key);
42 rc= MEMCACHED_WRITE_FAILURE;
43 goto error;
44 }
45
46 if ((ptr->flags & MEM_NO_BLOCK))
47 {
48 rc= MEMCACHED_SUCCESS;
49 memcached_server_response_increment(ptr, server_key);
50 }
51 else
52 {
53 rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
54 if (rc == MEMCACHED_DELETED)
55 rc= MEMCACHED_SUCCESS;
56 }
57
58 LIBMEMCACHED_MEMCACHED_DELETE_END();
59
60 error:
61 return rc;
62 }