1 /* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
5 * Copyright (C) 2011 Data Differential, http://datadifferential.com/
6 * Copyright (C) 2006-2009 Brian Aker All rights reserved.
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are
12 * * Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
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
20 * * The names of its contributors may not be used to endorse or
21 * promote products derived from this software without specific prior
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.
38 #include <libmemcached/common.h>
40 #include <libmemcached/options.hpp>
41 #include <libmemcached/virtual_bucket.h>
43 static inline bool _memcached_init(memcached_st
*self
)
45 self
->state
.is_purging
= false;
46 self
->state
.is_processing_input
= false;
47 self
->state
.is_time_for_rebuild
= false;
49 self
->flags
.auto_eject_hosts
= false;
50 self
->flags
.binary_protocol
= false;
51 self
->flags
.buffer_requests
= false;
52 self
->flags
.hash_with_namespace
= false;
53 self
->flags
.no_block
= false;
54 self
->flags
.reply
= true;
55 self
->flags
.randomize_replica_read
= false;
56 self
->flags
.support_cas
= false;
57 self
->flags
.tcp_nodelay
= false;
58 self
->flags
.use_sort_hosts
= false;
59 self
->flags
.use_udp
= false;
60 self
->flags
.verify_key
= false;
61 self
->flags
.tcp_keepalive
= false;
62 self
->flags
.is_aes
= false;
63 self
->flags
.is_fetching_version
= false;
65 self
->virtual_bucket
= NULL
;
67 self
->distribution
= MEMCACHED_DISTRIBUTION_MODULA
;
69 if (hashkit_create(&self
->hashkit
) == NULL
)
74 self
->server_info
.version
= 0;
76 self
->ketama
.continuum
= NULL
;
77 self
->ketama
.continuum_count
= 0;
78 self
->ketama
.continuum_points_counter
= 0;
79 self
->ketama
.next_distribution_rebuild
= 0;
80 self
->ketama
.weighted_
= false;
82 self
->number_of_hosts
= 0;
84 self
->last_disconnected_server
= NULL
;
88 self
->server_failure_limit
= MEMCACHED_SERVER_FAILURE_LIMIT
;
89 self
->query_id
= 1; // 0 is considered invalid
91 /* TODO, Document why we picked these defaults */
92 self
->io_msg_watermark
= 500;
93 self
->io_bytes_watermark
= 65 * 1024;
95 self
->tcp_keepidle
= 0;
97 self
->io_key_prefetch
= 0;
98 self
->poll_timeout
= MEMCACHED_DEFAULT_TIMEOUT
;
99 self
->connect_timeout
= MEMCACHED_DEFAULT_CONNECT_TIMEOUT
;
100 self
->retry_timeout
= MEMCACHED_SERVER_FAILURE_RETRY_TIMEOUT
;
101 self
->dead_timeout
= MEMCACHED_SERVER_FAILURE_DEAD_TIMEOUT
;
106 self
->user_data
= NULL
;
107 self
->number_of_replicas
= 0;
109 self
->allocators
= memcached_allocators_return_default();
111 self
->on_clone
= NULL
;
112 self
->on_cleanup
= NULL
;
113 self
->get_key_failure
= NULL
;
114 self
->delete_trigger
= NULL
;
115 self
->callbacks
= NULL
;
116 self
->sasl
.callbacks
= NULL
;
117 self
->sasl
.is_allocated
= false;
119 self
->error_messages
= NULL
;
120 self
->_namespace
= NULL
;
121 self
->configure
.initial_pool_size
= 1;
122 self
->configure
.max_pool_size
= 1;
123 self
->configure
.version
= -1;
124 self
->configure
.filename
= NULL
;
129 static void __memcached_free(memcached_st
*ptr
, bool release_st
)
131 /* If we have anything open, lets close it now */
133 memcached_instance_list_free(memcached_instance_list(ptr
), memcached_instance_list_count(ptr
));
134 memcached_result_free(&ptr
->result
);
136 memcached_virtual_bucket_free(ptr
);
138 memcached_instance_free((org::libmemcached::Instance
*)ptr
->last_disconnected_server
);
142 ptr
->on_cleanup(ptr
);
145 libmemcached_free(ptr
, ptr
->ketama
.continuum
);
147 memcached_array_free(ptr
->_namespace
);
148 ptr
->_namespace
= NULL
;
150 memcached_error_free(*ptr
);
152 if (LIBMEMCACHED_WITH_SASL_SUPPORT
and ptr
->sasl
.callbacks
)
154 memcached_destroy_sasl_auth_data(ptr
);
159 memcached_array_free(ptr
->configure
.filename
);
160 ptr
->configure
.filename
= NULL
;
163 hashkit_free(&ptr
->hashkit
);
165 if (memcached_is_allocated(ptr
) and release_st
)
167 libmemcached_free(ptr
, ptr
);
171 memcached_st
*memcached_create(memcached_st
*ptr
)
175 ptr
->options
.is_allocated
= false;
179 ptr
= (memcached_st
*)libmemcached_xmalloc(NULL
, memcached_st
);
183 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
186 ptr
->options
.is_allocated
= true;
189 if (_memcached_init(ptr
) == false)
195 if (memcached_result_create(ptr
, &ptr
->result
) == NULL
)
201 WATCHPOINT_ASSERT_INITIALIZED(&ptr
->result
);
206 memcached_st
*memcached(const char *string
, size_t length
)
208 if (length
== 0 and string
)
213 if (length
and string
== NULL
)
220 if (bool(getenv("LIBMEMCACHED")))
222 string
= getenv("LIBMEMCACHED");
223 length
= string
? strlen(string
) : 0;
227 memcached_st
*memc
= memcached_create(NULL
);
233 if (length
== 0 or string
== NULL
)
238 memcached_return_t rc
= memcached_parse_configuration(memc
, string
, length
);
239 if (memcached_success(rc
) and memcached_parse_filename(memc
))
241 rc
= memcached_parse_configure_file(*memc
, memcached_parse_filename(memc
), memcached_parse_filename_length(memc
));
244 if (memcached_failed(rc
))
246 memcached_free(memc
);
253 memcached_return_t
memcached_reset(memcached_st
*ptr
)
255 WATCHPOINT_ASSERT(ptr
);
258 return MEMCACHED_INVALID_ARGUMENTS
;
261 bool stored_is_allocated
= memcached_is_allocated(ptr
);
262 uint64_t query_id
= ptr
->query_id
;
263 __memcached_free(ptr
, false);
264 memcached_create(ptr
);
265 memcached_set_allocated(ptr
, stored_is_allocated
);
266 ptr
->query_id
= query_id
;
268 if (ptr
->configure
.filename
)
270 return memcached_parse_configure_file(*ptr
, memcached_param_array(ptr
->configure
.filename
));
273 return MEMCACHED_SUCCESS
;
276 void memcached_servers_reset(memcached_st
*self
)
280 memcached_instance_list_free(memcached_instance_list(self
), self
->number_of_hosts
);
282 memcached_instance_set(self
, NULL
);
283 self
->number_of_hosts
= 0;
284 memcached_instance_free((org::libmemcached::Instance
*)self
->last_disconnected_server
);
285 self
->last_disconnected_server
= NULL
;
289 void memcached_reset_last_disconnected_server(memcached_st
*self
)
293 memcached_instance_free((org::libmemcached::Instance
*)self
->last_disconnected_server
);
294 self
->last_disconnected_server
= NULL
;
298 void memcached_free(memcached_st
*ptr
)
302 __memcached_free(ptr
, true);
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
311 memcached_st
*memcached_clone(memcached_st
*clone
, const memcached_st
*source
)
315 return memcached_create(clone
);
318 if (clone
and memcached_is_allocated(clone
))
323 memcached_st
*new_clone
= memcached_create(clone
);
325 if (new_clone
== NULL
)
330 new_clone
->flags
= source
->flags
;
331 new_clone
->send_size
= source
->send_size
;
332 new_clone
->recv_size
= source
->recv_size
;
333 new_clone
->poll_timeout
= source
->poll_timeout
;
334 new_clone
->connect_timeout
= source
->connect_timeout
;
335 new_clone
->retry_timeout
= source
->retry_timeout
;
336 new_clone
->dead_timeout
= source
->dead_timeout
;
337 new_clone
->distribution
= source
->distribution
;
339 if (hashkit_clone(&new_clone
->hashkit
, &source
->hashkit
) == NULL
)
341 memcached_free(new_clone
);
345 new_clone
->user_data
= source
->user_data
;
347 new_clone
->snd_timeout
= source
->snd_timeout
;
348 new_clone
->rcv_timeout
= source
->rcv_timeout
;
350 new_clone
->on_clone
= source
->on_clone
;
351 new_clone
->on_cleanup
= source
->on_cleanup
;
353 new_clone
->allocators
= source
->allocators
;
355 new_clone
->get_key_failure
= source
->get_key_failure
;
356 new_clone
->delete_trigger
= source
->delete_trigger
;
357 new_clone
->server_failure_limit
= source
->server_failure_limit
;
358 new_clone
->io_msg_watermark
= source
->io_msg_watermark
;
359 new_clone
->io_bytes_watermark
= source
->io_bytes_watermark
;
360 new_clone
->io_key_prefetch
= source
->io_key_prefetch
;
361 new_clone
->number_of_replicas
= source
->number_of_replicas
;
362 new_clone
->tcp_keepidle
= source
->tcp_keepidle
;
364 if (memcached_server_count(source
))
366 if (memcached_failed(memcached_push(new_clone
, source
)))
373 new_clone
->_namespace
= memcached_array_clone(new_clone
, source
->_namespace
);
374 new_clone
->configure
.filename
= memcached_array_clone(new_clone
, source
->_namespace
);
375 new_clone
->configure
.version
= source
->configure
.version
;
377 if (LIBMEMCACHED_WITH_SASL_SUPPORT
and source
->sasl
.callbacks
)
379 if (memcached_failed(memcached_clone_sasl(new_clone
, source
)))
381 memcached_free(new_clone
);
386 if (memcached_failed(run_distribution(new_clone
)))
388 memcached_free(new_clone
);
393 if (source
->on_clone
)
395 source
->on_clone(new_clone
, source
);
401 void *memcached_get_user_data(const memcached_st
*ptr
)
408 return ptr
->user_data
;
411 void *memcached_set_user_data(memcached_st
*ptr
, void *data
)
418 void *ret
= ptr
->user_data
;
419 ptr
->user_data
= data
;
424 memcached_return_t
memcached_push(memcached_st
*destination
, const memcached_st
*source
)
426 return memcached_instance_push(destination
, (org::libmemcached::Instance
*)source
->servers
, source
->number_of_hosts
);
429 org::libmemcached::Instance
* memcached_instance_fetch(memcached_st
*ptr
, uint32_t server_key
)
436 return &ptr
->servers
[server_key
];
439 memcached_server_instance_st
memcached_server_instance_by_position(const memcached_st
*ptr
, uint32_t server_key
)
446 return &ptr
->servers
[server_key
];
449 org::libmemcached::Instance
* memcached_instance_by_position(const memcached_st
*ptr
, uint32_t server_key
)
456 return &ptr
->servers
[server_key
];
459 uint64_t memcached_query_id(const memcached_st
*self
)
466 return self
->query_id
;
469 org::libmemcached::Instance
* memcached_instance_list(const memcached_st
*self
)
473 return (org::libmemcached::Instance
*)self
->servers
;