3d255fc0cab5c8cca2396de95a732ecbf93983b5
[m6w6/libmemcached] / libmemcached / memcached.c
1 /* LibMemcached
2 * Copyright (C) 2006-2010 Brian Aker
3 * All rights reserved.
4 *
5 * Use and distribution licensed under the BSD license. See
6 * the COPYING file in the parent directory for full text.
7 *
8 * Summary:
9 *
10 */
11
12 #include "common.h"
13
14 static const memcached_st global_copy= {
15 .state= {
16 .is_purging= false,
17 .is_processing_input= false,
18 .is_time_for_rebuild= false,
19 },
20 .flags= {
21 .auto_eject_hosts= false,
22 .binary_protocol= false,
23 .buffer_requests= false,
24 .cork= false,
25 .hash_with_prefix_key= false,
26 .ketama_weighted= false,
27 .no_block= false,
28 .no_reply= false,
29 .randomize_replica_read= false,
30 .reuse_memory= false,
31 .support_cas= false,
32 .tcp_nodelay= false,
33 .use_cache_lookups= false,
34 .use_sort_hosts= false,
35 .use_udp= false,
36 .verify_key= false,
37 .tcp_keepalive= false,
38 .load_from_file= false
39
40 }
41 };
42
43 static inline bool _memcached_init(memcached_st *self)
44 {
45 self->state= global_copy.state;
46 self->flags= global_copy.flags;
47
48 self->distribution= MEMCACHED_DISTRIBUTION_MODULA;
49
50 hashkit_st *hash_ptr;
51 hash_ptr= hashkit_create(&self->hashkit);
52 if (! hash_ptr)
53 return false;
54
55 self->continuum_points_counter= 0;
56
57 self->number_of_hosts= 0;
58 self->servers= NULL;
59 self->last_disconnected_server= NULL;
60
61 self->snd_timeout= 0;
62 self->rcv_timeout= 0;
63 self->server_failure_limit= 0;
64
65 /* TODO, Document why we picked these defaults */
66 self->io_msg_watermark= 500;
67 self->io_bytes_watermark= 65 * 1024;
68
69 self->tcp_keepidle= 0;
70
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;
77
78 self->send_size= -1;
79 self->recv_size= -1;
80
81 self->user_data= NULL;
82 self->next_distribution_rebuild= 0;
83 self->number_of_replicas= 0;
84 hash_ptr= hashkit_create(&self->distribution_hashkit);
85 if (! hash_ptr)
86 return false;
87 self->continuum= NULL;
88
89 self->allocators= memcached_allocators_return_default();
90
91 self->on_clone= NULL;
92 self->on_cleanup= NULL;
93 self->get_key_failure= NULL;
94 self->delete_trigger= NULL;
95 self->callbacks= NULL;
96 self->sasl.callbacks= NULL;
97 self->sasl.is_allocated= false;
98
99 self->error_messages= NULL;
100 self->prefix_key= NULL;
101
102 return true;
103 }
104
105 static void _free(memcached_st *ptr, bool release_st)
106 {
107 /* If we have anything open, lets close it now */
108 memcached_quit(ptr);
109 memcached_server_list_free(memcached_server_list(ptr));
110 memcached_result_free(&ptr->result);
111
112 if (ptr->last_disconnected_server)
113 memcached_server_free(ptr->last_disconnected_server);
114
115 if (ptr->on_cleanup)
116 ptr->on_cleanup(ptr);
117
118 if (ptr->continuum)
119 libmemcached_free(ptr, ptr->continuum);
120
121 memcached_array_free(ptr->prefix_key);
122 ptr->prefix_key= NULL;
123
124 memcached_error_free(ptr);
125
126 if (ptr->sasl.callbacks)
127 {
128 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
129 memcached_destroy_sasl_auth_data(ptr);
130 #endif
131 }
132
133 if (memcached_is_allocated(ptr) && release_st)
134 {
135 libmemcached_free(ptr, ptr);
136 }
137 }
138
139 memcached_st *memcached_create(memcached_st *ptr)
140 {
141 if (ptr == NULL)
142 {
143 ptr= (memcached_st *)malloc(sizeof(memcached_st));
144
145 if (! ptr)
146 {
147 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
148 }
149
150 ptr->options.is_allocated= true;
151 }
152 else
153 {
154 ptr->options.is_allocated= false;
155 }
156
157 #if 0
158 memcached_set_purging(ptr, false);
159 memcached_set_processing_input(ptr, false);
160 #endif
161
162 if (! _memcached_init(ptr))
163 {
164 memcached_free(ptr);
165 return NULL;
166 }
167
168 if (! memcached_result_create(ptr, &ptr->result))
169 {
170 memcached_free(ptr);
171 return NULL;
172 }
173
174 WATCHPOINT_ASSERT_INITIALIZED(&ptr->result);
175
176 return ptr;
177 }
178
179 void memcached_reset(memcached_st *ptr)
180 {
181 WATCHPOINT_ASSERT(ptr);
182 if (! ptr)
183 return;
184
185 bool stored_is_allocated= memcached_is_allocated(ptr);
186 _free(ptr, false);
187 memcached_create(ptr);
188 memcached_set_allocated(ptr, stored_is_allocated);
189 }
190
191 void memcached_servers_reset(memcached_st *ptr)
192 {
193 memcached_server_list_free(memcached_server_list(ptr));
194
195 memcached_server_list_set(ptr, NULL);
196 ptr->number_of_hosts= 0;
197 if (ptr->last_disconnected_server)
198 {
199 memcached_server_free(ptr->last_disconnected_server);
200 }
201 ptr->last_disconnected_server= NULL;
202 ptr->server_failure_limit= 0;
203 }
204
205 void memcached_reset_last_disconnected_server(memcached_st *ptr)
206 {
207 if (ptr->last_disconnected_server)
208 {
209 memcached_server_free(ptr->last_disconnected_server);
210 ptr->last_disconnected_server= NULL;
211 }
212 }
213
214 void memcached_free(memcached_st *ptr)
215 {
216 _free(ptr, true);
217 }
218
219 /*
220 clone is the destination, while source is the structure to clone.
221 If source is NULL the call is the same as if a memcached_create() was
222 called.
223 */
224 memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source)
225 {
226 memcached_return_t rc= MEMCACHED_SUCCESS;
227 memcached_st *new_clone;
228
229 if (source == NULL)
230 return memcached_create(clone);
231
232 if (clone && memcached_is_allocated(clone))
233 {
234 return NULL;
235 }
236
237 new_clone= memcached_create(clone);
238
239 if (new_clone == NULL)
240 return NULL;
241
242 new_clone->flags= source->flags;
243 new_clone->send_size= source->send_size;
244 new_clone->recv_size= source->recv_size;
245 new_clone->poll_timeout= source->poll_timeout;
246 new_clone->connect_timeout= source->connect_timeout;
247 new_clone->retry_timeout= source->retry_timeout;
248 new_clone->distribution= source->distribution;
249
250 hashkit_st *hash_ptr;
251
252 hash_ptr= hashkit_clone(&new_clone->hashkit, &source->hashkit);
253 if (! hash_ptr)
254 {
255 memcached_free(new_clone);
256 return NULL;
257 }
258
259 hash_ptr= hashkit_clone(&new_clone->distribution_hashkit, &source->distribution_hashkit);
260 if (! hash_ptr)
261 {
262 memcached_free(new_clone);
263 return NULL;
264 }
265
266 new_clone->user_data= source->user_data;
267
268 new_clone->snd_timeout= source->snd_timeout;
269 new_clone->rcv_timeout= source->rcv_timeout;
270
271 new_clone->on_clone= source->on_clone;
272 new_clone->on_cleanup= source->on_cleanup;
273
274 new_clone->allocators= source->allocators;
275
276 new_clone->get_key_failure= source->get_key_failure;
277 new_clone->delete_trigger= source->delete_trigger;
278 new_clone->server_failure_limit= source->server_failure_limit;
279 new_clone->io_msg_watermark= source->io_msg_watermark;
280 new_clone->io_bytes_watermark= source->io_bytes_watermark;
281 new_clone->io_key_prefetch= source->io_key_prefetch;
282 new_clone->number_of_replicas= source->number_of_replicas;
283 new_clone->tcp_keepidle= source->tcp_keepidle;
284
285 if (memcached_server_count(source))
286 rc= memcached_push(new_clone, source);
287
288 if (rc != MEMCACHED_SUCCESS)
289 {
290 memcached_free(new_clone);
291
292 return NULL;
293 }
294
295
296 new_clone->prefix_key= memcached_array_clone(new_clone, source->prefix_key);
297
298 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
299 if (source->sasl.callbacks)
300 {
301 if (memcached_clone_sasl(new_clone, source) != MEMCACHED_SUCCESS)
302 {
303 memcached_free(new_clone);
304 return NULL;
305 }
306 }
307 #endif
308
309 rc= run_distribution(new_clone);
310
311 if (rc != MEMCACHED_SUCCESS)
312 {
313 memcached_free(new_clone);
314
315 return NULL;
316 }
317
318 if (source->on_clone)
319 source->on_clone(new_clone, source);
320
321 return new_clone;
322 }
323
324 void *memcached_get_user_data(const memcached_st *ptr)
325 {
326 return ptr->user_data;
327 }
328
329 void *memcached_set_user_data(memcached_st *ptr, void *data)
330 {
331 void *ret= ptr->user_data;
332 ptr->user_data= data;
333
334 return ret;
335 }
336
337 memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source)
338 {
339 return memcached_server_push(destination, source->servers);
340 }
341
342 memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key)
343 {
344 return &ptr->servers[server_key];
345 }
346
347 memcached_server_instance_st memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key)
348 {
349 return &ptr->servers[server_key];
350 }