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,
37 .tcp_keepalive
= false,
38 .load_from_file
= false
43 static inline bool _memcached_init(memcached_st
*self
)
45 self
->state
= global_copy
.state
;
46 self
->flags
= global_copy
.flags
;
48 self
->distribution
= MEMCACHED_DISTRIBUTION_MODULA
;
51 hash_ptr
= hashkit_create(&self
->hashkit
);
55 self
->continuum_points_counter
= 0;
57 self
->number_of_hosts
= 0;
59 self
->last_disconnected_server
= NULL
;
63 self
->server_failure_limit
= 0;
65 /* TODO, Document why we picked these defaults */
66 self
->io_msg_watermark
= 500;
67 self
->io_bytes_watermark
= 65 * 1024;
69 self
->tcp_keepidle
= 0;
71 self
->io_key_prefetch
= 0;
72 self
->cached_errno
= 0;
73 self
->poll_timeout
= MEMCACHED_DEFAULT_TIMEOUT
;
74 self
->connect_timeout
= MEMCACHED_DEFAULT_CONNECT_TIMEOUT
;
75 self
->retry_timeout
= 0;
76 self
->continuum_count
= 0;
81 self
->user_data
= NULL
;
82 self
->next_distribution_rebuild
= 0;
83 self
->prefix_key_length
= 0;
84 self
->number_of_replicas
= 0;
85 hash_ptr
= hashkit_create(&self
->distribution_hashkit
);
88 self
->continuum
= NULL
;
90 self
->allocators
= memcached_allocators_return_default();
93 self
->on_cleanup
= NULL
;
94 self
->get_key_failure
= NULL
;
95 self
->delete_trigger
= NULL
;
96 self
->callbacks
= NULL
;
97 self
->sasl
.callbacks
= NULL
;
98 self
->sasl
.is_allocated
= false;
100 self
->error_messages
= NULL
;
105 static void _free(memcached_st
*ptr
, bool release_st
)
107 /* If we have anything open, lets close it now */
109 memcached_server_list_free(memcached_server_list(ptr
));
110 memcached_result_free(&ptr
->result
);
112 if (ptr
->last_disconnected_server
)
113 memcached_server_free(ptr
->last_disconnected_server
);
116 ptr
->on_cleanup(ptr
);
119 libmemcached_free(ptr
, ptr
->continuum
);
121 memcached_error_free(ptr
);
123 if (ptr
->sasl
.callbacks
)
125 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
126 memcached_destroy_sasl_auth_data(ptr
);
130 if (memcached_is_allocated(ptr
) && release_st
)
132 libmemcached_free(ptr
, ptr
);
136 memcached_st
*memcached_create(memcached_st
*ptr
)
140 ptr
= (memcached_st
*)malloc(sizeof(memcached_st
));
144 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
147 ptr
->options
.is_allocated
= true;
151 ptr
->options
.is_allocated
= false;
155 memcached_set_purging(ptr
, false);
156 memcached_set_processing_input(ptr
, false);
159 if (! _memcached_init(ptr
))
165 if (! memcached_result_create(ptr
, &ptr
->result
))
171 WATCHPOINT_ASSERT_INITIALIZED(&ptr
->result
);
176 void memcached_reset(memcached_st
*ptr
)
178 WATCHPOINT_ASSERT(ptr
);
182 bool stored_is_allocated
= memcached_is_allocated(ptr
);
184 memcached_create(ptr
);
185 memcached_set_allocated(ptr
, stored_is_allocated
);
188 void memcached_servers_reset(memcached_st
*ptr
)
190 memcached_server_list_free(memcached_server_list(ptr
));
192 memcached_server_list_set(ptr
, NULL
);
193 ptr
->number_of_hosts
= 0;
194 if (ptr
->last_disconnected_server
)
196 memcached_server_free(ptr
->last_disconnected_server
);
198 ptr
->last_disconnected_server
= NULL
;
199 ptr
->server_failure_limit
= 0;
202 void memcached_reset_last_disconnected_server(memcached_st
*ptr
)
204 if (ptr
->last_disconnected_server
)
206 memcached_server_free(ptr
->last_disconnected_server
);
207 ptr
->last_disconnected_server
= NULL
;
211 void memcached_free(memcached_st
*ptr
)
217 clone is the destination, while source is the structure to clone.
218 If source is NULL the call is the same as if a memcached_create() was
221 memcached_st
*memcached_clone(memcached_st
*clone
, const memcached_st
*source
)
223 memcached_return_t rc
= MEMCACHED_SUCCESS
;
224 memcached_st
*new_clone
;
227 return memcached_create(clone
);
229 if (clone
&& memcached_is_allocated(clone
))
234 new_clone
= memcached_create(clone
);
236 if (new_clone
== NULL
)
239 new_clone
->flags
= source
->flags
;
240 new_clone
->send_size
= source
->send_size
;
241 new_clone
->recv_size
= source
->recv_size
;
242 new_clone
->poll_timeout
= source
->poll_timeout
;
243 new_clone
->connect_timeout
= source
->connect_timeout
;
244 new_clone
->retry_timeout
= source
->retry_timeout
;
245 new_clone
->distribution
= source
->distribution
;
247 hashkit_st
*hash_ptr
;
249 hash_ptr
= hashkit_clone(&new_clone
->hashkit
, &source
->hashkit
);
252 memcached_free(new_clone
);
256 hash_ptr
= hashkit_clone(&new_clone
->distribution_hashkit
, &source
->distribution_hashkit
);
259 memcached_free(new_clone
);
263 new_clone
->user_data
= source
->user_data
;
265 new_clone
->snd_timeout
= source
->snd_timeout
;
266 new_clone
->rcv_timeout
= source
->rcv_timeout
;
268 new_clone
->on_clone
= source
->on_clone
;
269 new_clone
->on_cleanup
= source
->on_cleanup
;
271 new_clone
->allocators
= source
->allocators
;
273 new_clone
->get_key_failure
= source
->get_key_failure
;
274 new_clone
->delete_trigger
= source
->delete_trigger
;
275 new_clone
->server_failure_limit
= source
->server_failure_limit
;
276 new_clone
->io_msg_watermark
= source
->io_msg_watermark
;
277 new_clone
->io_bytes_watermark
= source
->io_bytes_watermark
;
278 new_clone
->io_key_prefetch
= source
->io_key_prefetch
;
279 new_clone
->number_of_replicas
= source
->number_of_replicas
;
280 new_clone
->tcp_keepidle
= source
->tcp_keepidle
;
282 if (memcached_server_count(source
))
283 rc
= memcached_push(new_clone
, source
);
285 if (rc
!= MEMCACHED_SUCCESS
)
287 memcached_free(new_clone
);
293 if (source
->prefix_key_length
)
295 strcpy(new_clone
->prefix_key
, source
->prefix_key
);
296 new_clone
->prefix_key_length
= source
->prefix_key_length
;
299 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
300 if (source
->sasl
.callbacks
)
302 if (memcached_clone_sasl(new_clone
, source
) != MEMCACHED_SUCCESS
)
304 memcached_free(new_clone
);
310 rc
= run_distribution(new_clone
);
312 if (rc
!= MEMCACHED_SUCCESS
)
314 memcached_free(new_clone
);
319 if (source
->on_clone
)
320 source
->on_clone(new_clone
, source
);
325 void *memcached_get_user_data(const memcached_st
*ptr
)
327 return ptr
->user_data
;
330 void *memcached_set_user_data(memcached_st
*ptr
, void *data
)
332 void *ret
= ptr
->user_data
;
333 ptr
->user_data
= data
;
338 memcached_return_t
memcached_push(memcached_st
*destination
, const memcached_st
*source
)
340 return memcached_server_push(destination
, source
->servers
);
343 memcached_server_write_instance_st
memcached_server_instance_fetch(memcached_st
*ptr
, uint32_t server_key
)
345 return &ptr
->servers
[server_key
];
348 memcached_server_instance_st
memcached_server_instance_by_position(const memcached_st
*ptr
, uint32_t server_key
)
350 return &ptr
->servers
[server_key
];