Merge Lee
[awesomized/libmemcached] / libmemcached / result.c
1 /* LibMemcached
2 * Copyright (C) 2006-2009 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary: Functions to manipulate the result structure.
9 *
10 */
11
12 /*
13 memcached_result_st are used to internally represent the return values from
14 memcached. We use a structure so that long term as identifiers are added
15 to memcached we will be able to absorb new attributes without having
16 to addjust the entire API.
17 */
18 #include "common.h"
19
20 static inline void _result_init(memcached_result_st *self,
21 const memcached_st *memc)
22 {
23 self->item_flags= 0;
24 self->item_expiration= 0;
25 self->key_length= 0;
26 self->item_cas= 0;
27 self->root= memc;
28 self->item_key[0]= 0;
29 }
30
31 memcached_result_st *memcached_result_create(const memcached_st *memc,
32 memcached_result_st *ptr)
33 {
34 WATCHPOINT_ASSERT(memc);
35 WATCHPOINT_ASSERT(memc->options.is_initialized);
36
37 /* Saving malloc calls :) */
38 if (ptr)
39 {
40 ptr->options.is_allocated= false;
41 }
42 else
43 {
44 ptr= libmemcached_malloc(memc, sizeof(memcached_result_st));
45
46 if (ptr == NULL)
47 return NULL;
48
49 ptr->options.is_allocated= true;
50 }
51
52 ptr->options.is_initialized= true;
53
54 _result_init(ptr, memc);
55
56 ptr->root= memc;
57 memcached_string_create(memc, &ptr->value, 0);
58 WATCHPOINT_ASSERT_INITIALIZED(&ptr->value);
59 WATCHPOINT_ASSERT(ptr->value.string == NULL);
60
61 return ptr;
62 }
63
64 void memcached_result_reset(memcached_result_st *ptr)
65 {
66 ptr->key_length= 0;
67 memcached_string_reset(&ptr->value);
68 ptr->item_flags= 0;
69 ptr->item_cas= 0;
70 ptr->item_expiration= 0;
71 }
72
73 void memcached_result_free(memcached_result_st *ptr)
74 {
75 if (ptr == NULL)
76 return;
77
78 memcached_string_free(&ptr->value);
79
80 if (memcached_is_allocated(ptr))
81 {
82 WATCHPOINT_ASSERT(ptr->root); // Without a root, that means that result was not properly initialized.
83 libmemcached_free(ptr->root, ptr);
84 }
85 else
86 {
87 ptr->options.is_initialized= false;
88 }
89 }
90
91 memcached_return_t memcached_result_set_value(memcached_result_st *ptr,
92 const char *value,
93 size_t length)
94 {
95 return memcached_string_append(&ptr->value, value, length);
96 }