Merge in updates (including removal of some depcrated bits from the examples).
[awesomized/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 .support_cas= false,
56 .tcp_nodelay= false,
57 .use_sort_hosts= false,
58 .use_udp= false,
59 .verify_key= false,
60 .tcp_keepalive= false,
61 },
62 };
63
64 static inline bool _memcached_init(memcached_st *self)
65 {
66 self->state= global_copy.state;
67 self->flags= global_copy.flags;
68 self->virtual_bucket= NULL;
69
70 self->distribution= MEMCACHED_DISTRIBUTION_MODULA;
71
72 hashkit_st *hash_ptr;
73 hash_ptr= hashkit_create(&self->hashkit);
74 if (! hash_ptr)
75 return false;
76
77 self->ketama.continuum= NULL;
78 self->ketama.continuum_count= 0;
79 self->ketama.continuum_points_counter= 0;
80 self->ketama.next_distribution_rebuild= 0;
81 self->ketama.weighted= false;
82
83 self->number_of_hosts= 0;
84 self->servers= NULL;
85 self->last_disconnected_server= NULL;
86
87 self->snd_timeout= 0;
88 self->rcv_timeout= 0;
89 self->server_failure_limit= 0;
90 self->query_id= 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.initial_pool_size= 1;
125 self->configure.max_pool_size= 1;
126 self->configure.filename= NULL;
127
128 return true;
129 }
130
131 static void _free(memcached_st *ptr, bool release_st)
132 {
133 /* If we have anything open, lets close it now */
134 send_quit(ptr);
135 memcached_server_list_free(memcached_server_list(ptr));
136 memcached_result_free(&ptr->result);
137
138 memcached_virtual_bucket_free(ptr);
139
140 if (ptr->last_disconnected_server)
141 memcached_server_free(ptr->last_disconnected_server);
142
143 if (ptr->on_cleanup)
144 ptr->on_cleanup(ptr);
145
146 if (ptr->ketama.continuum)
147 libmemcached_free(ptr, ptr->ketama.continuum);
148
149 memcached_array_free(ptr->prefix_key);
150 ptr->prefix_key= NULL;
151
152 memcached_error_free(ptr);
153
154 if (ptr->sasl.callbacks)
155 {
156 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
157 memcached_destroy_sasl_auth_data(ptr);
158 #endif
159 }
160
161 if (release_st)
162 {
163 memcached_array_free(ptr->configure.filename);
164 ptr->configure.filename= NULL;
165 }
166
167 if (memcached_is_allocated(ptr) && release_st)
168 {
169 libmemcached_free(ptr, ptr);
170 }
171 }
172
173 memcached_st *memcached_create(memcached_st *ptr)
174 {
175 if (ptr == NULL)
176 {
177 ptr= (memcached_st *)malloc(sizeof(memcached_st));
178
179 if (! ptr)
180 {
181 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
182 }
183
184 ptr->options.is_allocated= true;
185 }
186 else
187 {
188 ptr->options.is_allocated= false;
189 }
190
191 #if 0
192 memcached_set_purging(ptr, false);
193 memcached_set_processing_input(ptr, false);
194 #endif
195
196 if (! _memcached_init(ptr))
197 {
198 memcached_free(ptr);
199 return NULL;
200 }
201
202 if (! memcached_result_create(ptr, &ptr->result))
203 {
204 memcached_free(ptr);
205 return NULL;
206 }
207
208 WATCHPOINT_ASSERT_INITIALIZED(&ptr->result);
209
210 return ptr;
211 }
212
213 memcached_st *memcached(const char *string, size_t length)
214 {
215 if (! length || ! string)
216 {
217 errno= EINVAL;
218 return NULL;
219 }
220
221 memcached_st *self= memcached_create(NULL);
222 if (! self)
223 {
224 errno= ENOMEM;
225 return NULL;
226 }
227
228 memcached_return_t rc;
229 rc= memcached_parse_configuration(self, string, length);
230
231 if (rc == MEMCACHED_SUCCESS && memcached_parse_filename(self))
232 {
233 rc= memcached_parse_configure_file(self, memcached_parse_filename(self), memcached_parse_filename_length(self));
234 }
235
236 if (rc != MEMCACHED_SUCCESS)
237 {
238 memcached_free(self);
239 errno= EINVAL;
240 return NULL;
241 }
242
243 errno= 0;
244
245 return self;
246 }
247
248 memcached_return_t memcached_reset(memcached_st *ptr)
249 {
250 WATCHPOINT_ASSERT(ptr);
251 if (! ptr)
252 return MEMCACHED_INVALID_ARGUMENTS;
253
254 bool stored_is_allocated= memcached_is_allocated(ptr);
255 uint64_t query_id= ptr->query_id;
256 _free(ptr, false);
257 memcached_create(ptr);
258 memcached_set_allocated(ptr, stored_is_allocated);
259 ptr->query_id= query_id;
260
261 if (ptr->configure.filename)
262 {
263 return memcached_parse_configure_file(ptr, memcached_param_array(ptr->configure.filename));
264 }
265
266 return MEMCACHED_SUCCESS;
267 }
268
269 void memcached_servers_reset(memcached_st *ptr)
270 {
271 if (! ptr)
272 return;
273
274 memcached_server_list_free(memcached_server_list(ptr));
275
276 memcached_server_list_set(ptr, NULL);
277 ptr->number_of_hosts= 0;
278 if (ptr->last_disconnected_server)
279 {
280 memcached_server_free(ptr->last_disconnected_server);
281 }
282 ptr->last_disconnected_server= NULL;
283 ptr->server_failure_limit= 0;
284 }
285
286 void memcached_reset_last_disconnected_server(memcached_st *ptr)
287 {
288 if (! ptr)
289 return;
290
291 if (ptr->last_disconnected_server)
292 {
293 memcached_server_free(ptr->last_disconnected_server);
294 ptr->last_disconnected_server= NULL;
295 }
296 }
297
298 void memcached_free(memcached_st *ptr)
299 {
300 if (! ptr)
301 return;
302
303 _free(ptr, true);
304 }
305
306 /*
307 clone is the destination, while source is the structure to clone.
308 If source is NULL the call is the same as if a memcached_create() was
309 called.
310 */
311 memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source)
312 {
313 memcached_return_t rc= MEMCACHED_SUCCESS;
314 memcached_st *new_clone;
315
316 if (source == NULL)
317 return memcached_create(clone);
318
319 if (clone && memcached_is_allocated(clone))
320 {
321 return NULL;
322 }
323
324 new_clone= memcached_create(clone);
325
326 if (new_clone == NULL)
327 return NULL;
328
329 new_clone->flags= source->flags;
330 new_clone->send_size= source->send_size;
331 new_clone->recv_size= source->recv_size;
332 new_clone->poll_timeout= source->poll_timeout;
333 new_clone->connect_timeout= source->connect_timeout;
334 new_clone->retry_timeout= source->retry_timeout;
335 new_clone->distribution= source->distribution;
336
337 hashkit_st *hash_ptr;
338
339 hash_ptr= hashkit_clone(&new_clone->hashkit, &source->hashkit);
340 if (! hash_ptr)
341 {
342 memcached_free(new_clone);
343 return NULL;
344 }
345
346 hash_ptr= hashkit_clone(&new_clone->distribution_hashkit, &source->distribution_hashkit);
347 if (! hash_ptr)
348 {
349 memcached_free(new_clone);
350 return NULL;
351 }
352
353 new_clone->user_data= source->user_data;
354
355 new_clone->snd_timeout= source->snd_timeout;
356 new_clone->rcv_timeout= source->rcv_timeout;
357
358 new_clone->on_clone= source->on_clone;
359 new_clone->on_cleanup= source->on_cleanup;
360
361 new_clone->allocators= source->allocators;
362
363 new_clone->get_key_failure= source->get_key_failure;
364 new_clone->delete_trigger= source->delete_trigger;
365 new_clone->server_failure_limit= source->server_failure_limit;
366 new_clone->io_msg_watermark= source->io_msg_watermark;
367 new_clone->io_bytes_watermark= source->io_bytes_watermark;
368 new_clone->io_key_prefetch= source->io_key_prefetch;
369 new_clone->number_of_replicas= source->number_of_replicas;
370 new_clone->tcp_keepidle= source->tcp_keepidle;
371
372 if (memcached_server_count(source))
373 rc= memcached_push(new_clone, source);
374
375 if (rc != MEMCACHED_SUCCESS)
376 {
377 memcached_free(new_clone);
378
379 return NULL;
380 }
381
382
383 new_clone->prefix_key= memcached_array_clone(new_clone, source->prefix_key);
384
385 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
386 if (source->sasl.callbacks)
387 {
388 if (memcached_clone_sasl(new_clone, source) != MEMCACHED_SUCCESS)
389 {
390 memcached_free(new_clone);
391 return NULL;
392 }
393 }
394 #endif
395
396 rc= run_distribution(new_clone);
397
398 if (rc != MEMCACHED_SUCCESS)
399 {
400 memcached_free(new_clone);
401
402 return NULL;
403 }
404
405 if (source->on_clone)
406 source->on_clone(new_clone, source);
407
408 return new_clone;
409 }
410
411 void *memcached_get_user_data(const memcached_st *ptr)
412 {
413 return ptr->user_data;
414 }
415
416 void *memcached_set_user_data(memcached_st *ptr, void *data)
417 {
418 void *ret= ptr->user_data;
419 ptr->user_data= data;
420
421 return ret;
422 }
423
424 memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source)
425 {
426 return memcached_server_push(destination, source->servers);
427 }
428
429 memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key)
430 {
431 return &ptr->servers[server_key];
432 }
433
434 memcached_server_instance_st memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key)
435 {
436 return &ptr->servers[server_key];
437 }