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 Michael Wallner <mike@php.net> |
13 +--------------------------------------------------------------------+
16 #include "libmemcached/common.h"
18 static void auto_response(memcached_instance_st
*instance
, const bool reply
, memcached_return_t
&rc
,
20 // If the message was successfully sent, then get the response, otherwise
22 if (memcached_success(rc
)) {
28 rc
= memcached_response(instance
, &instance
->root
->result
);
31 if (memcached_fatal(rc
)) {
32 assert(memcached_last_error(instance
->root
) != MEMCACHED_SUCCESS
);
34 } else if (memcached_failed(rc
)) {
37 assert(memcached_last_error(instance
->root
) != MEMCACHED_NOTFOUND
);
38 *value
= instance
->root
->result
.numeric_value
;
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
);
48 libmemcached_io_vector_st io_vec
[10] = {};
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
};
56 io_vec
[io_num
++] = {memcached_literal_param(" MD")};
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
};
65 dlt_len
+= snprintf(dlt_buf
+ dlt_len
, sizeof(dlt_buf
) - dlt_len
, "%" PRIu64
, offset
);
66 io_vec
[io_num
++] = {dlt_buf
, dlt_len
};
69 exp_len
+= snprintf(exp_buf
+ exp_len
, sizeof(exp_buf
) - exp_len
, "%" PRIu32
, expiration
);
70 io_vec
[io_num
++] = {exp_buf
, exp_len
};
73 if (memcached_is_replying(instance
->root
)) {
74 io_vec
[io_num
++] = {memcached_literal_param(" v")};
76 io_vec
[io_num
++] = {memcached_literal_param(" q")};
78 io_vec
[io_num
++] = {memcached_literal_param(" O+\r\n")};
80 return memcached_vdo(instance
, io_vec
, io_num
, true);
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
,
86 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
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)"));
94 libmemcached_io_vector_st vector
[] = {
96 {memcached_literal_param("incr ")},
97 {memcached_array_string(instance
->root
->_namespace
),
98 memcached_array_size(instance
->root
->_namespace
)},
100 {buffer
, size_t(send_length
)},
101 {" noreply", reply
? 0 : memcached_literal_param_size(" noreply")},
102 {memcached_literal_param("\r\n")}};
104 if (is_incr
== false) {
105 vector
[1].buffer
= "decr ";
108 return memcached_vdo(instance
, vector
, 7, true);
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
,
116 if (reply
== false) {
117 if (cmd
== PROTOCOL_BINARY_CMD_DECREMENT
) {
118 cmd
= PROTOCOL_BINARY_CMD_DECREMENTQ
;
121 if (cmd
== PROTOCOL_BINARY_CMD_INCREMENT
) {
122 cmd
= PROTOCOL_BINARY_CMD_INCREMENTQ
;
125 protocol_binary_request_incr request
= {}; // = {.bytes= {0}};
127 initialize_binary_request(instance
, request
.message
.header
);
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
);
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
)},
147 return memcached_vdo(instance
, vector
, 4, true);
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
);
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
,
160 uint64_t local_value
;
162 value
= &local_value
;
165 memcached_return_t rc
;
166 if (memcached_failed(rc
= initialize_query(memc
, true))) {
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"));
176 if (memcached_failed(rc
= memcached_key_test(*memc
, (const char **) &key
, &key_length
, 1))) {
177 return memcached_last_error(memc
);
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
);
184 bool reply
= memcached_is_replying(instance
->root
);
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);
192 rc
= text_incr_decr(instance
, command
== PROTOCOL_BINARY_CMD_INCREMENT
? true : false, key
,
193 key_length
, offset
, reply
);
196 auto_response(instance
, reply
, rc
, value
);
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
;
208 value
= &local_value
;
211 memcached_return_t rc
;
212 if (memcached_failed(rc
= initialize_query(memc
, true))) {
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"));
222 if (memcached_failed(rc
= memcached_key_test(*memc
, (const char **) &key
, &key_length
, 1))) {
223 return memcached_last_error(memc
);
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
);
230 bool reply
= memcached_is_replying(instance
->root
);
232 if (memcached_is_binary(memc
)) {
233 rc
= binary_incr_decr(instance
, command
, key
, key_length
, offset
, initial
, uint32_t(expiration
),
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
));
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"));
245 auto_response(instance
, reply
, rc
, value
);
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
);
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
);
264 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
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();
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
,
286 return memcached_increment_with_initial_by_key(memc
, key
, key_length
, key
, key_length
, offset
,
287 initial
, expiration
, value
);
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();
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
,
307 return memcached_decrement_with_initial_by_key(memc
, key
, key_length
, key
, key_length
, offset
,
308 initial
, expiration
, value
);
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
);
320 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();