2 #include "memcached_io.h"
5 What happens if no servers exist?
7 char *memcached_get(memcached_st
*ptr
, char *key
, size_t key_length
,
10 memcached_return
*error
)
12 return memcached_get_by_key(ptr
, NULL
, 0, key
, key_length
, value_length
,
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
,
21 memcached_return
*error
)
26 memcached_return dummy_error
;
29 *error
= memcached_mget_by_key(ptr
,
32 &key
, &key_length
, 1);
34 value
= memcached_fetch(ptr
, NULL
, NULL
,
35 value_length
, flags
, error
);
36 /* This is for historical reasons */
37 if (*error
== MEMCACHED_END
)
38 *error
= MEMCACHED_NOTFOUND
;
43 (void)memcached_fetch(ptr
, NULL
, NULL
,
44 &dummy_length
, &dummy_flags
,
50 memcached_return
memcached_mget(memcached_st
*ptr
,
51 char **keys
, size_t *key_length
,
52 unsigned int number_of_keys
)
54 return memcached_mget_by_key(ptr
, NULL
, 0, keys
, key_length
, number_of_keys
);
57 memcached_return
memcached_mget_by_key(memcached_st
*ptr
,
58 char *master_key
, size_t master_key_length
,
59 char **keys
, size_t *key_length
,
60 unsigned int number_of_keys
)
63 memcached_return rc
= MEMCACHED_NOTFOUND
;
64 char *get_command
= "get ";
65 uint8_t get_command_length
= 4;
66 unsigned int master_server_key
= 0;
68 LIBMEMCACHED_MEMCACHED_MGET_START();
69 ptr
->cursor_server
= 0;
71 if (number_of_keys
== 0)
72 return MEMCACHED_NOTFOUND
;
74 if (ptr
->number_of_hosts
== 0)
75 return MEMCACHED_NO_SERVERS
;
77 if (ptr
->flags
& MEM_SUPPORT_CAS
)
80 get_command_length
= 5;
83 if (master_key
&& master_key_length
)
84 master_server_key
= memcached_generate_hash(ptr
, master_key
, master_key_length
);
87 Here is where we pay for the non-block API. We need to remove any data sitting
88 in the queue before we start our get.
90 It might be optimum to bounce the connection if count > some number.
92 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
94 if (memcached_server_response_count(ptr
, x
))
96 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
98 if (ptr
->flags
& MEM_NO_BLOCK
)
99 (void)memcached_io_write(ptr
, x
, NULL
, 0, 1);
101 while(memcached_server_response_count(ptr
, x
))
102 (void)memcached_response(ptr
, buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, &ptr
->result
, x
);
107 If a server fails we warn about errors and start all over with sending keys
110 for (x
= 0; x
< number_of_keys
; x
++)
112 unsigned int server_key
;
114 if (master_server_key
)
115 server_key
= master_server_key
;
117 server_key
= memcached_generate_hash(ptr
, keys
[x
], key_length
[x
]);
119 if (memcached_server_response_count(ptr
, server_key
) == 0)
121 rc
= memcached_connect(ptr
, server_key
);
123 if (rc
!= MEMCACHED_SUCCESS
)
126 if ((memcached_io_write(ptr
, server_key
, get_command
, get_command_length
, 0)) == -1)
128 rc
= MEMCACHED_SOME_ERRORS
;
131 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].cursor_active
== 0);
132 memcached_server_response_increment(ptr
, server_key
);
133 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].cursor_active
== 1);
136 if ((memcached_io_write(ptr
, server_key
, keys
[x
], key_length
[x
], 0)) == -1)
138 memcached_server_response_reset(ptr
, server_key
);
139 rc
= MEMCACHED_SOME_ERRORS
;
143 if ((memcached_io_write(ptr
, server_key
, " ", 1, 0)) == -1)
145 memcached_server_response_reset(ptr
, server_key
);
146 rc
= MEMCACHED_SOME_ERRORS
;
152 Should we muddle on if some servers are dead?
154 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
156 if (memcached_server_response_count(ptr
, x
))
158 /* We need to do something about non-connnected hosts in the future */
159 if ((memcached_io_write(ptr
, x
, "\r\n", 2, 1)) == -1)
161 rc
= MEMCACHED_SOME_ERRORS
;
166 LIBMEMCACHED_MEMCACHED_MGET_END();