2 #include "memcached_io.h"
5 What happens if no servers exist?
7 char *memcached_get(memcached_st
*ptr
, const char *key
,
11 memcached_return
*error
)
13 return memcached_get_by_key(ptr
, NULL
, 0, key
, key_length
, value_length
,
17 char *memcached_get_by_key(memcached_st
*ptr
,
18 const char *master_key
,
19 size_t master_key_length
,
20 const char *key
, size_t key_length
,
23 memcached_return
*error
)
28 memcached_return dummy_error
;
31 *error
= memcached_mget_by_key(ptr
,
34 (char **)&key
, &key_length
, 1);
36 value
= memcached_fetch(ptr
, NULL
, NULL
,
37 value_length
, flags
, error
);
38 /* This is for historical reasons */
39 if (*error
== MEMCACHED_END
)
40 *error
= MEMCACHED_NOTFOUND
;
44 if (ptr
->get_key_failure
&& *error
== MEMCACHED_NOTFOUND
)
48 memcached_result_reset(&ptr
->result
);
49 rc
= ptr
->get_key_failure(ptr
, key
, key_length
, &ptr
->result
);
51 /* On all failure drop to returning NULL */
52 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
)
54 if (rc
== MEMCACHED_BUFFERED
)
56 uint8_t latch
; /* We use latch to track the state of the original socket */
57 latch
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
);
59 memcached_behavior_set(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
61 rc
= memcached_set(ptr
, key
, key_length
,
62 memcached_result_value(&ptr
->result
),
63 memcached_result_length(&ptr
->result
),
64 0, memcached_result_flags(&ptr
->result
));
66 if (rc
== MEMCACHED_BUFFERED
&& latch
== 0)
67 memcached_behavior_set(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0);
71 rc
= memcached_set(ptr
, key
, key_length
,
72 memcached_result_value(&ptr
->result
),
73 memcached_result_length(&ptr
->result
),
74 0, memcached_result_flags(&ptr
->result
));
77 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
)
80 *value_length
= memcached_result_length(&ptr
->result
);
81 *flags
= memcached_result_flags(&ptr
->result
);
82 return memcached_string_c_copy(&ptr
->result
.value
);
90 (void)memcached_fetch(ptr
, NULL
, NULL
,
91 &dummy_length
, &dummy_flags
,
93 WATCHPOINT_ASSERT(dummy_length
== 0);
98 memcached_return
memcached_mget(memcached_st
*ptr
,
99 char **keys
, size_t *key_length
,
100 unsigned int number_of_keys
)
102 return memcached_mget_by_key(ptr
, NULL
, 0, keys
, key_length
, number_of_keys
);
105 memcached_return
memcached_mget_by_key(memcached_st
*ptr
,
106 const char *master_key
,
107 size_t master_key_length
,
110 unsigned int number_of_keys
)
113 memcached_return rc
= MEMCACHED_NOTFOUND
;
114 char *get_command
= "get ";
115 uint8_t get_command_length
= 4;
116 unsigned int master_server_key
= 0;
118 LIBMEMCACHED_MEMCACHED_MGET_START();
119 ptr
->cursor_server
= 0;
121 if (number_of_keys
== 0)
122 return MEMCACHED_NOTFOUND
;
124 if (ptr
->number_of_hosts
== 0)
125 return MEMCACHED_NO_SERVERS
;
127 if ((ptr
->flags
& MEM_VERIFY_KEY
) && (memcachd_key_test(keys
, key_length
, number_of_keys
) == MEMCACHED_BAD_KEY_PROVIDED
))
128 return MEMCACHED_BAD_KEY_PROVIDED
;
130 if (ptr
->flags
& MEM_SUPPORT_CAS
)
132 get_command
= "gets ";
133 get_command_length
= 5;
136 if (master_key
&& master_key_length
)
137 master_server_key
= memcached_generate_hash(ptr
, master_key
, master_key_length
);
140 Here is where we pay for the non-block API. We need to remove any data sitting
141 in the queue before we start our get.
143 It might be optimum to bounce the connection if count > some number.
145 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
147 if (memcached_server_response_count(&ptr
->hosts
[x
]))
149 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
151 if (ptr
->flags
& MEM_NO_BLOCK
)
152 (void)memcached_io_write(&ptr
->hosts
[x
], NULL
, 0, 1);
154 while(memcached_server_response_count(&ptr
->hosts
[x
]))
155 (void)memcached_response(&ptr
->hosts
[x
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, &ptr
->result
);
160 If a server fails we warn about errors and start all over with sending keys
163 for (x
= 0; x
< number_of_keys
; x
++)
165 unsigned int server_key
;
167 if (master_server_key
)
168 server_key
= master_server_key
;
170 server_key
= memcached_generate_hash(ptr
, keys
[x
], key_length
[x
]);
172 if (memcached_server_response_count(&ptr
->hosts
[server_key
]) == 0)
174 rc
= memcached_connect(&ptr
->hosts
[server_key
]);
176 if (rc
!= MEMCACHED_SUCCESS
)
179 if ((memcached_io_write(&ptr
->hosts
[server_key
], get_command
, get_command_length
, 0)) == -1)
181 rc
= MEMCACHED_SOME_ERRORS
;
184 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].cursor_active
== 0);
185 memcached_server_response_increment(&ptr
->hosts
[server_key
]);
186 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].cursor_active
== 1);
189 /* Only called when we have a prefix key */
190 if (ptr
->prefix_key
[0] != 0)
192 if ((memcached_io_write(&ptr
->hosts
[server_key
], ptr
->prefix_key
, ptr
->prefix_key_length
, 0)) == -1)
194 memcached_server_response_reset(&ptr
->hosts
[server_key
]);
195 rc
= MEMCACHED_SOME_ERRORS
;
200 if ((memcached_io_write(&ptr
->hosts
[server_key
], keys
[x
], key_length
[x
], 0)) == -1)
202 memcached_server_response_reset(&ptr
->hosts
[server_key
]);
203 rc
= MEMCACHED_SOME_ERRORS
;
207 if ((memcached_io_write(&ptr
->hosts
[server_key
], " ", 1, 0)) == -1)
209 memcached_server_response_reset(&ptr
->hosts
[server_key
]);
210 rc
= MEMCACHED_SOME_ERRORS
;
216 Should we muddle on if some servers are dead?
218 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
220 if (memcached_server_response_count(&ptr
->hosts
[x
]))
222 /* We need to do something about non-connnected hosts in the future */
223 if ((memcached_io_write(&ptr
->hosts
[x
], "\r\n", 2, 1)) == -1)
225 rc
= MEMCACHED_SOME_ERRORS
;
230 LIBMEMCACHED_MEMCACHED_MGET_END();