3 static memcached_return
memcached_auto(memcached_st
*ptr
,
5 const char *master_key
, size_t master_key_length
,
6 const char *key
, size_t key_length
,
12 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
13 unsigned int server_key
;
14 bool no_reply
= ptr
->flags
.no_reply
;
16 unlikely (ptr
->hosts
== NULL
|| ptr
->number_of_hosts
== 0)
17 return MEMCACHED_NO_SERVERS
;
19 if (ptr
->flags
.verify_key
&& (memcached_key_test((const char **)&key
, &key_length
, 1) == MEMCACHED_BAD_KEY_PROVIDED
))
20 return MEMCACHED_BAD_KEY_PROVIDED
;
22 server_key
= memcached_generate_hash(ptr
, master_key
, master_key_length
);
24 send_length
= (size_t)snprintf(buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
,
25 "%s %s%.*s %" PRIu64
"%s\r\n", verb
,
28 offset
, no_reply
? " noreply" : "");
29 unlikely (send_length
>= MEMCACHED_DEFAULT_COMMAND_SIZE
)
30 return MEMCACHED_WRITE_FAILURE
;
32 rc
= memcached_do(&ptr
->hosts
[server_key
], buffer
, send_length
, 1);
33 if (no_reply
|| rc
!= MEMCACHED_SUCCESS
)
36 rc
= memcached_response(&ptr
->hosts
[server_key
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, NULL
);
39 So why recheck responce? Because the protocol is brain dead :)
40 The number returned might end up equaling one of the string
41 values. Less chance of a mistake with strncmp() so we will
42 use it. We still called memcached_response() though since it
43 worked its magic for non-blocking IO.
45 if (!strncmp(buffer
, "ERROR\r\n", 7))
48 rc
= MEMCACHED_PROTOCOL_ERROR
;
50 else if (!strncmp(buffer
, "NOT_FOUND\r\n", 11))
53 rc
= MEMCACHED_NOTFOUND
;
57 *value
= strtoull(buffer
, (char **)NULL
, 10);
58 rc
= MEMCACHED_SUCCESS
;
64 static memcached_return
binary_incr_decr(memcached_st
*ptr
, uint8_t cmd
,
65 const char *master_key
, size_t master_key_length
,
66 const char *key
, size_t key_length
,
67 uint64_t offset
, uint64_t initial
,
71 unsigned int server_key
;
72 bool no_reply
= ptr
->flags
.no_reply
;
74 unlikely (ptr
->hosts
== NULL
|| ptr
->number_of_hosts
== 0)
75 return MEMCACHED_NO_SERVERS
;
77 server_key
= memcached_generate_hash(ptr
, master_key
, master_key_length
);
81 if(cmd
== PROTOCOL_BINARY_CMD_DECREMENT
)
82 cmd
= PROTOCOL_BINARY_CMD_DECREMENTQ
;
83 if(cmd
== PROTOCOL_BINARY_CMD_INCREMENT
)
84 cmd
= PROTOCOL_BINARY_CMD_INCREMENTQ
;
86 protocol_binary_request_incr request
= {.bytes
= {0}};
88 request
.message
.header
.request
.magic
= PROTOCOL_BINARY_REQ
;
89 request
.message
.header
.request
.opcode
= cmd
;
90 request
.message
.header
.request
.keylen
= htons((uint16_t) key_length
);
91 request
.message
.header
.request
.extlen
= 20;
92 request
.message
.header
.request
.datatype
= PROTOCOL_BINARY_RAW_BYTES
;
93 request
.message
.header
.request
.bodylen
= htonl((uint32_t) (key_length
+ request
.message
.header
.request
.extlen
));
94 request
.message
.body
.delta
= htonll(offset
);
95 request
.message
.body
.initial
= htonll(initial
);
96 request
.message
.body
.expiration
= htonl((uint32_t) expiration
);
98 if ((memcached_do(&ptr
->hosts
[server_key
], request
.bytes
,
99 sizeof(request
.bytes
), 0)!=MEMCACHED_SUCCESS
) ||
100 (memcached_io_write(&ptr
->hosts
[server_key
], key
, key_length
, 1) == -1))
102 memcached_io_reset(&ptr
->hosts
[server_key
]);
103 return MEMCACHED_WRITE_FAILURE
;
107 return MEMCACHED_SUCCESS
;
108 return memcached_response(&ptr
->hosts
[server_key
], (char*)value
, sizeof(*value
), NULL
);
111 memcached_return
memcached_increment(memcached_st
*ptr
,
112 const char *key
, size_t key_length
,
116 return memcached_increment_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
119 memcached_return
memcached_decrement(memcached_st
*ptr
,
120 const char *key
, size_t key_length
,
124 return memcached_decrement_by_key(ptr
, key
, key_length
, key
, key_length
, offset
, value
);
127 memcached_return
memcached_increment_by_key(memcached_st
*ptr
,
128 const char *master_key
, size_t master_key_length
,
129 const char *key
, size_t key_length
,
133 memcached_return rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
);
134 unlikely (rc
!= MEMCACHED_SUCCESS
)
137 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
138 if (ptr
->flags
.binary_protocol
)
140 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
141 master_key
, master_key_length
, key
, key_length
,
142 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
147 rc
= memcached_auto(ptr
, "incr", master_key
, master_key_length
, key
, key_length
, offset
, value
);
150 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
155 memcached_return
memcached_decrement_by_key(memcached_st
*ptr
,
156 const char *master_key
, size_t master_key_length
,
157 const char *key
, size_t key_length
,
161 memcached_return rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
);
162 unlikely (rc
!= MEMCACHED_SUCCESS
)
165 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
166 if (ptr
->flags
.binary_protocol
)
167 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
168 master_key
, master_key_length
, key
, key_length
,
169 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
172 rc
= memcached_auto(ptr
, "decr", master_key
, master_key_length
, key
, key_length
, offset
, value
);
174 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
179 memcached_return
memcached_increment_with_initial(memcached_st
*ptr
,
187 return memcached_increment_with_initial_by_key(ptr
, key
, key_length
,
189 offset
, initial
, expiration
, value
);
192 memcached_return
memcached_increment_with_initial_by_key(memcached_st
*ptr
,
193 const char *master_key
,
194 size_t master_key_length
,
202 memcached_return rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
);
203 unlikely (rc
!= MEMCACHED_SUCCESS
)
206 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
207 if (ptr
->flags
.binary_protocol
)
208 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
209 master_key
, master_key_length
, key
, key_length
,
210 offset
, initial
, (uint32_t)expiration
,
213 rc
= MEMCACHED_PROTOCOL_ERROR
;
215 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
220 memcached_return
memcached_decrement_with_initial(memcached_st
*ptr
,
228 return memcached_decrement_with_initial_by_key(ptr
, key
, key_length
,
230 offset
, initial
, expiration
, value
);
233 memcached_return
memcached_decrement_with_initial_by_key(memcached_st
*ptr
,
234 const char *master_key
,
235 size_t master_key_length
,
243 memcached_return rc
= memcached_validate_key_length(key_length
, ptr
->flags
.binary_protocol
);
244 unlikely (rc
!= MEMCACHED_SUCCESS
)
247 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
248 if (ptr
->flags
.binary_protocol
)
250 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
251 master_key
, master_key_length
, key
, key_length
,
252 offset
, initial
, (uint32_t)expiration
,
257 rc
= MEMCACHED_PROTOCOL_ERROR
;
260 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();