libmemcached: add MEMCACHED_BEHAVIOR_META_PROTOCOL
[awesomized/libmemcached] / src / libmemcached / auto.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached-awesome - 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 void auto_response(memcached_instance_st *instance, const bool reply, memcached_return_t &rc,
19 uint64_t *value) {
20 // If the message was successfully sent, then get the response, otherwise
21 // fail.
22 if (memcached_success(rc)) {
23 if (reply == false) {
24 *value = UINT64_MAX;
25 return;
26 }
27
28 rc = memcached_response(instance, &instance->root->result);
29 }
30
31 if (memcached_fatal(rc)) {
32 assert(memcached_last_error(instance->root) != MEMCACHED_SUCCESS);
33 *value = UINT64_MAX;
34 } else if (memcached_failed(rc)) {
35 *value = UINT64_MAX;
36 } else {
37 assert(memcached_last_error(instance->root) != MEMCACHED_NOTFOUND);
38 *value = instance->root->result.numeric_value;
39 }
40 }
41
42 static memcached_return_t meta_incr_decr(memcached_instance_st *instance, bool is_incr, bool w_init,
43 const char *key, size_t key_len,
44 uint64_t offset, uint64_t initial, uint32_t expiration) {
45 char new_buf[32] = " N", inl_buf[32] = " J", dlt_buf[32] = " D", exp_buf[32] = " T";
46 size_t new_len = strlen(new_buf), inl_len = strlen(inl_buf), dlt_len = strlen(dlt_buf), exp_len = strlen(exp_buf);
47 size_t io_num = 0;
48 libmemcached_io_vector_st io_vec[10] = {};
49
50 io_vec[io_num++] = {memcached_literal_param("ma ")};
51 io_vec[io_num++] = {memcached_array_string(instance->root->_namespace),
52 memcached_array_size(instance->root->_namespace)},
53 io_vec[io_num++] = {key, key_len};
54
55 if (!is_incr) {
56 io_vec[io_num++] = {memcached_literal_param(" MD")};
57 }
58 if (w_init) {
59 new_len += snprintf(new_buf + new_len, sizeof(new_buf) - new_len, "%" PRIu32, expiration);
60 io_vec[io_num++] = {new_buf, new_len};
61 inl_len += snprintf(inl_buf + inl_len, sizeof(inl_buf) - inl_len, "%" PRIu64, initial);
62 io_vec[io_num++] = {inl_buf, inl_len};
63 }
64 if (offset != 1) {
65 dlt_len += snprintf(dlt_buf + dlt_len, sizeof(dlt_buf) - dlt_len, "%" PRIu64, offset);
66 io_vec[io_num++] = {dlt_buf, dlt_len};
67 }
68 if (expiration) {
69 exp_len += snprintf(exp_buf + exp_len, sizeof(exp_buf) - exp_len, "%" PRIu32, expiration);
70 io_vec[io_num++] = {exp_buf, exp_len};
71 }
72
73 if (memcached_is_replying(instance->root)) {
74 io_vec[io_num++] = {memcached_literal_param(" v")};
75 } else {
76 io_vec[io_num++] = {memcached_literal_param(" q")};
77 }
78 io_vec[io_num++] = {memcached_literal_param(" O+\r\n")};
79
80 return memcached_vdo(instance, io_vec, io_num, true);
81 }
82
83 static memcached_return_t text_incr_decr(memcached_instance_st *instance, const bool is_incr,
84 const char *key, size_t key_length, const uint64_t offset,
85 const bool reply) {
86 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
87
88 int send_length = snprintf(buffer, sizeof(buffer), " %" PRIu64, offset);
89 if (size_t(send_length) >= sizeof(buffer) or send_length < 0) {
90 return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
91 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
92 }
93
94 libmemcached_io_vector_st vector[] = {
95 {NULL, 0},
96 {memcached_literal_param("incr ")},
97 {memcached_array_string(instance->root->_namespace),
98 memcached_array_size(instance->root->_namespace)},
99 {key, key_length},
100 {buffer, size_t(send_length)},
101 {" noreply", reply ? 0 : memcached_literal_param_size(" noreply")},
102 {memcached_literal_param("\r\n")}};
103
104 if (is_incr == false) {
105 vector[1].buffer = "decr ";
106 }
107
108 return memcached_vdo(instance, vector, 7, true);
109 }
110
111 static memcached_return_t binary_incr_decr(memcached_instance_st *instance,
112 protocol_binary_command cmd, const char *key,
113 const size_t key_length, const uint64_t offset,
114 const uint64_t initial, const uint32_t expiration,
115 const bool reply) {
116 if (reply == false) {
117 if (cmd == PROTOCOL_BINARY_CMD_DECREMENT) {
118 cmd = PROTOCOL_BINARY_CMD_DECREMENTQ;
119 }
120
121 if (cmd == PROTOCOL_BINARY_CMD_INCREMENT) {
122 cmd = PROTOCOL_BINARY_CMD_INCREMENTQ;
123 }
124 }
125 protocol_binary_request_incr request = {}; // = {.bytes= {0}};
126
127 initialize_binary_request(instance, request.message.header);
128
129 request.message.header.request.opcode = cmd;
130 request.message.header.request.keylen =
131 htons((uint16_t)(key_length + memcached_array_size(instance->root->_namespace)));
132 request.message.header.request.extlen = 20;
133 request.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES;
134 request.message.header.request.bodylen =
135 htonl((uint32_t)(key_length + memcached_array_size(instance->root->_namespace)
136 + request.message.header.request.extlen));
137 request.message.body.delta = memcached_htonll(offset);
138 request.message.body.initial = memcached_htonll(initial);
139 request.message.body.expiration = htonl((uint32_t) expiration);
140
141 libmemcached_io_vector_st vector[] = {{NULL, 0},
142 {request.bytes, sizeof(request.bytes)},
143 {memcached_array_string(instance->root->_namespace),
144 memcached_array_size(instance->root->_namespace)},
145 {key, key_length}};
146
147 return memcached_vdo(instance, vector, 4, true);
148 }
149
150 memcached_return_t memcached_increment(memcached_st *memc, const char *key, size_t key_length,
151 uint32_t offset, uint64_t *value) {
152 return memcached_increment_by_key(memc, key, key_length, key, key_length, offset, value);
153 }
154
155 static memcached_return_t increment_decrement_by_key(const protocol_binary_command command,
156 Memcached *memc, const char *group_key,
157 size_t group_key_length, const char *key,
158 size_t key_length, uint64_t offset,
159 uint64_t *value) {
160 uint64_t local_value;
161 if (value == NULL) {
162 value = &local_value;
163 }
164
165 memcached_return_t rc;
166 if (memcached_failed(rc = initialize_query(memc, true))) {
167 return rc;
168 }
169
170 if (memcached_is_encrypted(memc)) {
171 return memcached_set_error(
172 *memc, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT,
173 memcached_literal_param("Operation not allowed while encyrption is enabled"));
174 }
175
176 if (memcached_failed(rc = memcached_key_test(*memc, (const char **) &key, &key_length, 1))) {
177 return memcached_last_error(memc);
178 }
179
180 uint32_t server_key =
181 memcached_generate_hash_with_redistribution(memc, group_key, group_key_length);
182 memcached_instance_st *instance = memcached_instance_fetch(memc, server_key);
183
184 bool reply = memcached_is_replying(instance->root);
185
186 if (memcached_is_binary(memc)) {
187 rc = binary_incr_decr(instance, command, key, key_length, uint64_t(offset), 0,
188 MEMCACHED_EXPIRATION_NOT_ADD, reply);
189 } else if (memcached_is_meta(memc)) {
190 rc = meta_incr_decr(instance, command == PROTOCOL_BINARY_CMD_INCREMENT, false, key, key_length, offset, 0, 0);
191 } else {
192 rc = text_incr_decr(instance, command == PROTOCOL_BINARY_CMD_INCREMENT ? true : false, key,
193 key_length, offset, reply);
194 }
195
196 auto_response(instance, reply, rc, value);
197
198 return rc;
199 }
200
201 static memcached_return_t
202 increment_decrement_with_initial_by_key(const protocol_binary_command command, Memcached *memc,
203 const char *group_key, size_t group_key_length,
204 const char *key, size_t key_length, uint64_t offset,
205 uint64_t initial, time_t expiration, uint64_t *value) {
206 uint64_t local_value;
207 if (value == NULL) {
208 value = &local_value;
209 }
210
211 memcached_return_t rc;
212 if (memcached_failed(rc = initialize_query(memc, true))) {
213 return rc;
214 }
215
216 if (memcached_is_encrypted(memc)) {
217 return memcached_set_error(
218 *memc, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT,
219 memcached_literal_param("Operation not allowed while encryption is enabled"));
220 }
221
222 if (memcached_failed(rc = memcached_key_test(*memc, (const char **) &key, &key_length, 1))) {
223 return memcached_last_error(memc);
224 }
225
226 uint32_t server_key =
227 memcached_generate_hash_with_redistribution(memc, group_key, group_key_length);
228 memcached_instance_st *instance = memcached_instance_fetch(memc, server_key);
229
230 bool reply = memcached_is_replying(instance->root);
231
232 if (memcached_is_binary(memc)) {
233 rc = binary_incr_decr(instance, command, key, key_length, offset, initial, uint32_t(expiration),
234 reply);
235 } else if (memcached_is_meta(memc)) {
236 rc = meta_incr_decr(instance, command == PROTOCOL_BINARY_CMD_INCREMENT, true,
237 key, key_length, offset, initial, uint32_t(expiration));
238 } else {
239 rc = memcached_set_error(
240 *memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
241 memcached_literal_param(
242 "memcached_increment_with_initial_by_key() is not supported via the ASCII protocol"));
243 }
244
245 auto_response(instance, reply, rc, value);
246
247 return rc;
248 }
249
250 memcached_return_t memcached_decrement(memcached_st *memc, const char *key, size_t key_length,
251 uint32_t offset, uint64_t *value) {
252 return memcached_decrement_by_key(memc, key, key_length, key, key_length, offset, value);
253 }
254
255 memcached_return_t memcached_increment_by_key(memcached_st *shell, const char *group_key,
256 size_t group_key_length, const char *key,
257 size_t key_length, uint64_t offset, uint64_t *value) {
258 Memcached *memc = memcached2Memcached(shell);
259 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
260 memcached_return_t rc =
261 increment_decrement_by_key(PROTOCOL_BINARY_CMD_INCREMENT, memc, group_key, group_key_length,
262 key, key_length, offset, value);
263
264 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
265
266 return rc;
267 }
268
269 memcached_return_t memcached_decrement_by_key(memcached_st *shell, const char *group_key,
270 size_t group_key_length, const char *key,
271 size_t key_length, uint64_t offset, uint64_t *value) {
272 Memcached *memc = memcached2Memcached(shell);
273 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
274 memcached_return_t rc =
275 increment_decrement_by_key(PROTOCOL_BINARY_CMD_DECREMENT, memc, group_key, group_key_length,
276 key, key_length, offset, value);
277 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
278
279 return rc;
280 }
281
282 memcached_return_t memcached_increment_with_initial(memcached_st *memc, const char *key,
283 size_t key_length, uint64_t offset,
284 uint64_t initial, time_t expiration,
285 uint64_t *value) {
286 return memcached_increment_with_initial_by_key(memc, key, key_length, key, key_length, offset,
287 initial, expiration, value);
288 }
289
290 memcached_return_t memcached_increment_with_initial_by_key(
291 memcached_st *shell, const char *group_key, size_t group_key_length, const char *key,
292 size_t key_length, uint64_t offset, uint64_t initial, time_t expiration, uint64_t *value) {
293 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
294 Memcached *memc = memcached2Memcached(shell);
295 memcached_return_t rc = increment_decrement_with_initial_by_key(
296 PROTOCOL_BINARY_CMD_INCREMENT, memc, group_key, group_key_length, key, key_length, offset,
297 initial, expiration, value);
298 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
299
300 return rc;
301 }
302
303 memcached_return_t memcached_decrement_with_initial(memcached_st *memc, const char *key,
304 size_t key_length, uint64_t offset,
305 uint64_t initial, time_t expiration,
306 uint64_t *value) {
307 return memcached_decrement_with_initial_by_key(memc, key, key_length, key, key_length, offset,
308 initial, expiration, value);
309 }
310
311 memcached_return_t memcached_decrement_with_initial_by_key(
312 memcached_st *shell, const char *group_key, size_t group_key_length, const char *key,
313 size_t key_length, uint64_t offset, uint64_t initial, time_t expiration, uint64_t *value) {
314 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
315 Memcached *memc = memcached2Memcached(shell);
316 memcached_return_t rc = increment_decrement_with_initial_by_key(
317 PROTOCOL_BINARY_CMD_DECREMENT, memc, group_key, group_key_length, key, key_length, offset,
318 initial, expiration, value);
319
320 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
321
322 return rc;
323 }