Drizzled Public API Documentation

memc_behavior_get.cc
00001 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
00002  *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
00003  *
00004  * Copyright (C) 2009, Patrick "CaptTofu" Galbraith, Padraig O'Sullivan
00005  * All rights reserved.
00006  *
00007  * Redistribution and use in source and binary forms, with or without
00008  * modification, are permitted provided that the following conditions are met:
00009  *
00010  *   * Redistributions of source code must retain the above copyright notice,
00011  *     this list of conditions and the following disclaimer.
00012  *   * Redistributions in binary form must reproduce the above copyright notice,
00013  *     this list of conditions and the following disclaimer in the documentation
00014  *     and/or other materials provided with the distribution.
00015  *   * Neither the name of Patrick Galbraith nor the names of its contributors
00016  *     may be used to endorse or promote products derived from this software
00017  *     without specific prior written permission.
00018  *
00019  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00022  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00023  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00024  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00025  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00026  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00027  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00028  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
00029  * THE POSSIBILITY OF SUCH DAMAGE.
00030  */
00031 
00032 #include <config.h>
00033 #include <drizzled/item/func.h>
00034 #include <drizzled/function/str/strfunc.h>
00035 
00036 #include "memcached_functions.h"
00037 #include "memc_behavior_get.h"
00038 
00039 #include <libmemcached/memcached.h>
00040 
00041 #include <string>
00042 #include <algorithm>
00043 
00044 using namespace std;
00045 using namespace drizzled;
00046 
00047 void MemcachedBehaviorGet::setFailureString(const char *error)
00048 {
00049   size_t size= strlen(error);
00050   failure_buff.realloc(size);
00051   failure_buff.length(size);
00052   memcpy(failure_buff.ptr(), error, size);
00053 }
00054 
00055 String *MemcachedBehaviorGet::val_str(String *str)
00056 {
00057   String *tmp_behavior;
00058 
00059   if (arg_count != 1 ||
00060       ! (tmp_behavior= args[0]->val_str(str)) ||
00061       ! memc)
00062   {
00063     setFailureString("USAGE: memc_behavior_get('<behavior type>')");
00064     return &failure_buff;
00065   }
00066 
00067   /*
00068    * We don't want the user to have to type in all input in upper
00069    * case so we transform the input strings to upper case here.
00070    */
00071 
00072   memcached_behavior* it = find_ptr(behavior_map, boost::to_upper_copy(string(tmp_behavior->c_ptr())));
00073   if (not it) 
00074   {
00075     setFailureString("UNKNOWN BEHAVIOR TYPE!");
00076     return &failure_buff;
00077   }
00078   
00079   memcached_behavior mbehavior= *it;
00080 
00081   uint64_t isetting= memcached_behavior_get(memc, mbehavior);
00082 
00083   switch (mbehavior)
00084   {
00085   case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
00086   case MEMCACHED_BEHAVIOR_NO_BLOCK:
00087   case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
00088   case MEMCACHED_BEHAVIOR_USER_DATA:
00089   case MEMCACHED_BEHAVIOR_SORT_HOSTS:
00090   case MEMCACHED_BEHAVIOR_VERIFY_KEY:
00091   case MEMCACHED_BEHAVIOR_TCP_NODELAY:
00092   case MEMCACHED_BEHAVIOR_KETAMA:
00093   case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS:
00094     if (isetting == 1)
00095       return_buff.append("1");
00096     else if (isetting == 0)
00097       return_buff.append("0");
00098     else
00099     {
00100       setFailureString("INVALID VALUE FOR BEHAVIOR - MUST be 1 OR 0!");
00101       return &failure_buff;
00102     }
00103     break;
00104   case MEMCACHED_BEHAVIOR_DISTRIBUTION:
00105     {
00106       string setting(dist_settings_reverse_map[isetting]);
00107       return_buff.append(setting.c_str());
00108     }
00109     break;
00110   case MEMCACHED_BEHAVIOR_HASH:
00111     {
00112       string setting(hash_settings_reverse_map[isetting]);
00113       return_buff.append(setting.c_str());
00114     }
00115     break;
00116   case MEMCACHED_BEHAVIOR_KETAMA_HASH:
00117     {
00118       string setting(ketama_hash_settings_reverse_map[isetting]);
00119       return_buff.append(setting.c_str());
00120     }
00121     break;
00122   case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
00123   case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
00124   case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
00125   case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
00126   case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
00127   case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK:
00128   case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK:
00129     {
00130       size_t setting_len= 0;
00131       char tmp_buff[16];
00132 
00133       snprintf(tmp_buff, 16, "%"PRIu64, isetting);
00134       setting_len= strlen(tmp_buff);
00135       return_buff.realloc(setting_len);
00136       return_buff.length(setting_len);
00137       memcpy(return_buff.ptr(),tmp_buff, setting_len);
00138     }
00139     break;
00140   default:
00141     break;
00142   }
00143 
00144   return &return_buff;
00145 }