Drizzled Public API Documentation

memc_behavior_set.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_set.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 MemcachedBehaviorSet::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 *MemcachedBehaviorSet::val_str(String *str)
00056 {
00057   memcached_return rc;
00058   memcached_behavior mbehavior;
00059   uint64_t isetting= 0;
00060   map<const string, memcached_behavior>::iterator it_behav;
00061   map<const string, uint64_t>::iterator it_hash;
00062   map<const string, uint64_t>::iterator it_dist;
00063   String *tmp_behavior;
00064   String *tmp_setting;
00065 
00066   if (arg_count != 2 ||
00067       ! (tmp_behavior= args[0]->val_str(str)) ||
00068       ! (tmp_setting= args[1]->val_str(str)) ||
00069       ! memc)
00070   {
00071     setFailureString("USAGE: memc_behavior_set('<behavior type>','<value>')");
00072     return &failure_buff;
00073   }
00074 
00075   string behavior(tmp_behavior->c_ptr());
00076   string setting(tmp_setting->c_ptr());
00077 
00078   /*
00079    * We don't want the user to have to type in all input in upper
00080    * case so we transform the input strings to upper case here.
00081    */
00082   std::transform(behavior.begin(), behavior.end(),
00083                  behavior.begin(), ::toupper);
00084   std::transform(setting.begin(), setting.end(),
00085                  setting.begin(), ::toupper);
00086 
00087   it_behav= behavior_map.find(behavior);
00088   if (it_behav == behavior_map.end())
00089   {
00090     setFailureString("UNKNOWN BEHAVIOR TYPE!");
00091     return &failure_buff;
00092   }
00093   mbehavior= behavior_map[behavior];
00094 
00095   switch (mbehavior)
00096   {
00097   case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
00098   case MEMCACHED_BEHAVIOR_NO_BLOCK:
00099   case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
00100   case MEMCACHED_BEHAVIOR_USER_DATA:
00101   case MEMCACHED_BEHAVIOR_SORT_HOSTS:
00102   case MEMCACHED_BEHAVIOR_VERIFY_KEY:
00103   case MEMCACHED_BEHAVIOR_TCP_NODELAY:
00104   case MEMCACHED_BEHAVIOR_KETAMA:
00105   case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS:
00106     if (setting.compare("1") == 0)
00107     {
00108       isetting= 1;
00109     }
00110     else if (setting.compare("0") == 0)
00111     {
00112       isetting= 0;
00113     }
00114     else
00115     {
00116       setFailureString("INVALID VALUE FOR BEHAVIOR - MUST be 1 OR 0!");
00117       return &failure_buff;
00118     }
00119     break;
00120   case MEMCACHED_BEHAVIOR_DISTRIBUTION:
00121     it_dist= dist_settings_map.find(setting);
00122     if (it_dist == dist_settings_map.end())
00123     {
00124       setFailureString("INVALID VALUE FOR DISTRIBUTION!");
00125       return &failure_buff;
00126     }
00127     isetting= dist_settings_map[setting];
00128     break;
00129   case MEMCACHED_BEHAVIOR_HASH:
00130     it_hash= hash_settings_map.find(setting);
00131     if (it_hash == hash_settings_map.end())
00132     {
00133       setFailureString("INVALID VALUE FOR MEMCACHED HASH ALGORITHM!");
00134       return &failure_buff;
00135     }
00136     isetting= hash_settings_map[setting];
00137     break;
00138   case MEMCACHED_BEHAVIOR_KETAMA_HASH:
00139     isetting= ketama_hash_settings_map[setting];
00140     if (! isetting)
00141     {
00142       setFailureString("INVALID VALUE FOR KETAMA HASH ALGORITHM!");
00143       return &failure_buff;
00144     }
00145     break;
00146   case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
00147   case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
00148   case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
00149   case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
00150   case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
00151   case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK:
00152   case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK:
00153     /*
00154       What type of check the values passed to these behaviors?
00155       Range?
00156     */
00157     break;
00158   default:
00159     break;
00160   }
00161 
00162   rc= memcached_behavior_set(memc, mbehavior, isetting);
00163 
00164   if (rc != MEMCACHED_SUCCESS)
00165   {
00166     return &failure_buff;
00167   }
00168 
00169   return &success_buff;
00170 }
00171