Updating interface.
[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 memcached_result_st *result_ptr;
9
10 if (ptr == NULL)
11 {
12 ptr= (memcached_st *)calloc(1, sizeof(memcached_st));
13
14 if (! ptr)
15 {
16 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
17 }
18
19 ptr->options.is_allocated= true;
20 }
21 else
22 {
23 memset(ptr, 0, sizeof(memcached_st));
24 }
25
26 ptr->options.is_initialized= true;
27
28 memcached_set_memory_allocators(ptr, NULL, NULL, NULL, NULL);
29
30 result_ptr= memcached_result_create(ptr, &ptr->result);
31 WATCHPOINT_ASSERT(result_ptr);
32 ptr->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT;
33 ptr->connect_timeout= MEMCACHED_DEFAULT_TIMEOUT;
34 ptr->retry_timeout= 0;
35 ptr->distribution= MEMCACHED_DISTRIBUTION_MODULA;
36
37 /* TODO, Document why we picked these defaults */
38 ptr->io_msg_watermark= 500;
39 ptr->io_bytes_watermark= 65 * 1024;
40
41 WATCHPOINT_ASSERT_INITIALIZED(&ptr->result);
42 WATCHPOINT_ASSERT_INITIALIZED(&ptr->hashkit);
43
44 return ptr;
45 }
46
47 void memcached_free(memcached_st *ptr)
48 {
49 /* If we have anything open, lets close it now */
50 memcached_quit(ptr);
51 server_list_free(ptr, ptr->hosts);
52 memcached_result_free(&ptr->result);
53
54 if (ptr->on_cleanup)
55 ptr->on_cleanup(ptr);
56
57 if (ptr->continuum)
58 ptr->call_free(ptr, ptr->continuum);
59
60 if (memcached_is_allocated(ptr))
61 {
62 ptr->call_free(ptr, ptr);
63 }
64 else
65 {
66 ptr->options.is_initialized= false;
67 }
68 }
69
70 /*
71 clone is the destination, while source is the structure to clone.
72 If source is NULL the call is the same as if a memcached_create() was
73 called.
74 */
75 memcached_st *memcached_clone(memcached_st *clone, memcached_st *source)
76 {
77 memcached_return_t rc= MEMCACHED_SUCCESS;
78 memcached_st *new_clone;
79
80 if (source == NULL)
81 return memcached_create(clone);
82
83 if (clone && memcached_is_allocated(clone))
84 {
85 return NULL;
86 }
87
88 new_clone= memcached_create(clone);
89
90 if (new_clone == NULL)
91 return NULL;
92
93 new_clone->flags= source->flags;
94 new_clone->send_size= source->send_size;
95 new_clone->recv_size= source->recv_size;
96 new_clone->poll_timeout= source->poll_timeout;
97 new_clone->connect_timeout= source->connect_timeout;
98 new_clone->retry_timeout= source->retry_timeout;
99 new_clone->distribution= source->distribution;
100 new_clone->hash= source->hash;
101 new_clone->distribution_hash= source->distribution_hash;
102 new_clone->user_data= source->user_data;
103
104 new_clone->snd_timeout= source->snd_timeout;
105 new_clone->rcv_timeout= source->rcv_timeout;
106
107 new_clone->on_clone= source->on_clone;
108 new_clone->on_cleanup= source->on_cleanup;
109 new_clone->call_free= source->call_free;
110 new_clone->call_malloc= source->call_malloc;
111 new_clone->call_realloc= source->call_realloc;
112 new_clone->call_calloc= source->call_calloc;
113 new_clone->get_key_failure= source->get_key_failure;
114 new_clone->delete_trigger= source->delete_trigger;
115 new_clone->server_failure_limit= source->server_failure_limit;
116 new_clone->io_msg_watermark= source->io_msg_watermark;
117 new_clone->io_bytes_watermark= source->io_bytes_watermark;
118 new_clone->io_key_prefetch= source->io_key_prefetch;
119 new_clone->number_of_replicas= source->number_of_replicas;
120
121 if (source->hosts)
122 rc= memcached_server_push(new_clone, source->hosts);
123
124 if (rc != MEMCACHED_SUCCESS)
125 {
126 memcached_free(new_clone);
127
128 return NULL;
129 }
130
131
132 if (source->prefix_key[0] != 0)
133 {
134 strcpy(new_clone->prefix_key, source->prefix_key);
135 new_clone->prefix_key_length= source->prefix_key_length;
136 }
137
138 rc= run_distribution(new_clone);
139
140 if (rc != MEMCACHED_SUCCESS)
141 {
142 memcached_free(new_clone);
143
144 return NULL;
145 }
146
147 if (source->on_clone)
148 source->on_clone(source, new_clone);
149
150 return new_clone;
151 }
152
153 void *memcached_get_user_data(memcached_st *ptr)
154 {
155 return ptr->user_data;
156 }
157
158 void *memcached_set_user_data(memcached_st *ptr, void *data)
159 {
160 void *ret= ptr->user_data;
161 ptr->user_data= data;
162
163 return ret;
164 }