Merge in simple extrace for strings, style issues in io
authorBrian Aker <brian@tangent.org>
Fri, 29 Jul 2011 22:00:34 +0000 (15:00 -0700)
committerBrian Aker <brian@tangent.org>
Fri, 29 Jul 2011 22:00:34 +0000 (15:00 -0700)
libmemcached/common.h
libmemcached/delete.cc
libmemcached/include.am
libmemcached/io.cc
libmemcached/io.h
libmemcached/io.hpp [new file with mode: 0644]
libmemcached/response.cc
libmemcached/string.h
libmemcached/string.hpp [new file with mode: 0644]

index d4b2c7df3446660f26307541ec29a3560068767d..49d309c8759ed0a2a8e7f2f788bb2b03eda02203 100644 (file)
@@ -92,7 +92,11 @@ memcached_return_t memcached_server_execute(memcached_st *ptr,
 #include <libmemcached/error.hpp>
 #include <libmemcached/memory.h>
 #include <libmemcached/io.h>
+#ifdef __cplusplus
+#include <libmemcached/string.hpp>
+#include <libmemcached/io.hpp>
 #include <libmemcached/do.hpp>
+#endif
 #include <libmemcached/internal.h>
 #include <libmemcached/array.h>
 #include <libmemcached/libmemcached_probes.h>
index de4a53dc46a51931b3ce96e84f271ed0d3f13d36..ea3b602539cf557558869e2feb43a67360caf78d 100644 (file)
@@ -161,7 +161,9 @@ memcached_return_t memcached_delete_by_key(memcached_st *ptr,
         return MEMCACHED_WRITE_FAILURE;
 
       if (send_length + instance->write_buffer_offset > MAX_UDP_DATAGRAM_LENGTH)
+      {
         memcached_io_write(instance, NULL, 0, true);
+      }
     }
 
     rc= memcached_do(instance, buffer, (size_t)send_length, to_write);
index f8e20d582662f0f3a4b3783fc19814699746bedc..3a0a0ac4336723a3ff2edf670f1901640f063f98 100644 (file)
@@ -21,6 +21,7 @@ noinst_HEADERS+= \
                 libmemcached/initialize_query.h \
                 libmemcached/internal.h \
                 libmemcached/io.h \
+                libmemcached/io.hpp \
                 libmemcached/is.h \
                 libmemcached/libmemcached_probes.h \
                 libmemcached/memory.h \
@@ -29,6 +30,7 @@ noinst_HEADERS+= \
                 libmemcached/protocol/binary_handler.h \
                 libmemcached/protocol/common.h \
                 libmemcached/response.h \
+                libmemcached/string.hpp \
                 libmemcached/virtual_bucket.h
 
 nobase_include_HEADERS+= \
index f706048e71341289add79a30931d529c5825de03..0ed565d7e28a436e7d3bd9b26ba6598175253430 100644 (file)
@@ -846,10 +846,11 @@ memcached_return_t memcached_safe_read(memcached_server_write_instance_st ptr,
 
 memcached_return_t memcached_io_readline(memcached_server_write_instance_st ptr,
                                          char *buffer_ptr,
-                                         size_t size)
+                                         size_t size,
+                                         size_t& total_nr)
 {
+  total_nr= 0;
   bool line_complete= false;
-  size_t total_nr= 0;
 
   while (not line_complete)
   {
index 0d1e6193c3839945b7a0c1a4981229698f28d920..0a05b92a99492f1a35531180c6194027c9d01938 100644 (file)
@@ -38,8 +38,6 @@
 
 #pragma once
 
-#include <libmemcached/memcached.h>
-
 #define MAX_UDP_DATAGRAM_LENGTH 1400
 #define UDP_DATAGRAM_HEADER_LENGTH 8
 #define UDP_REQUEST_ID_MSG_SIG_DIGITS 10
@@ -66,54 +64,12 @@ struct libmemcached_io_vector_st
   const void *buffer;
 };
 
-#ifdef __cplusplus
-extern "C" {
-#endif
-
 LIBMEMCACHED_LOCAL
-memcached_return_t memcached_io_wait_for_write(memcached_server_write_instance_st ptr);
+ssize_t memcached_io_write(memcached_server_write_instance_st ptr,
+                           const void *buffer, size_t length, bool with_flush);
 
 LIBMEMCACHED_LOCAL
 ssize_t memcached_io_writev(memcached_server_write_instance_st ptr,
                             const struct libmemcached_io_vector_st *vector,
                             size_t number_of, bool with_flush);
 
-LIBMEMCACHED_LOCAL
-ssize_t memcached_io_write(memcached_server_write_instance_st ptr,
-                           const void *buffer, size_t length, bool with_flush);
-
-LIBMEMCACHED_LOCAL
-void memcached_io_reset(memcached_server_write_instance_st ptr);
-
-LIBMEMCACHED_LOCAL
-memcached_return_t memcached_io_read(memcached_server_write_instance_st ptr,
-                                     void *buffer, size_t length, ssize_t *nread);
-
-/* Read a line (terminated by '\n') into the buffer */
-LIBMEMCACHED_LOCAL
-memcached_return_t memcached_io_readline(memcached_server_write_instance_st ptr,
-                                         char *buffer_ptr,
-                                         size_t size);
-
-LIBMEMCACHED_LOCAL
-void memcached_io_close(memcached_server_write_instance_st ptr);
-
-/* Read n bytes of data from the server and store them in dta */
-LIBMEMCACHED_LOCAL
-memcached_return_t memcached_safe_read(memcached_server_write_instance_st ptr,
-                                       void *dta,
-                                       size_t size);
-
-LIBMEMCACHED_LOCAL
-memcached_return_t memcached_io_init_udp_header(memcached_server_write_instance_st ptr,
-                                                uint16_t thread_id);
-
-LIBMEMCACHED_LOCAL
-memcached_server_write_instance_st memcached_io_get_readable_server(memcached_st *memc);
-
-LIBMEMCACHED_LOCAL
-memcached_return_t memcached_io_slurp(memcached_server_write_instance_st ptr);
-
-#ifdef __cplusplus
-}
-#endif
diff --git a/libmemcached/io.hpp b/libmemcached/io.hpp
new file mode 100644 (file)
index 0000000..7413ac4
--- /dev/null
@@ -0,0 +1,83 @@
+/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ * 
+ *  LibMemcached
+ *
+ *  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.
+ *
+ */
+
+#pragma once
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+LIBMEMCACHED_LOCAL
+memcached_return_t memcached_io_wait_for_write(memcached_server_write_instance_st ptr);
+
+LIBMEMCACHED_LOCAL
+void memcached_io_reset(memcached_server_write_instance_st ptr);
+
+LIBMEMCACHED_LOCAL
+memcached_return_t memcached_io_read(memcached_server_write_instance_st ptr,
+                                     void *buffer, size_t length, ssize_t *nread);
+
+/* Read a line (terminated by '\n') into the buffer */
+LIBMEMCACHED_LOCAL
+memcached_return_t memcached_io_readline(memcached_server_write_instance_st ptr,
+                                         char *buffer_ptr,
+                                         size_t size,
+                                         size_t& total);
+
+LIBMEMCACHED_LOCAL
+void memcached_io_close(memcached_server_write_instance_st ptr);
+
+/* Read n bytes of data from the server and store them in dta */
+LIBMEMCACHED_LOCAL
+memcached_return_t memcached_safe_read(memcached_server_write_instance_st ptr,
+                                       void *dta,
+                                       size_t size);
+
+LIBMEMCACHED_LOCAL
+memcached_return_t memcached_io_init_udp_header(memcached_server_write_instance_st ptr,
+                                                uint16_t thread_id);
+
+LIBMEMCACHED_LOCAL
+memcached_server_write_instance_st memcached_io_get_readable_server(memcached_st *memc);
+
+LIBMEMCACHED_LOCAL
+memcached_return_t memcached_io_slurp(memcached_server_write_instance_st ptr);
+
+#ifdef __cplusplus
+}
+#endif
index 1548b4ed23c847f1ad2a6f70d4680809803b959a..dddcb349ca48aac4d1965afc16f6d8a64bec5e61 100644 (file)
@@ -36,6 +36,7 @@
  */
 
 #include <libmemcached/common.h>
+#include <libmemcached/string.hpp>
 
 static memcached_return_t textual_read_one_response(memcached_server_write_instance_st ptr,
                                                     char *buffer, size_t buffer_length,
@@ -252,7 +253,9 @@ static memcached_return_t textual_read_one_response(memcached_server_write_insta
                                                     char *buffer, size_t buffer_length,
                                                     memcached_result_st *result)
 {
-  memcached_return_t rc= memcached_io_readline(ptr, buffer, buffer_length);
+  size_t total_read;
+  memcached_return_t rc= memcached_io_readline(ptr, buffer, buffer_length, total_read);
+
   if (memcached_failed(rc))
   {
     return rc;
@@ -287,8 +290,25 @@ static memcached_return_t textual_read_one_response(memcached_server_write_insta
       }
       else if (buffer[1] == 'E') /* SERVER_ERROR */
       {
-        char *startptr= buffer + 13, *endptr= startptr;
+        if (total_read == memcached_literal_param_size("SERVER_ERROR"))
+        {
+          return MEMCACHED_SERVER_ERROR;
+        }
+
+        if (total_read > memcached_literal_param_size("SERVER_ERROR object too large for cache") and
+            (memcmp(buffer, memcached_literal_param("SERVER_ERROR object too large for cache")) == 0))
+        {
+          return MEMCACHED_E2BIG;
+        }
+
+        // Move past the basic error message and whitespace
+        char *startptr= buffer + memcached_literal_param_size("SERVER_ERROR");
+        if (startptr[0] == ' ')
+        {
+          startptr++;
+        }
 
+        char *endptr= startptr;
         while (*endptr != '\r' && *endptr != '\n') endptr++;
 
         return memcached_set_error(*ptr, MEMCACHED_SERVER_ERROR, MEMCACHED_AT, startptr, size_t(endptr - startptr));
index 3a3fdf530fd1add85d247bda40f8c86d46de0334..360d9008b02718c492bce616750ebb15c30f6389 100644 (file)
@@ -110,12 +110,4 @@ void memcached_string_set_length(memcached_string_st *self, size_t length);
 }
 #endif
 
-#ifdef __cplusplus
-#define memcached_literal_param(X) (X), (static_cast<size_t>((sizeof(X) - 1)))
-#else
-#define memcached_literal_param(X) (X), ((size_t)((sizeof(X) - 1)))
-#endif
-
-#define memcached_string_make_from_cstr(X) (X), ((X) ? strlen(X) : 0)
-
 #endif
diff --git a/libmemcached/string.hpp b/libmemcached/string.hpp
new file mode 100644 (file)
index 0000000..5703bb4
--- /dev/null
@@ -0,0 +1,47 @@
+/*  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
+ * 
+ *  libmcachedd client library.
+ *
+ *  Copyright (C) 2011 Data Differential, http://datadifferential.com/
+ *  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.
+ *
+ */
+
+#pragma once
+
+#include "util/string.hpp"
+
+#define memcached_literal_param util_literal_param
+#define memcached_literal_param_size util_literal_param_size
+#define memcached_string_make_from_cstr util_string_make_from_cstr
+#define memcached_array_length util_array_length
+
+