X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=lib%2Fmemcached_get.c;h=12d301ec35319d0faabbe336f75e002a0d024777;hb=8ce1a267a6593221cdd887643517364694ea985f;hp=8c3180ab9caf7e39ea7e43815e34d2354bfe7424;hpb=ac7b4013b6ada79981f5385c10c2ba242df58143;p=awesomized%2Flibmemcached diff --git a/lib/memcached_get.c b/lib/memcached_get.c index 8c3180ab..12d301ec 100644 --- a/lib/memcached_get.c +++ b/lib/memcached_get.c @@ -20,150 +20,88 @@ char *memcached_get_by_key(memcached_st *ptr, uint32_t *flags, memcached_return *error) { - char *value; - size_t dummy_length; - uint32_t dummy_flags; - memcached_return dummy_error; - - /* Request the key */ - *error= memcached_mget_by_key(ptr, - master_key, - master_key_length, - &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; - - (void)memcached_fetch(ptr, NULL, NULL, - &dummy_length, &dummy_flags, - &dummy_error); - WATCHPOINT_ASSERT(dummy_length == 0); - - return value; -} - -memcached_return memcached_mget(memcached_st *ptr, - char **keys, size_t *key_length, - unsigned int number_of_keys) -{ - return memcached_mget_by_key(ptr, NULL, 0, keys, key_length, number_of_keys); -} - -memcached_return memcached_mget_by_key(memcached_st *ptr, - char *master_key, size_t master_key_length, - char **keys, size_t *key_length, - unsigned int number_of_keys) -{ - unsigned int x; - memcached_return rc= MEMCACHED_NOTFOUND; - char *get_command= "get "; - uint8_t get_command_length= 4; - unsigned int master_server_key= 0; - - 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; } - if (master_key && master_key_length) - master_server_key= memcached_generate_hash(ptr, master_key, master_key_length); + if (master_key) + server_key= memcached_generate_hash(ptr, master_key, master_key_length); + else + server_key= memcached_generate_hash(ptr, key, key_length); - /* - Here is where we pay for the non-block API. We need to remove any data sitting - in the queue before we start our get. + send_length= snprintf(buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, + "get %.*s\r\n", (int)key_length, key); - It might be optimum to bounce the connection if count > some number. - */ - for (x= 0; x < ptr->number_of_hosts; x++) + do { - if (memcached_server_response_count(&ptr->hosts[x])) - { - char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; + char response_buffer[MEMCACHED_DEFAULT_COMMAND_SIZE]; + if (memcached_server_response_count(&ptr->hosts[server_key])) + { if (ptr->flags & MEM_NO_BLOCK) - (void)memcached_io_write(&ptr->hosts[x], NULL, 0, 1); + (void)memcached_io_write(&ptr->hosts[server_key], NULL, 0, 1); - while(memcached_server_response_count(&ptr->hosts[x])) - (void)memcached_response(&ptr->hosts[x], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, &ptr->result); + 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 a server fails we warn about errors and start all over with sending keys - to the server. - */ - for (x= 0; x < number_of_keys; x++) - { - unsigned int server_key; + rc[replicas]= memcached_do(&ptr->hosts[server_key], buffer, send_length, 1); + if (rc[replicas] != MEMCACHED_SUCCESS) + goto error; - if (master_server_key) - server_key= master_server_key; - else - server_key= memcached_generate_hash(ptr, keys[x], key_length[x]); + rc[replicas]= memcached_response(&ptr->hosts[server_key], response_buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, result_buffer); - if (memcached_server_response_count(&ptr->hosts[server_key]) == 0) - { - rc= memcached_connect(&ptr->hosts[server_key]); - - if (rc != MEMCACHED_SUCCESS) - continue; - - if ((memcached_io_write(&ptr->hosts[server_key], get_command, get_command_length, 0)) == -1) - { - rc= MEMCACHED_SOME_ERRORS; - continue; - } - WATCHPOINT_ASSERT(ptr->hosts[server_key].cursor_active == 0); - memcached_server_response_increment(&ptr->hosts[server_key]); - WATCHPOINT_ASSERT(ptr->hosts[server_key].cursor_active == 1); - } - - if ((memcached_io_write(&ptr->hosts[server_key], keys[x], key_length[x], 0)) == -1) + /* On no key found, we check the replica */ + if (rc[replicas] == MEMCACHED_END) /* END means that we move on to the next */ { memcached_server_response_reset(&ptr->hosts[server_key]); - rc= MEMCACHED_SOME_ERRORS; - continue; } - - if ((memcached_io_write(&ptr->hosts[server_key], " ", 1, 0)) == -1) + else if (rc[replicas] == MEMCACHED_SUCCESS) { - memcached_server_response_reset(&ptr->hosts[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 (memcached_server_response_count(&ptr->hosts[x])) + /* On error we just jump to the next potential server */ +error: + if (ptr->number_of_replicas > 1) { - /* We need to do something about non-connnected hosts in the future */ - if ((memcached_io_write(&ptr->hosts[x], "\r\n", 2, 1)) == -1) - { - 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; }