dfb187976158c75b0ac7a0788111006c68292f2a
[awesomized/libmemcached] / libmemcached / memcached_server.c
1 /*
2 This is a partial implementation for fetching/creating memcached_server_st objects.
3 */
4 #include "common.h"
5
6 memcached_server_st *memcached_server_create(memcached_st *memc, memcached_server_st *ptr)
7 {
8 if (ptr == NULL)
9 {
10 ptr= (memcached_server_st *)malloc(sizeof(memcached_server_st));
11
12 if (!ptr)
13 return NULL; /* MEMCACHED_MEMORY_ALLOCATION_FAILURE */
14
15 memset(ptr, 0, sizeof(memcached_server_st));
16 ptr->is_allocated= MEMCACHED_ALLOCATED;
17 }
18 else
19 {
20 ptr->is_allocated= MEMCACHED_USED;
21 memset(ptr, 0, sizeof(memcached_server_st));
22 }
23
24 ptr->root= memc;
25
26 return ptr;
27 }
28
29 void memcached_server_free(memcached_server_st *ptr)
30 {
31 WATCHPOINT_NUMBER(ptr->port);
32 WATCHPOINT_ASSERT(ptr->is_allocated != MEMCACHED_NOT_ALLOCATED);
33
34 memcached_quit_server(ptr, 0);
35
36 if (ptr->address_info)
37 {
38 freeaddrinfo(ptr->address_info);
39 ptr->address_info= NULL;
40 }
41
42 if (ptr->is_allocated == MEMCACHED_ALLOCATED)
43 {
44 if (ptr->root && ptr->root->call_free)
45 ptr->root->call_free(ptr->root, ptr);
46 else
47 free(ptr);
48 }
49 }
50
51 /*
52 If we do not have a valid object to clone from, we toss an error.
53 */
54 memcached_server_st *memcached_server_clone(memcached_server_st *clone, memcached_server_st *ptr)
55 {
56 memcached_server_st *new_clone;
57
58 /* We just do a normal create if ptr is missing */
59 if (ptr == NULL)
60 return NULL;
61
62 if (clone && clone->is_allocated == MEMCACHED_USED)
63 {
64 WATCHPOINT_ASSERT(0);
65 return NULL;
66 }
67
68 new_clone= memcached_server_create(ptr->root, clone);
69
70 if (new_clone == NULL)
71 return NULL;
72
73 new_clone->root= ptr->root;
74
75 host_reset(new_clone->root, new_clone,
76 ptr->hostname, ptr->port, ptr->weight,
77 ptr->type);
78
79 return new_clone;
80 }
81
82 memcached_return memcached_server_cursor(memcached_st *ptr,
83 memcached_server_function *callback,
84 void *context,
85 unsigned int number_of_callbacks)
86 {
87 unsigned int y;
88
89 for (y= 0; y < ptr->number_of_hosts; y++)
90 {
91 unsigned int x;
92
93 for (x= 0; x < number_of_callbacks; x++)
94 {
95 unsigned int iferror;
96
97 iferror= (*callback[x])(ptr, &ptr->hosts[y], context);
98
99 if (iferror)
100 continue;
101 }
102 }
103
104 return MEMCACHED_SUCCESS;
105 }
106
107 memcached_server_st *memcached_server_by_key(memcached_st *ptr, const char *key, size_t key_length, memcached_return *error)
108 {
109 uint32_t server_key;
110
111 unlikely (key_length == 0)
112 {
113 *error= MEMCACHED_NO_KEY_PROVIDED;
114 return NULL;
115 }
116
117 unlikely (ptr->number_of_hosts == 0)
118 {
119 *error= MEMCACHED_NO_SERVERS;
120 return NULL;
121 }
122
123 if ((ptr->flags & MEM_VERIFY_KEY) && (memcachd_key_test((char **)&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
124 {
125 *error= MEMCACHED_BAD_KEY_PROVIDED;
126 return NULL;
127 }
128
129 server_key= memcached_generate_hash(ptr, key, key_length);
130
131 return memcached_server_clone(NULL, &ptr->hosts[server_key]);
132
133 }