X-Git-Url: https://git.m6w6.name/?a=blobdiff_plain;f=libmemcached%2Fget.cc;h=bcc468a012414f86dbf8109be7c93b5f5bb2881b;hb=8985b4b6e67215904383dac7fca792b4d9be89c4;hp=29d01bc8c293d6d2a844b541af67e871a7f4fafb;hpb=ae6bc7501efd5aeaaee92dabe2da0ec2d1625c5b;p=awesomized%2Flibmemcached diff --git a/libmemcached/get.cc b/libmemcached/get.cc index 29d01bc8..bcc468a0 100644 --- a/libmemcached/get.cc +++ b/libmemcached/get.cc @@ -1,15 +1,42 @@ -/* LibMemcached - * Copyright (C) 2006-2009 Brian Aker - * All rights reserved. +/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab: + * + * Libmemcached library * - * Use and distribution licensed under the BSD license. See - * the COPYING file in the parent directory for full text. + * Copyright (C) 2011 Data Differential, http://datadifferential.com/ + * Copyright (C) 2006-2009 Brian Aker All rights reserved. * - * Summary: Get functions for libmemcached + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are + * met: + * + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * + * * Redistributions in binary form must reproduce the above + * copyright notice, this list of conditions and the following disclaimer + * in the documentation and/or other materials provided with the + * distribution. + * + * * The names of its contributors may not be used to endorse or + * promote products derived from this software without specific prior + * written permission. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT + * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR + * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT + * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, + * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT + * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, + * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY + * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE + * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. * */ -#include "common.h" +#include +#include /* What happens if no servers exist? @@ -40,24 +67,42 @@ char *memcached_get_by_key(memcached_st *ptr, uint32_t *flags, memcached_return_t *error) { - char *value; - size_t dummy_length; - uint32_t dummy_flags; - memcached_return_t dummy_error; - unlikely (ptr->flags.use_udp) { - *error= MEMCACHED_NOT_SUPPORTED; + if (value_length) + *value_length= 0; + + *error= memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT); return NULL; } + uint64_t query_id= ptr->query_id; + (void)query_id; + /* Request the key */ *error= memcached_mget_by_key_real(ptr, group_key, group_key_length, (const char * const *)&key, &key_length, 1, false); + assert(ptr->query_id == query_id +1); + + + if (memcached_failed(*error)) + { + if (memcached_has_current_error(*ptr)) // Find the most accurate error + { + *error= memcached_last_error(ptr); + } + + if (value_length) + *value_length= 0; + + return NULL; + } + + char *value= memcached_fetch(ptr, NULL, NULL, + value_length, flags, error); + assert(ptr->query_id == query_id +1); - value= memcached_fetch(ptr, NULL, NULL, - value_length, flags, error); /* This is for historical reasons */ if (*error == MEMCACHED_END) *error= MEMCACHED_NOTFOUND; @@ -66,10 +111,9 @@ char *memcached_get_by_key(memcached_st *ptr, { if (ptr->get_key_failure && *error == MEMCACHED_NOTFOUND) { - memcached_return_t rc; memcached_result_reset(&ptr->result); - rc= ptr->get_key_failure(ptr, key, key_length, &ptr->result); + memcached_return_t rc= ptr->get_key_failure(ptr, key, key_length, &ptr->result); /* On all failure drop to returning NULL */ if (rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED) @@ -108,14 +152,21 @@ char *memcached_get_by_key(memcached_st *ptr, } } } + assert(ptr->query_id == query_id +1); return NULL; } - (void)memcached_fetch(ptr, NULL, NULL, - &dummy_length, &dummy_flags, - &dummy_error); + size_t dummy_length; + uint32_t dummy_flags; + memcached_return_t dummy_error; + + char *dummy_value= memcached_fetch(ptr, NULL, NULL, + &dummy_length, &dummy_flags, + &dummy_error); WATCHPOINT_ASSERT(dummy_length == 0); + WATCHPOINT_ASSERT(dummy_value == 0); + assert(ptr->query_id == query_id +1); return value; } @@ -148,7 +199,6 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr, const char *get_command= "get "; uint8_t get_command_length= 4; unsigned int master_server_key= (unsigned int)-1; /* 0 is a valid server id! */ - bool is_group_key_set= false; memcached_return_t rc; if (memcached_failed(rc= initialize_query(ptr))) @@ -157,22 +207,29 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr, } unlikely (ptr->flags.use_udp) - return MEMCACHED_NOT_SUPPORTED; + { + return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT); + } LIBMEMCACHED_MEMCACHED_MGET_START(); if (number_of_keys == 0) - return MEMCACHED_NOTFOUND; + { + return memcached_set_error(*ptr, MEMCACHED_NOTFOUND, MEMCACHED_AT, memcached_literal_param("number_of_keys was zero")); + } if (ptr->flags.verify_key && (memcached_key_test(keys, key_length, number_of_keys) == MEMCACHED_BAD_KEY_PROVIDED)) { - return MEMCACHED_BAD_KEY_PROVIDED; + return memcached_set_error(*ptr, MEMCACHED_BAD_KEY_PROVIDED, MEMCACHED_AT, memcached_literal_param("A bad key value was provided")); } + bool is_group_key_set= false; if (group_key && group_key_length) { if (ptr->flags.verify_key and (memcached_key_test((const char * const *)&group_key, &group_key_length, 1) == MEMCACHED_BAD_KEY_PROVIDED)) - return MEMCACHED_BAD_KEY_PROVIDED; + { + return memcached_set_error(*ptr, MEMCACHED_BAD_KEY_PROVIDED, MEMCACHED_AT, memcached_literal_param("A bad group key was provided.")); + } master_server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length); is_group_key_set= true; @@ -248,8 +305,9 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr, { rc= memcached_connect(instance); - if (rc != MEMCACHED_SUCCESS) + if (memcached_failed(rc)) { + memcached_set_error(*instance, rc, MEMCACHED_AT); continue; } hosts_connected++; @@ -278,10 +336,10 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr, { LIBMEMCACHED_MEMCACHED_MGET_END(); - if (rc != MEMCACHED_SUCCESS) + if (memcached_failed(rc)) return rc; - return MEMCACHED_NO_SERVERS; + return memcached_set_error(*ptr, MEMCACHED_NO_SERVERS, MEMCACHED_AT); } @@ -311,12 +369,14 @@ static memcached_return_t memcached_mget_by_key_real(memcached_st *ptr, LIBMEMCACHED_MEMCACHED_MGET_END(); if (failures_occured_in_sending && success_happened) + { return MEMCACHED_SOME_ERRORS; + } if (success_happened) return MEMCACHED_SUCCESS; - return MEMCACHED_FAILURE; + return MEMCACHED_FAILURE; // Complete failure occurred } memcached_return_t memcached_mget_by_key(memcached_st *ptr, @@ -389,7 +449,6 @@ static memcached_return_t simple_binary_mget(memcached_st *ptr, for (uint32_t x= 0; x < number_of_keys; ++x) { uint32_t server_key; - memcached_server_write_instance_st instance; if (is_group_key_set) { @@ -400,12 +459,12 @@ static memcached_return_t simple_binary_mget(memcached_st *ptr, server_key= memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]); } - instance= memcached_server_instance_fetch(ptr, server_key); + memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, server_key); if (memcached_server_response_count(instance) == 0) { rc= memcached_connect(instance); - if (rc != MEMCACHED_SUCCESS) + if (memcached_failed(rc)) continue; } @@ -516,8 +575,6 @@ static memcached_return_t replication_binary_mget(memcached_st *ptr, for (uint32_t x= 0; x < number_of_keys; ++x) { - memcached_server_write_instance_st instance; - if (hash[x] == memcached_server_count(ptr)) continue; /* Already successfully sent */ @@ -533,12 +590,12 @@ static memcached_return_t replication_binary_mget(memcached_st *ptr, if (dead_servers[server]) continue; - instance= memcached_server_instance_fetch(ptr, server); + memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, server); if (memcached_server_response_count(instance) == 0) { rc= memcached_connect(instance); - if (rc != MEMCACHED_SUCCESS) + if (memcached_failed(rc)) { memcached_io_reset(instance); dead_servers[server]= true; @@ -598,51 +655,43 @@ static memcached_return_t binary_mget_by_key(memcached_st *ptr, size_t number_of_keys, bool mget_mode) { - memcached_return_t rc; - if (ptr->number_of_replicas == 0) { - rc= simple_binary_mget(ptr, master_server_key, is_group_key_set, - keys, key_length, number_of_keys, mget_mode); + return simple_binary_mget(ptr, master_server_key, is_group_key_set, + keys, key_length, number_of_keys, mget_mode); } - else - { - uint32_t* hash; - bool* dead_servers; - hash= static_cast(libmemcached_malloc(ptr, sizeof(uint32_t) * number_of_keys)); - dead_servers= static_cast(libmemcached_calloc(ptr, memcached_server_count(ptr), sizeof(bool))); + uint32_t* hash= static_cast(libmemcached_malloc(ptr, sizeof(uint32_t) * number_of_keys)); + bool* dead_servers= static_cast(libmemcached_calloc(ptr, memcached_server_count(ptr), sizeof(bool))); - if (hash == NULL || dead_servers == NULL) - { - libmemcached_free(ptr, hash); - libmemcached_free(ptr, dead_servers); - return MEMCACHED_MEMORY_ALLOCATION_FAILURE; - } + if (hash == NULL || dead_servers == NULL) + { + libmemcached_free(ptr, hash); + libmemcached_free(ptr, dead_servers); + return MEMCACHED_MEMORY_ALLOCATION_FAILURE; + } - if (is_group_key_set) + if (is_group_key_set) + { + for (size_t x= 0; x < number_of_keys; x++) { - for (size_t x= 0; x < number_of_keys; x++) - { - hash[x]= master_server_key; - } + hash[x]= master_server_key; } - else + } + else + { + for (size_t x= 0; x < number_of_keys; x++) { - for (size_t x= 0; x < number_of_keys; x++) - { - hash[x]= memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]); - } + hash[x]= memcached_generate_hash_with_redistribution(ptr, keys[x], key_length[x]); } + } - rc= replication_binary_mget(ptr, hash, dead_servers, keys, - key_length, number_of_keys); - - libmemcached_free(ptr, hash); - libmemcached_free(ptr, dead_servers); + memcached_return_t rc= replication_binary_mget(ptr, hash, dead_servers, keys, + key_length, number_of_keys); - return MEMCACHED_SUCCESS; - } + WATCHPOINT_IFERROR(rc); + libmemcached_free(ptr, hash); + libmemcached_free(ptr, dead_servers); - return rc; + return MEMCACHED_SUCCESS; }