move repository from m6w6 to awesomized
[awesomized/libmemcached] / src / libmemcached / result.cc
1 /*
2 +--------------------------------------------------------------------+
3 | libmemcached - C/C++ Client Library for memcached |
4 +--------------------------------------------------------------------+
5 | Redistribution and use in source and binary forms, with or without |
6 | modification, are permitted under the terms of the BSD license. |
7 | You should have received a copy of the license in a bundled file |
8 | named LICENSE; in case you did not receive a copy you can review |
9 | the terms online at: https://opensource.org/licenses/BSD-3-Clause |
10 +--------------------------------------------------------------------+
11 | Copyright (c) 2006-2014 Brian Aker https://datadifferential.com/ |
12 | Copyright (c) 2020-2021 Michael Wallner https://awesome.co/ |
13 +--------------------------------------------------------------------+
14 */
15
16 #include "libmemcached/common.h"
17
18 static inline void _result_init(memcached_result_st *self, Memcached *memc) {
19 self->item_flags = 0;
20 self->item_expiration = 0;
21 self->key_length = 0;
22 self->item_cas = 0;
23 self->root = memc;
24 self->numeric_value = UINT64_MAX;
25 self->count = 0;
26 self->item_key[0] = 0;
27 }
28
29 memcached_result_st *memcached_result_create(const memcached_st *shell, memcached_result_st *ptr) {
30 const Memcached *memc = memcached2Memcached(shell);
31
32 /* Saving malloc calls :) */
33 if (ptr) {
34 ptr->options.is_allocated = false;
35 } else {
36 ptr = libmemcached_xmalloc(memc, memcached_result_st);
37
38 if (not ptr) {
39 return NULL;
40 }
41
42 ptr->options.is_allocated = true;
43 }
44
45 ptr->options.is_initialized = true;
46
47 _result_init(ptr, (memcached_st *) memc);
48
49 WATCHPOINT_SET(ptr->value.options.is_initialized = false);
50 memcached_string_create((memcached_st *) memc, &ptr->value, 0);
51 WATCHPOINT_ASSERT_INITIALIZED(&ptr->value);
52 WATCHPOINT_ASSERT(ptr->value.string == NULL);
53
54 return ptr;
55 }
56
57 void memcached_result_reset(memcached_result_st *ptr) {
58 ptr->key_length = 0;
59 memcached_string_reset(&ptr->value);
60 ptr->item_flags = 0;
61 ptr->item_cas = 0;
62 ptr->item_expiration = 0;
63 ptr->numeric_value = UINT64_MAX;
64 }
65
66 void memcached_result_free(memcached_result_st *ptr) {
67 if (ptr == NULL) {
68 return;
69 }
70
71 memcached_string_free(&ptr->value);
72 ptr->numeric_value = UINT64_MAX;
73
74 if (memcached_is_allocated(ptr)) {
75 WATCHPOINT_ASSERT(
76 ptr->root); // Without a root, that means that result was not properly initialized.
77 libmemcached_free(ptr->root, ptr);
78 } else {
79 ptr->count = 0;
80 ptr->options.is_initialized = false;
81 }
82 }
83
84 void memcached_result_reset_value(memcached_result_st *ptr) {
85 memcached_string_reset(&ptr->value);
86 }
87
88 memcached_return_t memcached_result_set_value(memcached_result_st *ptr, const char *value,
89 size_t length) {
90 if (memcached_failed(memcached_string_append(&ptr->value, value, length))) {
91 return memcached_set_errno(*ptr->root, errno, MEMCACHED_AT);
92 }
93
94 return MEMCACHED_SUCCESS;
95 }
96
97 const char *memcached_result_key_value(const memcached_result_st *self) {
98 return self->key_length ? self->item_key : NULL;
99 }
100
101 size_t memcached_result_key_length(const memcached_result_st *self) {
102 return self->key_length;
103 }
104
105 const char *memcached_result_value(const memcached_result_st *self) {
106 const memcached_string_st *sptr = &self->value;
107 return memcached_string_value(sptr);
108 }
109
110 size_t memcached_result_length(const memcached_result_st *self) {
111 const memcached_string_st *sptr = &self->value;
112 return memcached_string_length(sptr);
113 }
114
115 char *memcached_result_take_value(memcached_result_st *self) {
116 memcached_string_st *sptr = &self->value;
117 return memcached_string_take_value(sptr);
118 }
119
120 uint32_t memcached_result_flags(const memcached_result_st *self) {
121 return self->item_flags;
122 }
123
124 uint64_t memcached_result_cas(const memcached_result_st *self) {
125 return self->item_cas;
126 }
127
128 void memcached_result_set_flags(memcached_result_st *self, uint32_t flags) {
129 self->item_flags = flags;
130 }
131
132 void memcached_result_set_expiration(memcached_result_st *self, time_t expiration) {
133 self->item_expiration = expiration;
134 }