MEMCACHED_BEHAVIOR_RETRY_TIMEOUT added for timeout
[awesomized/libmemcached] / lib / memcached_stats.c
index decf22b614c8a864634c3abdaabba95d1ba1e31e..a1ef99f3a945b374bdf7b5ad1d3bbc05e7da4a26 100644 (file)
@@ -32,28 +32,33 @@ static char *memcached_stat_keys[] = {
 
 static void set_data(memcached_stat_st *stat, char *key, char *value)
 {
-  if (!memcmp("pid", key, strlen("pid")))
+
+  if(strlen(key) < 1) 
+  {
+    fprintf(stderr, "Invalid key %s\n", key);
+  }
+  else if (!strcmp("pid", key))
   {
     stat->pid= strtol(value, (char **)NULL, 10);
   }
-  else if (!memcmp("uptime", key, strlen("uptime")))
+  else if (!strcmp("uptime", key))
   {
     stat->uptime= strtol(value, (char **)NULL, 10);
   }
-  else if (!memcmp("time", key, strlen("time")))
+  else if (!strcmp("time", key))
   {
     stat->time= strtol(value, (char **)NULL, 10);
   }
-  else if (!memcmp("version", key, strlen("version")))
+  else if (!strcmp("version", key))
   {
     memcpy(stat->version, value, strlen(value));
     stat->version[strlen(value)]= 0;
   }
-  else if (!memcmp("pointer_size", key, strlen("pointer_size")))
+  else if (!strcmp("pointer_size", key))
   {
     stat->pointer_size= strtol(value, (char **)NULL, 10);
   }
-  else if (!memcmp("rusage_user", key, strlen("rusage_user")))
+  else if (!strcmp("rusage_user", key))
   {
     char *walk_ptr;
     for (walk_ptr= value; (!ispunct(*walk_ptr)); walk_ptr++);
@@ -62,7 +67,7 @@ static void set_data(memcached_stat_st *stat, char *key, char *value)
     stat->rusage_user_seconds= strtol(value, (char **)NULL, 10);
     stat->rusage_user_microseconds= strtol(walk_ptr, (char **)NULL, 10);
   }
-  else if (!memcmp("rusage_system", key, strlen("rusage_system")))
+  else if (!strcmp("rusage_system", key))
   {
     char *walk_ptr;
     for (walk_ptr= value; (!ispunct(*walk_ptr)); walk_ptr++);
@@ -71,63 +76,63 @@ static void set_data(memcached_stat_st *stat, char *key, char *value)
     stat->rusage_system_seconds= strtol(value, (char **)NULL, 10);
     stat->rusage_system_microseconds= strtol(walk_ptr, (char **)NULL, 10);
   }
-  else if (!memcmp("curr_items", key, strlen("curr_items")))
+  else if (!strcmp("curr_items", key))
   {
     stat->curr_items= strtol(value, (char **)NULL, 10); 
   }
-  else if (!memcmp("total_items", key, strlen("total_items")))
+  else if (!strcmp("total_items", key))
   {
     stat->total_items= strtol(value, (char **)NULL, 10);
   }
-  else if (!memcmp("bytes", key, strlen("bytes")))
+  else if (!strcmp("bytes", key))
   {
-    //stat->bytes= strtoll(value, (char **)NULL, 10);
+    stat->bytes= strtoll(value, (char **)NULL, 10);
   }
-  else if (!memcmp("curr_connections", key, strlen("curr_connections")))
+  else if (!strcmp("curr_connections", key))
   {
-    //stat->curr_connections= strtoll(value, (char **)NULL, 10);
+    stat->curr_connections= strtoll(value, (char **)NULL, 10);
   }
-  else if (!memcmp("total_connections", key, strlen("total_connections")))
+  else if (!strcmp("total_connections", key))
   {
-    //stat->total_connections= strtoll(value, (char **)NULL, 10);
+    stat->total_connections= strtoll(value, (char **)NULL, 10);
   }
-  else if (!memcmp("connection_structures", key, strlen("connection_structures")))
+  else if (!strcmp("connection_structures", key))
   {
     stat->connection_structures= strtol(value, (char **)NULL, 10);
   }
-  else if (!memcmp("cmd_get", key, strlen("cmd_get")))
+  else if (!strcmp("cmd_get", key))
   {
     stat->cmd_get= strtoll(value, (char **)NULL, 10);
   }
-  else if (!memcmp("cmd_set", key, strlen("cmd_set")))
+  else if (!strcmp("cmd_set", key))
   {
     stat->cmd_set= strtoll(value, (char **)NULL, 10);
   }
-  else if (!memcmp("get_hits", key, strlen("get_hits")))
+  else if (!strcmp("get_hits", key))
   {
     stat->get_hits= strtoll(value, (char **)NULL, 10);
   }
-  else if (!memcmp("get_misses", key, strlen("get_misses")))
+  else if (!strcmp("get_misses", key))
   {
     stat->get_misses= (uint64_t)strtoll(value, (char **)NULL, 10);
   }
-  else if (!memcmp("evictions", key, strlen("evictions")))
+  else if (!strcmp("evictions", key))
   {
     stat->evictions= (uint64_t)strtoll(value, (char **)NULL, 10);
   }
-  else if (!memcmp("bytes_read", key, strlen("bytes_read")))
+  else if (!strcmp("bytes_read", key))
   {
     stat->bytes_read= strtoll(value, (char **)NULL, 10);
   }
-  else if (!memcmp("bytes_written", key, strlen("bytes_written")))
+  else if (!strcmp("bytes_written", key))
   {
     stat->bytes_written= strtoll(value, (char **)NULL, 10);
   }
-  else if (!memcmp("limit_maxbytes", key, strlen("limit_maxbytes")))
+  else if (!strcmp("limit_maxbytes", key))
   {
     stat->limit_maxbytes= strtol(value, (char **)NULL, 10);
   }
-  else if (!memcmp("threads", key, strlen("threads")))
+  else if (!strcmp("threads", key))
   {
     stat->threads= strtol(key, (char **)NULL, 10);
   }
@@ -141,59 +146,69 @@ char *memcached_stat_get_value(memcached_st *ptr, memcached_stat_st *stat,
                                char *key, memcached_return *error)
 {
   char buffer[SMALL_STRING_LEN];
+  size_t length;
+  char *ret;
+
   *error= MEMCACHED_SUCCESS;
 
   if (!memcmp("pid", key, strlen("pid")))
-    snprintf(buffer, SMALL_STRING_LEN,"%u", stat->pid);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->pid);
   else if (!memcmp("uptime", key, strlen("uptime")))
-    snprintf(buffer, SMALL_STRING_LEN,"%u", stat->uptime);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->uptime);
   else if (!memcmp("time", key, strlen("time")))
-    snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->time);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->time);
   else if (!memcmp("version", key, strlen("version")))
