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 uint8_t latch
; /* We use latch to track the state of the original socket */
56 if (rc
== MEMCACHED_BUFFERED
)
58 latch
= memcached_behavior_get(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
);
60 memcached_behavior_set(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 1);
63 rc
= memcached_set(ptr
, key
, key_length
,
64 memcached_result_value(&ptr
->result
),
65 memcached_result_length(&ptr
->result
),
66 0, memcached_result_flags(&ptr
->result
));
68 if (rc
== MEMCACHED_BUFFERED
&& latch
== 0)
69 memcached_behavior_set(ptr
, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS
, 0);
71 if (rc
== MEMCACHED_SUCCESS
|| rc
== MEMCACHED_BUFFERED
)
74 *value_length
= memcached_result_length(&ptr
->result
);
75 *flags
= memcached_result_flags(&ptr
->result
);
76 return memcached_string_c_copy(&ptr
->result
.value
);
84 (void)memcached_fetch(ptr
, NULL
, NULL
,
85 &dummy_length
, &dummy_flags
,
87 WATCHPOINT_ASSERT(dummy_length
== 0);
92 memcached_return
memcached_mget(memcached_st
*ptr
,
93 char **keys
, size_t *key_length
,
94 unsigned int number_of_keys
)
96 return memcached_mget_by_key(ptr
, NULL
, 0, keys
, key_length
, number_of_keys
);
99 memcached_return
memcached_mget_by_key(memcached_st
*ptr
,
100 const char *master_key
,
101 size_t master_key_length
,
104 unsigned int number_of_keys
)
107 memcached_return rc
= MEMCACHED_NOTFOUND
;
108 char *get_command
= "get ";
109 uint8_t get_command_length
= 4;
110 unsigned int master_server_key
= 0;
112 LIBMEMCACHED_MEMCACHED_MGET_START();
113 ptr
->cursor_server
= 0;
115 if (number_of_keys
== 0)
116 return MEMCACHED_NOTFOUND
;
118 if (ptr
->number_of_hosts
== 0)
119 return MEMCACHED_NO_SERVERS
;
121 if ((ptr
->flags
& MEM_VERIFY_KEY
) && (memcachd_key_test(keys
, key_length
, number_of_keys
) == MEMCACHED_BAD_KEY_PROVIDED
))
122 return MEMCACHED_BAD_KEY_PROVIDED
;
124 if (ptr
->flags
& MEM_SUPPORT_CAS
)
126 get_command
= "gets ";
127 get_command_length
= 5;
130 if (master_key
&& master_key_length
)
131 master_server_key
= memcached_generate_hash(ptr
, master_key
, master_key_length
);
134 Here is where we pay for the non-block API. We need to remove any data sitting
135 in the queue before we start our get.
137 It might be optimum to bounce the connection if count > some number.
139 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
141 if (memcached_server_response_count(&ptr
->hosts
[x
]))
143 char buffer
[MEMCACHED_DEFAULT_COMMAND_SIZE
];
145 if (ptr
->flags
& MEM_NO_BLOCK
)
146 (void)memcached_io_write(&ptr
->hosts
[x
], NULL
, 0, 1);
148 while(memcached_server_response_count(&ptr
->hosts
[x
]))
149 (void)memcached_response(&ptr
->hosts
[x
], buffer
, MEMCACHED_DEFAULT_COMMAND_SIZE
, &ptr
->result
);
154 If a server fails we warn about errors and start all over with sending keys
157 for (x
= 0; x
< number_of_keys
; x
++)
159 unsigned int server_key
;
161 if (master_server_key
)
162 server_key
= master_server_key
;
164 server_key
= memcached_generate_hash(ptr
, keys
[x
], key_length
[x
]);
166 if (memcached_server_response_count(&ptr
->hosts
[server_key
]) == 0)
168 rc
= memcached_connect(&ptr
->hosts
[server_key
]);
170 if (rc
!= MEMCACHED_SUCCESS
)
173 if ((memcached_io_write(&ptr
->hosts
[server_key
], get_command
, get_command_length
, 0)) == -1)
175 rc
= MEMCACHED_SOME_ERRORS
;
178 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].cursor_active
== 0);
179 memcached_server_response_increment(&ptr
->hosts
[server_key
]);
180 WATCHPOINT_ASSERT(ptr
->hosts
[server_key
].cursor_active
== 1);
183 if ((memcached_io_write(&ptr
->hosts
[server_key
], keys
[x
], key_length
[x
], 0)) == -1)
185 memcached_server_response_reset(&ptr
->hosts
[server_key
]);
186 rc
= MEMCACHED_SOME_ERRORS
;
190 if ((memcached_io_write(&ptr
->hosts
[server_key
], " ", 1, 0)) == -1)
192 memcached_server_response_reset(&ptr
->hosts
[server_key
]);
193 rc
= MEMCACHED_SOME_ERRORS
;
199 Should we muddle on if some servers are dead?
201 for (x
= 0; x
< ptr
->number_of_hosts
; x
++)
203 if (memcached_server_response_count(&ptr
->hosts
[x
]))
205 /* We need to do something about non-connnected hosts in the future */
206 if ((memcached_io_write(&ptr
->hosts
[x
], "\r\n", 2, 1)) == -1)
208 rc
= MEMCACHED_SOME_ERRORS
;
213 LIBMEMCACHED_MEMCACHED_MGET_END();