Initial support for the binary protocol
[awesomized/libmemcached] / libmemcached / memcached_flush.c
1 #include "common.h"
2
3 static memcached_return memcached_flush_binary(memcached_st *ptr,
4 time_t expiration);
5 static memcached_return memcached_flush_textual(memcached_st *ptr,
6 time_t expiration);
7
8 memcached_return memcached_flush(memcached_st *ptr, time_t expiration)
9 {
10 memcached_return rc;
11
12 LIBMEMCACHED_MEMCACHED_FLUSH_START();
13 if (ptr->flags & MEM_BINARY_PROTOCOL)
14 rc= memcached_flush_binary(ptr, expiration);
15 else
16 rc= memcached_flush_textual(ptr, expiration);
17 LIBMEMCACHED_MEMCACHED_FLUSH_END();
18 return rc;
19 }
20
21 static memcached_return memcached_flush_textual(memcached_st *ptr,
22 time_t expiration)
23 {
24 unsigned int x;
25 size_t send_length;
26 memcached_return rc;
27 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
28
29 unlikely (ptr->number_of_hosts == 0)
30 return MEMCACHED_NO_SERVERS;
31
32 for (x= 0; x < ptr->number_of_hosts; x++)
33 {
34 if (expiration)
35 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
36 "flush_all %llu\r\n", (unsigned long long)expiration);
37 else
38 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
39 "flush_all\r\n");
40
41 rc= memcached_do(&ptr->hosts[x], buffer, send_length, 1);
42
43 if (rc == MEMCACHED_SUCCESS)
44 (void)memcached_response(&ptr->hosts[x], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
45 }
46
47 return MEMCACHED_SUCCESS;
48 }
49
50 static memcached_return memcached_flush_binary(memcached_st *ptr,
51 time_t expiration)
52 {
53 unsigned int x;
54 protocol_binary_request_flush request= {0};
55
56 unlikely (ptr->number_of_hosts == 0)
57 return MEMCACHED_NO_SERVERS;
58
59 request.message.header.request.magic= (uint8_t)PROTOCOL_BINARY_REQ;
60 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH;
61 request.message.header.request.extlen= 4;
62 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
63 request.message.header.request.bodylen= htonl(request.message.header.request.extlen);
64 request.message.body.expiration= htonl(expiration);
65
66 for (x= 0; x < ptr->number_of_hosts; x++)
67 {
68 if (memcached_do(&ptr->hosts[x], request.bytes,
69 sizeof(request.bytes), 1) != MEMCACHED_SUCCESS)
70 {
71 memcached_io_reset(&ptr->hosts[x]);
72 return MEMCACHED_WRITE_FAILURE;
73 }
74 }
75
76 for (x= 0; x < ptr->number_of_hosts; x++)
77 {
78 if (memcached_server_response_count(&ptr->hosts[x]) > 0)
79 (void)memcached_response(&ptr->hosts[x], NULL, 0, NULL);
80 }
81
82 return MEMCACHED_SUCCESS;
83 }