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.cc 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.
11 #include "mem_config.h"
19 #include <sys/types.h>
22 #include <libmemcachedprotocol-0.0/handler.h>
23 #include <example/byteorder.h>
24 #include "example/memcached_light.h"
25 #include "example/storage.h"
26 #include "util/log.hpp"
28 static datadifferential::util::log_info_st
*log_file
= NULL
;
30 static protocol_binary_response_status
add_handler(const void *cookie
,
40 protocol_binary_response_status rval
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
41 struct item
* item
= get_item(key
, keylen
);
44 item
= create_item(key
, keylen
, data
, datalen
, flags
, (time_t)exptime
);
47 rval
= PROTOCOL_BINARY_RESPONSE_ENOMEM
;
58 rval
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
64 static protocol_binary_response_status
append_handler(const void *cookie
,
73 protocol_binary_response_status rval
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
75 struct item
*item
= get_item(key
, keylen
);
80 rval
= PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
82 else if (cas
!= 0 && cas
!= item
->cas
)
84 rval
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
86 else if ((nitem
= create_item(key
, keylen
, NULL
, item
->size
+ vallen
,
87 item
->flags
, item
->exp
)) == NULL
)
90 rval
= PROTOCOL_BINARY_RESPONSE_ENOMEM
;
94 memcpy(nitem
->data
, item
->data
, item
->size
);
95 memcpy(((char*)(nitem
->data
)) + item
->size
, val
, vallen
);
97 delete_item(key
, keylen
);
99 *result_cas
= nitem
->cas
;
106 static protocol_binary_response_status
decrement_handler(const void *cookie
,
113 uint64_t *result_cas
) {
115 protocol_binary_response_status rval
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
116 uint64_t val
= initial
;
117 struct item
*item
= get_item(key
, keylen
);
121 if (delta
> *(uint64_t*)item
->data
)
124 val
= *(uint64_t*)item
->data
- delta
;
126 expiration
= (uint32_t)item
->exp
;
128 delete_item(key
, keylen
);
131 item
= create_item(key
, keylen
, NULL
, sizeof(initial
), 0, (time_t)expiration
);
134 rval
= PROTOCOL_BINARY_RESPONSE_ENOMEM
;
138 memcpy(item
->data
, &val
, sizeof(val
));
141 *result_cas
= item
->cas
;
148 static protocol_binary_response_status
delete_handler(const void *, // cookie
153 protocol_binary_response_status rval
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
157 struct item
*item
= get_item(key
, keylen
);
160 if (item
->cas
!= cas
)
163 return PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
169 if (!delete_item(key
, keylen
))
171 rval
= PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
178 static protocol_binary_response_status
flush_handler(const void * /* cookie */, uint32_t /* when */)
180 return PROTOCOL_BINARY_RESPONSE_SUCCESS
;
183 static protocol_binary_response_status
get_handler(const void *cookie
,
186 memcached_binary_protocol_get_response_handler response_handler
) {
187 struct item
*item
= get_item(key
, keylen
);
191 return PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
194 protocol_binary_response_status rc
;
195 rc
= response_handler(cookie
, key
, (uint16_t)keylen
,
196 item
->data
, (uint32_t)item
->size
, item
->flags
,
202 static protocol_binary_response_status
increment_handler(const void *cookie
,
209 uint64_t *result_cas
) {
211 protocol_binary_response_status rval
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
212 uint64_t val
= initial
;
213 struct item
*item
= get_item(key
, keylen
);
217 val
= (*(uint64_t*)item
->data
) + delta
;
218 expiration
= (uint32_t)item
->exp
;
220 delete_item(key
, keylen
);
223 item
= create_item(key
, keylen
, NULL
, sizeof(initial
), 0, (time_t)expiration
);
226 rval
= PROTOCOL_BINARY_RESPONSE_ENOMEM
;
230 char buffer
[1024] = {0};
231 memcpy(buffer
, key
, keylen
);
232 memcpy(item
->data
, &val
, sizeof(val
));
235 *result_cas
= item
->cas
;
242 static protocol_binary_response_status
noop_handler(const void *cookie
) {
244 return PROTOCOL_BINARY_RESPONSE_SUCCESS
;
247 static protocol_binary_response_status
prepend_handler(const void *cookie
,
253 uint64_t *result_cas
) {
255 protocol_binary_response_status rval
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
257 struct item
*item
= get_item(key
, keylen
);
258 struct item
*nitem
= NULL
;
262 rval
= PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
264 else if (cas
!= 0 && cas
!= item
->cas
)
266 rval
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
268 else if ((nitem
= create_item(key
, keylen
, NULL
, item
->size
+ vallen
,
269 item
->flags
, item
->exp
)) == NULL
)
271 rval
= PROTOCOL_BINARY_RESPONSE_ENOMEM
;
275 memcpy(nitem
->data
, val
, vallen
);
276 memcpy(((char*)(nitem
->data
)) + vallen
, item
->data
, item
->size
);
279 delete_item(key
, keylen
);
281 *result_cas
= nitem
->cas
;
293 static protocol_binary_response_status
quit_handler(const void *) //cookie
295 return PROTOCOL_BINARY_RESPONSE_SUCCESS
;
298 static protocol_binary_response_status
replace_handler(const void *, // cookie
306 uint64_t *result_cas
)
308 protocol_binary_response_status rval
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
309 struct item
* item
= get_item(key
, keylen
);
313 rval
= PROTOCOL_BINARY_RESPONSE_KEY_ENOENT
;
315 else if (cas
== 0 || cas
== item
->cas
)
318 delete_item(key
, keylen
);
319 item
= create_item(key
, keylen
, data
, datalen
, flags
, (time_t)exptime
);
322 rval
= PROTOCOL_BINARY_RESPONSE_ENOMEM
;
327 *result_cas
= item
->cas
;
333 rval
= PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
340 static protocol_binary_response_status
set_handler(const void *cookie
,
348 uint64_t *result_cas
) {
350 protocol_binary_response_status rval
= PROTOCOL_BINARY_RESPONSE_SUCCESS
;
354 struct item
* item
= get_item(key
, keylen
);
355 if (item
!= NULL
&& cas
!= item
->cas
)
357 /* Invalid CAS value */
359 return PROTOCOL_BINARY_RESPONSE_KEY_EEXISTS
;
363 delete_item(key
, keylen
);
364 struct item
* item
= create_item(key
, keylen
, data
, datalen
, flags
, (time_t)exptime
);
367 rval
= PROTOCOL_BINARY_RESPONSE_ENOMEM
;
372 *result_cas
= item
->cas
;
379 static protocol_binary_response_status
stat_handler(const void *cookie
,
382 memcached_binary_protocol_stat_response_handler response_handler
)
384 /* Just return an empty packet */
385 return response_handler(cookie
, NULL
, 0, NULL
, 0);
388 static protocol_binary_response_status
version_handler(const void *cookie
,
389 memcached_binary_protocol_version_response_handler response_handler
)
391 const char *version
= "0.1.1";
392 return response_handler(cookie
, version
, (uint32_t)strlen(version
));
395 memcached_binary_protocol_callback_st interface_v1_impl
;
397 void initialize_interface_v1_handler(datadifferential::util::log_info_st
& arg
)
400 memset(&interface_v1_impl
, 0, sizeof(memcached_binary_protocol_callback_st
));
402 interface_v1_impl
.interface_version
= MEMCACHED_PROTOCOL_HANDLER_V1
;
403 interface_v1_impl
.interface
.v1
.add
= add_handler
;
404 interface_v1_impl
.interface
.v1
.append
= append_handler
;
405 interface_v1_impl
.interface
.v1
.decrement
= decrement_handler
;
406 interface_v1_impl
.interface
.v1
.delete_object
= delete_handler
;
407 interface_v1_impl
.interface
.v1
.flush_object
= flush_handler
;
408 interface_v1_impl
.interface
.v1
.get
= get_handler
;
409 interface_v1_impl
.interface
.v1
.increment
= increment_handler
;
410 interface_v1_impl
.interface
.v1
.noop
= noop_handler
;
411 interface_v1_impl
.interface
.v1
.prepend
= prepend_handler
;
412 interface_v1_impl
.interface
.v1
.quit
= quit_handler
;
413 interface_v1_impl
.interface
.v1
.replace
= replace_handler
;
414 interface_v1_impl
.interface
.v1
.set
= set_handler
;
415 interface_v1_impl
.interface
.v1
.stat
= stat_handler
;
416 interface_v1_impl
.interface
.v1
.version
= version_handler
;