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   memcached_behavior mbehavior;
00058   uint64_t isetting;
00059   map<const string, memcached_behavior>::iterator it;
00060   String *tmp_behavior;
00061 
00062   if (arg_count != 1 ||
00063       ! (tmp_behavior= args[0]->val_str(str)) ||
00064       ! memc)
00065   {
00066     setFailureString("USAGE: memc_behavior_get('<behavior type>')");
00067     return &failure_buff;
00068   }
00069 
00070   string behavior(tmp_behavior->c_ptr());
00071 
00072   /*
00073    * We don't want the user to have to type in all input in upper
00074    * case so we transform the input strings to upper case here.
00075    */
00076   std::transform(behavior.begin(), behavior.end(),
00077                  behavior.begin(), ::toupper);
00078 
00079   it = behavior_map.find(behavior);
00080   if (it == behavior_map.end()) 
00081   {
00082     setFailureString("UNKNOWN BEHAVIOR TYPE!");
00083     return &failure_buff;
00084   }
00085   
00086   mbehavior= behavior_map[behavior];
00087 
00088   isetting= memcached_behavior_get(memc, mbehavior);
00089 
00090   switch (mbehavior)
00091   {
00092   case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
00093   case MEMCACHED_BEHAVIOR_NO_BLOCK:
00094   case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
00095   case MEMCACHED_BEHAVIOR_USER_DATA:
00096   case MEMCACHED_BEHAVIOR_SORT_HOSTS:
00097   case MEMCACHED_BEHAVIOR_VERIFY_KEY:
00098   case MEMCACHED_BEHAVIOR_TCP_NODELAY:
00099   case MEMCACHED_BEHAVIOR_KETAMA:
00100   case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS:
00101     if (isetting == 1)
00102       return_buff.append("1");
00103     else if (isetting == 0)
00104       return_buff.append("0");
00105     else
00106     {
00107       setFailureString("INVALID VALUE FOR BEHAVIOR - MUST be 1 OR 0!");
00108       return &failure_buff;
00109     }
00110     break;
00111   case MEMCACHED_BEHAVIOR_DISTRIBUTION:
00112     {
00113       string setting(dist_settings_reverse_map[isetting]);
00114       return_buff.append(setting.c_str());
00115     }
00116     break;
00117   case MEMCACHED_BEHAVIOR_HASH:
00118     {
00119       string setting(hash_settings_reverse_map[isetting]);
00120       return_buff.append(setting.c_str());
00121     }
00122     break;
00123   case MEMCACHED_BEHAVIOR_KETAMA_HASH:
00124     {
00125       string setting(ketama_hash_settings_reverse_map[isetting]);
00126       return_buff.append(setting.c_str());
00127     }
00128     break;
00129   case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
00130   case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
00131   case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
00132   case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
00133   case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
00134   case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK:
00135   case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK:
00136     {
00137       size_t setting_len= 0;
00138       char tmp_buff[16];
00139 
00140       snprintf(tmp_buff, 16, "%"PRIu64, isetting);
00141       setting_len= strlen(tmp_buff);
00142       return_buff.realloc(setting_len);
00143       return_buff.length(setting_len);
00144       memcpy(return_buff.ptr(),tmp_buff, setting_len);
00145     }
00146     break;
00147   default:
00148     break;
00149   }
00150 
00151   return &return_buff;
00152 }