1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions are
11 * * Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
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
19 * * The names of its contributors may not be used to endorse or
20 * promote products derived from this software without specific prior
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.
37 #include <libmemcached/common.h>
39 static memcached_return_t
memcached_flush_binary(memcached_st
*ptr
,
41 static memcached_return_t
memcached_flush_textual(memcached_st
*ptr
,
44 memcached_return_t
memcached_flush(memcached_st
*ptr
, time_t expiration
)
46 memcached_return_t rc
;
47 if (memcached_failed(rc
= initialize_query(ptr
)))
52 LIBMEMCACHED_MEMCACHED_FLUSH_START();
53 if (ptr
->flags
.binary_protocol
)
55 rc
= memcached_flush_binary(ptr
, expiration
);
59 rc
= memcached_flush_textual(ptr
, expiration
);
61 LIBMEMCACHED_MEMCACHED_FLUSH_END();
66 static memcached_return_t
memcached_flush_textual(memcached_st
*ptr
,
69 bool reply
= ptr
->flags
.no_reply
? false : true;
71 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
75 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
76 "flush_all %llu%s\r\n",
77 (unsigned long long)expiration
, reply
? "" : " noreply");
81 send_length
= snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
82 "flush_all%s\r\n", reply
? "" : " noreply");
85 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
or send_length
< 0)
87 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
88 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
92 memcached_return_t rc
= MEMCACHED_SUCCESS
;
93 for (unsigned int x
= 0; x
< memcached_server_count(ptr
); x
++)
95 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, x
);
97 memcached_return_t rrc
= memcached_do(instance
, buffer
, (size_t)send_length
, true);
98 if (rrc
== MEMCACHED_SUCCESS
and reply
== true)
100 char response_buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
101 rrc
= memcached_response(instance
, response_buffer
, sizeof(response_buffer
), NULL
);
104 if (memcached_failed(rrc
))
106 // If an error has already been reported, then don't add to it
107 if (instance
->error_messages
== NULL
)
109 memcached_set_error(*instance
, rrc
, MEMCACHED_AT
);
111 rc
= MEMCACHED_SOME_ERRORS
;
118 static memcached_return_t
memcached_flush_binary(memcached_st
*ptr
,
121 protocol_binary_request_flush request
= {};
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
);
130 memcached_return_t rc
= MEMCACHED_SUCCESS
;
132 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
134 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, x
);
136 if (ptr
->flags
.no_reply
)
138 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_FLUSHQ
;
142 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_FLUSH
;
145 memcached_return_t rrc
;
146 if ((rrc
= memcached_do(instance
, request
.bytes
, sizeof(request
.bytes
), true)))
148 memcached_set_error(*instance
, rrc
, MEMCACHED_AT
);
149 memcached_io_reset(instance
);
150 rc
= MEMCACHED_SOME_ERRORS
;
154 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
156 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, x
);
158 if (memcached_server_response_count(instance
) > 0)
160 (void)memcached_response(instance
, NULL
, 0, NULL
);