Merge pull request #140 from hussainnaqvee/patch-1
[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 text_incr_decr(memcached_instance_st *instance, const bool is_incr,
43 const char *key, size_t key_length, const uint64_t offset,
44 const bool reply) {
45 char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
46
47 int send_length = snprintf(buffer, sizeof(buffer), " %" PRIu64, offset);
48 if (size_t(send_length) >= sizeof(buffer) or send_length < 0) {
49 return memcached_set_error(*instance, MEMCACHED_MEMORY_ALLOCATION_FAILURE, MEMCACHED_AT,
50 memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
51 }
52
53 libmemcached_io_vector_st vector[] = {
54 {NULL, 0},
55 {memcached_literal_param("incr ")},
56 {memcached_array_string(instance->root->_namespace),
57 memcached_array_size(instance->root->_namespace)},
58 {key, key_length},
59 {buffer, size_t(send_length)},
60 {" noreply", reply ? 0 : memcached_literal_param_size(" noreply")},
61 {memcached_literal_param("\r\n")}};
62
63 if (is_incr == false) {
64 vector[1].buffer = "decr ";
65 }
66
67 return memcached_vdo(instance, vector, 7, true);
68 }
69
70 static memcached_return_t binary_incr_decr(memcached_instance_st *instance,
71 protocol_binary_command cmd, const char *key,
72 const size_t key_length, const uint64_t offset,
73 const uint64_t initial, const uint32_t expiration,
74 const bool reply) {
75 if (reply == false) {
76 if (cmd == PROTOCOL_BINARY_CMD_DECREMENT) {
77 cmd = PROTOCOL_BINARY_CMD_DECREMENTQ;
78 }
79
80 if (cmd == PROTOCOL_BINARY_CMD_INCREMENT) {
81 cmd = PROTOCOL_BINARY_CMD_INCREMENTQ;
82 }
83 }
84 protocol_binary_request_incr request = {}; // = {.bytes= {0}};
85
86 initialize_binary_request(instance, request.message.header);
87
88 request.message.header.request.opcode = cmd;
89 request.message.header.request.keylen =
90 htons((uint16_t)(key_length + memcached_array_size(instance->root->_namespace)));
91 request.message.header.request.extlen = 20;
92 request.message.header.request.datatype = PROTOCOL_BINARY_RAW_BYTES;
93 request.message.header.request.bodylen =
94 htonl((uint32_t)(key_length + memcached_array_size(instance->root->_namespace)
95 + request.message.header.request.extlen));
96 request.message.body.delta = memcached_htonll(offset);
97 request.message.body.initial = memcached_htonll(initial);
98 request.message.body.expiration = htonl((uint32_t) expiration);
99
100 libmemcached_io_vector_st vector[] = {{NULL, 0},
101 {request.bytes, sizeof(request.bytes)},
102 {memcached_array_string(instance->root->_namespace),
103 memcached_array_size(instance->root->_namespace)},
104 {key, key_length}};
105
106 return memcached_vdo(instance, vector, 4, true);
107 }
108
109 memcached_return_t memcached_increment(memcached_st *memc, const char *key, size_t key_length,
110 uint32_t offset, uint64_t *value) {
111 return memcached_increment_by_key(memc, key, key_length, key, key_length, offset, value);
112 }
113
114 static memcached_return_t increment_decrement_by_key(const protocol_binary_command command,
115 Memcached *memc, const char *group_key,
116 size_t group_key_length, const char *key,
117 size_t key_length, uint64_t offset,
118 uint64_t *value) {
119 uint64_t local_value;
120 if (value == NULL) {
121 value = &local_value;
122 }
123
124 memcached_return_t rc;
125 if (memcached_failed(rc = initialize_query(memc, true))) {
126 return rc;
127 }
128
129 if (memcached_is_encrypted(memc)) {
130 return memcached_set_error(
131 *memc, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT,
132 memcached_literal_param("Operation not allowed while encyrption is enabled"));
133 }
134
135 if (memcached_failed(rc = memcached_key_test(*memc, (const char **) &key, &key_length, 1))) {
136 return memcached_last_error(memc);
137 }
138
139 uint32_t server_key =
140 memcached_generate_hash_with_redistribution(memc, group_key, group_key_length);
141 memcached_instance_st *instance = memcached_instance_fetch(memc, server_key);
142
143 bool reply = memcached_is_replying(instance->root);
144
145 if (memcached_is_binary(memc)) {
146 rc = binary_incr_decr(instance, command, key, key_length, uint64_t(offset), 0,
147 MEMCACHED_EXPIRATION_NOT_ADD, reply);
148 } else {
149 rc = text_incr_decr(instance, command == PROTOCOL_BINARY_CMD_INCREMENT ? true : false, key,
150 key_length, offset, reply);
151 }
152
153 auto_response(instance, reply, rc, value);
154
155 return rc;
156 }
157
158 static memcached_return_t
159 increment_decrement_with_initial_by_key(const protocol_binary_command command, Memcached *memc,
160 const char *group_key, size_t group_key_length,
161 const char *key, size_t key_length, uint64_t offset,
162 uint64_t initial, time_t expiration, uint64_t *value) {
163 uint64_t local_value;
164 if (value == NULL) {
165 value = &local_value;
166 }
167
168 memcached_return_t rc;
169 if (memcached_failed(rc = initialize_query(memc, true))) {
170 return rc;
171 }
172
173 if (memcached_is_encrypted(memc)) {
174 return memcached_set_error(
175 *memc, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT,
176 memcached_literal_param("Operation not allowed while encryption is enabled"));
177 }
178
179 if (memcached_failed(rc = memcached_key_test(*memc, (const char **) &key, &key_length, 1))) {
180 return memcached_last_error(memc);
181 }
182
183 uint32_t server_key =
184 memcached_generate_hash_with_redistribution(memc, group_key, group_key_length);
185 memcached_instance_st *instance = memcached_instance_fetch(memc, server_key);
186
187 bool reply = memcached_is_replying(instance->root);
188
189 if (memcached_is_binary(memc)) {
190 rc = binary_incr_decr(instance, command, key, key_length, offset, initial, uint32_t(expiration),
191 reply);
192
193 } else {
194 rc = memcached_set_error(
195 *memc, MEMCACHED_INVALID_ARGUMENTS, MEMCACHED_AT,
196 memcached_literal_param(
197 "memcached_increment_with_initial_by_key() is not supported via the ASCII protocol"));
198 }
199
200 auto_response(instance, reply, rc, value);
201
202 return rc;
203 }
204
205 memcached_return_t memcached_decrement(memcached_st *memc, const char *key, size_t key_length,
206 uint32_t offset, uint64_t *value) {
207 return memcached_decrement_by_key(memc, key, key_length, key, key_length, offset, value);
208 }
209
210 memcached_return_t memcached_increment_by_key(memcached_st *shell, const char *group_key,
211 size_t group_key_length, const char *key,
212 size_t key_length, uint64_t offset, uint64_t *value) {
213 Memcached *memc = memcached2Memcached(shell);
214 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
215 memcached_return_t rc =
216 increment_decrement_by_key(PROTOCOL_BINARY_CMD_INCREMENT, memc, group_key, group_key_length,
217 key, key_length, offset, value);
218
219 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
220
221 return rc;
222 }
223
224 memcached_return_t memcached_decrement_by_key(memcached_st *shell, const char *group_key,
225 size_t group_key_length, const char *key,
226 size_t key_length, uint64_t offset, uint64_t *value) {
227 Memcached *memc = memcached2Memcached(shell);
228 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
229 memcached_return_t rc =
230 increment_decrement_by_key(PROTOCOL_BINARY_CMD_DECREMENT, memc, group_key, group_key_length,
231 key, key_length, offset, value);
232 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
233
234 return rc;
235 }
236
237 memcached_return_t memcached_increment_with_initial(memcached_st *memc, const char *key,
238 size_t key_length, uint64_t offset,
239 uint64_t initial, time_t expiration,
240 uint64_t *value) {
241 return memcached_increment_with_initial_by_key(memc, key, key_length, key, key_length, offset,
242 initial, expiration, value);
243 }
244
245 memcached_return_t memcached_increment_with_initial_by_key(
246 memcached_st *shell, const char *group_key, size_t group_key_length, const char *key,
247 size_t key_length, uint64_t offset, uint64_t initial, time_t expiration, uint64_t *value) {
248 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
249 Memcached *memc = memcached2Memcached(shell);
250 memcached_return_t rc = increment_decrement_with_initial_by_key(
251 PROTOCOL_BINARY_CMD_INCREMENT, memc, group_key, group_key_length, key, key_length, offset,
252 initial, expiration, value);
253 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
254
255 return rc;
256 }
257
258 memcached_return_t memcached_decrement_with_initial(memcached_st *memc, const char *key,
259 size_t key_length, uint64_t offset,
260 uint64_t initial, time_t expiration,
261 uint64_t *value) {
262 return memcached_decrement_with_initial_by_key(memc, key, key_length, key, key_length, offset,
263 initial, expiration, value);
264 }
265
266 memcached_return_t memcached_decrement_with_initial_by_key(
267 memcached_st *shell, const char *group_key, size_t group_key_length, const char *key,
268 size_t key_length, uint64_t offset, uint64_t initial, time_t expiration, uint64_t *value) {
269 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
270 Memcached *memc = memcached2Memcached(shell);
271 memcached_return_t rc = increment_decrement_with_initial_by_key(
272 PROTOCOL_BINARY_CMD_DECREMENT, memc, group_key, group_key_length, key, key_length, offset,
273 initial, expiration, value);
274
275 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
276
277 return rc;
278 }