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,
25 .hash_with_prefix_key
= false,
26 .ketama_weighted
= false,
29 .randomize_replica_read
= false,
33 .use_cache_lookups
= false,
34 .use_sort_hosts
= 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
->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
->cached_errno
= 0;
71 self
->poll_timeout
= MEMCACHED_DEFAULT_TIMEOUT
;
72 self
->connect_timeout
= MEMCACHED_DEFAULT_CONNECT_TIMEOUT
;
73 self
->retry_timeout
= 0;
74 self
->continuum_count
= 0;
79 self
->user_data
= NULL
;
80 self
->next_distribution_rebuild
= 0;
81 self
->prefix_key_length
= 0;
82 self
->number_of_replicas
= 0;
83 hash_ptr
= hashkit_create(&self
->distribution_hashkit
);
86 self
->continuum
= NULL
;
88 self
->allocators
= memcached_allocators_return_default();
91 self
->on_cleanup
= NULL
;
92 self
->get_key_failure
= NULL
;
93 self
->delete_trigger
= NULL
;
94 self
->callbacks
= NULL
;
100 memcached_st
*memcached_create(memcached_st
*ptr
)
104 ptr
= (memcached_st
*)malloc(sizeof(memcached_st
));
108 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
111 ptr
->options
.is_allocated
= true;
115 ptr
->options
.is_allocated
= false;
119 memcached_set_purging(ptr
, false);
120 memcached_set_processing_input(ptr
, false);
123 if (! _memcached_init(ptr
))
129 if (! memcached_result_create(ptr
, &ptr
->result
))
135 WATCHPOINT_ASSERT_INITIALIZED(&ptr
->result
);
140 void memcached_servers_reset(memcached_st
*ptr
)
142 memcached_server_list_free(memcached_server_list(ptr
));
144 memcached_server_list_set(ptr
, NULL
);
145 ptr
->number_of_hosts
= 0;
146 if (ptr
->last_disconnected_server
)
148 memcached_server_free(ptr
->last_disconnected_server
);
150 ptr
->last_disconnected_server
= NULL
;
151 ptr
->server_failure_limit
= 0;
154 void memcached_reset_last_disconnected_server(memcached_st
*ptr
)
156 if (ptr
->last_disconnected_server
)
158 memcached_server_free(ptr
->last_disconnected_server
);
159 ptr
->last_disconnected_server
= NULL
;
163 void memcached_free(memcached_st
*ptr
)
165 /* If we have anything open, lets close it now */
167 memcached_server_list_free(memcached_server_list(ptr
));
168 memcached_result_free(&ptr
->result
);
170 if (ptr
->last_disconnected_server
)
171 memcached_server_free(ptr
->last_disconnected_server
);
174 ptr
->on_cleanup(ptr
);
177 libmemcached_free(ptr
, ptr
->continuum
);
181 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
182 memcached_destroy_sasl_auth_data(ptr
);
186 if (memcached_is_allocated(ptr
))
188 libmemcached_free(ptr
, ptr
);
193 clone is the destination, while source is the structure to clone.
194 If source is NULL the call is the same as if a memcached_create() was
197 memcached_st
*memcached_clone(memcached_st
*clone
, const memcached_st
*source
)
199 memcached_return_t rc
= MEMCACHED_SUCCESS
;
200 memcached_st
*new_clone
;
203 return memcached_create(clone
);
205 if (clone
&& memcached_is_allocated(clone
))
210 new_clone
= memcached_create(clone
);
212 if (new_clone
== NULL
)
215 new_clone
->flags
= source
->flags
;
216 new_clone
->send_size
= source
->send_size
;
217 new_clone
->recv_size
= source
->recv_size
;
218 new_clone
->poll_timeout
= source
->poll_timeout
;
219 new_clone
->connect_timeout
= source
->connect_timeout
;
220 new_clone
->retry_timeout
= source
->retry_timeout
;
221 new_clone
->distribution
= source
->distribution
;
223 hashkit_st
*hash_ptr
;
225 hash_ptr
= hashkit_clone(&new_clone
->hashkit
, &source
->hashkit
);
228 memcached_free(new_clone
);
232 hash_ptr
= hashkit_clone(&new_clone
->distribution_hashkit
, &source
->distribution_hashkit
);
235 memcached_free(new_clone
);
239 new_clone
->user_data
= source
->user_data
;
241 new_clone
->snd_timeout
= source
->snd_timeout
;
242 new_clone
->rcv_timeout
= source
->rcv_timeout
;
244 new_clone
->on_clone
= source
->on_clone
;
245 new_clone
->on_cleanup
= source
->on_cleanup
;
247 new_clone
->allocators
= source
->allocators
;
249 new_clone
->get_key_failure
= source
->get_key_failure
;
250 new_clone
->delete_trigger
= source
->delete_trigger
;
251 new_clone
->server_failure_limit
= source
->server_failure_limit
;
252 new_clone
->io_msg_watermark
= source
->io_msg_watermark
;
253 new_clone
->io_bytes_watermark
= source
->io_bytes_watermark
;
254 new_clone
->io_key_prefetch
= source
->io_key_prefetch
;
255 new_clone
->number_of_replicas
= source
->number_of_replicas
;
256 new_clone
->tcp_keepidle
= source
->tcp_keepidle
;
258 if (memcached_server_count(source
))
259 rc
= memcached_push(new_clone
, source
);
261 if (rc
!= MEMCACHED_SUCCESS
)
263 memcached_free(new_clone
);
269 if (source
->prefix_key_length
)
271 strcpy(new_clone
->prefix_key
, source
->prefix_key
);
272 new_clone
->prefix_key_length
= source
->prefix_key_length
;
275 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
276 if (source
->sasl
&& source
->sasl
->callbacks
)
278 if (memcached_clone_sasl(new_clone
, source
) != MEMCACHED_SUCCESS
)
280 memcached_free(new_clone
);
286 rc
= run_distribution(new_clone
);
288 if (rc
!= MEMCACHED_SUCCESS
)
290 memcached_free(new_clone
);
295 if (source
->on_clone
)
296 source
->on_clone(new_clone
, source
);
301 void *memcached_get_user_data(const memcached_st
*ptr
)
303 return ptr
->user_data
;
306 void *memcached_set_user_data(memcached_st
*ptr
, void *data
)
308 void *ret
= ptr
->user_data
;
309 ptr
->user_data
= data
;
314 memcached_return_t
memcached_push(memcached_st
*destination
, const memcached_st
*source
)
316 return memcached_server_push(destination
, source
->servers
);
319 memcached_server_write_instance_st
memcached_server_instance_fetch(memcached_st
*ptr
, uint32_t server_key
)
321 return &ptr
->servers
[server_key
];
324 memcached_server_instance_st
memcached_server_instance_by_position(const memcached_st
*ptr
, uint32_t server_key
)
326 return &ptr
->servers
[server_key
];