number_of_hosts <-- encapsulated.
[awesomized/libmemcached] / libmemcached / memcached.c
1 /*
2 Memcached library
3 */
4 #include "common.h"
5
6 memcached_st *memcached_create(memcached_st *ptr)
7 {
8 if (ptr == NULL)
9 {
10 ptr= (memcached_st *)calloc(1, sizeof(memcached_st));
11
12 if (! ptr)
13 {
14 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
15 }
16
17 ptr->options.is_allocated= true;
18 }
19 else
20 {
21 memset(ptr, 0, sizeof(memcached_st));
22 }
23
24 ptr->options.is_initialized= true;
25
26 memcached_set_memory_allocators(ptr, NULL, NULL, NULL, NULL);
27
28 if (! memcached_result_create(ptr, &ptr->result))
29 {
30 memcached_free(ptr);
31 return NULL;
32 }
33 ptr->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT;
34 ptr->connect_timeout= MEMCACHED_DEFAULT_TIMEOUT;
35 ptr->retry_timeout= 0;
36 ptr->distribution= MEMCACHED_DISTRIBUTION_MODULA;
37
38 /* TODO, Document why we picked these defaults */
39 ptr->io_msg_watermark= 500;
40 ptr->io_bytes_watermark= 65 * 1024;
41
42 WATCHPOINT_ASSERT_INITIALIZED(&ptr->result);
43
44 return ptr;
45 }
46
47 void server_list_free(memcached_st *ptr, memcached_server_st *servers)
48 {
49 uint32_t x;
50
51 if (servers == NULL)
52 return;
53
54 for (x= 0; x < memcached_servers_count(servers); x++)
55 if (servers[x].address_info)
56 {
57 freeaddrinfo(servers[x].address_info);
58 servers[x].address_info= NULL;
59 }
60
61 if (ptr)
62 {
63 ptr->call_free(ptr, servers);
64 }
65 else
66 free(servers);
67 }
68
69 void memcached_servers_reset(memcached_st *ptr)
70 {
71 server_list_free(ptr, ptr->hosts);
72
73 ptr->hosts= NULL;
74 ptr->number_of_hosts= 0;
75 ptr->cursor_server= 0;
76 ptr->last_disconnected_server= NULL;
77 ptr->server_failure_limit= 0;
78 }
79
80 void memcached_free(memcached_st *ptr)
81 {
82 /* If we have anything open, lets close it now */
83 memcached_quit(ptr);
84 server_list_free(ptr, ptr->hosts);
85 memcached_result_free(&ptr->result);
86
87 if (ptr->on_cleanup)
88 ptr->on_cleanup(ptr);
89
90 if (ptr->continuum)
91 ptr->call_free(ptr, ptr->continuum);
92
93 if (memcached_is_allocated(ptr))
94 {
95 ptr->call_free(ptr, ptr);
96 }
97 else
98 {
99 ptr->options.is_initialized= false;
100 }
101 }
102
103 /*
104 clone is the destination, while source is the structure to clone.
105 If source is NULL the call is the same as if a memcached_create() was
106 called.
107 */
108 memcached_st *memcached_clone(memcached_st *clone, memcached_st *source)
109 {
110 memcached_return_t rc= MEMCACHED_SUCCESS;
111 memcached_st *new_clone;
112
113 if (source == NULL)
114 return memcached_create(clone);
115
116 if (clone && memcached_is_allocated(clone))
117 {
118 return NULL;
119 }
120
121 new_clone= memcached_create(clone);
122
123 if (new_clone == NULL)
124 return NULL;
125
126 new_clone->flags= source->flags;
127 new_clone->send_size= source->send_size;
128 new_clone->recv_size= source->recv_size;
129 new_clone->poll_timeout= source->poll_timeout;
130 new_clone->connect_timeout= source->connect_timeout;
131 new_clone->retry_timeout= source->retry_timeout;
132 new_clone->distribution= source->distribution;
133 new_clone->hash= source->hash;
134 new_clone->distribution_hash= source->distribution_hash;
135 new_clone->user_data= source->user_data;
136
137 new_clone->snd_timeout= source->snd_timeout;
138 new_clone->rcv_timeout= source->rcv_timeout;
139
140 new_clone->on_clone= source->on_clone;
141 new_clone->on_cleanup= source->on_cleanup;
142 new_clone->call_free= source->call_free;
143 new_clone->call_malloc= source->call_malloc;
144 new_clone->call_realloc= source->call_realloc;
145 new_clone->call_calloc= source->call_calloc;
146 new_clone->get_key_failure= source->get_key_failure;
147 new_clone->delete_trigger= source->delete_trigger;
148 new_clone->server_failure_limit= source->server_failure_limit;
149 new_clone->io_msg_watermark= source->io_msg_watermark;
150 new_clone->io_bytes_watermark= source->io_bytes_watermark;
151 new_clone->io_key_prefetch= source->io_key_prefetch;
152 new_clone->number_of_replicas= source->number_of_replicas;
153
154 if (source->hosts)
155 rc= memcached_server_push(new_clone, source->hosts);
156
157 if (rc != MEMCACHED_SUCCESS)
158 {
159 memcached_free(new_clone);
160
161 return NULL;
162 }
163
164
165 if (source->prefix_key[0] != 0)
166 {
167 strcpy(new_clone->prefix_key, source->prefix_key);
168 new_clone->prefix_key_length= source->prefix_key_length;
169 }
170
171 rc= run_distribution(new_clone);
172
173 if (rc != MEMCACHED_SUCCESS)
174 {
175 memcached_free(new_clone);
176
177 return NULL;
178 }
179
180 if (source->on_clone)
181 source->on_clone(source, new_clone);
182
183 return new_clone;
184 }
185
186 void *memcached_get_user_data(memcached_st *ptr)
187 {
188 return ptr->user_data;
189 }
190
191 void *memcached_set_user_data(memcached_st *ptr, void *data)
192 {
193 void *ret= ptr->user_data;
194 ptr->user_data= data;
195
196 return ret;
197 }