1 /* -*- Mode: C; tab-width: 2; c-basic-offset: 2; indent-tabs-mode: nil -*- */
3 * This file contains an implementation of the callback interface for level 1
4 * in the protocol library. If you compare the implementation with the one
5 * in interface_v0.c you will see that this implementation is much easier and
6 * hides all of the protocol logic and let you focus on the application
7 * logic. One "problem" with this layer is that it is synchronous, so that
8 * you will not receive the next command before a answer to the previous
9 * command is being sent.
13 #include <sys/types.h>
21 #include <libmemcachedprotocol-0.0/handler.h>
22 #include <example/byteorder.h>
23 #include "example/memcached_light.h"
24 #include "example/storage.h"
26 static protocol_binary_response_status
add_handler(const void *cookie
,
36 protocol_binary_response_status rval
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
37 struct item
* item
= get_item(key
, keylen
);
40 item
= create_item(key
, keylen
, data
, datalen
, flags
, (time_t)exptime
);
43 rval
= PROTOCOL_BINARY_RESPONSE_ENOMEM
;
54 rval
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
60 static protocol_binary_response_status
append_handler(const void *cookie
,
69 protocol_binary_response_status rval
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
71 struct item
*item
= get_item(key
, keylen
);
76 rval
= PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
78 else if (cas
!= 0 && cas
!= item
->cas
)
80 rval
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
82 else if ((nitem
= create_item(key
, keylen
, NULL
, item
->size
+ vallen
,
83 item
->flags
, item
->exp
)) == NULL
)
86 rval
= PROTOCOL_BINARY_RESPONSE_ENOMEM
;
90 memcpy(nitem
->data
, item
->data
, item
->size
);
91 memcpy(((char*)(nitem
->data
)) + item
->size
, val
, vallen
);
93 delete_item(key
, keylen
);
95 *result_cas
= nitem
->cas
;
102 static protocol_binary_response_status
decrement_handler(const void *cookie
,
109 uint64_t *result_cas
) {
111 protocol_binary_response_status rval
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
112 uint64_t val
= initial
;
113 struct item
*item
= get_item(key
, keylen
);
117 if (delta
> *(uint64_t*)item
->data
)
120 val
= *(uint64_t*)item
->data
- delta
;
122 expiration
= (uint32_t)item
->exp
;
124 delete_item(key
, keylen
);
127 item
= create_item(key
, keylen
, NULL
, sizeof(initial
), 0, (time_t)expiration
);
130 rval
= PROTOCOL_BINARY_RESPONSE_ENOMEM
;
134 memcpy(item
->data
, &val
, sizeof(val
));
137 *result_cas
= item
->cas
;
144 static protocol_binary_response_status
delete_handler(const void *, // cookie
149 protocol_binary_response_status rval
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
153 struct item
*item
= get_item(key
, keylen
);
156 if (item
->cas
!= cas
)
159 return PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
165 if (!delete_item(key
, keylen
))
167 rval
= PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
174 static protocol_binary_response_status
flush_handler(const void * /* cookie */, uint32_t /* when */)
176 return PROTOCOL_BINARY_RESPONSE_SUCCESS
;
179 static protocol_binary_response_status
get_handler(const void *cookie
,
182 memcached_binary_protocol_get_response_handler response_handler
) {
183 struct item
*item
= get_item(key
, keylen
);
187 return PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
190 protocol_binary_response_status rc
;
191 rc
= response_handler(cookie
, key
, (uint16_t)keylen
,
192 item
->data
, (uint32_t)item
->size
, item
->flags
,
198 static protocol_binary_response_status
increment_handler(const void *cookie
,
205 uint64_t *result_cas
) {
207 protocol_binary_response_status rval
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
208 uint64_t val
= initial
;
209 struct item
*item
= get_item(key
, keylen
);
213 val
= (*(uint64_t*)item
->data
) + delta
;
214 expiration
= (uint32_t)item
->exp
;
216 delete_item(key
, keylen
);
219 item
= create_item(key
, keylen
, NULL
, sizeof(initial
), 0, (time_t)expiration
);
222 rval
= PROTOCOL_BINARY_RESPONSE_ENOMEM
;
226 char buffer
[1024] = {0};
227 memcpy(buffer
, key
, keylen
);
228 memcpy(item
->data
, &val
, sizeof(val
));
231 *result_cas
= item
->cas
;
238 static protocol_binary_response_status
noop_handler(const void *cookie
) {
240 return PROTOCOL_BINARY_RESPONSE_SUCCESS
;
243 static protocol_binary_response_status
prepend_handler(const void *cookie
,
249 uint64_t *result_cas
) {
251 protocol_binary_response_status rval
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
253 struct item
*item
= get_item(key
, keylen
);
254 struct item
*nitem
= NULL
;
258 rval
= PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
260 else if (cas
!= 0 && cas
!= item
->cas
)
262 rval
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
264 else if ((nitem
= create_item(key
, keylen
, NULL
, item
->size
+ vallen
,
265 item
->flags
, item
->exp
)) == NULL
)
267 rval
= PROTOCOL_BINARY_RESPONSE_ENOMEM
;
271 memcpy(nitem
->data
, val
, vallen
);
272 memcpy(((char*)(nitem
->data
)) + vallen
, item
->data
, item
->size
);
275 delete_item(key
, keylen
);
277 *result_cas
= nitem
->cas
;
289 static protocol_binary_response_status
quit_handler(const void *) //cookie
291 return PROTOCOL_BINARY_RESPONSE_SUCCESS
;
294 static protocol_binary_response_status
replace_handler(const void *, // cookie
302 uint64_t *result_cas
)
304 protocol_binary_response_status rval
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
305 struct item
* item
= get_item(key
, keylen
);
309 rval
= PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
311 else if (cas
== 0 || cas
== item
->cas
)
314 delete_item(key
, keylen
);
315 item
= create_item(key
, keylen
, data
, datalen
, flags
, (time_t)exptime
);
318 rval
= PROTOCOL_BINARY_RESPONSE_ENOMEM
;
323 *result_cas
= item
->cas
;
329 rval
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
336 static protocol_binary_response_status
set_handler(const void *cookie
,
344 uint64_t *result_cas
) {
346 protocol_binary_response_status rval
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
350 struct item
* item
= get_item(key
, keylen
);
351 if (item
!= NULL
&& cas
!= item
->cas
)
353 /* Invalid CAS value */
355 return PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
359 delete_item(key
, keylen
);
360 struct item
* item
= create_item(key
, keylen
, data
, datalen
, flags
, (time_t)exptime
);
363 rval
= PROTOCOL_BINARY_RESPONSE_ENOMEM
;
368 *result_cas
= item
->cas
;
375 static protocol_binary_response_status
stat_handler(const void *cookie
,
378 memcached_binary_protocol_stat_response_handler response_handler
)
380 /* Just return an empty packet */
381 return response_handler(cookie
, NULL
, 0, NULL
, 0);
384 static protocol_binary_response_status
version_handler(const void *cookie
,
385 memcached_binary_protocol_version_response_handler response_handler
)
387 const char *version
= "0.1.1";
388 return response_handler(cookie
, version
, (uint32_t)strlen(version
));
391 memcached_binary_protocol_callback_st interface_v1_impl
;
393 void initialize_interface_v1_handler(void)
395 memset(&interface_v1_impl
, 0, sizeof(memcached_binary_protocol_callback_st
));
397 interface_v1_impl
.interface_version
= MEMCACHED_PROTOCOL_HANDLER_V1
;
398 interface_v1_impl
.interface
.v1
.add
= add_handler
;
399 interface_v1_impl
.interface
.v1
.append
= append_handler
;
400 interface_v1_impl
.interface
.v1
.decrement
= decrement_handler
;
401 interface_v1_impl
.interface
.v1
.delete_object
= delete_handler
;
402 interface_v1_impl
.interface
.v1
.flush_object
= flush_handler
;
403 interface_v1_impl
.interface
.v1
.get
= get_handler
;
404 interface_v1_impl
.interface
.v1
.increment
= increment_handler
;
405 interface_v1_impl
.interface
.v1
.noop
= noop_handler
;
406 interface_v1_impl
.interface
.v1
.prepend
= prepend_handler
;
407 interface_v1_impl
.interface
.v1
.quit
= quit_handler
;
408 interface_v1_impl
.interface
.v1
.replace
= replace_handler
;
409 interface_v1_impl
.interface
.v1
.set
= set_handler
;
410 interface_v1_impl
.interface
.v1
.stat
= stat_handler
;
411 interface_v1_impl
.interface
.v1
.version
= version_handler
;