-/* 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 <libmemcached/common.h>
/*
What happens if no servers exist?
uint32_t *flags,
memcached_return_t *error)
{
- char *value;
- size_t dummy_length;
- uint32_t dummy_flags;
- memcached_return_t dummy_error;
+ memcached_return_t unused;
+ if (error == NULL)
+ {
+ error= &unused;
+ }
- unlikely (ptr->flags.use_udp)
+ uint64_t query_id= 0;
+ if (ptr)
{
- *error= MEMCACHED_NOT_SUPPORTED;
- return NULL;
+ query_id= ptr->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);
+ (const char * const *)&key, &key_length,
+ 1, false);
+ if (ptr)
+ {
+ assert_msg(ptr->query_id == query_id +1, "Programmer error, the query_id was not incremented.");
+ }
+
+ if (memcached_failed(*error))
+ {
+ if (ptr)
+ {
+ 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_msg(ptr->query_id == query_id +1, "Programmer error, the query_id was not incremented.");
- 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)
{
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)
+ if (rc == MEMCACHED_SUCCESS or rc == MEMCACHED_BUFFERED)
{
if (rc == MEMCACHED_BUFFERED)
{
uint64_t latch; /* We use latch to track the state of the original socket */
latch= memcached_behavior_get(ptr, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS);
if (latch == 0)
+ {
memcached_behavior_set(ptr, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 1);
+ }
rc= memcached_set(ptr, key, key_length,
(memcached_result_value(&ptr->result)),
(memcached_result_flags(&ptr->result)));
if (rc == MEMCACHED_BUFFERED && latch == 0)
+ {
memcached_behavior_set(ptr, MEMCACHED_BEHAVIOR_BUFFER_REQUESTS, 0);
+ }
}
else
{
*error= rc;
*value_length= memcached_result_length(&ptr->result);
*flags= memcached_result_flags(&ptr->result);
- return memcached_string_c_copy(&ptr->result.value);
+ return memcached_string_take_value(&ptr->result.value);
}
}
}
+ assert_msg(ptr->query_id == query_id +1, "Programmer error, the query_id was not incremented.");
return NULL;
}
- (void)memcached_fetch(ptr, NULL, NULL,
- &dummy_length, &dummy_flags,
- &dummy_error);
- WATCHPOINT_ASSERT(dummy_length == 0);
-
return value;
}
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)))
+ if (memcached_failed(rc= initialize_query(ptr, true)))
{
return rc;
}
- unlikely (ptr->flags.use_udp)
- return MEMCACHED_NOT_SUPPORTED;
+ if (memcached_is_udp(ptr))
+ {
+ return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT);
+ }
LIBMEMCACHED_MEMCACHED_MGET_START();
if (number_of_keys == 0)
- return MEMCACHED_NOTFOUND;
-
- 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_NOTFOUND, MEMCACHED_AT, memcached_literal_param("number_of_keys was zero"));
}
- if (group_key && group_key_length)
+ if (memcached_failed(memcached_key_test(*ptr, keys, key_length, number_of_keys)))
{
- 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_last_error(ptr);
+ }
+ bool is_group_key_set= false;
+ if (group_key and group_key_length)
+ {
master_server_key= memcached_generate_hash_with_redistribution(ptr, group_key, group_key_length);
is_group_key_set= true;
}
*/
for (uint32_t x= 0; x < memcached_server_count(ptr); x++)
{
- memcached_server_write_instance_st instance=
- memcached_server_instance_fetch(ptr, x);
+ memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
if (memcached_server_response_count(instance))
{
char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
if (ptr->flags.no_block)
- (void)memcached_io_write(instance, NULL, 0, true);
+ {
+ memcached_io_write(instance);
+ }
while(memcached_server_response_count(instance))
+ {
(void)memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, &ptr->result);
+ }
}
}
- if (ptr->flags.binary_protocol)
+ if (memcached_is_binary(ptr))
{
return binary_mget_by_key(ptr, master_server_key, is_group_key_set, keys,
key_length, number_of_keys, mget_mode);
instance= memcached_server_instance_fetch(ptr, server_key);
- struct libmemcached_io_vector_st vector[]=
+ libmemcached_io_vector_st vector[]=
{
- { get_command_length, get_command },
- { memcached_array_size(ptr->prefix_key), memcached_array_string(ptr->prefix_key) },
- { key_length[x], keys[x] },
- { 1, " " }
+ { get_command, get_command_length },
+ { memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace) },
+ { keys[x], key_length[x] },
+ { memcached_literal_param(" ") }
};
{
rc= memcached_connect(instance);
- if (rc != MEMCACHED_SUCCESS)
+ if (memcached_failed(rc))
{
+ memcached_set_error(*instance, rc, MEMCACHED_AT);
continue;
}
hosts_connected++;
{
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);
}
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,
void *context,
unsigned int number_of_callbacks)
{
- if ((ptr->flags.binary_protocol) == 0)
- return MEMCACHED_NOT_SUPPORTED;
-
memcached_return_t rc;
+ if (memcached_failed(rc= initialize_query(ptr, false)))
+ {
+ return rc;
+ }
+
+ if (memcached_is_udp(ptr))
+ {
+ return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT);
+ }
+
+ if (memcached_is_binary(ptr) == false)
+ {
+ return memcached_set_error(*ptr, MEMCACHED_NOT_SUPPORTED, MEMCACHED_AT,
+ memcached_literal_param("ASCII protocol is not supported for memcached_mget_execute_by_key()"));
+ }
+
memcached_callback_st *original_callbacks= ptr->callbacks;
memcached_callback_st cb= {
callback,
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)
{
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;
+ }
}
protocol_binary_request_getk request= { }; //= {.bytes= {0}};
request.message.header.request.magic= PROTOCOL_BINARY_REQ;
if (mget_mode)
+ {
request.message.header.request.opcode= PROTOCOL_BINARY_CMD_GETKQ;
+ }
else
+ {
request.message.header.request.opcode= PROTOCOL_BINARY_CMD_GETK;
+ }
memcached_return_t vk;
vk= memcached_validate_key_length(key_length[x],
ptr->flags.binary_protocol);
- unlikely (vk != MEMCACHED_SUCCESS)
+ if (vk != MEMCACHED_SUCCESS)
{
if (x > 0)
{
return vk;
}
- request.message.header.request.keylen= htons((uint16_t)(key_length[x] + memcached_array_size(ptr->prefix_key)));
+ request.message.header.request.keylen= htons((uint16_t)(key_length[x] + memcached_array_size(ptr->_namespace)));
request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
- request.message.header.request.bodylen= htonl((uint32_t)( key_length[x] + memcached_array_size(ptr->prefix_key)));
+ request.message.header.request.bodylen= htonl((uint32_t)( key_length[x] + memcached_array_size(ptr->_namespace)));
- struct libmemcached_io_vector_st vector[]=
+ libmemcached_io_vector_st vector[]=
{
- { sizeof(request.bytes), request.bytes },
- { memcached_array_size(ptr->prefix_key), memcached_array_string(ptr->prefix_key) },
- { key_length[x], keys[x] }
+ { request.bytes, sizeof(request.bytes) },
+ { memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace) },
+ { keys[x], key_length[x] }
};
if (memcached_io_writev(instance, vector, 3, flush) == -1)
for (uint32_t x= 0; x < memcached_server_count(ptr); ++x)
{
- memcached_server_write_instance_st instance=
- memcached_server_instance_fetch(ptr, x);
+ memcached_server_write_instance_st instance= memcached_server_instance_fetch(ptr, x);
if (memcached_server_response_count(instance))
{
- if (memcached_io_write(instance, NULL, 0, true) == -1)
+ if (memcached_io_write(instance) == false)
{
memcached_server_response_reset(instance);
memcached_io_reset(instance);
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 */
server += start;
while (server >= memcached_server_count(ptr))
+ {
server -= memcached_server_count(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;
protocol_binary_request_getk request= {};
request.message.header.request.magic= PROTOCOL_BINARY_REQ;
request.message.header.request.opcode= PROTOCOL_BINARY_CMD_GETK;
- request.message.header.request.keylen= htons((uint16_t)(key_length[x] + memcached_array_size(ptr->prefix_key)));
+ request.message.header.request.keylen= htons((uint16_t)(key_length[x] + memcached_array_size(ptr->_namespace)));
request.message.header.request.datatype= PROTOCOL_BINARY_RAW_BYTES;
- request.message.header.request.bodylen= htonl((uint32_t)(key_length[x] + memcached_array_size(ptr->prefix_key)));
+ request.message.header.request.bodylen= htonl((uint32_t)(key_length[x] + memcached_array_size(ptr->_namespace)));
/*
* We need to disable buffering to actually know that the request was
* that we might have processed some of the responses etc. For now,
* just make sure we work _correctly_
*/
- struct libmemcached_io_vector_st vector[]=
+ libmemcached_io_vector_st vector[]=
{
- { sizeof(request.bytes), request.bytes },
- { memcached_array_size(ptr->prefix_key), memcached_array_string(ptr->prefix_key) },
- { key_length[x], keys[x] }
+ { request.bytes, sizeof(request.bytes) },
+ { memcached_array_string(ptr->_namespace), memcached_array_size(ptr->_namespace) },
+ { keys[x], key_length[x] }
};
if (memcached_io_writev(instance, vector, 3, true) == -1)
}
if (success)
+ {
break;
+ }
}
return rc;
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<uint32_t*>(libmemcached_malloc(ptr, sizeof(uint32_t) * number_of_keys));
- dead_servers= static_cast<bool*>(libmemcached_calloc(ptr, memcached_server_count(ptr), sizeof(bool)));
+ uint32_t* hash= libmemcached_xvalloc(ptr, number_of_keys, uint32_t);
+ bool* dead_servers= libmemcached_xcalloc(ptr, memcached_server_count(ptr), 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;
}