Drizzled Public API Documentation

memc_behavior_set.cc
1 /* - mode: c; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Copyright (C) 2009, Patrick "CaptTofu" Galbraith, Padraig O'Sullivan
5  * All rights reserved.
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * * Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the following disclaimer.
12  * * Redistributions in binary form must reproduce the above copyright notice,
13  * this list of conditions and the following disclaimer in the documentation
14  * and/or other materials provided with the distribution.
15  * * Neither the name of Patrick Galbraith nor the names of its contributors
16  * may be used to endorse or promote products derived from this software
17  * without specific prior written permission.
18  *
19  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
20  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
23  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
29  * THE POSSIBILITY OF SUCH DAMAGE.
30  */
31 
32 #include <config.h>
33 #include <drizzled/item/func.h>
34 #include <drizzled/function/str/strfunc.h>
35 
36 #include "memcached_functions.h"
37 #include "memc_behavior_set.h"
38 
39 #include <libmemcached/memcached.h>
40 
41 #include <string>
42 #include <algorithm>
43 
44 using namespace std;
45 using namespace drizzled;
46 
47 void MemcachedBehaviorSet::setFailureString(const char *error)
48 {
49  size_t size= strlen(error);
50  failure_buff.realloc(size);
51  failure_buff.length(size);
52  memcpy(failure_buff.ptr(), error, size);
53 }
54 
56 {
57  memcached_return rc;
58  memcached_behavior mbehavior;
59  uint64_t isetting= 0;
60  map<const string, memcached_behavior>::iterator it_behav;
61  map<const string, uint64_t>::iterator it_hash;
62  map<const string, uint64_t>::iterator it_dist;
63  String *tmp_behavior;
64  String *tmp_setting;
65 
66  if (arg_count != 2 ||
67  ! (tmp_behavior= args[0]->val_str(str)) ||
68  ! (tmp_setting= args[1]->val_str(str)) ||
69  ! memc)
70  {
71  setFailureString("USAGE: memc_behavior_set('<behavior type>','<value>')");
72  return &failure_buff;
73  }
74 
75  string behavior(tmp_behavior->c_ptr());
76  string setting(tmp_setting->c_ptr());
77 
78  /*
79  * We don't want the user to have to type in all input in upper
80  * case so we transform the input strings to upper case here.
81  */
82  boost::to_upper(behavior);
83  boost::to_upper(setting);
84 
85  it_behav= behavior_map.find(behavior);
86  if (it_behav == behavior_map.end())
87  {
88  setFailureString("UNKNOWN BEHAVIOR TYPE!");
89  return &failure_buff;
90  }
91  mbehavior= behavior_map[behavior];
92 
93  switch (mbehavior)
94  {
95  case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
96  case MEMCACHED_BEHAVIOR_NO_BLOCK:
97  case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
98  case MEMCACHED_BEHAVIOR_USER_DATA:
99  case MEMCACHED_BEHAVIOR_SORT_HOSTS:
100  case MEMCACHED_BEHAVIOR_VERIFY_KEY:
101  case MEMCACHED_BEHAVIOR_TCP_NODELAY:
102  case MEMCACHED_BEHAVIOR_KETAMA:
103  case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS:
104  if (setting.compare("1") == 0)
105  {
106  isetting= 1;
107  }
108  else if (setting.compare("0") == 0)
109  {
110  isetting= 0;
111  }
112  else
113  {
114  setFailureString("INVALID VALUE FOR BEHAVIOR - MUST be 1 OR 0!");
115  return &failure_buff;
116  }
117  break;
118  case MEMCACHED_BEHAVIOR_DISTRIBUTION:
119  it_dist= dist_settings_map.find(setting);
120  if (it_dist == dist_settings_map.end())
121  {
122  setFailureString("INVALID VALUE FOR DISTRIBUTION!");
123  return &failure_buff;
124  }
125  isetting= dist_settings_map[setting];
126  break;
127  case MEMCACHED_BEHAVIOR_HASH:
128  it_hash= hash_settings_map.find(setting);
129  if (it_hash == hash_settings_map.end())
130  {
131  setFailureString("INVALID VALUE FOR MEMCACHED HASH ALGORITHM!");
132  return &failure_buff;
133  }
134  isetting= hash_settings_map[setting];
135  break;
136  case MEMCACHED_BEHAVIOR_KETAMA_HASH:
137  isetting= ketama_hash_settings_map[setting];
138  if (! isetting)
139  {
140  setFailureString("INVALID VALUE FOR KETAMA HASH ALGORITHM!");
141  return &failure_buff;
142  }
143  break;
144  case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
145  case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
146  case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
147  case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
148  case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
149  case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK:
150  case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK:
151  /*
152  What type of check the values passed to these behaviors?
153  Range?
154  */
155  break;
156  default:
157  break;
158  }
159 
160  rc= memcached_behavior_set(memc, mbehavior, isetting);
161 
162  if (rc != MEMCACHED_SUCCESS)
163  {
164  return &failure_buff;
165  }
166 
167  return &success_buff;
168 }
169 
drizzled::String * val_str(drizzled::String *)