-    snprintf(buffer, SMALL_STRING_LEN,"%s", stat->version);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%s", stat->version);
   else if (!memcmp("pointer_size", key, strlen("pointer_size")))
-    snprintf(buffer, SMALL_STRING_LEN,"%u", stat->pointer_size);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->pointer_size);
   else if (!memcmp("rusage_user", key, strlen("rusage_user")))
-    snprintf(buffer, SMALL_STRING_LEN,"%u.%u", stat->rusage_user_seconds, stat->rusage_user_microseconds);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u.%u", stat->rusage_user_seconds, stat->rusage_user_microseconds);
   else if (!memcmp("rusage_system", key, strlen("rusage_system")))
-    snprintf(buffer, SMALL_STRING_LEN,"%u.%u", stat->rusage_system_seconds, stat->rusage_system_microseconds);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u.%u", stat->rusage_system_seconds, stat->rusage_system_microseconds);
   else if (!memcmp("curr_items", key, strlen("curr_items")))
-    snprintf(buffer, SMALL_STRING_LEN,"%u", stat->curr_items);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->curr_items);
   else if (!memcmp("total_items", key, strlen("total_items")))
-    snprintf(buffer, SMALL_STRING_LEN,"%u", stat->total_items);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->total_items);
   else if (!memcmp("bytes", key, strlen("bytes")))
-    snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->bytes);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->bytes);
   else if (!memcmp("curr_connections", key, strlen("curr_connections")))
-    snprintf(buffer, SMALL_STRING_LEN,"%u", stat->curr_connections);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->curr_connections);
   else if (!memcmp("total_connections", key, strlen("total_connections")))
-    snprintf(buffer, SMALL_STRING_LEN,"%u", stat->total_connections);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->total_connections);
   else if (!memcmp("connection_structures", key, strlen("connection_structures")))
-    snprintf(buffer, SMALL_STRING_LEN,"%u", stat->connection_structures);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->connection_structures);
   else if (!memcmp("cmd_get", key, strlen("cmd_get")))
