~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/gearman_udf/function_map.cc

MergedĀ inĀ trunk.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* Copyright (C) 2009 Sun Microsystems
 
2
 
 
3
   This program is free software; you can redistribute it and/or modify
 
4
   it under the terms of the GNU General Public License as published by
 
5
   the Free Software Foundation; version 2 of the License.
 
6
 
 
7
   This program is distributed in the hope that it will be useful,
 
8
   but WITHOUT ANY WARRANTY; without even the implied warranty of
 
9
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
10
   GNU General Public License for more details.
 
11
 
 
12
   You should have received a copy of the GNU General Public License
 
13
   along with this program; if not, write to the Free Software
 
14
   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
 
15
 
 
16
#include "function_map.h"
 
17
#include <string.h>
 
18
#include <stdlib.h>
 
19
 
 
20
using namespace std;
 
21
 
 
22
/* Constructor and destructor happen during module dlopen/dlclose. */
 
23
static GearmanFunctionMap _functionMap;
 
24
 
 
25
GearmanFunctionMap& GetFunctionMap(void)
 
26
{
 
27
  return _functionMap;
 
28
}
 
29
 
 
30
GearmanFunctionMap::GearmanFunctionMap()
 
31
{
 
32
  (void) pthread_mutex_init(&lock, NULL);
 
33
}
 
34
 
 
35
GearmanFunctionMap::~GearmanFunctionMap()
 
36
{
 
37
  map<string, gearman_client_st>::iterator x;
 
38
 
 
39
  for (x= functionMap.begin(); x != functionMap.end(); x++)
 
40
    gearman_client_free(&((*x).second));
 
41
 
 
42
  (void) pthread_mutex_destroy(&lock);
 
43
}
 
44
 
 
45
bool GearmanFunctionMap::add(string function, string servers)
 
46
{
 
47
  map<string, gearman_client_st>::iterator x;
 
48
  string host;
 
49
  string port;
 
50
  size_t begin_pos= 0;
 
51
  size_t end_pos;
 
52
  size_t port_pos;
 
53
 
 
54
  pthread_mutex_lock(&lock);
 
55
 
 
56
  x= functionMap.find(function);
 
57
  if (x == functionMap.end())
 
58
  {
 
59
    if (gearman_client_create(&(functionMap[function])) == NULL)
 
60
    {
 
61
      pthread_mutex_unlock(&lock);
 
62
      return false;
 
63
    }
 
64
  }
 
65
 
 
66
  /* Parse server strings in the format "host[:port][,host[:port]]..." */
 
67
  while (1)
 
68
  {
 
69
    end_pos= servers.find(',', begin_pos);
 
70
    if (end_pos == string::npos)
 
71
      host= servers.substr(begin_pos);
 
72
    else
 
73
      host= servers.substr(begin_pos, end_pos - begin_pos);
 
74
 
 
75
    port_pos= host.find(':');
 
76
    if (port_pos == string::npos)
 
77
      port.clear();
 
78
    else
 
79
    {
 
80
      port= host.substr(port_pos + 1);
 
81
      host[port_pos]= 0;
 
82
    }
 
83
 
 
84
    /* For each host:port pair, add a server to the cloning object. */
 
85
    if (gearman_client_add_server(&(functionMap[function]), host.c_str(),
 
86
                                  port.size() == 0 ?
 
87
                                  0 : atoi(port.c_str())) != GEARMAN_SUCCESS)
 
88
    {
 
89
      pthread_mutex_unlock(&lock);
 
90
      return false;
 
91
    }
 
92
 
 
93
    if (end_pos == string::npos)
 
94
      break;
 
95
 
 
96
    begin_pos= end_pos + 1;
 
97
  }
 
98
 
 
99
  pthread_mutex_unlock(&lock);
 
100
  return true;
 
101
}
 
102
 
 
103
bool GearmanFunctionMap::get(string function, gearman_client_st *client)
 
104
{
 
105
  map<string, gearman_client_st>::iterator x;
 
106
 
 
107
  pthread_mutex_lock(&lock);
 
108
 
 
109
  x= functionMap.find(function);
 
110
  if (x == functionMap.end())
 
111
  {
 
112
    x= functionMap.find(string(""));
 
113
    if (x == functionMap.end())
 
114
    {
 
115
      pthread_mutex_unlock(&lock);
 
116
      return false;
 
117
    }
 
118
  }
 
119
 
 
120
  /* Clone the object, the list of host:port pairs get cloned with it. */
 
121
  if (gearman_client_clone(client, &((*x).second)) == NULL)
 
122
  {
 
123
    pthread_mutex_unlock(&lock);
 
124
    return false;
 
125
  }
 
126
 
 
127
  pthread_mutex_unlock(&lock);
 
128
  return true;
 
129
}