attempt to fix #12, #49 and #65
[awesomized/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 *ptr,
40 time_t expiration,
41 const bool reply)
42 {
43 protocol_binary_request_flush request= {};
44
45 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH;
46 request.message.header.request.extlen= 4;
47 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
48 request.message.header.request.bodylen= htonl(request.message.header.request.extlen);
49 request.message.body.expiration= htonl((uint32_t) expiration);
50
51 memcached_return_t rc= MEMCACHED_SUCCESS;
52
53 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
54 {
55 memcached_instance_st* instance= memcached_instance_fetch(ptr, x);
56 initialize_binary_request(instance, request.message.header);
57
58 if (reply)
59 {
60 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH;
61 }
62 else
63 {
64 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSHQ;
65 }
66
67 libmemcached_io_vector_st vector[]=
68 {
69 { NULL, 0 },
70 { request.bytes, sizeof(request.bytes) }
71 };
72
73 memcached_return_t rrc;
74 if (memcached_failed(rrc= memcached_vdo(instance, vector, 2, true)))
75 {
76 if (instance->error_messages == NULL or instance->root->error_messages == NULL)
77 {
78 memcached_set_error(*instance, rrc, MEMCACHED_AT);
79 }
80 rc= MEMCACHED_SOME_ERRORS;
81 }
82 }
83
84 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
85 {
86 memcached_instance_st* instance= memcached_instance_fetch(ptr, x);
87
88 if (instance->response_count() > 0)
89 {
90 (void)memcached_response(instance, NULL, 0, NULL);
91 }
92 }
93
94 return rc;
95 }
96
97 static memcached_return_t memcached_flush_textual(Memcached *ptr,
98 time_t expiration,
99 const bool reply)
100 {
101 char buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH +1];
102 int send_length= 0;
103 if (expiration)
104 {
105 send_length= snprintf(buffer, sizeof(buffer), "%llu", (unsigned long long)expiration);
106 }
107
108 if (size_t(send_length) >= sizeof(buffer) or send_length < 0)
109 {
110 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
111 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
112 }
113
114 memcached_return_t rc= MEMCACHED_SUCCESS;
115 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
116 {
117 memcached_instance_st* instance= memcached_instance_fetch(ptr, x);
118
119 libmemcached_io_vector_st vector[]=
120 {
121 { NULL, 0 },
122 { memcached_literal_param("flush_all ") },
123 { buffer, size_t(send_length) },
124 { " noreply", reply ? 0 : memcached_literal_param_size(" noreply") },
125 { memcached_literal_param("\r\n") }
126 };
127
128 memcached_return_t rrc= memcached_vdo(instance, vector, 5, true);
129 if (memcached_success(rrc) and reply == true)
130 {
131 char response_buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
132 rrc= memcached_response(instance, response_buffer, sizeof(response_buffer), NULL);
133 }
134
135 if (memcached_failed(rrc))
136 {
137 // If an error has already been reported, then don't add to it
138 if (instance->error_messages == NULL or instance->root->error_messages == NULL)
139 {
140 memcached_set_error(*instance, rrc, MEMCACHED_AT);
141 }
142 rc= MEMCACHED_SOME_ERRORS;
143 }
144 }
145
146 return rc;
147 }
148
149 memcached_return_t memcached_flush(memcached_st *shell, time_t expiration)
150 {
151 Memcached* ptr= memcached2Memcached(shell);
152 memcached_return_t rc;
153 if (memcached_failed(rc= initialize_query(ptr, true)))
154 {
155 return rc;
156 }
157
158 bool reply= memcached_is_replying(ptr);
159
160 LIBMEMCACHED_MEMCACHED_FLUSH_START();
161 if (memcached_is_binary(ptr))
162 {
163 rc= memcached_flush_binary(ptr, expiration, reply);
164 }
165 else
166 {
167 rc= memcached_flush_textual(ptr, expiration, reply);
168 }
169 LIBMEMCACHED_MEMCACHED_FLUSH_END();
170
171 return rc;
172 }