-    snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->cmd_get);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->cmd_get);
   else if (!memcmp("cmd_set", key, strlen("cmd_set")))
-    snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->cmd_set);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->cmd_set);
   else if (!memcmp("get_hits", key, strlen("get_hits")))
-    snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->get_hits);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->get_hits);
   else if (!memcmp("get_misses", key, strlen("get_misses")))
-    snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->get_misses);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->get_misses);
   else if (!memcmp("evictions", key, strlen("evictions")))
-    snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->evictions);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->evictions);
   else if (!memcmp("bytes_read", key, strlen("bytes_read")))
-    snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->bytes_read);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->bytes_read);
   else if (!memcmp("bytes_written", key, strlen("bytes_written")))
-    snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->bytes_written);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%llu", (unsigned long long)stat->bytes_written);
   else if (!memcmp("limit_maxbytes", key, strlen("limit_maxbytes")))
-    snprintf(buffer, SMALL_STRING_LEN,"%u", stat->limit_maxbytes);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%llu", stat->limit_maxbytes);
   else if (!memcmp("threads", key, strlen("threads")))
-    snprintf(buffer, SMALL_STRING_LEN,"%u", stat->threads);
+    length= snprintf(buffer, SMALL_STRING_LEN,"%u", stat->threads);
   else
   {
     *error= MEMCACHED_NOTFOUND;
     return NULL;
   }
 
-  return strdup(buffer);
+  if (ptr->call_malloc)
+    ret= ptr->call_malloc(ptr, length + 1);
+  else
+    ret= malloc(length + 1);
+  memcpy(ret, buffer, length);
+  ret[length]= '\0';
+
+  return ret;
 }
 
 static memcached_return memcached_stats_fetch(memcached_st *ptr,
@@ -215,13 +230,13 @@ static memcached_return memcached_stats_fetch(memcached_st *ptr,
   if (send_length >= MEMCACHED_DEFAULT_COMMAND_SIZE)
     return MEMCACHED_WRITE_FAILURE;
 
-  rc= memcached_do(ptr, server_key, buffer, send_length, 1);
+  rc= memcached_do(&ptr->hosts[server_key], buffer, send_length, 1);
   if (rc != MEMCACHED_SUCCESS)
       goto error;
 
   while (1)
   {
-    rc= memcached_response(ptr, buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, server_key);
+    rc= memcached_response(&ptr->hosts[server_key], buffer, MEMCACHED_DEFAULT_COMMAND_SIZE, NULL);
 
     if (rc == MEMCACHED_STAT)
     {
@@ -258,11 +273,19 @@ memcached_stat_st *memcached_stat(memcached_st *ptr, char *args, memcached_retur
   memcached_return rc;
   memcached_stat_st *stats;
 
-  stats= (memcached_stat_st *)malloc(sizeof(memcached_stat_st)*(ptr->number_of_hosts));
+  if (ptr->call_malloc)
+    stats= (memcached_stat_st *)ptr->call_malloc(ptr, sizeof(memcached_stat_st)*(ptr->number_of_hosts));
+  else
+    stats= (memcached_stat_st *)malloc(sizeof(memcached_stat_st)*(ptr->number_of_hosts));
+
   if (!stats)
   {
     *error= MEMCACHED_MEMORY_ALLOCATION_FAILURE;
-    free(stats);
+    if (ptr->call_free)
+      ptr->call_free(ptr, stats);
+    else
+      free(stats);
+
     return NULL;
   }
   memset(stats, 0, sizeof(memcached_stat_st)*(ptr->number_of_hosts));
@@ -307,7 +330,11 @@ char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *stat,
 {
   char **list;
   size_t length= sizeof(memcached_stat_keys);
-  list= (char **)malloc(length);
+
+  if (ptr->call_malloc)
+    list= (char **)ptr->call_malloc(ptr, length);
+  else
+    list= (char **)malloc(length);
 
   if (!list)
   {
@@ -325,5 +352,14 @@ char ** memcached_stat_get_keys(memcached_st *ptr, memcached_stat_st *stat,
 
 void memcached_stat_free(memcached_st *ptr, memcached_stat_st *stat)
 {
-  free(stat);
+  if (stat == NULL)
+  {
+    WATCHPOINT_ASSERT(0); /* Be polite, but when debugging catch this as an error */
+    return;
+  }
+
+  if (ptr && ptr->call_free)
+    ptr->call_free(ptr, stat);
+  else
+    free(stat);
 }