Remove requirements for rpm that were unused.
[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
40 #include <libmemcached/options.hpp>
41 #include <libmemcached/virtual_bucket.h>
42
43 static inline bool _memcached_init(memcached_st *self)
44 {
45 self->state.is_purging= false;
46 self->state.is_processing_input= false;
47 self->state.is_time_for_rebuild= false;
48 self->state.is_parsing= false;
49
50 self->flags.auto_eject_hosts= false;
51 self->flags.binary_protocol= false;
52 self->flags.buffer_requests= false;
53 self->flags.hash_with_namespace= false;
54 self->flags.no_block= false;
55 self->flags.reply= true;
56 self->flags.randomize_replica_read= false;
57 self->flags.support_cas= false;
58 self->flags.tcp_nodelay= false;
59 self->flags.use_sort_hosts= false;
60 self->flags.use_udp= false;
61 self->flags.verify_key= false;
62 self->flags.tcp_keepalive= false;
63 self->flags.is_aes= false;
64 self->flags.is_fetching_version= false;
65
66 self->virtual_bucket= NULL;
67
68 self->distribution= MEMCACHED_DISTRIBUTION_MODULA;
69
70 if (hashkit_create(&self->hashkit) == NULL)
71 {
72 return false;
73 }
74
75 self->server_info.version= 0;
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= MEMCACHED_SERVER_FAILURE_LIMIT;
90 self->query_id= 1; // 0 is considered invalid
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= MEMCACHED_SERVER_FAILURE_RETRY_TIMEOUT;
102 self->dead_timeout= MEMCACHED_SERVER_FAILURE_DEAD_TIMEOUT;
103
104 self->send_size= -1;
105 self->recv_size= -1;
106
107 self->user_data= NULL;
108 self->number_of_replicas= 0;
109
110 self->allocators= memcached_allocators_return_default();
111
112 self->on_clone= NULL;
113 self->on_cleanup= NULL;
114 self->get_key_failure= NULL;
115 self->delete_trigger= NULL;
116 self->callbacks= NULL;
117 self->sasl.callbacks= NULL;
118 self->sasl.is_allocated= false;
119
120 self->error_messages= NULL;
121 self->_namespace= NULL;
122 self->configure.initial_pool_size= 1;
123 self->configure.max_pool_size= 1;
124 self->configure.version= -1;
125 self->configure.filename= NULL;
126
127 return true;
128 }
129
130 static void __memcached_free(memcached_st *ptr, bool release_st)
131 {
132 /* If we have anything open, lets close it now */
133 send_quit(ptr);
134 memcached_instance_list_free(memcached_instance_list(ptr), memcached_instance_list_count(ptr));
135 memcached_result_free(&ptr->result);
136
137 memcached_virtual_bucket_free(ptr);
138
139 memcached_instance_free((org::libmemcached::Instance*)ptr->last_disconnected_server);
140
141 if (ptr->on_cleanup)
142 {
143 ptr->on_cleanup(ptr);
144 }
145
146 libmemcached_free(ptr, ptr->ketama.continuum);
147
148 memcached_array_free(ptr->_namespace);
149 ptr->_namespace= NULL;
150
151 memcached_error_free(*ptr);
152
153 if (LIBMEMCACHED_WITH_SASL_SUPPORT and ptr->sasl.callbacks)
154 {
155 memcached_destroy_sasl_auth_data(ptr);
156 }
157
158 if (release_st)
159 {
160 memcached_array_free(ptr->configure.filename);
161 ptr->configure.filename= NULL;
162 }
163
164 hashkit_free(&ptr->hashkit);
165
166 if (memcached_is_allocated(ptr) and release_st)
167 {
168 libmemcached_free(ptr, ptr);
169 }
170 }
171
172 memcached_st *memcached_create(memcached_st *ptr)
173 {
174 if (ptr)
175 {
176 ptr->options.is_allocated= false;
177 }
178 else
179 {
180 ptr= (memcached_st *)libmemcached_xmalloc(NULL, memcached_st);
181
182 if (ptr == NULL)
183 {
184 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
185 }
186
187 ptr->options.is_allocated= true;
188 }
189
190 if (_memcached_init(ptr) == false)
191 {
192 memcached_free(ptr);
193 return NULL;
194 }
195
196 if (memcached_result_create(ptr, &ptr->result) == NULL)
197 {
198 memcached_free(ptr);
199 return NULL;
200 }
201
202 WATCHPOINT_ASSERT_INITIALIZED(&ptr->result);
203
204 return ptr;
205 }
206
207 memcached_st *memcached(const char *string, size_t length)
208 {
209 if (length == 0 and string)
210 {
211 return NULL;
212 }
213
214 if (length and string == NULL)
215 {
216 return NULL;
217 }
218
219 if (length == 0)
220 {
221 if (bool(getenv("LIBMEMCACHED")))
222 {
223 string= getenv("LIBMEMCACHED");
224 length= string ? strlen(string) : 0;
225 }
226 }
227
228 memcached_st *memc= memcached_create(NULL);
229 if (memc == NULL)
230 {
231 return NULL;
232 }
233
234 if (length == 0 or string == NULL)
235 {
236 return memc;
237 }
238
239 memcached_return_t rc= memcached_parse_configuration(memc, string, length);
240 if (memcached_success(rc) and memcached_parse_filename(memc))
241 {
242 rc= memcached_parse_configure_file(*memc, memcached_parse_filename(memc), memcached_parse_filename_length(memc));
243 }
244
245 if (memcached_failed(rc))
246 {
247 memcached_free(memc);
248 return NULL;
249 }
250
251 return memc;
252 }
253
254 memcached_return_t memcached_reset(memcached_st *ptr)
255 {
256 WATCHPOINT_ASSERT(ptr);
257 if (ptr == NULL)
258 {
259 return MEMCACHED_INVALID_ARGUMENTS;
260 }
261
262 bool stored_is_allocated= memcached_is_allocated(ptr);
263 uint64_t query_id= ptr->query_id;
264 __memcached_free(ptr, false);
265 memcached_create(ptr);
266 memcached_set_allocated(ptr, stored_is_allocated);
267 ptr->query_id= query_id;
268
269 if (ptr->configure.filename)
270 {
271 return memcached_parse_configure_file(*ptr, memcached_param_array(ptr->configure.filename));
272 }
273
274 return MEMCACHED_SUCCESS;
275 }
276
277 void memcached_servers_reset(memcached_st *self)
278 {
279 if (self)
280 {
281 memcached_instance_list_free(memcached_instance_list(self), self->number_of_hosts);
282
283 memcached_instance_set(self, NULL, 0);
284 self->number_of_hosts= 0;
285 memcached_instance_free((org::libmemcached::Instance*)self->last_disconnected_server);
286 self->last_disconnected_server= NULL;
287 }
288 }
289
290 void memcached_reset_last_disconnected_server(memcached_st *self)
291 {
292 if (self)
293 {
294 memcached_instance_free((org::libmemcached::Instance*)self->last_disconnected_server);
295 self->last_disconnected_server= NULL;
296 }
297 }
298
299 void memcached_free(memcached_st *ptr)
300 {
301 if (ptr)
302 {
303 __memcached_free(ptr, true);
304 }
305 }
306
307 /*
308 clone is the destination, while source is the structure to clone.
309 If source is NULL the call is the same as if a memcached_create() was
310 called.
311 */
312 memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source)
313 {
314 if (source == NULL)
315 {
316 return memcached_create(clone);
317 }
318
319 if (clone and memcached_is_allocated(clone))
320 {
321 return NULL;
322 }
323
324 memcached_st *new_clone= memcached_create(clone);
325
326 if (new_clone == NULL)
327 {
328 return NULL;
329 }
330
331 new_clone->flags= source->flags;
332 new_clone->send_size= source->send_size;
333 new_clone->recv_size= source->recv_size;
334 new_clone->poll_timeout= source->poll_timeout;
335 new_clone->connect_timeout= source->connect_timeout;
336 new_clone->retry_timeout= source->retry_timeout;
337 new_clone->dead_timeout= source->dead_timeout;
338 new_clone->distribution= source->distribution;
339
340 if (hashkit_clone(&new_clone->hashkit, &source->hashkit) == NULL)
341 {
342 memcached_free(new_clone);
343 return NULL;
344 }
345
346 new_clone->user_data= source->user_data;
347
348 new_clone->snd_timeout= source->snd_timeout;
349 new_clone->rcv_timeout= source->rcv_timeout;
350
351 new_clone->on_clone= source->on_clone;
352 new_clone->on_cleanup= source->on_cleanup;
353
354 new_clone->allocators= source->allocators;
355
356 new_clone->get_key_failure= source->get_key_failure;
357 new_clone->delete_trigger= source->delete_trigger;
358 new_clone->server_failure_limit= source->server_failure_limit;
359 new_clone->io_msg_watermark= source->io_msg_watermark;
360 new_clone->io_bytes_watermark= source->io_bytes_watermark;
361 new_clone->io_key_prefetch= source->io_key_prefetch;
362 new_clone->number_of_replicas= source->number_of_replicas;
363 new_clone->tcp_keepidle= source->tcp_keepidle;
364
365 if (memcached_server_count(source))
366 {
367 if (memcached_failed(memcached_push(new_clone, source)))
368 {
369 return NULL;
370 }
371 }
372
373
374 new_clone->_namespace= memcached_array_clone(new_clone, source->_namespace);
375 new_clone->configure.filename= memcached_array_clone(new_clone, source->_namespace);
376 new_clone->configure.version= source->configure.version;
377
378 if (LIBMEMCACHED_WITH_SASL_SUPPORT and source->sasl.callbacks)
379 {
380 if (memcached_failed(memcached_clone_sasl(new_clone, source)))
381 {
382 memcached_free(new_clone);
383 return NULL;
384 }
385 }
386
387 if (memcached_failed(run_distribution(new_clone)))
388 {
389 memcached_free(new_clone);
390
391 return NULL;
392 }
393
394 if (source->on_clone)
395 {
396 source->on_clone(new_clone, source);
397 }
398
399 return new_clone;
400 }
401
402 void *memcached_get_user_data(const memcached_st *ptr)
403 {
404 if (ptr == NULL)
405 {
406 return NULL;
407 }
408
409 return ptr->user_data;
410 }
411
412 void *memcached_set_user_data(memcached_st *ptr, void *data)
413 {
414 if (ptr == NULL)
415 {
416 return NULL;
417 }
418
419 void *ret= ptr->user_data;
420 ptr->user_data= data;
421
422 return ret;
423 }
424
425 memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source)
426 {
427 return memcached_instance_push(destination, (org::libmemcached::Instance*)source->servers, source->number_of_hosts);
428 }
429
430 org::libmemcached::Instance* memcached_instance_fetch(memcached_st *ptr, uint32_t server_key)
431 {
432 if (ptr == NULL)
433 {
434 return NULL;
435 }
436
437 return &ptr->servers[server_key];
438 }
439
440 memcached_server_instance_st memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key)
441 {
442 if (ptr == NULL)
443 {
444 return NULL;
445 }
446
447 return &ptr->servers[server_key];
448 }
449
450 org::libmemcached::Instance* memcached_instance_by_position(const memcached_st *ptr, uint32_t server_key)
451 {
452 if (ptr == NULL)
453 {
454 return NULL;
455 }
456
457 return &ptr->servers[server_key];
458 }
459
460 uint64_t memcached_query_id(const memcached_st *self)
461 {
462 if (self == NULL)
463 {
464 return 0;
465 }
466
467 return self->query_id;
468 }
469
470 org::libmemcached::Instance* memcached_instance_list(const memcached_st *self)
471 {
472 if (self)
473 {
474 return (org::libmemcached::Instance*)self->servers;
475 }
476
477 return NULL;
478 }
479