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
& MEM_NOREPLY
);
16 unlikely (ptr
->hosts
== NULL
|| ptr
->number_of_hosts
== 0)
17 return MEMCACHED_NO_SERVERS
;
19 if ((ptr
->flags
& MEM_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
& MEM_NOREPLY
);
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
& MEM_BINARY_PROTOCOL
);
134 unlikely (rc
!= MEMCACHED_SUCCESS
)
137 LIBMEMCACHED_MEMCACHED_INCREMENT_START();
138 if (ptr
->flags
& MEM_BINARY_PROTOCOL
)
139 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
140 master_key
, master_key_length
, key
, key_length
,
141 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
144 rc
= memcached_auto(ptr
, "incr", master_key
, master_key_length
, key
, key_length
, offset
, value
);
146 LIBMEMCACHED_MEMCACHED_INCREMENT_END();
151 memcached_return
memcached_decrement_by_key(memcached_st
*ptr
,
152 const char *master_key
, size_t master_key_length
,
153 const char *key
, size_t key_length
,
157 memcached_return rc
= memcached_validate_key_length(key_length
, ptr
->flags
& MEM_BINARY_PROTOCOL
);
158 unlikely (rc
!= MEMCACHED_SUCCESS
)
161 LIBMEMCACHED_MEMCACHED_DECREMENT_START();
162 if (ptr
->flags
& MEM_BINARY_PROTOCOL
)
163 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
164 master_key
, master_key_length
, key
, key_length
,
165 (uint64_t)offset
, 0, MEMCACHED_EXPIRATION_NOT_ADD
,
168 rc
= memcached_auto(ptr
, "decr", master_key
, master_key_length
, key
, key_length
, offset
, value
);
170 LIBMEMCACHED_MEMCACHED_DECREMENT_END();
175 memcached_return
memcached_increment_with_initial(memcached_st
*ptr
,
183 return memcached_increment_with_initial_by_key(ptr
, key
, key_length
,
185 offset
, initial
, expiration
, value
);
188 memcached_return
memcached_increment_with_initial_by_key(memcached_st
*ptr
,
189 const char *master_key
,
190 size_t master_key_length
,
198 memcached_return rc
= memcached_validate_key_length(key_length
, ptr
->flags
& MEM_BINARY_PROTOCOL
);
199 unlikely (rc
!= MEMCACHED_SUCCESS
)
202 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
203 if (ptr
->flags
& MEM_BINARY_PROTOCOL
)
204 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_INCREMENT
,
205 master_key
, master_key_length
, key
, key_length
,
206 offset
, initial
, (uint32_t)expiration
,
209 rc
= MEMCACHED_PROTOCOL_ERROR
;
211 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();
216 memcached_return
memcached_decrement_with_initial(memcached_st
*ptr
,
224 return memcached_decrement_with_initial_by_key(ptr
, key
, key_length
,
226 offset
, initial
, expiration
, value
);
229 memcached_return
memcached_decrement_with_initial_by_key(memcached_st
*ptr
,
230 const char *master_key
,
231 size_t master_key_length
,
239 memcached_return rc
= memcached_validate_key_length(key_length
, ptr
->flags
& MEM_BINARY_PROTOCOL
);
240 unlikely (rc
!= MEMCACHED_SUCCESS
)
243 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_START();
244 if (ptr
->flags
& MEM_BINARY_PROTOCOL
)
245 rc
= binary_incr_decr(ptr
, PROTOCOL_BINARY_CMD_DECREMENT
,
246 master_key
, master_key_length
, key
, key_length
,
247 offset
, initial
, (uint32_t)expiration
,
250 rc
= MEMCACHED_PROTOCOL_ERROR
;
252 LIBMEMCACHED_MEMCACHED_INCREMENT_WITH_INITIAL_END();