c279241118c4e70224e00aa1a60c215ed15aca0d
[awesomized/libmemcached] / src / libmemcached / flush.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020-2021 Michael Wallner https://awesome.co/ |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "libmemcached/common.h"
17
18 static memcached_return_t memcached_flush_binary(Memcached *ptr, time_t expiration,
19 const bool reply) {
20 protocol_binary_request_flush request = {};
21
22 request.message.header.request.opcode = PROTOCOL_BINARY_CMD_FLUSH;
23 request.message.header.request.extlen = 4;
24 request.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES;
25 request.message.header.request.bodylen = htonl(request.message.header.request.extlen);
26 request.message.body.expiration = htonl((uint32_t) expiration);
27
28 memcached_return_t rc = MEMCACHED_SUCCESS;
29
30 for (uint32_t x = 0; x < memcached_server_count(ptr); x++) {
31 memcached_instance_st *instance = memcached_instance_fetch(ptr, x);
32 initialize_binary_request(instance, request.message.header);
33
34 if (reply) {
35 request.message.header.request.opcode = PROTOCOL_BINARY_CMD_FLUSH;
36 } else {
37 request.message.header.request.opcode = PROTOCOL_BINARY_CMD_FLUSHQ;
38 }
39
40 libmemcached_io_vector_st vector[] = {{NULL, 0}, {request.bytes, sizeof(request.bytes)}};
41
42 memcached_return_t rrc;
43 if (memcached_failed(rrc = memcached_vdo(instance, vector, 2, true))) {
44 if (instance->error_messages == NULL or instance->root->error_messages == NULL) {
45 memcached_set_error(*instance, rrc, MEMCACHED_AT);
46 }
47 rc = MEMCACHED_SOME_ERRORS;
48 }
49 }
50
51 for (uint32_t x = 0; x < memcached_server_count(ptr); x++) {
52 memcached_instance_st *instance = memcached_instance_fetch(ptr, x);
53
54 if (instance->response_count() > 0) {
55 (void) memcached_response(instance, NULL, 0, NULL);
56 }
57 }
58
59 return rc;
60 }
61
62 static memcached_return_t memcached_flush_textual(Memcached *ptr, time_t expiration,
63 const bool reply) {
64 char buffer[MEMCACHED_MAXIMUM_INTEGER_DISPLAY_LENGTH + 1];
65 int send_length = 0;
66 if (expiration) {
67 send_length = snprintf(buffer, sizeof(buffer), "%llu", (unsigned long long) expiration);
68 }
69
70 if (size_t(send_length) >= sizeof(buffer) or send_length < 0) {
71 return memcached_set_error(*ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
72 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
73 }
74
75 memcached_return_t rc = MEMCACHED_SUCCESS;
76 for (uint32_t x = 0; x < memcached_server_count(ptr); x++) {
77 memcached_instance_st *instance = memcached_instance_fetch(ptr, x);
78
79 libmemcached_io_vector_st vector[] = {
80 {NULL, 0},
81 {memcached_literal_param("flush_all ")},
82 {buffer, size_t(send_length)},
83 {" noreply", reply ? 0 : memcached_literal_param_size(" noreply")},
84 {memcached_literal_param("\r\n")}};
85
86 memcached_return_t rrc = memcached_vdo(instance, vector, 5, true);
87 if (memcached_success(rrc) and reply == true) {
88 char response_buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
89 rrc = memcached_response(instance, response_buffer, sizeof(response_buffer), NULL);
90 }
91
92 if (memcached_failed(rrc)) {
93 // If an error has already been reported, then don't add to it
94 if (instance->error_messages == NULL or instance->root->error_messages == NULL) {
95 memcached_set_error(*instance, rrc, MEMCACHED_AT);
96 }
97 rc = MEMCACHED_SOME_ERRORS;
98 }
99 }
100
101 return rc;
102 }
103
104 memcached_return_t memcached_flush(memcached_st *shell, time_t expiration) {
105 Memcached *ptr = memcached2Memcached(shell);
106 memcached_return_t rc;
107 if (memcached_failed(rc = initialize_query(ptr, true))) {
108 return rc;
109 }
110
111 bool reply = memcached_is_replying(ptr);
112
113 LIBMEMCACHED_MEMCACHED_FLUSH_START();
114 if (memcached_is_binary(ptr)) {
115 rc = memcached_flush_binary(ptr, expiration, reply);
116 } else {
117 rc = memcached_flush_textual(ptr, expiration, reply);
118 }
119 LIBMEMCACHED_MEMCACHED_FLUSH_END();
120
121 return rc;
122 }