Update flush.
[m6w6/libmemcached] / libmemcached / flush.cc
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
9 * met:
10 *
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 *
14 * * Redistributions in binary form must reproduce the above
15 * copyright notice, this list of conditions and the following disclaimer
16 * in the documentation and/or other materials provided with the
17 * distribution.
18 *
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
21 * written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
24 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
25 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
26 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
27 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
28 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
29 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
30 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
31 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
32 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
33 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
34 *
35 */
36
37 #include <libmemcached/common.h>
38
39 static memcached_return_t memcached_flush_binary(memcached_st *ptr,
40 time_t expiration);
41 static memcached_return_t memcached_flush_textual(memcached_st *ptr,
42 time_t expiration);
43
44 memcached_return_t memcached_flush(memcached_st *ptr, time_t expiration)
45 {
46 memcached_return_t rc;
47 if (memcached_failed(rc= initialize_query(ptr)))
48 {
49 return rc;
50 }
51
52 LIBMEMCACHED_MEMCACHED_FLUSH_START();
53 if (ptr->flags.binary_protocol)
54 {
55 rc= memcached_flush_binary(ptr, expiration);
56 }
57 else
58 {
59 rc= memcached_flush_textual(ptr, expiration);
60 }
61 LIBMEMCACHED_MEMCACHED_FLUSH_END();
62
63 return rc;
64 }
65
66 static memcached_return_t memcached_flush_textual(memcached_st *ptr,
67 time_t expiration)
68 {
69 // Invert the logic to make it simpler to read the code
70 bool reply= (ptr->flags.no_reply) ? false : true;
71
72 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
73 int send_length= 0;
74 if (expiration)
75 {
76 send_length= snprintf(buffer, sizeof(buffer), "%llu", (unsigned long long)expiration);
77 }
78
79 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE or send_length < 0)
80 {
81 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
82 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
83 }
84
85 struct libmemcached_io_vector_st vector[]=
86 {
87 { memcached_literal_param("flush_all ") },
88 { buffer, send_length },
89 { " noreply", reply ? 0 : memcached_literal_param_size(" noreply") },
90 { memcached_literal_param("\r\n") }
91 };
92
93 memcached_return_t rc= MEMCACHED_SUCCESS;
94 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
95 {
96 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
97
98 memcached_return_t rrc= memcached_vdo(instance, vector, 4, true);
99 if (rrc == MEMCACHED_SUCCESS and reply == true)
100 {
101 char response_buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
102 rrc= memcached_response(instance, response_buffer, sizeof(response_buffer), NULL);
103 }
104
105 if (memcached_failed(rrc))
106 {
107 // If an error has already been reported, then don't add to it
108 if (instance->error_messages == NULL)
109 {
110 memcached_set_error(*instance, rrc, MEMCACHED_AT);
111 }
112 rc= MEMCACHED_SOME_ERRORS;
113 }
114 }
115
116 return rc;
117 }
118
119 static memcached_return_t memcached_flush_binary(memcached_st *ptr,
120 time_t expiration)
121 {
122 protocol_binary_request_flush request= {};
123
124 request.message.header.request.magic= (uint8_t)PROTOCOL_BINARY_REQ;
125 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH;
126 request.message.header.request.extlen= 4;
127 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
128 request.message.header.request.bodylen= htonl(request.message.header.request.extlen);
129 request.message.body.expiration= htonl((uint32_t) expiration);
130
131 memcached_return_t rc= MEMCACHED_SUCCESS;
132
133 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
134 {
135 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
136
137 if (ptr->flags.no_reply)
138 {
139 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSHQ;
140 }
141 else
142 {
143 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH;
144 }
145
146 memcached_return_t rrc;
147 if ((rrc= memcached_do(instance, request.bytes, sizeof(request.bytes), true)))
148 {
149 memcached_set_error(*instance, rrc, MEMCACHED_AT);
150 memcached_io_reset(instance);
151 rc= MEMCACHED_SOME_ERRORS;
152 }
153 }
154
155 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
156 {
157 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
158
159 if (memcached_server_response_count(instance) > 0)
160 {
161 (void)memcached_response(instance, NULL, 0, NULL);
162 }
163 }
164
165 return rc;
166 }