Merge in all of the new parser work.
[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 .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 return true;
101 }
102
103 memcached_st *memcached_create(memcached_st *ptr)
104 {
105 if (ptr == NULL)
106 {
107 ptr= (memcached_st *)malloc(sizeof(memcached_st));
108
109 if (! ptr)
110 {
111 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
112 }
113
114 ptr->options.is_allocated= true;
115 }
116 else
117 {
118 ptr->options.is_allocated= false;
119 }
120
121 #if 0
122 memcached_set_purging(ptr, false);
123 memcached_set_processing_input(ptr, false);
124 #endif
125
126 if (! _memcached_init(ptr))
127 {
128 memcached_free(ptr);
129 return NULL;
130 }
131
132 if (! memcached_result_create(ptr, &ptr->result))
133 {
134 memcached_free(ptr);
135 return NULL;
136 }
137
138 WATCHPOINT_ASSERT_INITIALIZED(&ptr->result);
139
140 return ptr;
141 }
142
143 void memcached_servers_reset(memcached_st *ptr)
144 {
145 memcached_server_list_free(memcached_server_list(ptr));
146
147 memcached_server_list_set(ptr, NULL);
148 ptr->number_of_hosts= 0;
149 if (ptr->last_disconnected_server)
150 {
151 memcached_server_free(ptr->last_disconnected_server);
152 }
153 ptr->last_disconnected_server= NULL;
154 ptr->server_failure_limit= 0;
155 }
156
157 void memcached_reset_last_disconnected_server(memcached_st *ptr)
158 {
159 if (ptr->last_disconnected_server)
160 {
161 memcached_server_free(ptr->last_disconnected_server);
162 ptr->last_disconnected_server= NULL;
163 }
164 }
165
166 void memcached_free(memcached_st *ptr)
167 {
168 /* If we have anything open, lets close it now */
169 memcached_quit(ptr);
170 memcached_server_list_free(memcached_server_list(ptr));
171 memcached_result_free(&ptr->result);
172
173 if (ptr->last_disconnected_server)
174 memcached_server_free(ptr->last_disconnected_server);
175
176 if (ptr->on_cleanup)
177 ptr->on_cleanup(ptr);
178
179 if (ptr->continuum)
180 libmemcached_free(ptr, ptr->continuum);
181
182 if (ptr->sasl.callbacks)
183 {
184 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
185 memcached_destroy_sasl_auth_data(ptr);
186 #endif
187 }
188
189 if (memcached_is_allocated(ptr))
190 {
191 libmemcached_free(ptr, ptr);
192 }
193 }
194
195 /*
196 clone is the destination, while source is the structure to clone.
197 If source is NULL the call is the same as if a memcached_create() was
198 called.
199 */
200 memcached_st *memcached_clone(memcached_st *clone, const memcached_st *source)
201 {
202 memcached_return_t rc= MEMCACHED_SUCCESS;
203 memcached_st *new_clone;
204
205 if (source == NULL)
206 return memcached_create(clone);
207
208 if (clone && memcached_is_allocated(clone))
209 {
210 return NULL;
211 }
212
213 new_clone= memcached_create(clone);
214
215 if (new_clone == NULL)
216 return NULL;
217
218 new_clone->flags= source->flags;
219 new_clone->send_size= source->send_size;
220 new_clone->recv_size= source->recv_size;
221 new_clone->poll_timeout= source->poll_timeout;
222 new_clone->connect_timeout= source->connect_timeout;
223 new_clone->retry_timeout= source->retry_timeout;
224 new_clone->distribution= source->distribution;
225
226 hashkit_st *hash_ptr;
227
228 hash_ptr= hashkit_clone(&new_clone->hashkit, &source->hashkit);
229 if (! hash_ptr)
230 {
231 memcached_free(new_clone);
232 return NULL;
233 }
234
235 hash_ptr= hashkit_clone(&new_clone->distribution_hashkit, &source->distribution_hashkit);
236 if (! hash_ptr)
237 {
238 memcached_free(new_clone);
239 return NULL;
240 }
241
242 new_clone->user_data= source->user_data;
243
244 new_clone->snd_timeout= source->snd_timeout;
245 new_clone->rcv_timeout= source->rcv_timeout;
246
247 new_clone->on_clone= source->on_clone;
248 new_clone->on_cleanup= source->on_cleanup;
249
250 new_clone->allocators= source->allocators;
251
252 new_clone->get_key_failure= source->get_key_failure;
253 new_clone->delete_trigger= source->delete_trigger;
254 new_clone->server_failure_limit= source->server_failure_limit;
255 new_clone->io_msg_watermark= source->io_msg_watermark;
256 new_clone->io_bytes_watermark= source->io_bytes_watermark;
257 new_clone->io_key_prefetch= source->io_key_prefetch;
258 new_clone->number_of_replicas= source->number_of_replicas;
259 new_clone->tcp_keepidle= source->tcp_keepidle;
260
261 if (memcached_server_count(source))
262 rc= memcached_push(new_clone, source);
263
264 if (rc != MEMCACHED_SUCCESS)
265 {
266 memcached_free(new_clone);
267
268 return NULL;
269 }
270
271
272 if (source->prefix_key_length)
273 {
274 strcpy(new_clone->prefix_key, source->prefix_key);
275 new_clone->prefix_key_length= source->prefix_key_length;
276 }
277
278 #ifdef LIBMEMCACHED_WITH_SASL_SUPPORT
279 if (source->sasl.callbacks)
280 {
281 if (memcached_clone_sasl(new_clone, source) != MEMCACHED_SUCCESS)
282 {
283 memcached_free(new_clone);
284 return NULL;
285 }
286 }
287 #endif
288
289 rc= run_distribution(new_clone);
290
291 if (rc != MEMCACHED_SUCCESS)
292 {
293 memcached_free(new_clone);
294
295 return NULL;
296 }
297
298 if (source->on_clone)
299 source->on_clone(new_clone, source);
300
301 return new_clone;
302 }
303
304 void *memcached_get_user_data(const memcached_st *ptr)
305 {
306 return ptr->user_data;
307 }
308
309 void *memcached_set_user_data(memcached_st *ptr, void *data)
310 {
311 void *ret= ptr->user_data;
312 ptr->user_data= data;
313
314 return ret;
315 }
316
317 memcached_return_t memcached_push(memcached_st *destination, const memcached_st *source)
318 {
319 return memcached_server_push(destination, source->servers);
320 }
321
322 memcached_server_write_instance_st memcached_server_instance_fetch(memcached_st *ptr, uint32_t server_key)
323 {
324 return &ptr->servers[server_key];
325 }
326
327 memcached_server_instance_st memcached_server_instance_by_position(const memcached_st *ptr, uint32_t server_key)
328 {
329 return &ptr->servers[server_key];
330 }