Fixed memcached_get() to now use finish instead of faking extra response.
[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 for (x= 0; x < number_of_keys; x++)
59 {
60 unsigned int server_key;
61
62 server_key= memcached_generate_hash(ptr, keys[x], key_length[x]);
63
64 if (ptr->hosts[server_key].cursor_active == 0)
65 {
66 rc= memcached_connect(ptr, server_key);
67
68 if ((memcached_io_write(ptr, server_key, get_command, get_command_length, 0)) == -1)
69 {
70 memcached_quit_server(ptr, server_key);
71 rc= MEMCACHED_SOME_ERRORS;
72 continue;
73 }
74 ptr->hosts[server_key].cursor_active= 1;
75 }
76
77 if ((memcached_io_write(ptr, server_key, keys[x], key_length[x], 0)) == -1)
78 {
79 ptr->hosts[server_key].cursor_active= 0;
80 memcached_quit_server(ptr, server_key);
81 rc= MEMCACHED_SOME_ERRORS;
82 continue;
83 }
84
85 if ((memcached_io_write(ptr, server_key, " ", 1, 0)) == -1)
86 {
87 ptr->hosts[server_key].cursor_active= 0;
88 memcached_quit_server(ptr, server_key);
89 rc= MEMCACHED_SOME_ERRORS;
90 continue;
91 }
92 }
93
94 /*
95 Should we muddle on if some servers are dead?
96 */
97 for (x= 0; x < ptr->number_of_hosts; x++)
98 {
99 if (ptr->hosts[x].cursor_active == 1)
100 {
101 /* We need to doo something about non-connnected hosts in the future */
102 if ((memcached_io_write(ptr, x, "\r\n", 2, 1)) == -1)
103 {
104 memcached_quit_server(ptr, x);
105 rc= MEMCACHED_SOME_ERRORS;
106 }
107 }
108 }
109
110 LIBMEMCACHED_MEMCACHED_MGET_END();
111 return rc;
112 }