1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
15 * * Redistributions in binary form must reproduce the above
16 * copyright notice, this list of conditions and the following disclaimer
17 * in the documentation and/or other materials provided with the
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
25 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
26 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
27 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
28 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
29 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
30 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
31 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
32 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
33 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
34 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
40 memcached_result_st are used to internally represent the return values from
41 memcached. We use a structure so that long term as identifiers are added
42 to memcached we will be able to absorb new attributes without having
43 to addjust the entire API.
45 #include <libmemcached/common.h>
47 static inline void _result_init(memcached_result_st
*self
,
51 self
->item_expiration
= 0;
59 memcached_result_st
*memcached_result_create(const memcached_st
*memc
,
60 memcached_result_st
*ptr
)
62 WATCHPOINT_ASSERT(memc
);
64 /* Saving malloc calls :) */
67 ptr
->options
.is_allocated
= false;
71 ptr
= static_cast<memcached_result_st
*>(libmemcached_malloc(memc
, sizeof(memcached_result_st
)));
78 ptr
->options
.is_allocated
= true;
81 ptr
->options
.is_initialized
= true;
83 _result_init(ptr
, (memcached_st
*)memc
);
85 WATCHPOINT_SET(ptr
->value
.options
.is_initialized
= false);
86 memcached_string_create((memcached_st
*)memc
, &ptr
->value
, 0);
87 WATCHPOINT_ASSERT_INITIALIZED(&ptr
->value
);
88 WATCHPOINT_ASSERT(ptr
->value
.string
== NULL
);
93 void memcached_result_reset(memcached_result_st
*ptr
)
96 memcached_string_reset(&ptr
->value
);
99 ptr
->item_expiration
= 0;
102 void memcached_result_free(memcached_result_st
*ptr
)
109 memcached_string_free(&ptr
->value
);
111 if (memcached_is_allocated(ptr
))
113 WATCHPOINT_ASSERT(ptr
->root
); // Without a root, that means that result was not properly initialized.
114 libmemcached_free(ptr
->root
, ptr
);
119 ptr
->options
.is_initialized
= false;
123 memcached_return_t
memcached_result_set_value(memcached_result_st
*ptr
,
127 if (memcached_failed(memcached_string_append(&ptr
->value
, value
, length
)))
129 return memcached_set_errno(*ptr
->root
, errno
, MEMCACHED_AT
);
132 return MEMCACHED_SUCCESS
;
135 const char *memcached_result_key_value(const memcached_result_st
*self
)
137 return self
->key_length
? self
->item_key
: NULL
;
140 size_t memcached_result_key_length(const memcached_result_st
*self
)
142 return self
->key_length
;
145 const char *memcached_result_value(const memcached_result_st
*self
)
147 const memcached_string_st
*sptr
= &self
->value
;
148 return memcached_string_value(sptr
);
151 size_t memcached_result_length(const memcached_result_st
*self
)
153 const memcached_string_st
*sptr
= &self
->value
;
154 return memcached_string_length(sptr
);
157 uint32_t memcached_result_flags(const memcached_result_st
*self
)
159 return self
->item_flags
;
162 uint64_t memcached_result_cas(const memcached_result_st
*self
)
164 return self
->item_cas
;
167 void memcached_result_set_flags(memcached_result_st
*self
, uint32_t flags
)
169 self
->item_flags
= flags
;
172 void memcached_result_set_expiration(memcached_result_st
*self
, time_t expiration
)
174 self
->item_expiration
= expiration
;