2 * Copyright (C) 2006-2009 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
8 * Summary: Functions to manipulate the result structure.
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.
20 static inline void _result_init(memcached_result_st
*self
,
21 const memcached_st
*memc
)
24 self
->item_expiration
= 0;
31 memcached_result_st
*memcached_result_create(const memcached_st
*memc
,
32 memcached_result_st
*ptr
)
34 WATCHPOINT_ASSERT(memc
);
36 /* Saving malloc calls :) */
39 ptr
->options
.is_allocated
= false;
43 ptr
= libmemcached_malloc(memc
, sizeof(memcached_result_st
));
48 ptr
->options
.is_allocated
= true;
51 ptr
->options
.is_initialized
= true;
53 _result_init(ptr
, memc
);
56 WATCHPOINT_SET(ptr
->value
.options
.is_initialized
= false);
57 memcached_string_create(memc
, &ptr
->value
, 0);
58 WATCHPOINT_ASSERT_INITIALIZED(&ptr
->value
);
59 WATCHPOINT_ASSERT(ptr
->value
.string
== NULL
);
64 void memcached_result_reset(memcached_result_st
*ptr
)
67 memcached_string_reset(&ptr
->value
);
70 ptr
->item_expiration
= 0;
73 void memcached_result_free(memcached_result_st
*ptr
)
78 memcached_string_free(&ptr
->value
);
80 if (memcached_is_allocated(ptr
))
82 WATCHPOINT_ASSERT(ptr
->root
); // Without a root, that means that result was not properly initialized.
83 libmemcached_free(ptr
->root
, ptr
);
87 ptr
->options
.is_initialized
= false;
91 memcached_return_t
memcached_result_set_value(memcached_result_st
*ptr
,
95 memcached_return_t rc
= memcached_string_append(&ptr
->value
, value
, length
);
97 if (rc
== MEMCACHED_MEMORY_ALLOCATION_FAILURE
)
99 ((memcached_st
*)ptr
->root
)->cached_errno
= errno
;
105 const char *memcached_result_key_value(const memcached_result_st
*self
)
107 return self
->key_length
? self
->item_key
: NULL
;
110 size_t memcached_result_key_length(const memcached_result_st
*self
)
112 return self
->key_length
;
115 const char *memcached_result_value(const memcached_result_st
*self
)
117 const memcached_string_st
*sptr
= &self
->value
;
118 return memcached_string_value(sptr
);
121 size_t memcached_result_length(const memcached_result_st
*self
)
123 const memcached_string_st
*sptr
= &self
->value
;
124 return memcached_string_length(sptr
);
127 uint32_t memcached_result_flags(const memcached_result_st
*self
)
129 return self
->item_flags
;
132 uint64_t memcached_result_cas(const memcached_result_st
*self
)
134 return self
->item_cas
;
137 void memcached_result_set_flags(memcached_result_st
*self
, uint32_t flags
)
139 self
->item_flags
= flags
;
142 void memcached_result_set_expiration(memcached_result_st
*self
, time_t expiration
)
144 self
->item_expiration
= expiration
;