delete/set operations.
char hostname[MEMCACHED_MAX_HOST_LENGTH];
unsigned int port;
int fd;
- unsigned int stack_responses;
unsigned int cursor_active;
char write_buffer[MEMCACHED_MAX_BUFFER];
size_t write_buffer_offset;
int send_size;
int recv_size;
int32_t poll_timeout;
- memcached_string_st result_buffer;
+ memcached_result_st result;
memcached_hash hash;
memcached_server_distribution distribution;
unsigned int wheel[MEMCACHED_WHEEL_SIZE];
#define memcached_server_name(A,B) (B).hostname
#define memcached_server_port(A,B) (B).port
#define memcached_server_list(A) (A)->hosts
-#define memcached_server_response_count(A,B) (A)->hosts[B].stack_responses
+#define memcached_server_response_count(A,B) (A)->hosts[B].cursor_active
memcached_return memcached_server_add_udp(memcached_st *ptr,
char *hostname,
void memcached_result_free(memcached_result_st *result);
memcached_result_st *memcached_result_create(memcached_st *ptr,
memcached_result_st *result);
-#define memcached_result_key_value(A) A->key
-#define memcached_result_key_length(A) A->key_length
+#define memcached_result_key_value(A) (A)->key
+#define memcached_result_key_length(A) (A)->key_length
#ifdef FIX
-#define memcached_result_value(A) memcached_string_value(A->value)
-#define memcached_result_length(A) memcached_string_length(A->value)
+#define memcached_result_value(A) memcached_string_value((A)->value)
+#define memcached_result_length(A) memcached_string_length((A)->value)
#else
char *memcached_result_value(memcached_result_st *ptr);
size_t memcached_result_length(memcached_result_st *ptr);
#endif
-#define memcached_result_flags(A) A->flags
-#define memcached_result_cas(A) A->cas
+#define memcached_result_flags(A) (A)->flags
+#define memcached_result_cas(A) (A)->cas
#ifndef __WATCHPOINT_H__
memcached_return memcached_connect(memcached_st *ptr, unsigned int server_key);
memcached_return memcached_response(memcached_st *ptr,
char *buffer, size_t buffer_length,
+ memcached_result_st *result,
unsigned int server_key);
unsigned int memcached_generate_hash(memcached_st *ptr, char *key, size_t key_length);
void memcached_quit_server(memcached_st *ptr, unsigned int server_key, uint8_t io_death);
-#define memcached_server_response_increment(A,B) A->hosts[B].stack_responses++
-#define memcached_server_response_decrement(A,B) A->hosts[B].stack_responses--
+#define memcached_server_response_increment(A,B) A->hosts[B].cursor_active++
+#define memcached_server_response_decrement(A,B) A->hosts[B].cursor_active--
+#define memcached_server_response_reset(A,B) A->hosts[B].cursor_active=0
/* String Struct */
-#define memcached_string_length(A) (size_t)(A->end - A->string)
-#define memcached_string_set_length(A, B) A->end= A->string + B
-#define memcached_string_size(A) A->current_size
-#define memcached_string_value(A) A->string
+#define memcached_string_length(A) (size_t)((A)->end - (A)->string)
+#define memcached_string_set_length(A, B) (A)->end= (A)->string + B
+#define memcached_string_size(A) (A)->current_size
+#define memcached_string_value(A) (A)->string
memcached_string_st *memcached_string_create(memcached_st *ptr,
memcached_string_st *string,
memcached_return memcached_do(memcached_st *ptr, unsigned int server_key, char *commmand,
size_t command_length, char with_flush);
memcached_return memcached_version(memcached_st *ptr);
-memcached_return memcached_finish_server(memcached_st *ptr, unsigned int server_key);
-void memcached_finish(memcached_st *ptr);
-
+memcached_return value_fetch(memcached_st *ptr,
+ char *buffer,
+ memcached_result_st *result,
+ unsigned int server_key);
#endif /* __COMMON_H__ */
memcached_st *memcached_create(memcached_st *ptr)
{
- memcached_string_st *string_ptr;
+ memcached_result_st *result_ptr;
if (!ptr)
{
ptr= (memcached_st *)malloc(sizeof(memcached_st));
{
memset(ptr, 0, sizeof(memcached_st));
}
- string_ptr= memcached_string_create(ptr, &ptr->result_buffer, 0);
- WATCHPOINT_ASSERT(string_ptr);
+ result_ptr= memcached_result_create(ptr, &ptr->result);
+ WATCHPOINT_ASSERT(result_ptr);
ptr->poll_timeout= MEMCACHED_DEFAULT_TIMEOUT;
ptr->distribution= MEMCACHED_DISTRIBUTION_MODULA;
/* If we have anything open, lets close it now */
memcached_quit(ptr);
memcached_server_list_free(ptr->hosts);
- memcached_string_free(&ptr->result_buffer);
+ memcached_result_free(&ptr->result);
if (ptr->is_allocated == MEMCACHED_ALLOCATED)
free(ptr);
if (rc != MEMCACHED_SUCCESS)
return rc;
- rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
+ rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL, server_key);
/*
So why recheck responce? Because the protocol is brain dead :)
if (ptr->hosts[server_key].fd == -1)
{
/* Old connection junk still is in the structure */
- WATCHPOINT_ASSERT(ptr->hosts[server_key].stack_responses == 0);
+ WATCHPOINT_ASSERT(ptr->hosts[server_key].cursor_active == 0);
/*
If we have not allocated the hosts object.
if (ptr->hosts[server_key].fd == -1)
{
/* Old connection junk still is in the structure */
- WATCHPOINT_ASSERT(ptr->hosts[server_key].stack_responses == 0);
+ WATCHPOINT_ASSERT(ptr->hosts[server_key].cursor_active == 0);
struct addrinfo *use;
if (ptr->hosts[server_key].sockaddr_inited == MEMCACHED_NOT_ALLOCATED ||
ptr->connected++;
}
- WATCHPOINT_ASSERT(ptr->hosts[server_key].stack_responses == 0);
+ WATCHPOINT_ASSERT(ptr->hosts[server_key].cursor_active == 0);
}
return MEMCACHED_SUCCESS;
}
else
{
- rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
+ rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL, server_key);
if (rc == MEMCACHED_DELETED)
rc= MEMCACHED_SUCCESS;
}
WATCHPOINT_ASSERT(command_length);
WATCHPOINT_ASSERT(command);
- if (ptr->hosts[server_key].cursor_active)
- (void)memcached_finish_server(ptr, server_key);
-
if ((rc= memcached_connect(ptr, server_key)) != MEMCACHED_SUCCESS)
return rc;
#include "common.h"
#include "memcached_io.h"
-static memcached_return memcached_value_fetch(memcached_st *ptr, char *key, size_t *key_length,
- memcached_string_st *value,
- uint32_t *flags,
- uint64_t *cas,
- unsigned int server_key)
+memcached_return value_fetch(memcached_st *ptr,
+ char *buffer,
+ memcached_result_st *result,
+ unsigned int server_key)
{
memcached_return rc;
- char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
char *string_ptr;
char *end_ptr;
+ char *next_ptr;
+ size_t value_length;
end_ptr= buffer + MEMCACHED_DEFAULT_COMMAND_SIZE;
- if (flags)
- *flags= 0;
+ result->key_length= 0;
+ result->flags= 0;
+ memcached_string_reset(&result->value);
- memcached_string_reset(value);
+ string_ptr= buffer;
+ string_ptr+= 6; /* "VALUE " */
- rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
- if (rc == MEMCACHED_SUCCESS)
+ /* We load the key */
{
- char *next_ptr;
- size_t value_length;
+ char *key;
- string_ptr= buffer;
- string_ptr+= 6; /* "VALUE " */
+ key= result->key;
+ result->key_length= 0;
- /* We load the key */
- if (key)
+ for (; isgraph(*string_ptr); string_ptr++)
{
- *key_length= 0;
-
- for (; isgraph(*string_ptr); string_ptr++)
- {
- *key= *string_ptr;
- key++;
- (*key_length)++;
- }
+ *key= *string_ptr;
+ key++;
+ result->key_length++;
}
- else /* Skip characters */
- for (; isgraph(*string_ptr); string_ptr++);
+ }
- if (end_ptr == string_ptr)
- goto read_error;
+ if (end_ptr == string_ptr)
+ goto read_error;
- /* Flags fetch move past space */
- string_ptr++;
- if (end_ptr == string_ptr)
- goto read_error;
- for (next_ptr= string_ptr; isdigit(*string_ptr); string_ptr++);
- if (flags)
- *flags= (uint32_t)strtol(next_ptr, &string_ptr, 10);
+ /* Flags fetch move past space */
+ string_ptr++;
+ if (end_ptr == string_ptr)
+ goto read_error;
+ for (next_ptr= string_ptr; isdigit(*string_ptr); string_ptr++);
+ result->flags= (uint32_t)strtol(next_ptr, &string_ptr, 10);
- if (end_ptr == string_ptr)
- goto read_error;
+ if (end_ptr == string_ptr)
+ goto read_error;
- /* Length fetch move past space*/
- string_ptr++;
- if (end_ptr == string_ptr)
- goto read_error;
+ /* Length fetch move past space*/
+ string_ptr++;
+ if (end_ptr == string_ptr)
+ goto read_error;
- for (next_ptr= string_ptr; isdigit(*string_ptr); string_ptr++);
- value_length= (size_t)strtoll(next_ptr, &string_ptr, 10);
+ for (next_ptr= string_ptr; isdigit(*string_ptr); string_ptr++);
+ value_length= (size_t)strtoll(next_ptr, &string_ptr, 10);
- if (end_ptr == string_ptr)
- goto read_error;
+ if (end_ptr == string_ptr)
+ goto read_error;
- /* Skip spaces */
- if (*string_ptr == '\r')
- {
- /* Skip past the \r\n */
- string_ptr+= 2;
- }
- else
- {
- string_ptr++;
- for (next_ptr= string_ptr; isdigit(*string_ptr); string_ptr++);
- if (cas)
- *cas= (size_t)strtoll(next_ptr, &string_ptr, 10);
- }
-
- if (end_ptr < string_ptr)
- goto read_error;
+ /* Skip spaces */
+ if (*string_ptr == '\r')
+ {
+ /* Skip past the \r\n */
+ string_ptr+= 2;
+ result->cas= 0;
+ }
+ else
+ {
+ string_ptr++;
+ for (next_ptr= string_ptr; isdigit(*string_ptr); string_ptr++);
+ result->cas= (size_t)strtoll(next_ptr, &string_ptr, 10);
+ }
- if (value_length)
- {
- size_t read_length;
- size_t to_read;
- char *value_ptr;
+ if (end_ptr < string_ptr)
+ goto read_error;
- /* We add two bytes so that we can walk the \r\n */
- rc= memcached_string_check(value, value_length+2);
- if (rc != MEMCACHED_SUCCESS)
- {
- value_length= 0;
- return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
- }
+ if (value_length)
+ {
+ size_t read_length;
+ size_t to_read;
+ char *value_ptr;
- value_ptr= memcached_string_value(value);
- read_length= 0;
- /*
- We read the \r\n into the string since not doing so is more
- cycles then the waster of memory to do so.
+ /* We add two bytes so that we can walk the \r\n */
+ rc= memcached_string_check(&result->value, value_length+2);
+ if (rc != MEMCACHED_SUCCESS)
+ {
+ value_length= 0;
+ return MEMCACHED_MEMORY_ALLOCATION_FAILURE;
+ }
- We are null terminating through, which will most likely make
- some people lazy about using the return length.
- */
- to_read= (value_length) + 2;
+ value_ptr= memcached_string_value(&result->value);
+ read_length= 0;
+ /*
+ We read the \r\n into the string since not doing so is more
+ cycles then the waster of memory to do so.
- read_length= memcached_io_read(ptr, server_key,
- value_ptr, to_read);
+ We are null terminating through, which will most likely make
+ some people lazy about using the return length.
+ */
+ to_read= (value_length) + 2;
- if (read_length != (size_t)(value_length + 2))
- {
- goto read_error;
- }
+ read_length= memcached_io_read(ptr, server_key,
+ value_ptr, to_read);
- /* This next bit blows the API, but this is internal....*/
- {
- char *char_ptr;
- char_ptr= memcached_string_value(value);;
- char_ptr[value_length]= 0;
- char_ptr[value_length + 1]= 0;
- memcached_string_set_length(value, value_length);
- }
+ if (read_length != (size_t)(value_length + 2))
+ {
+ goto read_error;
+ }
- return MEMCACHED_SUCCESS;
+ /* This next bit blows the API, but this is internal....*/
+ {
+ char *char_ptr;
+ char_ptr= memcached_string_value(&result->value);;
+ char_ptr[value_length]= 0;
+ char_ptr[value_length + 1]= 0;
+ memcached_string_set_length(&result->value, value_length);
}
+
+ return MEMCACHED_SUCCESS;
}
return rc;
uint32_t *flags,
memcached_return *error)
{
- memcached_string_st *result_buffer;
- result_buffer= &ptr->result_buffer;
-
- if (ptr->flags & MEM_NO_BLOCK)
- memcached_io_preread(ptr);
+ memcached_result_st *result_buffer= &ptr->result;
while (ptr->cursor_server < ptr->number_of_hosts)
{
- if (!ptr->hosts[ptr->cursor_server].cursor_active)
+ char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
+
+ if (memcached_server_response_count(ptr, ptr->cursor_server) == 0)
{
ptr->cursor_server++;
continue;
}
- *error = memcached_value_fetch(ptr, key, key_length, result_buffer,
- flags, NULL, ptr->cursor_server);
- *value_length= memcached_string_length(result_buffer);
-
+ *error= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, result_buffer, ptr->cursor_server);
+
if (*error == MEMCACHED_END) /* END means that we move on to the next */
{
- ptr->hosts[ptr->cursor_server].cursor_active= 0;
+ memcached_server_response_reset(ptr, ptr->cursor_server);
ptr->cursor_server++;
continue;
}
else if (*error == MEMCACHED_SUCCESS)
- return memcached_string_c_copy(result_buffer);
+ {
+ *value_length= memcached_string_length(&result_buffer->value);
+
+ if (key)
+ {
+ strncpy(key, result_buffer->key, result_buffer->key_length);
+ *key_length= result_buffer->key_length;
+ }
+ *flags= result_buffer->flags;
+
+ return memcached_string_c_copy(&result_buffer->value);
+ }
else
+ {
+ *value_length= 0;
return NULL;
+ }
}
ptr->cursor_server= 0;
while (ptr->cursor_server < ptr->number_of_hosts)
{
- if (!ptr->hosts[ptr->cursor_server].cursor_active)
+ char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
+
+ if (memcached_server_response_count(ptr, ptr->cursor_server) == 0)
{
ptr->cursor_server++;
continue;
}
- result->cas= 0; /* We do this so we do not send in any junk */
- *error= memcached_value_fetch(ptr, result->key, &result->key_length,
- &result->value,
- &result->flags,
- &result->cas,
- ptr->cursor_server);
+ *error= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, result, ptr->cursor_server);
if (*error == MEMCACHED_END) /* END means that we move on to the next */
{
- ptr->hosts[ptr->cursor_server].cursor_active= 0;
+ memcached_server_response_reset(ptr, ptr->cursor_server);
ptr->cursor_server++;
continue;
}
ptr->cursor_server= 0;
return NULL;
}
-
-memcached_return memcached_finish_server(memcached_st *ptr, unsigned int server_key)
-{
- memcached_return rc;
- memcached_string_st *result_buffer;
-
- result_buffer= &ptr->result_buffer;
-
- rc= MEMCACHED_SUCCESS;
- while (rc == MEMCACHED_SUCCESS)
- {
- rc= memcached_value_fetch(ptr, NULL, NULL, result_buffer,
- NULL, NULL, server_key);
- }
- ptr->hosts[server_key].cursor_active= 0;
-
- return rc;
-}
-
-void memcached_finish(memcached_st *ptr)
-{
- unsigned int x;
-
- for (x= 0; x < ptr->number_of_hosts; x++)
- {
- if (ptr->hosts[x].cursor_active)
- (void)memcached_finish_server(ptr, x);
- }
-
- ptr->cursor_server= 0;
-}
rc= memcached_do(ptr, x, buffer, send_length, 1);
if (rc == MEMCACHED_SUCCESS)
- (void)memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, x);
+ (void)memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL, x);
}
LIBMEMCACHED_MEMCACHED_FLUSH_END();
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,
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);
+ (void)memcached_fetch(ptr, NULL, NULL,
+ &dummy_length, &dummy_flags,
+ &dummy_error);
return value;
}
get_command_length= 5;
}
- memcached_finish(ptr);
-
if (master_key && master_key_length)
master_server_key= memcached_generate_hash(ptr, master_key, master_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.
+
+ It might be optimum to bounce the connection if count > some number.
+ */
+ for (x= 0; x < ptr->number_of_hosts; x++)
+ {
+ if (memcached_server_response_count(ptr, x))
+ {
+ char buffer[MEMCACHED_DEFAULT_COMMAND_SIZE];
+
+ if (ptr->flags & MEM_NO_BLOCK)
+ (void)memcached_io_write(ptr, x, NULL, 0, 1);
+
+ while(memcached_server_response_count(ptr, x))
+ (void)memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, &ptr->result, x);
+ }
+ }
+
/*
If a server fails we warn about errors and start all over with sending keys
to the server.
else
server_key= memcached_generate_hash(ptr, keys[x], key_length[x]);
- if (ptr->hosts[server_key].cursor_active == 0)
+ if (memcached_server_response_count(ptr, server_key) == 0)
{
rc= memcached_connect(ptr, server_key);
rc= MEMCACHED_SOME_ERRORS;
continue;
}
- ptr->hosts[server_key].cursor_active++;
+ WATCHPOINT_ASSERT(ptr->hosts[server_key].cursor_active == 0);
+ memcached_server_response_increment(ptr, server_key);
+ WATCHPOINT_ASSERT(ptr->hosts[server_key].cursor_active == 1);
}
if ((memcached_io_write(ptr, server_key, keys[x], key_length[x], 0)) == -1)
{
- ptr->hosts[server_key].cursor_active= 0;
+ memcached_server_response_reset(ptr, server_key);
rc= MEMCACHED_SOME_ERRORS;
continue;
}
if ((memcached_io_write(ptr, server_key, " ", 1, 0)) == -1)
{
- ptr->hosts[server_key].cursor_active= 0;
+ memcached_server_response_reset(ptr, server_key);
rc= MEMCACHED_SOME_ERRORS;
continue;
}
*/
for (x= 0; x < ptr->number_of_hosts; x++)
{
- if (ptr->hosts[x].cursor_active)
+ if (memcached_server_response_count(ptr, x))
{
- /* We need to doo something about non-connnected hosts in the future */
+ /* We need to do something about non-connnected hosts in the future */
if ((memcached_io_write(ptr, x, "\r\n", 2, 1)) == -1)
{
rc= MEMCACHED_SOME_ERRORS;
}
- memcached_server_response_increment(ptr, x);
}
}
for (x= 0; x < ptr->number_of_hosts; x++)
{
- if (ptr->hosts[x].cursor_active &&
+ if (memcached_server_response_count(ptr, x) &&
ptr->hosts[x].read_data_length < MEMCACHED_MAX_BUFFER )
{
size_t data_read;
}
ptr->hosts[server_key].fd= -1;
- ptr->hosts[server_key].stack_responses= 0;
- ptr->hosts[server_key].cursor_active= 0;
ptr->hosts[server_key].write_buffer_offset= 0;
ptr->hosts[server_key].read_buffer_length= 0;
ptr->hosts[server_key].read_ptr= ptr->hosts[server_key].read_buffer;
ptr->hosts[server_key].write_ptr= ptr->hosts[server_key].write_buffer;
+ memcached_server_response_reset(ptr, server_key);
}
ptr->connected--;
void memcached_quit(memcached_st *ptr)
{
unsigned int x;
+
if (ptr->hosts == NULL ||
ptr->number_of_hosts == 0)
return;
memcached_return memcached_response(memcached_st *ptr,
char *buffer, size_t buffer_length,
+ memcached_result_st *result,
unsigned int server_key)
{
unsigned int x;
send_length= 0;
+ /* We may have old commands in the buffer not set, first purge */
+ if (ptr->flags & MEM_NO_BLOCK)
+ (void)memcached_io_write(ptr, server_key, NULL, 0, 1);
+
max_messages= memcached_server_response_count(ptr, server_key);
for (x= 0; x < max_messages; x++)
{
case 'V': /* VALUE || VERSION */
if (buffer[1] == 'A') /* VALUE */
{
+ memcached_return rc;
+
/* We add back in one because we will need to search for END */
memcached_server_response_increment(ptr, server_key);
- return MEMCACHED_SUCCESS;
+ if (result)
+ rc= value_fetch(ptr, buffer, result, server_key);
+ else
+ rc= value_fetch(ptr, buffer, &ptr->result, server_key);
+
+ return rc;
}
else if (buffer[1] == 'E') /* VERSION */
{
while (1)
{
- rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
+ rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL, server_key);
if (rc == MEMCACHED_STAT)
{
if (to_write == 0)
rc= MEMCACHED_SUCCESS;
else
- rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
+ rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL, server_key);
if (rc == MEMCACHED_STORED)
return MEMCACHED_SUCCESS;
continue;
}
- rrc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, x);
+ rrc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL, x);
if (rrc != MEMCACHED_SUCCESS)
rc= MEMCACHED_SOME_ERRORS;
}
continue;
}
- rrc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, x);
+ rrc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL, x);
if (rrc != MEMCACHED_SUCCESS)
rc= MEMCACHED_SOME_ERRORS;
for (x= 0; x < memcached_server_list_count(servers); x++)
{
printf("\t%s : %u\n", servers[x].hostname, servers[x].port);
- assert(servers[x].stack_responses == 0);
assert(servers[x].fd == -1);
assert(servers[x].cursor_active == 0);
}
for (loop= 0; loop < memcached_server_list_count(servers); loop++)
{
- assert(memc->hosts[loop].stack_responses == 0);
assert(memc->hosts[loop].fd == -1);
assert(memc->hosts[loop].cursor_active == 0);
}