*/
char *memcached_get(memcached_st *ptr, char *key, size_t key_length,
size_t *value_length,
- uint16_t *flags,
+ uint32_t *flags,
memcached_return *error)
{
- char *value;
-
- /* Request the key */
- *error= memcached_mget(ptr, &key, &key_length, 1);
-
- value= memcached_fetch(ptr, NULL, NULL,
- value_length, flags, error);
-
- /* This is for historical reasons */
- if (*error == MEMCACHED_END)
- *error= MEMCACHED_NOTFOUND;
-
- if (value == NULL)
- return NULL;
-
- memcached_finish(ptr);
-
- return value;
+ return memcached_get_by_key(ptr, NULL, 0, key, key_length, value_length,
+ flags, error);
}
-memcached_return memcached_mget(memcached_st *ptr,
- char **keys, size_t *key_length,
- unsigned int number_of_keys)
+char *memcached_get_by_key(memcached_st *ptr,
+ char *master_key, size_t master_key_length,
+ char *key, size_t key_length,
+ size_t *value_length,
+ uint32_t *flags,
+ memcached_return *error)
{
- unsigned int x;
- memcached_return rc= MEMCACHED_NOTFOUND;
- char *get_command= "get ";
- uint8_t get_command_length= 4
-
- LIBMEMCACHED_MEMCACHED_MGET_START();
- ptr->cursor_server= 0;
-
- if (number_of_keys == 0)
- return MEMCACHED_NOTFOUND;
+ unsigned int server_key;
+ size_t send_length;
+ char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
+ memcached_result_st *result_buffer= &ptr->result;
+ memcached_return rc[MEMCACHED_MAX_REPLICAS];
+ uint8_t replicas= 0;
if (ptr->number_of_hosts == 0)
- return MEMCACHED_NO_SERVERS;
+ {
+ *error= MEMCACHED_NO_SERVERS;
+ return NULL;
+ }
- if (ptr->flags & MEM_SUPPORT_CAS)
+ if ((ptr->flags & MEM_VERIFY_KEY) && (memcachd_key_test(&key, &key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED))
{
- get_command= "gets ";
- get_command_length= 5;
+ *value_length= 0;
+ *error= MEMCACHED_BAD_KEY_PROVIDED;
+ return NULL;
}
- memcached_finish(ptr);
+ if (master_key)
+ server_key= memcached_generate_hash(ptr, master_key, master_key_length);
+ else
+ server_key= memcached_generate_hash(ptr, key, key_length);
- for (x= 0; x < number_of_keys; x++)
- {
- unsigned int server_key;
+ send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE,
+ "get %.*s\r\n", (int)key_length, key);
- server_key= memcached_generate_hash(ptr, keys[x], key_length[x]);
+ do
+ {
+ char response_buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
- if (ptr->hosts[server_key].cursor_active == 0)
+ if (memcached_server_response_count(&ptr->hosts[server_key]))
{
- rc= memcached_connect(ptr, server_key);
-
- if ((memcached_io_write(ptr, server_key, get_command, get_command_length, 0)) == -1)
- {
- memcached_quit_server(ptr, server_key);
- rc= MEMCACHED_SOME_ERRORS;
- continue;
- }
- ptr->hosts[server_key].cursor_active= 1;
+ if (ptr->flags & MEM_NO_BLOCK)
+ (void)memcached_io_write(&ptr->hosts[server_key], NULL, 0, 1);
+
+ while(memcached_server_response_count(&ptr->hosts[server_key]))
+ (void)memcached_response(&ptr->hosts[server_key], response_buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, result_buffer);
}
- if ((memcached_io_write(ptr, server_key, keys[x], key_length[x], 0)) == -1)
+ rc[replicas]= memcached_do(&ptr->hosts[server_key], buffer, send_length, 1);
+ if (rc[replicas] != MEMCACHED_SUCCESS)
+ goto error;
+
+ rc[replicas]= memcached_response(&ptr->hosts[server_key], response_buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, result_buffer);
+
+ /* On no key found, we check the replica */
+ if (rc[replicas] == MEMCACHED_END) /* END means that we move on to the next */
{
- ptr->hosts[server_key].cursor_active= 0;
- memcached_quit_server(ptr, server_key);
- rc= MEMCACHED_SOME_ERRORS;
- continue;
+ memcached_server_response_reset(&ptr->hosts[server_key]);
}
-
- if ((memcached_io_write(ptr, server_key, " ", 1, 0)) == -1)
+ else if (rc[replicas] == MEMCACHED_SUCCESS)
{
- ptr->hosts[server_key].cursor_active= 0;
- memcached_quit_server(ptr, server_key);
- rc= MEMCACHED_SOME_ERRORS;
- continue;
+ *value_length= memcached_string_length(&result_buffer->value);
+
+ if (result_buffer->flags)
+ *flags= result_buffer->flags;
+
+ *error= MEMCACHED_SUCCESS;
+ return memcached_string_c_copy(&result_buffer->value);
}
- }
- /*
- Should we muddle on if some servers are dead?
- */
- for (x= 0; x < ptr->number_of_hosts; x++)
- {
- if (ptr->hosts[x].cursor_active == 1)
+ /* On error we just jump to the next potential server */
+error:
+ if (ptr->number_of_replicas > 1)
{
- /* We need to doo something about non-connnected hosts in the future */
- if ((memcached_io_write(ptr, x, "\r\n", 2, 1)) == -1)
- {
- memcached_quit_server(ptr, x);
- rc= MEMCACHED_SOME_ERRORS;
- }
+ if (server_key == (ptr->number_of_hosts - 1))
+ server_key= 0;
+ else
+ server_key++;
}
- }
+ } while ((++replicas) < ptr->number_of_replicas);
+
+ /* TODO: An error on replica 1 of host down, but not found on 2, will give wrong error */
+ /* This is for historical reasons */
+ if (rc[0] == MEMCACHED_END)
+ *error= MEMCACHED_NOTFOUND;
+ else
+ *error= rc[0];
+
+ *value_length= 0;
- LIBMEMCACHED_MEMCACHED_MGET_END();
- return rc;
+ return NULL;
}