2 * Copyright (C) 2006-2010 Brian Aker
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
14 static const memcached_st global_copy
= {
17 .is_processing_input
= false,
18 .is_time_for_rebuild
= false,
21 .auto_eject_hosts
= false,
22 .binary_protocol
= false,
23 .buffer_requests
= false,
24 .hash_with_prefix_key
= false,
25 .ketama_weighted
= false,
28 .randomize_replica_read
= false,
32 .use_cache_lookups
= false,
33 .use_sort_hosts
= false,
36 .tcp_keepalive
= false,
41 static inline bool _memcached_init(memcached_st
*self
)
43 self
->state
= global_copy
.state
;
44 self
->flags
= global_copy
.flags
;
46 self
->distribution
= MEMCACHED_DISTRIBUTION_MODULA
;
49 hash_ptr
= hashkit_create(&self
->hashkit
);
53 self
->ketama
.continuum_points_counter
= 0;
55 self
->number_of_hosts
= 0;
57 self
->last_disconnected_server
= NULL
;
61 self
->server_failure_limit
= 0;
63 /* TODO, Document why we picked these defaults */
64 self
->io_msg_watermark
= 500;
65 self
->io_bytes_watermark
= 65 * 1024;
67 self
->tcp_keepidle
= 0;
69 self
->io_key_prefetch
= 0;
70 self
->poll_timeout
= MEMCACHED_DEFAULT_TIMEOUT
;
71 self
->connect_timeout
= MEMCACHED_DEFAULT_CONNECT_TIMEOUT
;
72 self
->retry_timeout
= 0;
73 self
->ketama
.continuum_count
= 0;
78 self
->user_data
= NULL
;
79 self
->ketama
.next_distribution_rebuild
= 0;
80 self
->number_of_replicas
= 0;
81 hash_ptr
= hashkit_create(&self
->distribution_hashkit
);
84 self
->ketama
.continuum
= NULL
;
86 self
->allocators
= memcached_allocators_return_default();
89 self
->on_cleanup
= NULL
;
90 self
->get_key_failure
= NULL
;
91 self
->delete_trigger
= NULL
;
92 self
->callbacks
= NULL
;
93 self
->sasl
.callbacks
= NULL
;
94 self
->sasl
.is_allocated
= false;
96 self
->error_messages
= NULL
;
97 self
->prefix_key
= NULL
;
98 self
->configure
.filename
= NULL
;
103 static void _free(memcached_st
*ptr
, bool release_st
)
105 /* If we have anything open, lets close it now */
107 memcached_server_list_free(memcached_server_list(ptr
));
108 memcached_result_free(&ptr
->result
);
110 if (ptr
->last_disconnected_server
)
111 memcached_server_free(ptr
->last_disconnected_server
);
114 ptr
->on_cleanup(ptr
);
116 if (ptr
->ketama
.continuum
)
117 libmemcached_free(ptr
, ptr
->ketama
.continuum
);
119 memcached_array_free(ptr
->prefix_key
);
120 ptr
->prefix_key
= NULL
;
122 memcached_error_free(ptr
);
124 if (ptr
->sasl
.callbacks
)
126 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
127 memcached_destroy_sasl_auth_data(ptr
);
133 memcached_array_free(ptr
->configure
.filename
);
134 ptr
->configure
.filename
= NULL
;
137 if (memcached_is_allocated(ptr
) && release_st
)
139 libmemcached_free(ptr
, ptr
);
143 memcached_st
*memcached_create(memcached_st
*ptr
)
147 ptr
= (memcached_st
*)malloc(sizeof(memcached_st
));
151 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
154 ptr
->options
.is_allocated
= true;
158 ptr
->options
.is_allocated
= false;
162 memcached_set_purging(ptr
, false);
163 memcached_set_processing_input(ptr
, false);
166 if (! _memcached_init(ptr
))
172 if (! memcached_result_create(ptr
, &ptr
->result
))
178 WATCHPOINT_ASSERT_INITIALIZED(&ptr
->result
);
183 memcached_st
*memcached_create_with_options(const char *string
, size_t length
)
185 memcached_st
*self
= memcached_create(NULL
);
190 memcached_return_t rc
;
191 if ((rc
= memcached_parse_configuration(self
, string
, length
)) != MEMCACHED_SUCCESS
)
196 if (memcached_parse_filename(self
))
198 rc
= memcached_parse_configure_file(self
, memcached_parse_filename(self
), memcached_parse_filename_length(self
));
204 memcached_return_t
memcached_reset(memcached_st
*ptr
)
206 WATCHPOINT_ASSERT(ptr
);
208 return MEMCACHED_INVALID_ARGUMENTS
;
210 bool stored_is_allocated
= memcached_is_allocated(ptr
);
212 memcached_create(ptr
);
213 memcached_set_allocated(ptr
, stored_is_allocated
);
215 if (ptr
->configure
.filename
)
217 return memcached_parse_configure_file(ptr
, memcached_param_array(ptr
->configure
.filename
));
220 return MEMCACHED_SUCCESS
;
223 void memcached_servers_reset(memcached_st
*ptr
)
225 memcached_server_list_free(memcached_server_list(ptr
));
227 memcached_server_list_set(ptr
, NULL
);
228 ptr
->number_of_hosts
= 0;
229 if (ptr
->last_disconnected_server
)
231 memcached_server_free(ptr
->last_disconnected_server
);
233 ptr
->last_disconnected_server
= NULL
;
234 ptr
->server_failure_limit
= 0;
237 void memcached_reset_last_disconnected_server(memcached_st
*ptr
)
239 if (ptr
->last_disconnected_server
)
241 memcached_server_free(ptr
->last_disconnected_server
);
242 ptr
->last_disconnected_server
= NULL
;
246 void memcached_free(memcached_st
*ptr
)
252 clone is the destination, while source is the structure to clone.
253 If source is NULL the call is the same as if a memcached_create() was
256 memcached_st
*memcached_clone(memcached_st
*clone
, const memcached_st
*source
)
258 memcached_return_t rc
= MEMCACHED_SUCCESS
;
259 memcached_st
*new_clone
;
262 return memcached_create(clone
);
264 if (clone
&& memcached_is_allocated(clone
))
269 new_clone
= memcached_create(clone
);
271 if (new_clone
== NULL
)
274 new_clone
->flags
= source
->flags
;
275 new_clone
->send_size
= source
->send_size
;
276 new_clone
->recv_size
= source
->recv_size
;
277 new_clone
->poll_timeout
= source
->poll_timeout
;
278 new_clone
->connect_timeout
= source
->connect_timeout
;
279 new_clone
->retry_timeout
= source
->retry_timeout
;
280 new_clone
->distribution
= source
->distribution
;
282 hashkit_st
*hash_ptr
;
284 hash_ptr
= hashkit_clone(&new_clone
->hashkit
, &source
->hashkit
);
287 memcached_free(new_clone
);
291 hash_ptr
= hashkit_clone(&new_clone
->distribution_hashkit
, &source
->distribution_hashkit
);
294 memcached_free(new_clone
);
298 new_clone
->user_data
= source
->user_data
;
300 new_clone
->snd_timeout
= source
->snd_timeout
;
301 new_clone
->rcv_timeout
= source
->rcv_timeout
;
303 new_clone
->on_clone
= source
->on_clone
;
304 new_clone
->on_cleanup
= source
->on_cleanup
;
306 new_clone
->allocators
= source
->allocators
;
308 new_clone
->get_key_failure
= source
->get_key_failure
;
309 new_clone
->delete_trigger
= source
->delete_trigger
;
310 new_clone
->server_failure_limit
= source
->server_failure_limit
;
311 new_clone
->io_msg_watermark
= source
->io_msg_watermark
;
312 new_clone
->io_bytes_watermark
= source
->io_bytes_watermark
;
313 new_clone
->io_key_prefetch
= source
->io_key_prefetch
;
314 new_clone
->number_of_replicas
= source
->number_of_replicas
;
315 new_clone
->tcp_keepidle
= source
->tcp_keepidle
;
317 if (memcached_server_count(source
))
318 rc
= memcached_push(new_clone
, source
);
320 if (rc
!= MEMCACHED_SUCCESS
)
322 memcached_free(new_clone
);
328 new_clone
->prefix_key
= memcached_array_clone(new_clone
, source
->prefix_key
);
330 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
331 if (source
->sasl
.callbacks
)
333 if (memcached_clone_sasl(new_clone
, source
) != MEMCACHED_SUCCESS
)
335 memcached_free(new_clone
);
341 rc
= run_distribution(new_clone
);
343 if (rc
!= MEMCACHED_SUCCESS
)
345 memcached_free(new_clone
);
350 if (source
->on_clone
)
351 source
->on_clone(new_clone
, source
);
356 void *memcached_get_user_data(const memcached_st
*ptr
)
358 return ptr
->user_data
;
361 void *memcached_set_user_data(memcached_st
*ptr
, void *data
)
363 void *ret
= ptr
->user_data
;
364 ptr
->user_data
= data
;
369 memcached_return_t
memcached_push(memcached_st
*destination
, const memcached_st
*source
)
371 return memcached_server_push(destination
, source
->servers
);
374 memcached_server_write_instance_st
memcached_server_instance_fetch(memcached_st
*ptr
, uint32_t server_key
)
376 return &ptr
->servers
[server_key
];
379 memcached_server_instance_st
memcached_server_instance_by_position(const memcached_st
*ptr
, uint32_t server_key
)
381 return &ptr
->servers
[server_key
];