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
*ptr
,
43 protocol_binary_request_flush request
= {};
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
);
51 memcached_return_t rc
= MEMCACHED_SUCCESS
;
53 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
55 memcached_instance_st
* instance
= memcached_instance_fetch(ptr
, x
);
56 initialize_binary_request(instance
, request
.message
.header
);
60 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_FLUSH
;
64 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_FLUSHQ
;
67 libmemcached_io_vector_st vector
[]=
70 { request
.bytes
, sizeof(request
.bytes
) }
73 memcached_return_t rrc
;
74 if (memcached_failed(rrc
= memcached_vdo(instance
, vector
, 2, true)))
76 if (instance
->error_messages
== NULL
or instance
->root
->error_messages
== NULL
)
78 memcached_set_error(*instance
, rrc
, MEMCACHED_AT
);
80 memcached_io_reset(instance
);
81 rc
= MEMCACHED_SOME_ERRORS
;
85 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
87 memcached_instance_st
* instance
= memcached_instance_fetch(ptr
, x
);
89 if (instance
->response_count() > 0)
91 (void)memcached_response(instance
, NULL
, 0, NULL
);
98 static memcached_return_t
memcached_flush_textual(Memcached
*ptr
,
102 char buffer
[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH
+1];
106 send_length
= snprintf(buffer
, sizeof(buffer
), "%llu", (unsigned long long)expiration
);
109 if (size_t(send_length
) >= sizeof(buffer
) or send_length
< 0)
111 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
112 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
115 memcached_return_t rc
= MEMCACHED_SUCCESS
;
116 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
118 memcached_instance_st
* instance
= memcached_instance_fetch(ptr
, x
);
120 libmemcached_io_vector_st vector
[]=
123 { memcached_literal_param("flush_all ") },
124 { buffer
, size_t(send_length
) },
125 { " noreply", reply
? 0 : memcached_literal_param_size(" noreply") },
126 { memcached_literal_param("\r\n") }
129 memcached_return_t rrc
= memcached_vdo(instance
, vector
, 5, true);
130 if (memcached_success(rrc
) and reply
== true)
132 char response_buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
133 rrc
= memcached_response(instance
, response_buffer
, sizeof(response_buffer
), NULL
);
136 if (memcached_failed(rrc
))
138 // If an error has already been reported, then don't add to it
139 if (instance
->error_messages
== NULL
or instance
->root
->error_messages
== NULL
)
141 memcached_set_error(*instance
, rrc
, MEMCACHED_AT
);
143 rc
= MEMCACHED_SOME_ERRORS
;
150 memcached_return_t
memcached_flush(memcached_st
*shell
, time_t expiration
)
152 Memcached
* ptr
= memcached2Memcached(shell
);
153 memcached_return_t rc
;
154 if (memcached_failed(rc
= initialize_query(ptr
, true)))
159 bool reply
= memcached_is_replying(ptr
);
161 LIBMEMCACHED_MEMCACHED_FLUSH_START();
162 if (memcached_is_binary(ptr
))
164 rc
= memcached_flush_binary(ptr
, expiration
, reply
);
168 rc
= memcached_flush_textual(ptr
, expiration
, reply
);
170 LIBMEMCACHED_MEMCACHED_FLUSH_END();