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   boost::to_upper(behavior);
00083   boost::to_upper(setting);
00084 
00085   it_behav= behavior_map.find(behavior);
00086   if (it_behav == behavior_map.end())
00087   {
00088     setFailureString("UNKNOWN BEHAVIOR TYPE!");
00089     return &failure_buff;
00090   }
00091   mbehavior= behavior_map[behavior];
00092 
00093   switch (mbehavior)
00094   {
00095   case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
00096   case MEMCACHED_BEHAVIOR_NO_BLOCK:
00097   case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
00098   case MEMCACHED_BEHAVIOR_USER_DATA:
00099   case MEMCACHED_BEHAVIOR_SORT_HOSTS:
00100   case MEMCACHED_BEHAVIOR_VERIFY_KEY:
00101   case MEMCACHED_BEHAVIOR_TCP_NODELAY:
00102   case MEMCACHED_BEHAVIOR_KETAMA:
00103   case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS:
00104     if (setting.compare("1") == 0)
00105     {
00106       isetting= 1;
00107     }
00108     else if (setting.compare("0") == 0)
00109     {
00110       isetting= 0;
00111     }
00112     else
00113     {
00114       setFailureString("INVALID VALUE FOR BEHAVIOR - MUST be 1 OR 0!");
00115       return &failure_buff;
00116     }
00117     break;
00118   case MEMCACHED_BEHAVIOR_DISTRIBUTION:
00119     it_dist= dist_settings_map.find(setting);
00120     if (it_dist == dist_settings_map.end())
00121     {
00122       setFailureString("INVALID VALUE FOR DISTRIBUTION!");
00123       return &failure_buff;
00124     }
00125     isetting= dist_settings_map[setting];
00126     break;
00127   case MEMCACHED_BEHAVIOR_HASH:
00128     it_hash= hash_settings_map.find(setting);
00129     if (it_hash == hash_settings_map.end())
00130     {
00131       setFailureString("INVALID VALUE FOR MEMCACHED HASH ALGORITHM!");
00132       return &failure_buff;
00133     }
00134     isetting= hash_settings_map[setting];
00135     break;
00136   case MEMCACHED_BEHAVIOR_KETAMA_HASH:
00137     isetting= ketama_hash_settings_map[setting];
00138     if (! isetting)
00139     {
00140       setFailureString("INVALID VALUE FOR KETAMA HASH ALGORITHM!");
00141       return &failure_buff;
00142     }
00143     break;
00144   case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
00145   case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
00146   case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
00147   case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
00148   case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
00149   case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK:
00150   case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK:
00151     /*
00152       What type of check the values passed to these behaviors?
00153       Range?
00154     */
00155     break;
00156   default:
00157     break;
00158   }
00159 
00160   rc= memcached_behavior_set(memc, mbehavior, isetting);
00161 
00162   if (rc != MEMCACHED_SUCCESS)
00163   {
00164     return &failure_buff;
00165   }
00166 
00167   return &success_buff;
00168 }
00169