This cleans up some accidental linking we were getting with curses.
[m6w6/libmemcached] / libmemcached / memcached.c
1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
2 *
3 * Libmemcached library
4 *
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
10 * met:
11 *
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 *
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
18 * distribution.
19 *
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
22 * written permission.
23 *
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.
35 *
36 */
37
38 #include <libmemcached/common.h>
39 #include <libmemcached/virtual_bucket.h>
40
41 static const memcached_st global_copy= {
42 .state= {
43 .is_purging= false,
44 .is_processing_input= false,
45 .is_time_for_rebuild= false,
46 },
47 .flags= {
48 .auto_eject_hosts= false,
49 .binary_protocol= false,
50 .buffer_requests= false,
51 .hash_with_prefix_key= false,
52 .no_block= false,
53 .no_reply= false,
54 .randomize_replica_read= false,
55 .reuse_memory= false,
56 .support_cas= false,
57 .tcp_nodelay= false,
58 .use_sort_hosts= false,
59 .use_udp= false,
60 .verify_key= false,
61 .tcp_keepalive= false,
62 },
63 };
64
65 static inline bool _memcached_init(memcached_st *self)
66 {
67 self->state= global_copy.state;
68 self->flags= global_copy.flags;
69 self->virtual_bucket= NULL;
70
71 self->distribution= MEMCACHED_DISTRIBUTION_MODULA;
72
73 hashkit_st *hash_ptr;
74 hash_ptr= hashkit_create(&self->hashkit);
75 if (! hash_ptr)
76 return false;
77
78 self->ketama.continuum= NULL;
79 self->ketama.continuum_count= 0;
80 self->ketama.continuum_points_counter= 0;
81 self->ketama.next_distribution_rebuild= 0;
82 self->ketama.weighted= false;
83
84 self->number_of_hosts= 0;
85 self->servers= NULL;
86 self->last_disconnected_server= NULL;
87
88 self->snd_timeout= 0;
89 self->rcv_timeout= 0;
90 self->server_failure_limit= 0;
91
92 /* TODO, Document why we picked these defaults */
93 self->io_msg_watermark= 500;
94 self->io_bytes_watermark= 65 * 1024;
95
96 self->tcp_keepidle= 0;
97
98 self->io_key_prefetch= 0;
99 self->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT;
100 self->connect_timeout= MEMCACHED_DEFAULT_CONNECT_TIMEOUT;
101 self->retry_timeout= 0;
102
103 self->send_size= -1;
104 self->recv_size= -1;
105
106 self->user_data= NULL;
107 self->number_of_replicas= 0;
108 hash_ptr= hashkit_create(&self->distribution_hashkit);
109 if (! hash_ptr)
110 return false;
111
112 self->allocators= memcached_allocators_return_default();
113
114 self->on_clone= NULL;
115 self->on_cleanup= NULL;
116 self->get_key_failure= NULL;
117 self->delete_trigger= NULL;
118 self->callbacks= NULL;
119 self->sasl.callbacks= NULL;
120 self->sasl.is_allocated= false;
121
122 self->error_messages= NULL;
123 self->prefix_key= NULL;
124 self->configure.filename= NULL;
125
126 return true;
127 }
128
129 static void _free(memcached_st *ptr, bool release_st)
130 {
131 /* If we have anything open, lets close it now */
132 memcached_quit(ptr);
133 memcached_server_list_free(memcached_server_list(ptr));
134 memcached_result_free(&ptr->result);
135
136 memcached_virtual_bucket_free(ptr);
137
138 if (ptr->last_disconnected_server)
139 memcached_server_free(ptr->last_disconnected_server);
140
141 if (ptr->on_cleanup)
142 ptr->on_cleanup(ptr);
143
144 if (ptr->ketama.continuum)
145 libmemcached_free(ptr, ptr->ketama.continuum);
146
147 memcached_array_free(ptr->prefix_key);
148 ptr->prefix_key= NULL;
149
150 memcached_error_free(ptr);
151
152 if (ptr->sasl.callbacks)
153 {
154 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
155 memcached_destroy_sasl_auth_data(ptr);
156 #endif
157 }
158
159 if (release_st)
160 {
161 memcached_array_free(ptr->configure.filename);
162 ptr->configure.filename= NULL;
163 }
164
165 if (memcached_is_allocated(ptr) && release_st)
166 {
167 libmemcached_free(ptr, ptr);
168 }
169 }
170
171 memcached_st *memcached_create(memcached_st *ptr)
172 {
173 if (ptr == NULL)
174 {
175 ptr= (memcached_st *)malloc(sizeof(memcached_st));
176
177 if (! ptr)
178 {
179 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
180 }
181
182 ptr->options.is_allocated= true;
183 }
184 else
185 {
186 ptr->options.is_allocated= false;
187 }
188
189 #if 0
190 memcached_set_purging(ptr, false);
191 memcached_set_processing_input(ptr, false);
192 #endif
193
194 if (! _memcached_init(ptr))
195 {
196 memcached_free(ptr);
197 return NULL;
198 }
199
200 if (! memcached_result_create(ptr, &ptr->result))
201 {
202 memcached_free(ptr);
203 return NULL;
204 }
205
206 WATCHPOINT_ASSERT_INITIALIZED(&ptr->result);
207
208 return ptr;
209 }
210
211 memcached_st *memcached_create_with_options(const char *string, size_t length)
212 {
213 memcached_st *self= memcached_create(NULL);
214
215 if (! self)
216 return NULL;
217
218 memcached_return_t rc;
219 if ((rc= memcached_parse_configuration(self, string, length)) != MEMCACHED_SUCCESS)
220 {
221 return self;
222 }
223
224 if (memcached_parse_filename(self))
225 {
226 rc= memcached_parse_configure_file(self, memcached_parse_filename(self), memcached_parse_filename_length(self));
227 }
228
229 return self;
230 }
231
232 memcached_return_t memcached_reset(memcached_st *ptr)
233 {
234 WATCHPOINT_ASSERT(ptr);
235 if (! ptr)
236 return MEMCACHED_INVALID_ARGUMENTS;
237
238 bool stored_is_allocated= memcached_is_allocated(ptr);
239 _free(ptr, false);
240 memcached_create(ptr);
241 memcached_set_allocated(ptr, stored_is_allocated);
242
243 if (ptr->configure.filename)
244 {
245 return memcached_parse_configure_file(ptr, memcached_param_array(ptr->configure.filename));
246 }
247
248 return MEMCACHED_SUCCESS;
249 }
250
251 void memcached_servers_reset(memcached_st *ptr)
252 {
253 memcached_server_list_free(memcached_server_list(ptr));
254
255 memcached_server_list_set(ptr, NULL);
256 ptr->number_of_hosts= 0;
257 if (ptr->last_disconnected_server)
258 {
259 memcached_server_free(ptr->last_disconnected_server);
260 }
261 ptr->last_disconnected_server= NULL;
262 ptr->server_failure_limit= 0;
263 }
264
265 void memcached_reset_last_disconnected_server(memcached_st *ptr)
266 {
267 if (ptr->last_disconnected_server)
268 {
269 memcached_server_free(ptr->last_disconnected_server);
270 ptr->last_disconnected_server= NULL;
271 }
272 }
273
274 void memcached_free(memcached_st *ptr)
275 {
276 _free(ptr, true);
277 }
278
279 /*
280 clone is the destination, while source is the structure to clone.
281 If source is NULL the call is the same as if a memcached_create() was
282 called.
283 */
284 memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source)
285 {
286 memcached_return_t rc= MEMCACHED_SUCCESS;
287 memcached_st *new_clone;
288
289 if (source == NULL)
290 return memcached_create(clone);
291
292 if (clone && memcached_is_allocated(clone))
293 {
294 return NULL;
295 }
296
297 new_clone= memcached_create(clone);
298
299 if (new_clone == NULL)
300 return NULL;
301
302 new_clone->flags= source->flags;
303 new_clone->send_size= source->send_size;
304 new_clone->recv_size= source->recv_size;
305 new_clone->poll_timeout= source->poll_timeout;
306 new_clone->connect_timeout= source->connect_timeout;
307 new_clone->retry_timeout= source->retry_timeout;
308 new_clone->distribution= source->distribution;
309
310 hashkit_st *hash_ptr;
311
312 hash_ptr= hashkit_clone(&new_clone->hashkit, &source->hashkit);
313 if (! hash_ptr)
314 {
315 memcached_free(new_clone);
316 return NULL;
317 }
318
319 hash_ptr= hashkit_clone(&new_clone->distribution_hashkit, &source->distribution_hashkit);
320 if (! hash_ptr)
321 {
322 memcached_free(new_clone);
323 return NULL;
324 }
325
326 new_clone->user_data= source->user_data;
327
328 new_clone->snd_timeout= source->snd_timeout;
329 new_clone->rcv_timeout= source->rcv_timeout;
330
331 new_clone->on_clone= source->on_clone;
332 new_clone->on_cleanup= source->on_cleanup;
333
334 new_clone->allocators= source->allocators;
335
336 new_clone->get_key_failure= source->get_key_failure;
337 new_clone->delete_trigger= source->delete_trigger;
338 new_clone->server_failure_limit= source->server_failure_limit;
339 new_clone->io_msg_watermark= source->io_msg_watermark;
340 new_clone->io_bytes_watermark= source->io_bytes_watermark;
341 new_clone->io_key_prefetch= source->io_key_prefetch;
342 new_clone->number_of_replicas= source->number_of_replicas;
343 new_clone->tcp_keepidle= source->tcp_keepidle;
344
345 if (memcached_server_count(source))
346 rc= memcached_push(new_clone, source);
347
348 if (rc != MEMCACHED_SUCCESS)
349 {
350 memcached_free(new_clone);
351
352 return NULL;
353 }
354
355
356 new_clone->prefix_key= memcached_array_clone(new_clone, source->prefix_key);
357
358 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
359 if (source->sasl.callbacks)
360 {
361 if (memcached_clone_sasl(new_clone, source) != MEMCACHED_SUCCESS)
362 {
363 memcached_free(new_clone);
364 return NULL;
365 }
366 }
367 #endif
368
369 rc= run_distribution(new_clone);
370
371 if (rc != MEMCACHED_SUCCESS)
372 {
373 memcached_free(new_clone);
374
375 return NULL;
376 }
377
378 if (source->on_clone)
379 source->on_clone(new_clone, source);
380
381 return new_clone;
382 }
383
384 void *memcached_get_user_data(const memcached_st *ptr)
385 {
386 return ptr->user_data;
387 }
388
389 void *memcached_set_user_data(memcached_st *ptr, void *data)
390 {
391 void *ret= ptr->user_data;
392 ptr->user_data= data;
393
394 return ret;
395 }
396
397 memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source)
398 {
399 return memcached_server_push(destination, source->servers);
400 }
401
402 memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key)
403 {
404 return &ptr->servers[server_key];
405 }
406
407 memcached_server_instance_st memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key)
408 {
409 return &ptr->servers[server_key];
410 }