Merge in fixes for UDP
[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 bool reply= ptr->flags.no_reply ? false : true;
70
71 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
72 int send_length;
73 if (expiration)
74 {
75 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
76 "flush_all %llu%s\r\n",
77 (unsigned long long)expiration, reply ? "" : " noreply");
78 }
79 else
80 {
81 send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
82 "flush_all%s\r\n", reply ? "" : " noreply");
83 }
84
85 if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE or send_length < 0)
86 {
87 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
88 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
89 }
90
91
92 memcached_return_t rc= MEMCACHED_SUCCESS;
93 for (unsigned int x= 0; x < memcached_server_count(ptr); x++)
94 {
95 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
96
97 memcached_return_t rrc= memcached_do(instance, buffer, (size_t)send_length, true);
98 if (rrc == MEMCACHED_SUCCESS and reply == true)
99 {
100 char response_buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
101 rrc= memcached_response(instance, response_buffer, sizeof(response_buffer), NULL);
102 }
103
104 if (memcached_failed(rrc))
105 {
106 // If an error has already been reported, then don't add to it
107 if (instance->error_messages == NULL)
108 {
109 memcached_set_error(*instance, rrc, MEMCACHED_AT);
110 }
111 rc= MEMCACHED_SOME_ERRORS;
112 }
113 }
114
115 return rc;
116 }
117
118 static memcached_return_t memcached_flush_binary(memcached_st *ptr,
119 time_t expiration)
120 {
121 protocol_binary_request_flush request= {};
122
123 request.message.header.request.magic= (uint8_t)PROTOCOL_BINARY_REQ;
124 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH;
125 request.message.header.request.extlen= 4;
126 request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
127 request.message.header.request.bodylen= htonl(request.message.header.request.extlen);
128 request.message.body.expiration= htonl((uint32_t) expiration);
129
130 memcached_return_t rc= MEMCACHED_SUCCESS;
131
132 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
133 {
134 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
135
136 if (ptr->flags.no_reply)
137 {
138 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSHQ;
139 }
140 else
141 {
142 request.message.header.request.opcode= PROTOCOL_BINARY_CMD_FLUSH;
143 }
144
145 memcached_return_t rrc;
146 if ((rrc= memcached_do(instance, request.bytes, sizeof(request.bytes), true)))
147 {
148 memcached_set_error(*instance, rrc, MEMCACHED_AT);
149 memcached_io_reset(instance);
150 rc= MEMCACHED_SOME_ERRORS;
151 }
152 }
153
154 for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
155 {
156 memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
157
158 if (memcached_server_response_count(instance) > 0)
159 {
160 (void)memcached_response(instance, NULL, 0, NULL);
161 }
162 }
163
164 return rc;
165 }