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