*
*/
-#include "libmemcached/common.h"
+#include <libmemcached/common.h>
struct memcached_array_st
{
- memcached_st *root;
+ struct memcached_st *root;
size_t size;
char c_str[];
};
-memcached_array_st *memcached_array_clone(memcached_st *memc, const memcached_array_st *original)
+
+memcached_array_st *memcached_array_clone(struct memcached_st *memc, const memcached_array_st *original)
{
if (! original)
return NULL;
return memcached_strcpy(memc, original->c_str, original->size);
}
-memcached_array_st *memcached_strcpy(memcached_st *memc, const char *str, size_t str_length)
+memcached_array_st *memcached_strcpy(struct memcached_st *memc, const char *str, size_t str_length)
{
memcached_array_st *array= (struct memcached_array_st *)libmemcached_malloc(memc, sizeof(struct memcached_array_st) +str_length +1);
uint64_t offset,
uint64_t *value)
{
- memcached_return_t rc;
char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
uint32_t server_key;
memcached_server_write_instance_st instance;
(int)key_length, key,
offset, no_reply ? " noreply" : "");
if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE || send_length < 0)
- return memcached_set_error(ptr, MEMCACHED_WRITE_FAILURE);
+ return memcached_set_error_string(ptr, MEMCACHED_MEMORY_ALLOCATION_FAILURE, memcached_literal_param("snprintf(MEMCACHED_DEFAULT_COMMAND_SIZE)"));
- rc= memcached_do(instance, buffer, (size_t)send_length, true);
- if (no_reply || rc != MEMCACHED_SUCCESS)
+ memcached_return_t rc= memcached_do(instance, buffer, (size_t)send_length, true);
+ if (no_reply or memcached_failed(rc))
return rc;
rc= memcached_response(instance, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
use it. We still called memcached_response() though since it
worked its magic for non-blocking IO.
*/
- if (! strncmp(buffer, "ERROR\r\n", 7))
+ if (not strncmp(buffer, memcached_literal_param("ERROR\r\n")))
{
*value= 0;
rc= MEMCACHED_PROTOCOL_ERROR;
}
- else if (! strncmp(buffer, "CLIENT_ERROR\r\n", 14))
+ else if (not strncmp(buffer, memcached_literal_param("CLIENT_ERROR\r\n")))
{
*value= 0;
rc= MEMCACHED_PROTOCOL_ERROR;
}
- else if (!strncmp(buffer, "NOT_FOUND\r\n", 11))
+ else if (not strncmp(buffer, memcached_literal_param("NOT_FOUND\r\n")))
{
*value= 0;
rc= MEMCACHED_NOTFOUND;
rc= MEMCACHED_SUCCESS;
}
- return rc;
+ return memcached_set_error(*instance, rc);
}
static memcached_return_t binary_incr_decr(memcached_st *ptr, uint8_t cmd,
--- /dev/null
+/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ *
+ * Libmemcached library
+ *
+ * Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *
+ * 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.
+ *
+ */
+
+#pragma once
+
+struct memcached_string_t {
+ size_t size;
+ const char *c_str;
+};
+
+#define memcached_size(X) (X).size;
+#define memcached_c_str(X) (X).c_str;
+#define memcached_string_param(X) (X).c_str, (X).size
+
+#ifdef __cplusplus
+#define memcached_string_printf(X) int((X).size), (X).c_str
+#else
+#define memcached_string_printf(X) (int)((X).size), (X).c_str
+#endif
+
case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS:
return memcached_set_error_string(ptr, MEMCACHED_DEPRECATED,
- memcached_string_with_size("MEMCACHED_BEHAVIOR_CACHE_LOOKUPS has been deprecated."));
+ memcached_literal_param("MEMCACHED_BEHAVIOR_CACHE_LOOKUPS has been deprecated."));
case MEMCACHED_BEHAVIOR_VERIFY_KEY:
if (ptr->flags.binary_protocol)
return memcached_set_error_string(ptr, MEMCACHED_INVALID_ARGUMENTS,
- memcached_string_with_size("MEMCACHED_BEHAVIOR_VERIFY_KEY if the binary protocol has been enabled."));
+ memcached_literal_param("MEMCACHED_BEHAVIOR_VERIFY_KEY if the binary protocol has been enabled."));
ptr->flags.verify_key= set_flag(data);
break;
case MEMCACHED_BEHAVIOR_SORT_HOSTS:
break;
case MEMCACHED_BEHAVIOR_USER_DATA:
return memcached_set_error_string(ptr, MEMCACHED_DEPRECATED,
- memcached_string_with_size("MEMCACHED_BEHAVIOR_USER_DATA deprecated."));
+ memcached_literal_param("MEMCACHED_BEHAVIOR_USER_DATA deprecated."));
case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY:
ptr->flags.hash_with_prefix_key= set_flag(data);
break;
case MEMCACHED_BEHAVIOR_CORK:
{
return memcached_set_error_string(ptr, MEMCACHED_DEPRECATED,
- memcached_string_with_size("MEMCACHED_BEHAVIOR_CORK is now incorporated into the driver by default."));
+ memcached_literal_param("MEMCACHED_BEHAVIOR_CORK is now incorporated into the driver by default."));
}
break;
case MEMCACHED_BEHAVIOR_LOAD_FROM_FILE:
return memcached_set_error_string(ptr, MEMCACHED_INVALID_ARGUMENTS,
- memcached_string_with_size("MEMCACHED_BEHAVIOR_LOAD_FROM_FILE can not be set with memcached_behavior_set()"));
+ memcached_literal_param("MEMCACHED_BEHAVIOR_LOAD_FROM_FILE can not be set with memcached_behavior_set()"));
case MEMCACHED_BEHAVIOR_MAX:
default:
/* Shouldn't get here */
WATCHPOINT_ASSERT(0);
return memcached_set_error_string(ptr, MEMCACHED_INVALID_ARGUMENTS,
- memcached_string_with_size("Invalid behavior passed to memcached_behavior_set()"));
+ memcached_literal_param("Invalid behavior passed to memcached_behavior_set()"));
}
return MEMCACHED_SUCCESS;
}
case MEMCACHED_BEHAVIOR_USER_DATA:
memcached_set_error_string(ptr, MEMCACHED_DEPRECATED,
- memcached_string_with_size("MEMCACHED_BEHAVIOR_USER_DATA deprecated."));
+ memcached_literal_param("MEMCACHED_BEHAVIOR_USER_DATA deprecated."));
return 0;
case MEMCACHED_BEHAVIOR_HASH_WITH_PREFIX_KEY:
return ptr->flags.hash_with_prefix_key;
}
return memcached_set_error_string(ptr, MEMCACHED_INVALID_ARGUMENTS,
- memcached_string_with_size("Invalid memcached_server_distribution_t"));
+ memcached_literal_param("Invalid memcached_server_distribution_t"));
}
return MEMCACHED_SUCCESS;
return memcached_set_error_string(ptr, MEMCACHED_INVALID_ARGUMENTS,
- memcached_string_with_size("Invalid memcached_hash_t()"));
+ memcached_literal_param("Invalid memcached_hash_t()"));
}
memcached_hash_t memcached_behavior_get_key_hash(memcached_st *ptr)
return MEMCACHED_SUCCESS;
return memcached_set_error_string(ptr, MEMCACHED_INVALID_ARGUMENTS,
- memcached_string_with_size("Invalid memcached_hash_t()"));
+ memcached_literal_param("Invalid memcached_hash_t()"));
}
memcached_hash_t memcached_behavior_get_distribution_hash(memcached_st *ptr)
#include <config.h>
+#ifdef __cplusplus
+#include <cstdio>
+#include <cstdlib>
+#include <cstring>
+#include <ctime>
+#include <ctype.h>
+#else
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <strings.h>
-#include <ctype.h>
-#include <sys/types.h>
-#include <unistd.h>
-#include <limits.h>
-#include <errno.h>
-#include <fcntl.h>
-#ifdef TIME_WITH_SYS_TIME
-# include <sys/time.h>
-# include <time.h>
-#else
-# ifdef HAVE_SYS_TIME_H
-# include <sys/time.h>
-# else
-# include <time.h>
-# endif
+#include <time.h>
#endif
+#include <errno.h>
+#include <fcntl.h>
+#include <limits.h>
+#include <sys/types.h>
+#include <unistd.h>
#include <libmemcached/memcached.h>
#include <libmemcached/watchpoint.h>
/* These are private not to be installed headers */
+#include <libmemcached/memory.h>
#include <libmemcached/io.h>
#include <libmemcached/do.h>
#include <libmemcached/internal.h>
return MEMCACHED_SUCCESS;
}
-static inline void libmemcached_free(const memcached_st *ptr, void *mem)
-{
- ptr->allocators.free(ptr, mem, ptr->allocators.context);
-}
-
-static inline void *libmemcached_malloc(const memcached_st *ptr, const size_t size)
-{
- return ptr->allocators.malloc(ptr, size, ptr->allocators.context);
-}
-
-static inline void *libmemcached_realloc(const memcached_st *ptr, void *mem, const size_t size)
-{
- return ptr->allocators.realloc(ptr, mem, size, ptr->allocators.context);
-}
-
-static inline void *libmemcached_calloc(const memcached_st *ptr, size_t nelem, size_t size)
-{
- return ptr->allocators.calloc(ptr, nelem, size, ptr->allocators.context);
-}
-
#ifdef __cplusplus
}
#endif
uint32_t counter= 5;
while (--counter)
{
- int e= getaddrinfo(server->hostname, str_port, &hints, &server->address_info);
-
- if (e == 0)
+ int errcode;
+ switch(errcode= getaddrinfo(server->hostname, str_port, &hints, &server->address_info))
{
+ case 0:
break;
- }
- else if (e == EAI_AGAIN)
- {
+ case EAI_AGAIN:
+ if (counter > 1)
+ {
#ifndef WIN32
- struct timespec dream, rem;
+ struct timespec dream, rem;
- dream.tv_nsec= 1000;
- dream.tv_sec= 0;
+ dream.tv_nsec= 1000;
+ dream.tv_sec= 0;
- nanosleep(&dream, &rem);
+ nanosleep(&dream, &rem);
#endif
- continue;
- }
- else
- {
- WATCHPOINT_STRING(server->hostname);
- WATCHPOINT_STRING(gai_strerror(e));
- return MEMCACHED_HOST_LOOKUP_FAILURE;
+ continue;
+ }
+ else
+ {
+ return memcached_set_error_string(*server, MEMCACHED_HOST_LOOKUP_FAILURE, gai_strerror(errcode), strlen(gai_strerror(errcode)));
+ }
+
+ case EAI_SYSTEM:
+ {
+ static memcached_string_t mesg= { memcached_string_make("getaddrinfo") };
+ return memcached_set_errno(*server, errno, &mesg);
+ }
+ case EAI_BADFLAGS:
+ return memcached_set_error_string(*server, MEMCACHED_HOST_LOOKUP_FAILURE, memcached_literal_param("getaddrinfo(EAI_BADFLAGS)"));
+
+ case EAI_MEMORY:
+ return memcached_set_error_string(*server, MEMCACHED_ERRNO, memcached_literal_param("getaddrinfo(EAI_MEMORY)"));
+
+ default:
+ {
+ WATCHPOINT_STRING(server->hostname);
+ WATCHPOINT_STRING(gai_strerror(e));
+ return memcached_set_error_string(*server, MEMCACHED_HOST_LOOKUP_FAILURE, gai_strerror(errcode), strlen(gai_strerror(errcode)));
+ }
}
}
return memcached_set_error_message(memc, rc, &tmp);
}
+memcached_return_t memcached_set_error_string(memcached_server_st& self, memcached_return_t rc, const char *str, size_t length)
+{
+ memcached_string_t tmp;
+ tmp.c_str= str;
+ tmp.size= length;
+ return memcached_set_error_message(self, rc, &tmp);
+}
+
memcached_return_t memcached_set_error_message(memcached_st *memc, memcached_return_t rc, memcached_string_t *str)
{
- if (rc == MEMCACHED_SUCCESS)
+ if (memcached_success(rc))
return MEMCACHED_SUCCESS;
_set(memc, str, rc);
return rc;
}
+memcached_return_t memcached_set_error_message(memcached_server_st& self, memcached_return_t rc, memcached_string_t *str)
+{
+ if (memcached_success(rc))
+ return MEMCACHED_SUCCESS;
+
+ char hostname_port_message[MAX_ERROR_LENGTH];
+ int size;
+ if (str and str->size)
+ {
+ size= snprintf(hostname_port_message, sizeof(hostname_port_message), "%.*s, host: %s:%d",
+ memcached_string_printf(*str),
+ self.hostname, int(self.port));
+ }
+ else
+ {
+ size= snprintf(hostname_port_message, sizeof(hostname_port_message), "host: %s:%d",
+ self.hostname, int(self.port));
+ }
+
+ memcached_string_t error_host= { size, hostname_port_message };
+
+ _set((memcached_st*)self.root, &error_host, rc);
+
+ return rc;
+}
+
memcached_return_t memcached_set_error(memcached_server_st& self, memcached_return_t rc)
{
- if (rc == MEMCACHED_SUCCESS)
+ if (memcached_success(rc))
return MEMCACHED_SUCCESS;
char hostname_port[NI_MAXHOST +NI_MAXSERV + sizeof("host : ")];
return rc;
}
-memcached_return_t memcached_set_errno(memcached_server_st& self, int local_errno, memcached_string_t *)
+memcached_return_t memcached_set_errno(memcached_server_st& self, int local_errno, memcached_string_t *str)
{
- char hostname_port[NI_MAXHOST +NI_MAXSERV + sizeof("host : ")];
- int size= snprintf(hostname_port, sizeof(hostname_port), "host: %s:%d", self.hostname, int(self.port));
+ char hostname_port_message[MAX_ERROR_LENGTH];
+ int size;
+ if (str and str->size)
+ {
+ size= snprintf(hostname_port_message, sizeof(hostname_port_message), "%.*s, host: %s:%d",
+ memcached_string_printf(*str),
+ self.hostname, int(self.port));
+ }
+ else
+ {
+ size= snprintf(hostname_port_message, sizeof(hostname_port_message), "host: %s:%d",
+ self.hostname, int(self.port));
+ }
- memcached_string_t error_host= { size, hostname_port };
+ memcached_string_t error_host= { size, hostname_port_message };
- self.cached_errno= local_errno;
+ self.cached_errno= local_errno; // Store in the actual server
memcached_return_t rc= MEMCACHED_ERRNO;
_set((memcached_st*)self.root, &error_host, rc, local_errno);
LIBMEMCACHED_LOCAL
memcached_return_t memcached_set_errno(memcached_server_st&, int local_errno, memcached_string_t *str);
-extern "C" {
-#endif
-
LIBMEMCACHED_LOCAL
memcached_return_t memcached_set_error_message(memcached_st *memc, memcached_return_t rc, memcached_string_t *str);
+LIBMEMCACHED_LOCAL
+ memcached_return_t memcached_set_error_message(memcached_server_st&, memcached_return_t rc, memcached_string_t *str);
+
LIBMEMCACHED_LOCAL
memcached_return_t memcached_set_error_string(memcached_st *memc, memcached_return_t rc, const char *str, size_t length);
+LIBMEMCACHED_LOCAL
+ memcached_return_t memcached_set_error_string(memcached_server_st&, memcached_return_t rc, const char *str, size_t length);
+
+extern "C" {
+#endif
+
LIBMEMCACHED_LOCAL
void memcached_error_free(memcached_st *error);
+++ /dev/null
-#include "common.h"
-
-char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length,
- size_t *value_length,
- uint32_t *flags,
- memcached_return_t *error)
-{
- memcached_result_st *result_buffer= &ptr->result;
-
- unlikely (ptr->flags.use_udp)
- {
- *error= MEMCACHED_NOT_SUPPORTED;
- return NULL;
- }
-
- result_buffer= memcached_fetch_result(ptr, result_buffer, error);
-
- if (result_buffer == NULL || *error != MEMCACHED_SUCCESS)
- {
- WATCHPOINT_ASSERT(result_buffer == NULL);
- *value_length= 0;
- return NULL;
- }
-
- *value_length= memcached_string_length(&result_buffer->value);
-
- if (key)
- {
- if (result_buffer->key_length > MEMCACHED_MAX_KEY)
- {
- *error= MEMCACHED_KEY_TOO_BIG;
- *value_length= 0;
-
- return NULL;
- }
- strncpy(key, result_buffer->item_key, result_buffer->key_length); // For the binary protocol we will cut off the key :(
- *key_length= result_buffer->key_length;
- }
-
- *flags= result_buffer->item_flags;
-
- return memcached_string_c_copy(&result_buffer->value);
-}
-
-memcached_result_st *memcached_fetch_result(memcached_st *ptr,
- memcached_result_st *result,
- memcached_return_t *error)
-{
- memcached_server_st *server;
-
- unlikely (ptr->flags.use_udp)
- {
- *error= MEMCACHED_NOT_SUPPORTED;
- return NULL;
- }
-
- if (result == NULL)
- if ((result= memcached_result_create(ptr, NULL)) == NULL)
- return NULL;
-
- while ((server= memcached_io_get_readable_server(ptr)) != NULL)
- {
- char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
- *error= memcached_response(server, buffer, sizeof(buffer), result);
-
- if (*error == MEMCACHED_SUCCESS)
- return result;
- else if (*error == MEMCACHED_END)
- memcached_server_response_reset(server);
- else if (*error != MEMCACHED_NOTFOUND)
- break;
- }
-
- /* We have completed reading data */
- if (memcached_is_allocated(result))
- {
- memcached_result_free(result);
- }
- else
- {
- memcached_string_reset(&result->value);
- }
-
- return NULL;
-}
-
-memcached_return_t memcached_fetch_execute(memcached_st *ptr,
- memcached_execute_fn *callback,
- void *context,
- uint32_t number_of_callbacks)
-{
- memcached_result_st *result= &ptr->result;
- memcached_return_t rc= MEMCACHED_FAILURE;
-
- while ((result= memcached_fetch_result(ptr, result, &rc)) != NULL)
- {
- if (rc == MEMCACHED_SUCCESS)
- {
- for (uint32_t x= 0; x < number_of_callbacks; x++)
- {
- rc= (*callback[x])(ptr, result, context);
- if (rc != MEMCACHED_SUCCESS)
- break;
- }
- }
- }
- return rc;
-}
--- /dev/null
+/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ *
+ * Libmemcached library
+ *
+ * Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ * Copyright (C) 2006-2009 Brian Aker All rights reserved.
+ *
+ * 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 <libmemcached/common.h>
+
+char *memcached_fetch(memcached_st *ptr, char *key, size_t *key_length,
+ size_t *value_length,
+ uint32_t *flags,
+ memcached_return_t *error)
+{
+ memcached_result_st *result_buffer= &ptr->result;
+ memcached_return_t unused;
+ if (not error)
+ error= &unused;
+
+
+ unlikely (ptr->flags.use_udp)
+ {
+ *error= MEMCACHED_NOT_SUPPORTED;
+ return NULL;
+ }
+
+ result_buffer= memcached_fetch_result(ptr, result_buffer, error);
+
+ if (result_buffer == NULL || *error != MEMCACHED_SUCCESS)
+ {
+ WATCHPOINT_ASSERT(result_buffer == NULL);
+ *value_length= 0;
+ return NULL;
+ }
+
+ *value_length= memcached_string_length(&result_buffer->value);
+
+ if (key)
+ {
+ if (result_buffer->key_length > MEMCACHED_MAX_KEY)
+ {
+ *error= MEMCACHED_KEY_TOO_BIG;
+ *value_length= 0;
+
+ return NULL;
+ }
+ strncpy(key, result_buffer->item_key, result_buffer->key_length); // For the binary protocol we will cut off the key :(
+ *key_length= result_buffer->key_length;
+ }
+
+ *flags= result_buffer->item_flags;
+
+ return memcached_string_c_copy(&result_buffer->value);
+}
+
+memcached_result_st *memcached_fetch_result(memcached_st *ptr,
+ memcached_result_st *result,
+ memcached_return_t *error)
+{
+ memcached_server_st *server;
+
+ unlikely (ptr->flags.use_udp)
+ {
+ *error= MEMCACHED_NOT_SUPPORTED;
+ return NULL;
+ }
+
+ if (result == NULL)
+ if ((result= memcached_result_create(ptr, NULL)) == NULL)
+ return NULL;
+
+ while ((server= memcached_io_get_readable_server(ptr)) != NULL)
+ {
+ char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
+ *error= memcached_response(server, buffer, sizeof(buffer), result);
+
+ if (*error == MEMCACHED_SUCCESS)
+ return result;
+ else if (*error == MEMCACHED_END)
+ memcached_server_response_reset(server);
+ else if (*error != MEMCACHED_NOTFOUND)
+ break;
+ }
+
+ /* We have completed reading data */
+ if (memcached_is_allocated(result))
+ {
+ memcached_result_free(result);
+ }
+ else
+ {
+ memcached_string_reset(&result->value);
+ }
+
+ return NULL;
+}
+
+memcached_return_t memcached_fetch_execute(memcached_st *ptr,
+ memcached_execute_fn *callback,
+ void *context,
+ uint32_t number_of_callbacks)
+{
+ memcached_result_st *result= &ptr->result;
+ memcached_return_t rc= MEMCACHED_FAILURE;
+
+ while ((result= memcached_fetch_result(ptr, result, &rc)) != NULL)
+ {
+ if (rc == MEMCACHED_SUCCESS)
+ {
+ for (uint32_t x= 0; x < number_of_callbacks; x++)
+ {
+ rc= (*callback[x])(ptr, result, context);
+ if (rc != MEMCACHED_SUCCESS)
+ break;
+ }
+ }
+ }
+ return rc;
+}
#include <libmemcached/common.h>
-#include <libmemcached/virtual_bucket.h>
+#include <sys/time.h>
+
+#include <libmemcached/virtual_bucket.h>
uint32_t memcached_generate_hash_value(const char *key, size_t key_length, memcached_hash_t hash_algorithm)
{
-/* LibMemcached
- * Copyright (C) 2006-2010 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-2010 Brian Aker All rights reserved.
*
- * Summary:
+ * 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 <math.h>
+#include <libmemcached/common.h>
+
+#include <cmath>
+#include <sys/time.h>
/* Protoypes (static) */
static memcached_return_t server_add(memcached_st *ptr, const char *hostname,
libmemcached/io.h \
libmemcached/is.h \
libmemcached/libmemcached_probes.h \
+ libmemcached/memory.h \
libmemcached/protocol/ascii_handler.h \
libmemcached/protocol/binary_handler.h \
libmemcached/protocol/common.h \
libmemcached/analyze.h \
libmemcached/array.h \
libmemcached/auto.h \
+ libmemcached/basic_string.h \
libmemcached/behavior.h \
libmemcached/callback.h \
libmemcached/configure.h \
libmemcached/do.cc \
libmemcached/dump.cc \
libmemcached/error.cc \
- libmemcached/fetch.c \
+ libmemcached/fetch.cc \
libmemcached/flush.cc \
libmemcached/flush_buffers.cc \
libmemcached/get.cc \
--- /dev/null
+/* vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ *
+ * Libmemcached library
+ *
+ * Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *
+ * 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.
+ *
+ */
+
+#pragma once
+
+static inline void libmemcached_free(const memcached_st *self, void *mem)
+{
+ self->allocators.free(self, mem, self->allocators.context);
+}
+
+static inline void *libmemcached_malloc(const memcached_st *self, const size_t size)
+{
+ return self->allocators.malloc(self, size, self->allocators.context);
+}
+
+static inline void *libmemcached_realloc(const memcached_st *self, void *mem, const size_t size)
+{
+ return self->allocators.realloc(self, mem, size, self->allocators.context);
+}
+
+static inline void *libmemcached_calloc(const memcached_st *self, size_t nelem, size_t size)
+{
+ return self->allocators.calloc(self, nelem, size, self->allocators.context);
+}
rc= MEMCACHED_PARSE_ERROR;
memcached_string_st *error_string= memcached_string_create(memc, NULL, 1024);
- memcached_string_append(error_string, memcached_string_with_size("Error occured while parsing: "));
+ memcached_string_append(error_string, memcached_literal_param("Error occured while parsing: "));
memcached_string_append(error_string, memcached_string_make_from_cstr(begin));
- memcached_string_append(error_string, memcached_string_with_size(" ("));
+ memcached_string_append(error_string, memcached_literal_param(" ("));
if (rc == MEMCACHED_PARSE_ERROR and error)
{
{
memcached_string_append(error_string, memcached_string_make_from_cstr(memcached_strerror(NULL, rc)));
}
- memcached_string_append(error_string, memcached_string_with_size(")"));
+ memcached_string_append(error_string, memcached_literal_param(")"));
memcached_set_error_string(memc, rc, memcached_string_value(error_string), memcached_string_length(error_string));
enum memcached_return_t {
MEMCACHED_SUCCESS,
MEMCACHED_FAILURE,
- MEMCACHED_HOST_LOOKUP_FAILURE,
- MEMCACHED_CONNECTION_FAILURE,
- MEMCACHED_CONNECTION_BIND_FAILURE,
+ MEMCACHED_HOST_LOOKUP_FAILURE, // getaddrinfo() only
+ MEMCACHED_CONNECTION_FAILURE, // DEPRECATED
+ MEMCACHED_CONNECTION_BIND_FAILURE, // DEPRECATED
MEMCACHED_WRITE_FAILURE,
MEMCACHED_READ_FAILURE,
MEMCACHED_UNKNOWN_READ_FAILURE,
-/* LibMemcached
- * Copyright (C) 2006-2010 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: interface for memcached server
- * Description: main include file 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>
void memcached_set_sasl_callbacks(memcached_st *ptr,
const sasl_callback_t *callbacks)
memcached_return_t rc;
/* SANITY CHECK: SASL can only be used with the binary protocol */
- unlikely (!server->root->flags.binary_protocol)
+ if (!server->root->flags.binary_protocol)
return MEMCACHED_FAILURE;
/* Try to get the supported mech from the server. Servers without SASL
return "SUCCESS";
case MEMCACHED_FAILURE:
return "FAILURE";
- case MEMCACHED_HOST_LOOKUP_FAILURE:
- return "HOSTNAME LOOKUP FAILURE";
- case MEMCACHED_CONNECTION_FAILURE:
+ case MEMCACHED_HOST_LOOKUP_FAILURE: // getaddrinfo only
+ return "getaddrinfo() HOSTNAME LOOKUP FAILURE";
+ case MEMCACHED_CONNECTION_FAILURE: // DEPRECATED
return "CONNECTION FAILURE";
case MEMCACHED_CONNECTION_BIND_FAILURE:
return "CONNECTION BIND FAILURE";
#pragma once
+#include <libmemcached/basic_string.h>
+
/**
Strings are always under our control so we make some assumptions
about them.
} options;
};
-struct memcached_string_t {
- size_t size;
- const char *c_str;
-};
-
-#define memcached_size(X) (X).size;
-#define memcached_c_str(X) (X).c_str;
-#define memcached_string_param(X) (X).c_str, (X).size
-
#ifdef BUILDING_LIBMEMCACHED
#ifdef __cplusplus
#endif
#ifdef __cplusplus
-#define memcached_string_with_size(X) (X), (static_cast<size_t>((sizeof(X) - 1)))
+#define memcached_literal_param(X) (X), (static_cast<size_t>((sizeof(X) - 1)))
#define memcached_string_make(X) (static_cast<size_t>((sizeof(X) - 1))), (X)
#else
-#define memcached_string_with_size(X) (X), ((size_t)((sizeof(X) - 1)))
+#define memcached_literal_param(X) (X), ((size_t)((sizeof(X) - 1)))
#define memcached_string_make(X) (((size_t)((sizeof(X) - 1))), (X)
#endif
-/* 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: Types 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.
*
*/
+
#pragma once
#ifndef __LIBMEMCACHED_TYPES_H__
#define __LIBMEMCACHED_TYPES_H__
*
*/
-#include <libmemcached/common.h>
+#include <config.h>
+#include <libmemcached/memcached.h>
#include <libmemcached/virtual_bucket.h>
struct bucket_t {
#include <libtest/test.h>
#include <tests/error_conditions.h>
-#ifdef __cplusplus
-extern "C" {
-#endif
-
-test_return_t memcached_increment_MEMCACHED_NO_SERVERS(memcached_st *junk)
+test_return_t memcached_increment_MEMCACHED_NO_SERVERS(memcached_st *)
{
- (void)junk;
memcached_st *memc_ptr;
memc_ptr= memcached_create(NULL);
test_true(memc_ptr);
- memcached_increment(memc_ptr, memcached_string_with_size("dead key"), 1, NULL);
+ memcached_increment(memc_ptr, memcached_literal_param("dead key"), 1, NULL);
test_true(memcached_last_error(memc_ptr) == MEMCACHED_NO_SERVERS);
- memcached_increment(memc_ptr, memcached_string_with_size("dead key"), 1, NULL);
+ memcached_increment(memc_ptr, memcached_literal_param("dead key"), 1, NULL);
test_true(memcached_last_error(memc_ptr) == MEMCACHED_NO_SERVERS);
memcached_free(memc_ptr);
return TEST_SUCCESS;
}
-
-#ifdef __cplusplus
-}
-#endif
/*
- Sample test application.
+ Test cases
*/
-#include "config.h"
+#define BUILDING_LIBMEMCACHED
+// !NEVER use common.h, always use memcached.h in your own apps
+#include <libmemcached/common.h>
#include <stdint.h>
#include <cassert>
-#include <cstdio>
-#include <cstdlib>
-#include <cstring>
-#include <ctime>
#include <memory>
#include <signal.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
-#include "libmemcached/common.h"
-
#include <libtest/server.h>
#include "clients/generator.h"
static test_return_t error_test(memcached_st *memc)
{
- uint32_t values[] = { 851992627U, 2337886783U, 3196981036U, 4001849190U,
+ uint32_t values[] = { 851992627U, 2337886783U, 646418395U, 4001849190U,
982370485U, 1263635348U, 4242906218U, 3829656100U,
1891735253U, 334139633U, 2257084983U, 3088286104U,
13199785U, 2542027183U, 1097051614U, 199566778U,
static test_return_t set_test(memcached_st *memc)
{
- memcached_return_t rc;
- const char *key= "foo";
- const char *value= "when we sanitize";
-
- rc= memcached_set(memc, key, strlen(key),
- value, strlen(value),
- (time_t)0, (uint32_t)0);
+ memcached_return_t rc= memcached_set(memc,
+ memcached_literal_param("foo"),
+ memcached_literal_param("when we sanitize"),
+ time_t(0), (uint32_t)0);
test_true(rc == MEMCACHED_SUCCESS || rc == MEMCACHED_BUFFERED);
return TEST_SUCCESS;
test_return_t string_alloc_append_multiple(memcached_st *memc)
{
memcached_string_st *error_string= memcached_string_create(memc, NULL, 1024);
- memcached_string_append(error_string, memcached_string_with_size("Error occured while parsing: "));
+ memcached_string_append(error_string, memcached_literal_param("Error occured while parsing: "));
memcached_string_append(error_string, memcached_string_make_from_cstr("jog the strlen() method"));
- memcached_string_append(error_string, memcached_string_with_size(" ("));
+ memcached_string_append(error_string, memcached_literal_param(" ("));
memcached_string_append(error_string, memcached_string_make_from_cstr(memcached_strerror(NULL, MEMCACHED_SUCCESS)));
- memcached_string_append(error_string, memcached_string_with_size(")"));
+ memcached_string_append(error_string, memcached_literal_param(")"));
memcached_string_free(error_string);