First merge of Trond's patches (cherry picking).
[m6w6/libmemcached] / libmemcached / memcached_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
12 LIBMEMCACHED_MEMCACHED_FLUSH_START();
13 if (ptr->flags.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_t memcached_flush_textual(memcached_st *ptr,
22 time_t expiration)
23 {
24 unsigned int x;
25 size_t send_length;
26 memcached_return_t 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 bool no_reply= ptr->flags.no_reply;
35
36 if (expiration)
37 send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
38 "flush_all %llu%s\r\n",
39 (unsigned long long)expiration, no_reply ? " noreply" : "");
40 else
41 send_length= (size_t) snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
42 "flush_all%s\r\n", no_reply ? " noreply" : "");
43
44 rc= memcached_do(&ptr->hosts[x], buffer, send_length, 1);
45
46 if (rc == MEMCACHED_SUCCESS && !no_reply)
47 (void)memcached_response(&ptr->hosts[x], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
48 }
49
50 return MEMCACHED_SUCCESS;
51 }
52
53 static memcached_return_t memcached_flush_binary(memcached_st *ptr,
54 time_t expiration)
55 {
56 unsigned int x;
57 protocol_binary_request_flush request= {.bytes= {0}};
58
59 unlikely (ptr->number_of_hosts == 0)
60 return MEMCACHED_NO_SERVERS;
61
62 request.message.header.request.magic= (uint8_t)PROTOCOL_BINARY_REQ;
63 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH;
64 request.message.header.request.extlen= 4;
65 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
66 request.message.header.request.bodylen= htonl(request.message.header.request.extlen);
67 request.message.body.expiration= htonl((uint32_t) expiration);
68
69 for (x= 0; x < ptr->number_of_hosts; x++)
70 {
71 if (ptr->flags.no_reply)
72 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSHQ;
73 else
74 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH;
75 if (memcached_do(&ptr->hosts[x], request.bytes,
76 sizeof(request.bytes), 1) != MEMCACHED_SUCCESS)
77 {
78 memcached_io_reset(&ptr->hosts[x]);
79 return MEMCACHED_WRITE_FAILURE;
80 }
81 }
82
83 for (x= 0; x < ptr->number_of_hosts; x++)
84 {
85 if (memcached_server_response_count(&ptr->hosts[x]) > 0)
86 (void)memcached_response(&ptr->hosts[x], NULL, 0, NULL);
87 }
88
89 return MEMCACHED_SUCCESS;
90 }