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,
20 .auto_eject_hosts
= false,
21 .binary_protocol
= false,
22 .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,
39 static inline bool _memcached_init(memcached_st
*self
)
41 self
->state
= global_copy
.state
;
42 self
->flags
= global_copy
.flags
;
44 self
->distribution
= MEMCACHED_DISTRIBUTION_MODULA
;
47 hash_ptr
= hashkit_create(&self
->hashkit
);
51 self
->continuum_points_counter
= 0;
53 self
->number_of_hosts
= 0;
55 self
->last_disconnected_server
= NULL
;
59 self
->server_failure_limit
= 0;
61 /* TODO, Document why we picked these defaults */
62 self
->io_msg_watermark
= 500;
63 self
->io_bytes_watermark
= 65 * 1024;
65 self
->io_key_prefetch
= 0;
66 self
->cached_errno
= 0;
67 self
->poll_timeout
= MEMCACHED_DEFAULT_TIMEOUT
;
68 self
->connect_timeout
= MEMCACHED_DEFAULT_TIMEOUT
;
69 self
->retry_timeout
= 0;
70 self
->continuum_count
= 0;
75 self
->user_data
= NULL
;
76 self
->next_distribution_rebuild
= 0;
77 self
->prefix_key_length
= 0;
78 self
->number_of_replicas
= 0;
79 hash_ptr
= hashkit_create(&self
->distribution_hashkit
);
82 self
->continuum
= NULL
;
84 self
->allocators
= memcached_allocators_return_default();
87 self
->on_cleanup
= NULL
;
88 self
->get_key_failure
= NULL
;
89 self
->delete_trigger
= NULL
;
90 self
->callbacks
= NULL
;
91 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
92 self
->sasl
.callbacks
= NULL
;
93 self
->sasl
.is_allocated
= false;
99 memcached_st
*memcached_create(memcached_st
*ptr
)
103 ptr
= (memcached_st
*)malloc(sizeof(memcached_st
));
107 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
110 ptr
->options
.is_allocated
= true;
114 ptr
->options
.is_allocated
= false;
118 memcached_set_purging(ptr
, false);
119 memcached_set_processing_input(ptr
, false);
122 if (! _memcached_init(ptr
))
128 if (! memcached_result_create(ptr
, &ptr
->result
))
134 WATCHPOINT_ASSERT_INITIALIZED(&ptr
->result
);
139 void memcached_servers_reset(memcached_st
*ptr
)
141 memcached_server_list_free(memcached_server_list(ptr
));
143 memcached_server_list_set(ptr
, NULL
);
144 ptr
->number_of_hosts
= 0;
145 ptr
->last_disconnected_server
= NULL
;
146 ptr
->server_failure_limit
= 0;
149 void memcached_free(memcached_st
*ptr
)
151 /* If we have anything open, lets close it now */
153 memcached_server_list_free(memcached_server_list(ptr
));
154 memcached_result_free(&ptr
->result
);
157 ptr
->on_cleanup(ptr
);
160 libmemcached_free(ptr
, ptr
->continuum
);
162 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
163 if (ptr
->sasl
.callbacks
!= NULL
)
165 memcached_destroy_sasl_auth_data(ptr
);
169 if (memcached_is_allocated(ptr
))
171 libmemcached_free(ptr
, ptr
);
176 clone is the destination, while source is the structure to clone.
177 If source is NULL the call is the same as if a memcached_create() was
180 memcached_st
*memcached_clone(memcached_st
*clone
, const memcached_st
*source
)
182 memcached_return_t rc
= MEMCACHED_SUCCESS
;
183 memcached_st
*new_clone
;
186 return memcached_create(clone
);
188 if (clone
&& memcached_is_allocated(clone
))
193 new_clone
= memcached_create(clone
);
195 if (new_clone
== NULL
)
198 new_clone
->flags
= source
->flags
;
199 new_clone
->send_size
= source
->send_size
;
200 new_clone
->recv_size
= source
->recv_size
;
201 new_clone
->poll_timeout
= source
->poll_timeout
;
202 new_clone
->connect_timeout
= source
->connect_timeout
;
203 new_clone
->retry_timeout
= source
->retry_timeout
;
204 new_clone
->distribution
= source
->distribution
;
206 hashkit_st
*hash_ptr
;
208 hash_ptr
= hashkit_clone(&new_clone
->hashkit
, &source
->hashkit
);
211 memcached_free(new_clone
);
215 hash_ptr
= hashkit_clone(&new_clone
->distribution_hashkit
, &source
->distribution_hashkit
);
218 memcached_free(new_clone
);
222 new_clone
->user_data
= source
->user_data
;
224 new_clone
->snd_timeout
= source
->snd_timeout
;
225 new_clone
->rcv_timeout
= source
->rcv_timeout
;
227 new_clone
->on_clone
= source
->on_clone
;
228 new_clone
->on_cleanup
= source
->on_cleanup
;
230 new_clone
->allocators
= source
->allocators
;
232 new_clone
->get_key_failure
= source
->get_key_failure
;
233 new_clone
->delete_trigger
= source
->delete_trigger
;
234 new_clone
->server_failure_limit
= source
->server_failure_limit
;
235 new_clone
->io_msg_watermark
= source
->io_msg_watermark
;
236 new_clone
->io_bytes_watermark
= source
->io_bytes_watermark
;
237 new_clone
->io_key_prefetch
= source
->io_key_prefetch
;
238 new_clone
->number_of_replicas
= source
->number_of_replicas
;
240 if (memcached_server_count(source
))
241 rc
= memcached_push(new_clone
, source
);
243 if (rc
!= MEMCACHED_SUCCESS
)
245 memcached_free(new_clone
);
251 if (source
->prefix_key_length
)
253 strcpy(new_clone
->prefix_key
, source
->prefix_key
);
254 new_clone
->prefix_key_length
= source
->prefix_key_length
;
257 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
258 if (source
->sasl
.callbacks
)
260 if (memcached_clone_sasl(new_clone
, source
) != MEMCACHED_SUCCESS
)
262 memcached_free(new_clone
);
268 rc
= run_distribution(new_clone
);
270 if (rc
!= MEMCACHED_SUCCESS
)
272 memcached_free(new_clone
);
277 if (source
->on_clone
)
278 source
->on_clone(new_clone
, source
);
283 void *memcached_get_user_data(const memcached_st
*ptr
)
285 return ptr
->user_data
;
288 void *memcached_set_user_data(memcached_st
*ptr
, void *data
)
290 void *ret
= ptr
->user_data
;
291 ptr
->user_data
= data
;
296 memcached_return_t
memcached_push(memcached_st
*destination
, const memcached_st
*source
)
298 return memcached_server_push(destination
, source
->servers
);
301 inline memcached_server_write_instance_st
memcached_server_instance_fetch(memcached_st
*ptr
, uint32_t server_key
)
303 return &ptr
->servers
[server_key
];
306 inline memcached_server_instance_st
memcached_server_instance_by_position(const memcached_st
*ptr
, uint32_t server_key
)
308 return &ptr
->servers
[server_key
];