drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
memc_behavior_get.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_get.h"
38
39#include <libmemcached/memcached.h>
40
41#include <string>
42#include <algorithm>
43
44using namespace std;
45using namespace drizzled;
46
47void MemcachedBehaviorGet::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 String *tmp_behavior;
58
59 if (arg_count != 1 ||
60 ! (tmp_behavior= args[0]->val_str(str)) ||
61 ! memc)
62 {
63 setFailureString("USAGE: memc_behavior_get('<behavior type>')");
64 return &failure_buff;
65 }
66
67 /*
68 * We don't want the user to have to type in all input in upper
69 * case so we transform the input strings to upper case here.
70 */
71
72 memcached_behavior* it = find_ptr(behavior_map, boost::to_upper_copy(string(tmp_behavior->c_ptr())));
73 if (not it)
74 {
75 setFailureString("UNKNOWN BEHAVIOR TYPE!");
76 return &failure_buff;
77 }
78
79 memcached_behavior mbehavior= *it;
80
81 uint64_t isetting= memcached_behavior_get(memc, mbehavior);
82
83 switch (mbehavior)
84 {
85 case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
86 case MEMCACHED_BEHAVIOR_NO_BLOCK:
87 case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
88 case MEMCACHED_BEHAVIOR_USER_DATA:
89 case MEMCACHED_BEHAVIOR_SORT_HOSTS:
90 case MEMCACHED_BEHAVIOR_VERIFY_KEY:
91 case MEMCACHED_BEHAVIOR_TCP_NODELAY:
92 case MEMCACHED_BEHAVIOR_KETAMA:
93 case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS:
94 if (isetting == 1)
95 return_buff.append("1");
96 else if (isetting == 0)
97 return_buff.append("0");
98 else
99 {
100 setFailureString("INVALID VALUE FOR BEHAVIOR - MUST be 1 OR 0!");
101 return &failure_buff;
102 }
103 break;
104 case MEMCACHED_BEHAVIOR_DISTRIBUTION:
105 {
106 string setting(dist_settings_reverse_map[isetting]);
107 return_buff.append(setting.c_str());
108 }
109 break;
110 case MEMCACHED_BEHAVIOR_HASH:
111 {
112 string setting(hash_settings_reverse_map[isetting]);
113 return_buff.append(setting.c_str());
114 }
115 break;
116 case MEMCACHED_BEHAVIOR_KETAMA_HASH:
117 {
118 string setting(ketama_hash_settings_reverse_map[isetting]);
119 return_buff.append(setting.c_str());
120 }
121 break;
122 case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
123 case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
124 case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
125 case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
126 case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
127 case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK:
128 case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK:
129 {
130 size_t setting_len= 0;
131 char tmp_buff[16];
132
133 snprintf(tmp_buff, 16, "%" PRIu64, isetting);
134 setting_len= strlen(tmp_buff);
135 return_buff.realloc(setting_len);
136 return_buff.length(setting_len);
137 memcpy(return_buff.ptr(),tmp_buff, setting_len);
138 }
139 break;
140 default:
141 break;
142 }
143
144 return &return_buff;
145}
drizzled::String * val_str(drizzled::String *)
TODO: Rename this file - func.h is stupid.
STL namespace.