Remove cork from being optional, just use it as we need it.
[awesomized/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 .hash_with_prefix_key= false,
25 .ketama_weighted= false,
26 .no_block= false,
27 .no_reply= false,
28 .randomize_replica_read= false,
29 .reuse_memory= false,
30 .support_cas= false,
31 .tcp_nodelay= false,
32 .use_cache_lookups= false,
33 .use_sort_hosts= false,
34 .use_udp= false,
35 .verify_key= false,
36 .tcp_keepalive= false,
37 .ping_service= false
38 }
39 };
40
41 static inline bool _memcached_init(memcached_st *self)
42 {
43 self->state= global_copy.state;
44 self->flags= global_copy.flags;
45
46 self->distribution= MEMCACHED_DISTRIBUTION_MODULA;
47
48 hashkit_st *hash_ptr;
49 hash_ptr= hashkit_create(&self->hashkit);
50 if (! hash_ptr)
51 return false;
52
53 self->ketama.continuum_points_counter= 0;
54
55 self->number_of_hosts= 0;
56 self->servers= NULL;
57 self->last_disconnected_server= NULL;
58
59 self->snd_timeout= 0;
60 self->rcv_timeout= 0;
61 self->server_failure_limit= 0;
62
63 /* TODO, Document why we picked these defaults */
64 self->io_msg_watermark= 500;
65 self->io_bytes_watermark= 65 * 1024;
66
67 self->tcp_keepidle= 0;
68
69 self->io_key_prefetch= 0;
70 self->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT;
71 self->connect_timeout= MEMCACHED_DEFAULT_CONNECT_TIMEOUT;
72 self->retry_timeout= 0;
73 self->ketama.continuum_count= 0;
74
75 self->send_size= -1;
76 self->recv_size= -1;
77
78 self->user_data= NULL;
79 self->ketama.next_distribution_rebuild= 0;
80 self->number_of_replicas= 0;
81 hash_ptr= hashkit_create(&self->distribution_hashkit);
82 if (! hash_ptr)
83 return false;
84 self->ketama.continuum= NULL;
85
86 self->allocators= memcached_allocators_return_default();
87
88 self->on_clone= NULL;
89 self->on_cleanup= NULL;
90 self->get_key_failure= NULL;
91 self->delete_trigger= NULL;
92 self->callbacks= NULL;
93 self->sasl.callbacks= NULL;
94 self->sasl.is_allocated= false;
95
96 self->error_messages= NULL;
97 self->prefix_key= NULL;
98 self->configure.filename= NULL;
99
100 return true;
101 }
102
103 static void _free(memcached_st *ptr, bool release_st)
104 {
105 /* If we have anything open, lets close it now */
106 memcached_quit(ptr);
107 memcached_server_list_free(memcached_server_list(ptr));
108 memcached_result_free(&ptr->result);
109
110 if (ptr->last_disconnected_server)
111 memcached_server_free(ptr->last_disconnected_server);
112
113 if (ptr->on_cleanup)
114 ptr->on_cleanup(ptr);
115
116 if (ptr->ketama.continuum)
117 libmemcached_free(ptr, ptr->ketama.continuum);
118
119 memcached_array_free(ptr->prefix_key);
120 ptr->prefix_key= NULL;
121
122 memcached_error_free(ptr);
123
124 if (ptr->sasl.callbacks)
125 {
126 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
127 memcached_destroy_sasl_auth_data(ptr);
128 #endif
129 }
130
131 if (release_st)
132 {
133 memcached_array_free(ptr->configure.filename);
134 ptr->configure.filename= NULL;
135 }
136
137 if (memcached_is_allocated(ptr) && release_st)
138 {
139 libmemcached_free(ptr, ptr);
140 }
141 }
142
143 memcached_st *memcached_create(memcached_st *ptr)
144 {
145 if (ptr == NULL)
146 {
147 ptr= (memcached_st *)malloc(sizeof(memcached_st));
148
149 if (! ptr)
150 {
151 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
152 }
153
154 ptr->options.is_allocated= true;
155 }
156 else
157 {
158 ptr->options.is_allocated= false;
159 }
160
161 #if 0
162 memcached_set_purging(ptr, false);
163 memcached_set_processing_input(ptr, false);
164 #endif
165
166 if (! _memcached_init(ptr))
167 {
168 memcached_free(ptr);
169 return NULL;
170 }
171
172 if (! memcached_result_create(ptr, &ptr->result))
173 {
174 memcached_free(ptr);
175 return NULL;
176 }
177
178 WATCHPOINT_ASSERT_INITIALIZED(&ptr->result);
179
180 return ptr;
181 }
182
183 memcached_st *memcached_create_with_options(const char *string, size_t length)
184 {
185 memcached_st *self= memcached_create(NULL);
186
187 if (! self)
188 return NULL;
189
190 memcached_return_t rc;
191 if ((rc= memcached_parse_configuration(self, string, length)) != MEMCACHED_SUCCESS)
192 {
193 return self;
194 }
195
196 if (memcached_parse_filename(self))
197 {
198 rc= memcached_parse_configure_file(self, memcached_parse_filename(self), memcached_parse_filename_length(self));
199 }
200
201 return self;
202 }
203
204 memcached_return_t memcached_reset(memcached_st *ptr)
205 {
206 WATCHPOINT_ASSERT(ptr);
207 if (! ptr)
208 return MEMCACHED_INVALID_ARGUMENTS;
209
210 bool stored_is_allocated= memcached_is_allocated(ptr);
211 _free(ptr, false);
212 memcached_create(ptr);
213 memcached_set_allocated(ptr, stored_is_allocated);
214
215 if (ptr->configure.filename)
216 {
217 return memcached_parse_configure_file(ptr, memcached_param_array(ptr->configure.filename));
218 }
219
220 return MEMCACHED_SUCCESS;
221 }
222
223 void memcached_servers_reset(memcached_st *ptr)
224 {
225 memcached_server_list_free(memcached_server_list(ptr));
226
227 memcached_server_list_set(ptr, NULL);
228 ptr->number_of_hosts= 0;
229 if (ptr->last_disconnected_server)
230 {
231 memcached_server_free(ptr->last_disconnected_server);
232 }
233 ptr->last_disconnected_server= NULL;
234 ptr->server_failure_limit= 0;
235 }
236
237 void memcached_reset_last_disconnected_server(memcached_st *ptr)
238 {
239 if (ptr->last_disconnected_server)
240 {
241 memcached_server_free(ptr->last_disconnected_server);
242 ptr->last_disconnected_server= NULL;
243 }
244 }
245
246 void memcached_free(memcached_st *ptr)
247 {
248 _free(ptr, true);
249 }
250
251 /*
252 clone is the destination, while source is the structure to clone.
253 If source is NULL the call is the same as if a memcached_create() was
254 called.
255 */
256 memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source)
257 {
258 memcached_return_t rc= MEMCACHED_SUCCESS;
259 memcached_st *new_clone;
260
261 if (source == NULL)
262 return memcached_create(clone);
263
264 if (clone && memcached_is_allocated(clone))
265 {
266 return NULL;
267 }
268
269 new_clone= memcached_create(clone);
270
271 if (new_clone == NULL)
272 return NULL;
273
274 new_clone->flags= source->flags;
275 new_clone->send_size= source->send_size;
276 new_clone->recv_size= source->recv_size;
277 new_clone->poll_timeout= source->poll_timeout;
278 new_clone->connect_timeout= source->connect_timeout;
279 new_clone->retry_timeout= source->retry_timeout;
280 new_clone->distribution= source->distribution;
281
282 hashkit_st *hash_ptr;
283
284 hash_ptr= hashkit_clone(&new_clone->hashkit, &source->hashkit);
285 if (! hash_ptr)
286 {
287 memcached_free(new_clone);
288 return NULL;
289 }
290
291 hash_ptr= hashkit_clone(&new_clone->distribution_hashkit, &source->distribution_hashkit);
292 if (! hash_ptr)
293 {
294 memcached_free(new_clone);
295 return NULL;
296 }
297
298 new_clone->user_data= source->user_data;
299
300 new_clone->snd_timeout= source->snd_timeout;
301 new_clone->rcv_timeout= source->rcv_timeout;
302
303 new_clone->on_clone= source->on_clone;
304 new_clone->on_cleanup= source->on_cleanup;
305
306 new_clone->allocators= source->allocators;
307
308 new_clone->get_key_failure= source->get_key_failure;
309 new_clone->delete_trigger= source->delete_trigger;
310 new_clone->server_failure_limit= source->server_failure_limit;
311 new_clone->io_msg_watermark= source->io_msg_watermark;
312 new_clone->io_bytes_watermark= source->io_bytes_watermark;
313 new_clone->io_key_prefetch= source->io_key_prefetch;
314 new_clone->number_of_replicas= source->number_of_replicas;
315 new_clone->tcp_keepidle= source->tcp_keepidle;
316
317 if (memcached_server_count(source))
318 rc= memcached_push(new_clone, source);
319
320 if (rc != MEMCACHED_SUCCESS)
321 {
322 memcached_free(new_clone);
323
324 return NULL;
325 }
326
327
328 new_clone->prefix_key= memcached_array_clone(new_clone, source->prefix_key);
329
330 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
331 if (source->sasl.callbacks)
332 {
333 if (memcached_clone_sasl(new_clone, source) != MEMCACHED_SUCCESS)
334 {
335 memcached_free(new_clone);
336 return NULL;
337 }
338 }
339 #endif
340
341 rc= run_distribution(new_clone);
342
343 if (rc != MEMCACHED_SUCCESS)
344 {
345 memcached_free(new_clone);
346
347 return NULL;
348 }
349
350 if (source->on_clone)
351 source->on_clone(new_clone, source);
352
353 return new_clone;
354 }
355
356 void *memcached_get_user_data(const memcached_st *ptr)
357 {
358 return ptr->user_data;
359 }
360
361 void *memcached_set_user_data(memcached_st *ptr, void *data)
362 {
363 void *ret= ptr->user_data;
364 ptr->user_data= data;
365
366 return ret;
367 }
368
369 memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source)
370 {
371 return memcached_server_push(destination, source->servers);
372 }
373
374 memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key)
375 {
376 return &ptr->servers[server_key];
377 }
378
379 memcached_server_instance_st memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key)
380 {
381 return &ptr->servers[server_key];
382 }