Import parser/etc
[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->prefix_key_length= 0;
84 self->number_of_replicas= 0;
85 hash_ptr= hashkit_create(&self->distribution_hashkit);
86 if (! hash_ptr)
87 return false;
88 self->continuum= NULL;
89
90 self->allocators= memcached_allocators_return_default();
91
92 self->on_clone= NULL;
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;
99
100 self->error_messages= 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_error_free(ptr);
122
123 if (ptr->sasl.callbacks)
124 {
125 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
126 memcached_destroy_sasl_auth_data(ptr);
127 #endif
128 }
129
130 if (memcached_is_allocated(ptr) && release_st)
131 {
132 libmemcached_free(ptr, ptr);
133 }
134 }
135
136 memcached_st *memcached_create(memcached_st *ptr)
137 {
138 if (ptr == NULL)
139 {
140 ptr= (memcached_st *)malloc(sizeof(memcached_st));
141
142 if (! ptr)
143 {
144 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
145 }
146
147 ptr->options.is_allocated= true;
148 }
149 else
150 {
151 ptr->options.is_allocated= false;
152 }
153
154 #if 0
155 memcached_set_purging(ptr, false);
156 memcached_set_processing_input(ptr, false);
157 #endif
158
159 if (! _memcached_init(ptr))
160 {
161 memcached_free(ptr);
162 return NULL;
163 }
164
165 if (! memcached_result_create(ptr, &ptr->result))
166 {
167 memcached_free(ptr);
168 return NULL;
169 }
170
171 WATCHPOINT_ASSERT_INITIALIZED(&ptr->result);
172
173 return ptr;
174 }
175
176 void memcached_reset(memcached_st *ptr)
177 {
178 WATCHPOINT_ASSERT(ptr);
179 if (! ptr)
180 return;
181
182 bool stored_is_allocated= memcached_is_allocated(ptr);
183 _free(ptr, false);
184 memcached_create(ptr);
185 memcached_set_allocated(ptr, stored_is_allocated);
186 }
187
188 void memcached_servers_reset(memcached_st *ptr)
189 {
190 memcached_server_list_free(memcached_server_list(ptr));
191
192 memcached_server_list_set(ptr, NULL);
193 ptr->number_of_hosts= 0;
194 if (ptr->last_disconnected_server)
195 {
196 memcached_server_free(ptr->last_disconnected_server);
197 }
198 ptr->last_disconnected_server= NULL;
199 ptr->server_failure_limit= 0;
200 }
201
202 void memcached_reset_last_disconnected_server(memcached_st *ptr)
203 {
204 if (ptr->last_disconnected_server)
205 {
206 memcached_server_free(ptr->last_disconnected_server);
207 ptr->last_disconnected_server= NULL;
208 }
209 }
210
211 void memcached_free(memcached_st *ptr)
212 {
213 _free(ptr, true);
214 }
215
216 /*
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
219 called.
220 */
221 memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source)
222 {
223 memcached_return_t rc= MEMCACHED_SUCCESS;
224 memcached_st *new_clone;
225
226 if (source == NULL)
227 return memcached_create(clone);
228
229 if (clone && memcached_is_allocated(clone))
230 {
231 return NULL;
232 }
233
234 new_clone= memcached_create(clone);
235
236 if (new_clone == NULL)
237 return NULL;
238
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;
246
247 hashkit_st *hash_ptr;
248
249 hash_ptr= hashkit_clone(&new_clone->hashkit, &source->hashkit);
250 if (! hash_ptr)
251 {
252 memcached_free(new_clone);
253 return NULL;
254 }
255
256 hash_ptr= hashkit_clone(&new_clone->distribution_hashkit, &source->distribution_hashkit);
257 if (! hash_ptr)
258 {
259 memcached_free(new_clone);
260 return NULL;
261 }
262
263 new_clone->user_data= source->user_data;
264
265 new_clone->snd_timeout= source->snd_timeout;
266 new_clone->rcv_timeout= source->rcv_timeout;
267
268 new_clone->on_clone= source->on_clone;
269 new_clone->on_cleanup= source->on_cleanup;
270
271 new_clone->allocators= source->allocators;
272
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;
281
282 if (memcached_server_count(source))
283 rc= memcached_push(new_clone, source);
284
285 if (rc != MEMCACHED_SUCCESS)
286 {
287 memcached_free(new_clone);
288
289 return NULL;
290 }
291
292
293 if (source->prefix_key_length)
294 {
295 strcpy(new_clone->prefix_key, source->prefix_key);
296 new_clone->prefix_key_length= source->prefix_key_length;
297 }
298
299 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
300 if (source->sasl.callbacks)
301 {
302 if (memcached_clone_sasl(new_clone, source) != MEMCACHED_SUCCESS)
303 {
304 memcached_free(new_clone);
305 return NULL;
306 }
307 }
308 #endif
309
310 rc= run_distribution(new_clone);
311
312 if (rc != MEMCACHED_SUCCESS)
313 {
314 memcached_free(new_clone);
315
316 return NULL;
317 }
318
319 if (source->on_clone)
320 source->on_clone(new_clone, source);
321
322 return new_clone;
323 }
324
325 void *memcached_get_user_data(const memcached_st *ptr)
326 {
327 return ptr->user_data;
328 }
329
330 void *memcached_set_user_data(memcached_st *ptr, void *data)
331 {
332 void *ret= ptr->user_data;
333 ptr->user_data= data;
334
335 return ret;
336 }
337
338 memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source)
339 {
340 return memcached_server_push(destination, source->servers);
341 }
342
343 memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key)
344 {
345 return &ptr->servers[server_key];
346 }
347
348 memcached_server_instance_st memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key)
349 {
350 return &ptr->servers[server_key];
351 }