From: Brian Aker Date: Wed, 15 Jul 2009 10:09:06 +0000 (-0700) Subject: Updated with Twitter's memcached_server_error() function. X-Git-Tag: 0.32~16 X-Git-Url: https://git.m6w6.name/?a=commitdiff_plain;h=89608dc3370371a9fde46d6f60e9480fa576fc3a;p=awesomized%2Flibmemcached Updated with Twitter's memcached_server_error() function. --- 89608dc3370371a9fde46d6f60e9480fa576fc3a diff --cc ChangeLog index aefae4f1,a972319d..950364b9 --- a/ChangeLog +++ b/ChangeLog @@@ -1,10 -1,3 +1,11 @@@ ++ * Added Twitter's memcached_server_error() functions. + * Fix for OSX compiles in development builds. + * Updated C++ interface. + +0.31 Fri Jul 10 09:02:50 PDT 2009 + * Added support or HA via replication. + * malloc() removed for server key usage. + * Update build system. * Added support for memcached_set_memory_allocators(). * Fixed bug in configure.ac for have_htoll. diff --cc docs/memcached_server_st.pod index 0e6e7067,0e6e7067..08ff7785 --- a/docs/memcached_server_st.pod +++ b/docs/memcached_server_st.pod @@@ -23,6 -23,6 +23,10 @@@ C Client Library for memcached (libmemc memcached_server_st *memcached_servers_parse (const char *server_strings); ++ const char *memcached_server_error(memcached_server_st *ptr); ++ ++ void memcached_server_error_reset(memcached_server_st *ptr); ++ =head1 DESCRIPTION libmemcached(3) operates on a list of hosts which are stored in @@@ -44,6 -44,6 +48,12 @@@ command line applications, and parse i The example is "localhost, foo:555, foo, bar". All hosts except foo:555 will be set to the default port, while that host will have a port of 555. ++memcached_server_error() can be used to look at the text of the last error ++message sent by the server to to the client. Use memcached_server_error_reset() ++to reset the message (this does not currently free up the memory associated ++with the message). ++ ++ =head1 RETURN Varies, see particular functions. diff --cc libmemcached/memcached_response.c index 1ea0c725,6cb39df6..ba0691b5 --- a/libmemcached/memcached_response.c +++ b/libmemcached/memcached_response.c @@@ -237,8 -237,17 +237,34 @@@ static memcached_return textual_read_on memcached_server_response_increment(ptr); return MEMCACHED_STAT; } -- else if (buffer[1] == 'E') - return MEMCACHED_SERVER_ERROR; ++ else if (buffer[1] == 'E') /* SERVER_ERROR */ + { - /* SERVER_ERROR */ ++ char *rel_ptr; + char *startptr= buffer + 13, *endptr= startptr; ++ + while (*endptr != '\r' && *endptr != '\n') endptr++; - if (ptr->cached_server_error) ptr->root->call_free(ptr->root, ptr->cached_server_error); - ptr->cached_server_error= ptr->root->call_malloc(ptr->root, endptr - startptr + 1); ++ ++ /* ++ Yes, we could make this "efficent" but to do that we would need ++ to maintain more state for the size of the buffer. Why waste ++ memory in the struct, which is important, for something that ++ rarely should happen? ++ */ ++ rel_ptr= (char *)ptr->root->call_realloc(ptr->root, ptr->cached_server_error, endptr - startptr + 1); ++ ++ if (rel_ptr == NULL) ++ { ++ /* If we happened to have some memory, we just null it since we don't know the size */ ++ if (ptr->cached_server_error) ++ ptr->cached_server_error[0]= 0; ++ return MEMCACHED_SERVER_ERROR; ++ } ++ ptr->cached_server_error= rel_ptr; ++ + memcpy(ptr->cached_server_error, startptr, endptr - startptr); + ptr->cached_server_error[endptr - startptr]= 0; + return MEMCACHED_SERVER_ERROR; + } else if (buffer[1] == 'T') return MEMCACHED_STORED; else diff --cc libmemcached/memcached_server.c index 11e5988a,2359611e..7b9c2117 --- a/libmemcached/memcached_server.c +++ b/libmemcached/memcached_server.c @@@ -53,11 -53,17 +53,11 @@@ void memcached_server_free(memcached_se { memcached_quit_server(ptr, 0); + if (ptr->cached_server_error) - { + free(ptr->cached_server_error); - ptr->cached_server_error= NULL; - } + if (ptr->address_info) -- { freeaddrinfo(ptr->address_info); -- ptr->address_info= NULL; -- } if (ptr->is_allocated) ptr->root->call_free(ptr->root, ptr); @@@ -70,6 -76,8 +70,8 @@@ */ memcached_server_st *memcached_server_clone(memcached_server_st *clone, memcached_server_st *ptr) { - memcached_server_st *rv = NULL; ++ memcached_server_st *rv= NULL; + /* We just do a normal create if ptr is missing */ if (ptr == NULL) return NULL; @@@ -131,3 -147,3 +141,16 @@@ memcached_server_st *memcached_server_b return memcached_server_clone(NULL, &ptr->hosts[server_key]); } ++ ++const char *memcached_server_error(memcached_server_st *ptr) ++{ ++ if (ptr) ++ return ptr->cached_server_error; ++ else ++ return NULL; ++} ++ ++void memcached_server_error_reset(memcached_server_st *ptr) ++{ ++ ptr->cached_server_error[0]= 0; ++} diff --cc libmemcached/memcached_server.h index 8076877e,cffc5e80..ec1014a6 --- a/libmemcached/memcached_server.h +++ b/libmemcached/memcached_server.h @@@ -29,6 -30,6 +29,7 @@@ struct memcached_server_st uint8_t minor_version; memcached_connection type; char *read_ptr; ++ char *cached_server_error; size_t read_buffer_length; size_t read_data_length; size_t write_buffer_offset; @@@ -57,13 -56,9 +58,19 @@@ LIBMEMCACHED_AP memcached_server_st *memcached_server_by_key(memcached_st *ptr, const char *key, size_t key_length, memcached_return *error); ++LIBMEMCACHED_API ++const char *memcached_server_error(memcached_server_st *ptr); ++ ++LIBMEMCACHED_API ++void memcached_server_error_reset(memcached_server_st *ptr); ++ /* These should not currently be used by end users */ +/* TODO: Is the above comment valid? If so, how can we unit test these if they + * aren't exported. If not, we should remove the comment */ +LIBMEMCACHED_API memcached_server_st *memcached_server_create(memcached_st *memc, memcached_server_st *ptr); +LIBMEMCACHED_API memcached_server_st *memcached_server_create_with(memcached_st *memc, memcached_server_st *host, const char *hostname, unsigned int port, uint32_t weight, memcached_connection type);