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,
40 static inline bool _memcached_init(memcached_st
*self
)
42 self
->state
= global_copy
.state
;
43 self
->flags
= global_copy
.flags
;
45 self
->distribution
= MEMCACHED_DISTRIBUTION_MODULA
;
48 hash_ptr
= hashkit_create(&self
->hashkit
);
52 self
->continuum_points_counter
= 0;
54 self
->number_of_hosts
= 0;
56 self
->last_disconnected_server
= NULL
;
60 self
->server_failure_limit
= 0;
62 /* TODO, Document why we picked these defaults */
63 self
->io_msg_watermark
= 500;
64 self
->io_bytes_watermark
= 65 * 1024;
66 self
->tcp_keepidle
= 0;
68 self
->io_key_prefetch
= 0;
69 self
->cached_errno
= 0;
70 self
->poll_timeout
= MEMCACHED_DEFAULT_TIMEOUT
;
71 self
->connect_timeout
= MEMCACHED_DEFAULT_TIMEOUT
;
72 self
->retry_timeout
= 0;
73 self
->continuum_count
= 0;
78 self
->user_data
= NULL
;
79 self
->next_distribution_rebuild
= 0;
80 self
->prefix_key_length
= 0;
81 self
->number_of_replicas
= 0;
82 hash_ptr
= hashkit_create(&self
->distribution_hashkit
);
85 self
->continuum
= NULL
;
87 self
->allocators
= memcached_allocators_return_default();
90 self
->on_cleanup
= NULL
;
91 self
->get_key_failure
= NULL
;
92 self
->delete_trigger
= NULL
;
93 self
->callbacks
= NULL
;
94 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
95 self
->sasl
.callbacks
= NULL
;
96 self
->sasl
.is_allocated
= false;
102 memcached_st
*memcached_create(memcached_st
*ptr
)
106 ptr
= (memcached_st
*)malloc(sizeof(memcached_st
));
110 return NULL
; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
113 ptr
->options
.is_allocated
= true;
117 ptr
->options
.is_allocated
= false;
121 memcached_set_purging(ptr
, false);
122 memcached_set_processing_input(ptr
, false);
125 if (! _memcached_init(ptr
))
131 if (! memcached_result_create(ptr
, &ptr
->result
))
137 WATCHPOINT_ASSERT_INITIALIZED(&ptr
->result
);
142 void memcached_servers_reset(memcached_st
*ptr
)
144 memcached_server_list_free(memcached_server_list(ptr
));
146 memcached_server_list_set(ptr
, NULL
);
147 ptr
->number_of_hosts
= 0;
148 ptr
->last_disconnected_server
= NULL
;
149 ptr
->server_failure_limit
= 0;
152 void memcached_free(memcached_st
*ptr
)
154 /* If we have anything open, lets close it now */
156 memcached_server_list_free(memcached_server_list(ptr
));
157 memcached_result_free(&ptr
->result
);
160 ptr
->on_cleanup(ptr
);
163 libmemcached_free(ptr
, ptr
->continuum
);
165 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
166 if (ptr
->sasl
.callbacks
!= NULL
)
168 memcached_destroy_sasl_auth_data(ptr
);
172 if (memcached_is_allocated(ptr
))
174 libmemcached_free(ptr
, ptr
);
179 clone is the destination, while source is the structure to clone.
180 If source is NULL the call is the same as if a memcached_create() was
183 memcached_st
*memcached_clone(memcached_st
*clone
, const memcached_st
*source
)
185 memcached_return_t rc
= MEMCACHED_SUCCESS
;
186 memcached_st
*new_clone
;
189 return memcached_create(clone
);
191 if (clone
&& memcached_is_allocated(clone
))
196 new_clone
= memcached_create(clone
);
198 if (new_clone
== NULL
)
201 new_clone
->flags
= source
->flags
;
202 new_clone
->send_size
= source
->send_size
;
203 new_clone
->recv_size
= source
->recv_size
;
204 new_clone
->poll_timeout
= source
->poll_timeout
;
205 new_clone
->connect_timeout
= source
->connect_timeout
;
206 new_clone
->retry_timeout
= source
->retry_timeout
;
207 new_clone
->distribution
= source
->distribution
;
209 hashkit_st
*hash_ptr
;
211 hash_ptr
= hashkit_clone(&new_clone
->hashkit
, &source
->hashkit
);
214 memcached_free(new_clone
);
218 hash_ptr
= hashkit_clone(&new_clone
->distribution_hashkit
, &source
->distribution_hashkit
);
221 memcached_free(new_clone
);
225 new_clone
->user_data
= source
->user_data
;
227 new_clone
->snd_timeout
= source
->snd_timeout
;
228 new_clone
->rcv_timeout
= source
->rcv_timeout
;
230 new_clone
->on_clone
= source
->on_clone
;
231 new_clone
->on_cleanup
= source
->on_cleanup
;
233 new_clone
->allocators
= source
->allocators
;
235 new_clone
->get_key_failure
= source
->get_key_failure
;
236 new_clone
->delete_trigger
= source
->delete_trigger
;
237 new_clone
->server_failure_limit
= source
->server_failure_limit
;
238 new_clone
->io_msg_watermark
= source
->io_msg_watermark
;
239 new_clone
->io_bytes_watermark
= source
->io_bytes_watermark
;
240 new_clone
->io_key_prefetch
= source
->io_key_prefetch
;
241 new_clone
->number_of_replicas
= source
->number_of_replicas
;
242 new_clone
->tcp_keepidle
= source
->tcp_keepidle
;
244 if (memcached_server_count(source
))
245 rc
= memcached_push(new_clone
, source
);
247 if (rc
!= MEMCACHED_SUCCESS
)
249 memcached_free(new_clone
);
255 if (source
->prefix_key_length
)
257 strcpy(new_clone
->prefix_key
, source
->prefix_key
);
258 new_clone
->prefix_key_length
= source
->prefix_key_length
;
261 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
262 if (source
->sasl
.callbacks
)
264 if (memcached_clone_sasl(new_clone
, source
) != MEMCACHED_SUCCESS
)
266 memcached_free(new_clone
);
272 rc
= run_distribution(new_clone
);
274 if (rc
!= MEMCACHED_SUCCESS
)
276 memcached_free(new_clone
);
281 if (source
->on_clone
)
282 source
->on_clone(new_clone
, source
);
287 void *memcached_get_user_data(const memcached_st
*ptr
)
289 return ptr
->user_data
;
292 void *memcached_set_user_data(memcached_st
*ptr
, void *data
)
294 void *ret
= ptr
->user_data
;
295 ptr
->user_data
= data
;
300 memcached_return_t
memcached_push(memcached_st
*destination
, const memcached_st
*source
)
302 return memcached_server_push(destination
, source
->servers
);
305 memcached_server_write_instance_st
memcached_server_instance_fetch(memcached_st
*ptr
, uint32_t server_key
)
307 return &ptr
->servers
[server_key
];
310 memcached_server_instance_st
memcached_server_instance_by_position(const memcached_st
*ptr
, uint32_t server_key
)
312 return &ptr
->servers
[server_key
];