ffb2c5f6203b1b6b63b30c0f86307bc2e770f2d5
[awesomized/libmemcached] / lib / memcached_get.c
1 #include "common.h"
2 #include "memcached_io.h"
3
4 /*
5 What happens if no servers exist?
6 */
7 char *memcached_get(memcached_st *ptr, char *key, size_t key_length,
8 size_t *value_length,
9 uint32_t *flags,
10 memcached_return *error)
11 {
12 return memcached_get_by_key(ptr, NULL, 0, key, key_length, value_length,
13 flags, error);
14 }
15
16 char *memcached_get_by_key(memcached_st *ptr,
17 char *master_key, size_t master_key_length,
18 char *key, size_t key_length,
19 size_t *value_length,
20 uint32_t *flags,
21 memcached_return *error)
22 {
23 char *value;
24
25 /* Request the key */
26 *error= memcached_mget_by_key(ptr,
27 master_key,
28 master_key_length,
29 &key, &key_length, 1);
30
31 value= memcached_fetch(ptr, NULL, NULL,
32 value_length, flags, error);
33
34 /* This is for historical reasons */
35 if (*error == MEMCACHED_END)
36 *error= MEMCACHED_NOTFOUND;
37
38 if (value == NULL)
39 return NULL;
40
41 memcached_finish(ptr);
42
43 return value;
44 }
45
46 memcached_return memcached_mget(memcached_st *ptr,
47 char **keys, size_t *key_length,
48 unsigned int number_of_keys)
49 {
50 return memcached_mget_by_key(ptr, NULL, 0, keys, key_length, number_of_keys);
51 }
52
53 memcached_return memcached_mget_by_key(memcached_st *ptr,
54 char *master_key, size_t master_key_length,
55 char **keys, size_t *key_length,
56 unsigned int number_of_keys)
57 {
58 unsigned int x;
59 memcached_return rc= MEMCACHED_NOTFOUND;
60 char *get_command= "get ";
61 uint8_t get_command_length= 4;
62 unsigned int master_server_key= 0;
63
64 LIBMEMCACHED_MEMCACHED_MGET_START();
65 ptr->cursor_server= 0;
66
67 if (number_of_keys == 0)
68 return MEMCACHED_NOTFOUND;
69
70 if (ptr->number_of_hosts == 0)
71 return MEMCACHED_NO_SERVERS;
72
73 if (ptr->flags & MEM_SUPPORT_CAS)
74 {
75 get_command= "gets ";
76 get_command_length= 5;
77 }
78
79 memcached_finish(ptr);
80
81 if (master_key && master_key_length)
82 master_server_key= memcached_generate_hash(ptr, master_key, master_key_length);
83
84 /*
85 If a server fails we warn about errors and start all over with sending keys
86 to the server.
87 */
88 for (x= 0; x < number_of_keys; x++)
89 {
90 unsigned int server_key;
91
92 if (master_server_key)
93 server_key= master_server_key;
94 else
95 server_key= memcached_generate_hash(ptr, keys[x], key_length[x]);
96
97 if (ptr->hosts[server_key].cursor_active == 0)
98 {
99 rc= memcached_connect(ptr, server_key);
100
101 if ((memcached_io_write(ptr, server_key, get_command, get_command_length, 0)) == -1)
102 {
103 rc= MEMCACHED_SOME_ERRORS;
104 continue;
105 }
106 ptr->hosts[server_key].cursor_active++;
107 }
108
109 if ((memcached_io_write(ptr, server_key, keys[x], key_length[x], 0)) == -1)
110 {
111 ptr->hosts[server_key].cursor_active= 0;
112 rc= MEMCACHED_SOME_ERRORS;
113 continue;
114 }
115
116 if ((memcached_io_write(ptr, server_key, " ", 1, 0)) == -1)
117 {
118 ptr->hosts[server_key].cursor_active= 0;
119 rc= MEMCACHED_SOME_ERRORS;
120 continue;
121 }
122 }
123
124 /*
125 Should we muddle on if some servers are dead?
126 */
127 for (x= 0; x < ptr->number_of_hosts; x++)
128 {
129 if (ptr->hosts[x].cursor_active)
130 {
131 /* We need to doo something about non-connnected hosts in the future */
132 if ((memcached_io_write(ptr, x, "\r\n", 2, 1)) == -1)
133 {
134 rc= MEMCACHED_SOME_ERRORS;
135 }
136 memcached_server_response_increment(ptr, x);
137 }
138 }
139
140 LIBMEMCACHED_MEMCACHED_MGET_END();
141 return rc;
142 }