Fixing failure of socket issue.
[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 uint16_t *flags,
10 memcached_return *error)
11 {
12 char *value;
13
14 /* Request the key */
15 *error= memcached_mget(ptr, &key, &key_length, 1);
16
17 value= memcached_fetch(ptr, NULL, NULL,
18 value_length, flags, error);
19
20 /* This is for historical reasons */
21 if (*error == MEMCACHED_END)
22 *error= MEMCACHED_NOTFOUND;
23
24 if (value == NULL)
25 return NULL;
26
27 memcached_finish(ptr);
28
29 return value;
30 }
31
32 memcached_return memcached_mget(memcached_st *ptr,
33 char **keys, size_t *key_length,
34 unsigned int number_of_keys)
35 {
36 unsigned int x;
37 memcached_return rc= MEMCACHED_NOTFOUND;
38 char *get_command= "get ";
39 uint8_t get_command_length= 4
40
41 LIBMEMCACHED_MEMCACHED_MGET_START();
42 ptr->cursor_server= 0;
43
44 if (number_of_keys == 0)
45 return MEMCACHED_NOTFOUND;
46
47 if (ptr->number_of_hosts == 0)
48 return MEMCACHED_NO_SERVERS;
49
50 if (ptr->flags & MEM_SUPPORT_CAS)
51 {
52 get_command= "gets ";
53 get_command_length= 5;
54 }
55
56 memcached_finish(ptr);
57
58 /*
59 If a server fails we warn about errors and start all over with sending keys
60 to the server.
61 */
62 for (x= 0; x < number_of_keys; x++)
63 {
64 unsigned int server_key;
65
66 server_key= memcached_generate_hash(ptr, keys[x], key_length[x]);
67
68 if (ptr->hosts[server_key].cursor_active == 0)
69 {
70 rc= memcached_connect(ptr, server_key);
71
72 if ((memcached_io_write(ptr, server_key, get_command, get_command_length, 0)) == -1)
73 {
74 rc= MEMCACHED_SOME_ERRORS;
75 continue;
76 }
77 ptr->hosts[server_key].cursor_active= 1;
78 }
79
80 if ((memcached_io_write(ptr, server_key, keys[x], key_length[x], 0)) == -1)
81 {
82 ptr->hosts[server_key].cursor_active= 0;
83 rc= MEMCACHED_SOME_ERRORS;
84 continue;
85 }
86
87 if ((memcached_io_write(ptr, server_key, " ", 1, 0)) == -1)
88 {
89 ptr->hosts[server_key].cursor_active= 0;
90 rc= MEMCACHED_SOME_ERRORS;
91 continue;
92 }
93 }
94
95 /*
96 Should we muddle on if some servers are dead?
97 */
98 for (x= 0; x < ptr->number_of_hosts; x++)
99 {
100 if (ptr->hosts[x].cursor_active == 1)
101 {
102 /* We need to doo something about non-connnected hosts in the future */
103 if ((memcached_io_write(ptr, x, "\r\n", 2, 1)) == -1)
104 {
105 rc= MEMCACHED_SOME_ERRORS;
106 }
107 }
108 }
109
110 LIBMEMCACHED_MEMCACHED_MGET_END();
111 return rc;
112 }