8da6b5b57b10cc663689db00b2a24f4051c0839f
[awesomized/libmemcached] / libmemcached / flush.c
1 #include "common.h"
2
3 static memcached_return_t memcached_flush_binary(memcached_st *ptr,
4 time_t expiration);
5 static memcached_return_t memcached_flush_textual(memcached_st *ptr,
6 time_t expiration);
7
8 memcached_return_t memcached_flush(memcached_st *ptr, time_t expiration)
9 {
10 memcached_return_t rc;
11 if ((rc= initialize_query(ptr)) != MEMCACHED_SUCCESS)
12 {
13 return rc;
14 }
15
16 LIBMEMCACHED_MEMCACHED_FLUSH_START();
17 if (ptr->flags.binary_protocol)
18 rc= memcached_flush_binary(ptr, expiration);
19 else
20 rc= memcached_flush_textual(ptr, expiration);
21 LIBMEMCACHED_MEMCACHED_FLUSH_END();
22 return rc;
23 }
24
25 static memcached_return_t memcached_flush_textual(memcached_st *ptr,
26 time_t expiration)
27 {
28 unlikely (memcached_server_count(ptr) == 0)
29 return MEMCACHED_NO_SERVERS;
30
31 for (unsigned int x= 0; x < memcached_server_count(ptr); x++)
32 {
33 memcached_return_t rc;
34 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
35
36 bool no_reply= ptr->flags.no_reply;
37 memcached_server_write_instance_st instance=
38 memcached_server_instance_fetch(ptr, x);
39
40 int send_length;
41 if (expiration)
42 {
43 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
44 "flush_all %llu%s\r\n",
45 (unsigned long long)expiration, no_reply ? " noreply" : "");
46 }
47 else
48 {
49 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
50 "flush_all%s\r\n", no_reply ? " noreply" : "");
51 }
52
53 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE || send_length < 0)
54 {
55 return MEMCACHED_FAILURE;
56 }
57
58 rc= memcached_do(instance, buffer, (size_t)send_length, true);
59
60 if (rc == MEMCACHED_SUCCESS && !no_reply)
61 (void)memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
62 }
63
64 return MEMCACHED_SUCCESS;
65 }
66
67 static memcached_return_t memcached_flush_binary(memcached_st *ptr,
68 time_t expiration)
69 {
70 protocol_binary_request_flush request= {.bytes= {0}};
71
72 unlikely (memcached_server_count(ptr) == 0)
73 return MEMCACHED_NO_SERVERS;
74
75 request.message.header.request.magic= (uint8_t)PROTOCOL_BINARY_REQ;
76 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH;
77 request.message.header.request.extlen= 4;
78 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
79 request.message.header.request.bodylen= htonl(request.message.header.request.extlen);
80 request.message.body.expiration= htonl((uint32_t) expiration);
81
82 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
83 {
84 memcached_server_write_instance_st instance=
85 memcached_server_instance_fetch(ptr, x);
86
87 if (ptr->flags.no_reply)
88 {
89 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSHQ;
90 }
91 else
92 {
93 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH;
94 }
95
96 if (memcached_do(instance, request.bytes, sizeof(request.bytes), true) != MEMCACHED_SUCCESS)
97 {
98 memcached_io_reset(instance);
99 return MEMCACHED_WRITE_FAILURE;
100 }
101 }
102
103 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
104 {
105 memcached_server_write_instance_st instance=
106 memcached_server_instance_fetch(ptr, x);
107
108 if (memcached_server_response_count(instance) > 0)
109 (void)memcached_response(instance, NULL, 0, NULL);
110 }
111
112 return MEMCACHED_SUCCESS;
113 }