~drizzle-trunk/drizzle/development

1166.5.1 by Patrick Galbraith
Starting over with a fresh tree, moved in memcached functions.
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
1241.9.36 by Monty Taylor
ZOMG. I deleted drizzled/server_includes.h.
32
#include "config.h"
1166.5.1 by Patrick Galbraith
Starting over with a fresh tree, moved in memcached functions.
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
44
using namespace std;
1280.1.10 by Monty Taylor
Put everything in drizzled into drizzled namespace.
45
using namespace drizzled;
1166.5.1 by Patrick Galbraith
Starting over with a fresh tree, moved in memcached functions.
46
47
void 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
55
String *MemcachedBehaviorGet::val_str(String *str)
56
{
57
  memcached_behavior mbehavior;
58
  uint64_t isetting;
59
  map<const string, memcached_behavior>::iterator it;
60
  String *tmp_behavior;
61
62
  if (arg_count != 1 ||
63
      ! (tmp_behavior= args[0]->val_str(str)) ||
64
      ! memc)
65
  {
66
    setFailureString("USAGE: memc_behavior_get('<behavior type>')");
67
    return &failure_buff;
68
  }
69
70
  string behavior(tmp_behavior->c_ptr());
71
72
  /*
73
   * We don't want the user to have to type in all input in upper
74
   * case so we transform the input strings to upper case here.
75
   */
76
  std::transform(behavior.begin(), behavior.end(),
77
                 behavior.begin(), ::toupper);
78
79
  it = behavior_map.find(behavior);
80
  if (it == behavior_map.end()) 
81
  {
82
    setFailureString("UNKNOWN BEHAVIOR TYPE!");
83
    return &failure_buff;
84
  }
85
	
86
  mbehavior= behavior_map[behavior];
87
88
  isetting= memcached_behavior_get(memc, mbehavior);
89
90
  switch (mbehavior)
91
  {
92
  case MEMCACHED_BEHAVIOR_SUPPORT_CAS:
93
  case MEMCACHED_BEHAVIOR_NO_BLOCK:
94
  case MEMCACHED_BEHAVIOR_BUFFER_REQUESTS:
95
  case MEMCACHED_BEHAVIOR_USER_DATA:
96
  case MEMCACHED_BEHAVIOR_SORT_HOSTS:
97
  case MEMCACHED_BEHAVIOR_VERIFY_KEY:
98
  case MEMCACHED_BEHAVIOR_TCP_NODELAY:
99
  case MEMCACHED_BEHAVIOR_KETAMA:
100
  case MEMCACHED_BEHAVIOR_CACHE_LOOKUPS:
101
    if (isetting == 1)
102
      return_buff.append("1");
103
    else if (isetting == 0)
104
      return_buff.append("0");
105
    else
106
    {
107
      setFailureString("INVALID VALUE FOR BEHAVIOR - MUST be 1 OR 0!");
108
      return &failure_buff;
109
    }
110
    break;
111
  case MEMCACHED_BEHAVIOR_DISTRIBUTION:
112
    {
113
      string setting(dist_settings_reverse_map[isetting]);
114
      return_buff.append(setting.c_str());
115
    }
116
    break;
117
  case MEMCACHED_BEHAVIOR_HASH:
118
    {
119
      string setting(hash_settings_reverse_map[isetting]);
120
      return_buff.append(setting.c_str());
121
    }
122
    break;
123
  case MEMCACHED_BEHAVIOR_KETAMA_HASH:
124
    {
125
      string setting(ketama_hash_settings_reverse_map[isetting]);
126
      return_buff.append(setting.c_str());
127
    }
128
    break;
129
  case MEMCACHED_BEHAVIOR_SOCKET_SEND_SIZE:
130
  case MEMCACHED_BEHAVIOR_SOCKET_RECV_SIZE:
131
  case MEMCACHED_BEHAVIOR_POLL_TIMEOUT:
132
  case MEMCACHED_BEHAVIOR_CONNECT_TIMEOUT:
133
  case MEMCACHED_BEHAVIOR_RETRY_TIMEOUT:
134
  case MEMCACHED_BEHAVIOR_IO_MSG_WATERMARK:
135
  case MEMCACHED_BEHAVIOR_IO_BYTES_WATERMARK:
136
    {
137
      size_t setting_len= 0;
138
      char tmp_buff[16];
139
140
      snprintf(tmp_buff, 16, "%"PRIu64, isetting);
141
      setting_len= strlen(tmp_buff);
142
      return_buff.realloc(setting_len);
143
      return_buff.length(setting_len);
144
      memcpy(return_buff.ptr(),tmp_buff, setting_len);
145
    }
146
    break;
147
  default:
148
    break;
149
  }
150
151
  return &return_buff;
152
}