@$#$@$ gcc
[awesomized/libmemcached] / libmemcached / memcached.c
1 /* LibMemcached
2 * Copyright (C) 2006-2010 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:
9 *
10 */
11
12 #include "common.h"
13
14 static const memcached_st global_copy= {
15 .state= {
16 .is_purging= false,
17 .is_processing_input= false,
18 },
19 .flags= {
20 .auto_eject_hosts= false,
21 .binary_protocol= false,
22 .buffer_requests= false,
23 .cork= false,
24 .hash_with_prefix_key= false,
25 .ketama_weighted= false,
26 .no_block= false,
27 .no_reply= false,
28 .randomize_replica_read= false,
29 .reuse_memory= false,
30 .support_cas= false,
31 .tcp_nodelay= false,
32 .use_cache_lookups= false,
33 .use_sort_hosts= false,
34 .use_udp= false,
35 .verify_key= false
36 }
37 };
38
39 static inline bool _memcached_init(memcached_st *self)
40 {
41 self->state= global_copy.state;
42 self->flags= global_copy.flags;
43
44 self->distribution= MEMCACHED_DISTRIBUTION_MODULA;
45
46 hashkit_st *hash_ptr;
47 hash_ptr= hashkit_create(&self->hashkit);
48 if (! hash_ptr)
49 return false;
50
51 self->continuum_points_counter= 0;
52
53 self->number_of_hosts= 0;
54 self->servers= NULL;
55 self->last_disconnected_server= NULL;
56
57 self->snd_timeout= 0;
58 self->rcv_timeout= 0;
59 self->server_failure_limit= 0;
60
61 /* TODO, Document why we picked these defaults */
62 self->io_msg_watermark= 500;
63 self->io_bytes_watermark= 65 * 1024;
64
65 self->io_key_prefetch= 0;
66 self->cached_errno= 0;
67 self->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT;
68 self->connect_timeout= MEMCACHED_DEFAULT_TIMEOUT;
69 self->retry_timeout= 0;
70 self->continuum_count= 0;
71
72 self->send_size= -1;
73 self->recv_size= -1;
74
75 self->user_data= NULL;
76 self->next_distribution_rebuild= 0;
77 self->prefix_key_length= 0;
78 self->number_of_replicas= 0;
79 hash_ptr= hashkit_create(&self->distribution_hashkit);
80 if (! hash_ptr)
81 return false;
82 self->continuum= NULL;
83
84 self->allocators= memcached_allocators_return_default();
85
86 self->on_clone= NULL;
87 self->on_cleanup= NULL;
88 self->get_key_failure= NULL;
89 self->delete_trigger= NULL;
90 self->callbacks= NULL;
91 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
92 self->sasl.callbacks= NULL;
93 self->sasl.is_allocated= false;
94 #endif
95
96 return true;
97 }
98
99 memcached_st *memcached_create(memcached_st *ptr)
100 {
101 if (ptr == NULL)
102 {
103 ptr= (memcached_st *)malloc(sizeof(memcached_st));
104
105 if (! ptr)
106 {
107 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
108 }
109
110 ptr->options.is_allocated= true;
111 }
112 else
113 {
114 ptr->options.is_allocated= false;
115 }
116
117 #if 0
118 memcached_set_purging(ptr, false);
119 memcached_set_processing_input(ptr, false);
120 #endif
121
122 if (! _memcached_init(ptr))
123 {
124 memcached_free(ptr);
125 return NULL;
126 }
127
128 if (! memcached_result_create(ptr, &ptr->result))
129 {
130 memcached_free(ptr);
131 return NULL;
132 }
133
134 WATCHPOINT_ASSERT_INITIALIZED(&ptr->result);
135
136 return ptr;
137 }
138
139 void memcached_servers_reset(memcached_st *ptr)
140 {
141 memcached_server_list_free(memcached_server_list(ptr));
142
143 memcached_server_list_set(ptr, NULL);
144 ptr->number_of_hosts= 0;
145 ptr->last_disconnected_server= NULL;
146 ptr->server_failure_limit= 0;
147 }
148
149 void memcached_free(memcached_st *ptr)
150 {
151 /* If we have anything open, lets close it now */
152 memcached_quit(ptr);
153 memcached_server_list_free(memcached_server_list(ptr));
154 memcached_result_free(&ptr->result);
155
156 if (ptr->on_cleanup)
157 ptr->on_cleanup(ptr);
158
159 if (ptr->continuum)
160 libmemcached_free(ptr, ptr->continuum);
161
162 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
163 if (ptr->sasl.callbacks != NULL)
164 {
165 memcached_destroy_sasl_auth_data(ptr);
166 }
167 #endif
168
169 if (memcached_is_allocated(ptr))
170 {
171 libmemcached_free(ptr, ptr);
172 }
173 }
174
175 /*
176 clone is the destination, while source is the structure to clone.
177 If source is NULL the call is the same as if a memcached_create() was
178 called.
179 */
180 memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source)
181 {
182 memcached_return_t rc= MEMCACHED_SUCCESS;
183 memcached_st *new_clone;
184
185 if (source == NULL)
186 return memcached_create(clone);
187
188 if (clone && memcached_is_allocated(clone))
189 {
190 return NULL;
191 }
192
193 new_clone= memcached_create(clone);
194
195 if (new_clone == NULL)
196 return NULL;
197
198 new_clone->flags= source->flags;
199 new_clone->send_size= source->send_size;
200 new_clone->recv_size= source->recv_size;
201 new_clone->poll_timeout= source->poll_timeout;
202 new_clone->connect_timeout= source->connect_timeout;
203 new_clone->retry_timeout= source->retry_timeout;
204 new_clone->distribution= source->distribution;
205
206 hashkit_st *hash_ptr;
207
208 hash_ptr= hashkit_clone(&new_clone->hashkit, &source->hashkit);
209 if (! hash_ptr)
210 {
211 memcached_free(new_clone);
212 return NULL;
213 }
214
215 hash_ptr= hashkit_clone(&new_clone->distribution_hashkit, &source->distribution_hashkit);
216 if (! hash_ptr)
217 {
218 memcached_free(new_clone);
219 return NULL;
220 }
221
222 new_clone->user_data= source->user_data;
223
224 new_clone->snd_timeout= source->snd_timeout;
225 new_clone->rcv_timeout= source->rcv_timeout;
226
227 new_clone->on_clone= source->on_clone;
228 new_clone->on_cleanup= source->on_cleanup;
229
230 new_clone->allocators= source->allocators;
231
232 new_clone->get_key_failure= source->get_key_failure;
233 new_clone->delete_trigger= source->delete_trigger;
234 new_clone->server_failure_limit= source->server_failure_limit;
235 new_clone->io_msg_watermark= source->io_msg_watermark;
236 new_clone->io_bytes_watermark= source->io_bytes_watermark;
237 new_clone->io_key_prefetch= source->io_key_prefetch;
238 new_clone->number_of_replicas= source->number_of_replicas;
239
240 if (memcached_server_count(source))
241 rc= memcached_push(new_clone, source);
242
243 if (rc != MEMCACHED_SUCCESS)
244 {
245 memcached_free(new_clone);
246
247 return NULL;
248 }
249
250
251 if (source->prefix_key_length)
252 {
253 strcpy(new_clone->prefix_key, source->prefix_key);
254 new_clone->prefix_key_length= source->prefix_key_length;
255 }
256
257 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
258 if (source->sasl.callbacks)
259 {
260 if (memcached_clone_sasl(new_clone, source) != MEMCACHED_SUCCESS)
261 {
262 memcached_free(new_clone);
263 return NULL;
264 }
265 }
266 #endif
267
268 rc= run_distribution(new_clone);
269
270 if (rc != MEMCACHED_SUCCESS)
271 {
272 memcached_free(new_clone);
273
274 return NULL;
275 }
276
277 if (source->on_clone)
278 source->on_clone(new_clone, source);
279
280 return new_clone;
281 }
282
283 void *memcached_get_user_data(const memcached_st *ptr)
284 {
285 return ptr->user_data;
286 }
287
288 void *memcached_set_user_data(memcached_st *ptr, void *data)
289 {
290 void *ret= ptr->user_data;
291 ptr->user_data= data;
292
293 return ret;
294 }
295
296 memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source)
297 {
298 return memcached_server_push(destination, source->servers);
299 }
300
301 memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key)
302 {
303 return &ptr->servers[server_key];
304 }
305
306 memcached_server_instance_st memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key)
307 {
308 return &ptr->servers[server_key];
309 }