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
, true)))
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 // Invert the logic to make it simpler to read the code
70 bool reply
= memcached_is_replying(ptr
);
72 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
76 send_length
= snprintf(buffer
, sizeof(buffer
), "%llu", (unsigned long long)expiration
);
79 if (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
or send_length
< 0)
81 return memcached_set_error(*ptr
, MEMCACHED_MEMORY_ALLOCATION_FAILURE
, MEMCACHED_AT
,
82 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
85 libmemcached_io_vector_st vector
[]=
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") }
93 memcached_return_t rc
= MEMCACHED_SUCCESS
;
94 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
96 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, x
);
98 memcached_return_t rrc
= memcached_vdo(instance
, vector
, 4, true);
99 if (rrc
== MEMCACHED_SUCCESS
and reply
== true)
101 char response_buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
102 rrc
= memcached_response(instance
, response_buffer
, sizeof(response_buffer
), NULL
);
105 if (memcached_failed(rrc
))
107 // If an error has already been reported, then don't add to it
108 if (instance
->error_messages
== NULL
)
110 memcached_set_error(*instance
, rrc
, MEMCACHED_AT
);
112 rc
= MEMCACHED_SOME_ERRORS
;
119 static memcached_return_t
memcached_flush_binary(memcached_st
*ptr
,
122 protocol_binary_request_flush request
= {};
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
);
131 memcached_return_t rc
= MEMCACHED_SUCCESS
;
133 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
135 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, x
);
137 if (memcached_is_replying(ptr
))
139 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_FLUSH
;
143 request
.message
.header
.request
.opcode
= PROTOCOL_BINARY_CMD_FLUSHQ
;
146 libmemcached_io_vector_st vector
[]=
148 { request
.bytes
, sizeof(request
.bytes
) }
151 memcached_return_t rrc
;
152 if ((rrc
= memcached_vdo(instance
, vector
, 1, true)))
154 memcached_set_error(*instance
, rrc
, MEMCACHED_AT
);
155 memcached_io_reset(instance
);
156 rc
= MEMCACHED_SOME_ERRORS
;
160 for (uint32_t x
= 0; x
< memcached_server_count(ptr
); x
++)
162 memcached_server_write_instance_st instance
= memcached_server_instance_fetch(ptr
, x
);
164 if (memcached_server_response_count(instance
) > 0)
166 (void)memcached_response(instance
, NULL
, 0, NULL
);