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 +--------------------------------------------------------------------+
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
text_incr_decr(memcached_instance_st
*instance
, const bool is_incr
,
43 const char *key
, size_t key_length
, const uint64_t offset
,
45 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
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)"));
53 libmemcached_io_vector_st vector
[] = {
55 {memcached_literal_param("incr ")},
56 {memcached_array_string(instance
->root
->_namespace
),
57 memcached_array_size(instance
->root
->_namespace
)},
59 {buffer
, size_t(send_length
)},
60 {" noreply", reply
? 0 : memcached_literal_param_size(" noreply")},
61 {memcached_literal_param("\r\n")}};
63 if (is_incr
== false) {
64 vector
[1].buffer
= "decr ";
67 return memcached_vdo(instance
, vector
, 7, true);
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
,
76 if (cmd
== PROTOCOL_BINARY_CMD_DECREMENT
) {
77 cmd
= PROTOCOL_BINARY_CMD_DECREMENTQ
;
80 if (cmd
== PROTOCOL_BINARY_CMD_INCREMENT
) {
81 cmd
= PROTOCOL_BINARY_CMD_INCREMENTQ
;
84 protocol_binary_request_incr request
= {}; // = {.bytes= {0}};
86 initialize_binary_request(instance
, request
.message
.header
);
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
);
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
)},
106 return memcached_vdo(instance
, vector
, 4, true);
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
);
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
,
119 uint64_t local_value
;
121 value
= &local_value
;
124 memcached_return_t rc
;
125 if (memcached_failed(rc
= initialize_query(memc
, true))) {
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"));
135 if (memcached_failed(rc
= memcached_key_test(*memc
, (const char **) &key
, &key_length
, 1))) {
136 return memcached_last_error(memc
);
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
);
143 bool reply
= memcached_is_replying(instance
->root
);
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
);
149 rc
= text_incr_decr(instance
, command
== PROTOCOL_BINARY_CMD_INCREMENT
? true : false, key
,
150 key_length
, offset
, reply
);
153 auto_response(instance
, reply
, rc
, value
);
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
;
165 value
= &local_value
;
168 memcached_return_t rc
;
169 if (memcached_failed(rc
= initialize_query(memc
, true))) {
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"));
179 if (memcached_failed(rc
= memcached_key_test(*memc
, (const char **) &key
, &key_length
, 1))) {
180 return memcached_last_error(memc
);
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
);
187 bool reply
= memcached_is_replying(instance
->root
);
189 if (memcached_is_binary(memc
)) {
190 rc
= binary_incr_decr(instance
, command
, key
, key_length
, offset
, initial
, uint32_t(expiration
),
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"));
200 auto_response(instance
, reply
, rc
, value
);
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
);
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
);
219 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
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();
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
,
241 return memcached_increment_with_initial_by_key(memc
, key
, key_length
, key
, key_length
, offset
,
242 initial
, expiration
, value
);
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();
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
,
262 return memcached_decrement_with_initial_by_key(memc
, key
, key_length
, key
, key_length
, offset
,
263 initial
, expiration
, value
);
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
);
275 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();