Merge in updates (including removal of some depcrated bits from the examples).
[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 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
36 /* Saving malloc calls :) */
37 if (ptr)
38 {
39 ptr->options.is_allocated= false;
40 }
41 else
42 {
43 ptr= libmemcached_malloc(memc, sizeof(memcached_result_st));
44
45 if (ptr == NULL)
46 return NULL;
47
48 ptr->options.is_allocated= true;
49 }
50
51 ptr->options.is_initialized= true;
52
53 _result_init(ptr, (memcached_st *)memc);
54
55 WATCHPOINT_SET(ptr->value.options.is_initialized= false);
56 memcached_string_create(memc, &ptr->value, 0);
57 WATCHPOINT_ASSERT_INITIALIZED(&ptr->value);
58 WATCHPOINT_ASSERT(ptr->value.string == NULL);
59
60 return ptr;
61 }
62
63 void memcached_result_reset(memcached_result_st *ptr)
64 {
65 ptr->key_length= 0;
66 memcached_string_reset(&ptr->value);
67 ptr->item_flags= 0;
68 ptr->item_cas= 0;
69 ptr->item_expiration= 0;
70 }
71
72 void memcached_result_free(memcached_result_st *ptr)
73 {
74 if (ptr == NULL)
75 return;
76
77 memcached_string_free(&ptr->value);
78
79 if (memcached_is_allocated(ptr))
80 {
81 WATCHPOINT_ASSERT(ptr->root); // Without a root, that means that result was not properly initialized.
82 libmemcached_free(ptr->root, ptr);
83 }
84 else
85 {
86 ptr->options.is_initialized= false;
87 }
88 }
89
90 memcached_return_t memcached_result_set_value(memcached_result_st *ptr,
91 const char *value,
92 size_t length)
93 {
94 memcached_return_t rc= memcached_string_append(&ptr->value, value, length);
95
96 if (rc == MEMCACHED_MEMORY_ALLOCATION_FAILURE)
97 {
98 memcached_set_errno(ptr->root, errno, NULL);
99 }
100
101 return rc;
102 }
103
104 const char *memcached_result_key_value(const memcached_result_st *self)
105 {
106 return self->key_length ? self->item_key : NULL;
107 }
108
109 size_t memcached_result_key_length(const memcached_result_st *self)
110 {
111 return self->key_length;
112 }
113
114 const char *memcached_result_value(const memcached_result_st *self)
115 {
116 const memcached_string_st *sptr= &self->value;
117 return memcached_string_value(sptr);
118 }
119
120 size_t memcached_result_length(const memcached_result_st *self)
121 {
122 const memcached_string_st *sptr= &self->value;
123 return memcached_string_length(sptr);
124 }
125
126 uint32_t memcached_result_flags(const memcached_result_st *self)
127 {
128 return self->item_flags;
129 }
130
131 uint64_t memcached_result_cas(const memcached_result_st *self)
132 {
133 return self->item_cas;
134 }
135
136 void memcached_result_set_flags(memcached_result_st *self, uint32_t flags)
137 {
138 self->item_flags= flags;
139 }
140
141 void memcached_result_set_expiration(memcached_result_st *self, time_t expiration)
142 {
143 self->item_expiration= expiration;
144 }