~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/gearman_udf/function_map.cc

  • Committer: Brian Aker
  • Date: 2009-05-11 23:55:31 UTC
  • mfrom: (971.3.38 eday-dev)
  • Revision ID: brian@gaz-20090511235531-j6n8i97wg2tyfg51
Merge of Eric's work

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
static GearmanFunctionMap _functionMap;
 
23
 
 
24
GearmanFunctionMap& GetFunctionMap(void)
 
25
{
 
26
  return _functionMap;
 
27
}
 
28
 
 
29
GearmanFunctionMap::GearmanFunctionMap()
 
30
{
 
31
  (void) pthread_mutex_init(&lock, NULL);
 
32
}
 
33
 
 
34
GearmanFunctionMap::~GearmanFunctionMap()
 
35
{
 
36
  map<string, gearman_client_st>::iterator x;
 
37
 
 
38
  for (x= functionMap.begin(); x != functionMap.end(); x++)
 
39
    gearman_client_free(&((*x).second));
 
40
 
 
41
  (void) pthread_mutex_destroy(&lock);
 
42
}
 
43
 
 
44
bool GearmanFunctionMap::add(string function, string servers)
 
45
{
 
46
  map<string, gearman_client_st>::iterator x;
 
47
  string host;
 
48
  string port;
 
49
  size_t begin_pos= 0;
 
50
  size_t end_pos;
 
51
  size_t port_pos;
 
52
 
 
53
  pthread_mutex_lock(&lock);
 
54
 
 
55
  x= functionMap.find(function);
 
56
  if (x == functionMap.end())
 
57
  {
 
58
    if (gearman_client_create(&(functionMap[function])) == NULL)
 
59
    {
 
60
      pthread_mutex_unlock(&lock);
 
61
      return false;
 
62
    }
 
63
  }
 
64
 
 
65
  while (1)
 
66
  {
 
67
    end_pos= servers.find(',', begin_pos);
 
68
    if (end_pos == string::npos)
 
69
      host= servers.substr(begin_pos);
 
70
    else
 
71
      host= servers.substr(begin_pos, end_pos - begin_pos);
 
72
 
 
73
    port_pos= host.find(':');
 
74
    if (port_pos == string::npos)
 
75
      port.clear();
 
76
    else
 
77
    {
 
78
      port= host.substr(port_pos + 1);
 
79
      host[port_pos]= 0;
 
80
    }
 
81
 
 
82
    if (gearman_client_add_server(&(functionMap[function]), host.c_str(),
 
83
                                  port.size() == 0 ?
 
84
                                  0 : atoi(port.c_str())) != GEARMAN_SUCCESS)
 
85
    {
 
86
      pthread_mutex_unlock(&lock);
 
87
      return false;
 
88
    }
 
89
 
 
90
    if (end_pos == string::npos)
 
91
      break;
 
92
 
 
93
    begin_pos= end_pos + 1;
 
94
  }
 
95
 
 
96
  pthread_mutex_unlock(&lock);
 
97
  return true;
 
98
}
 
99
 
 
100
bool GearmanFunctionMap::get(string function, gearman_client_st *client)
 
101
{
 
102
  map<string, gearman_client_st>::iterator x;
 
103
 
 
104
  pthread_mutex_lock(&lock);
 
105
 
 
106
  x= functionMap.find(function);
 
107
  if (x == functionMap.end())
 
108
  {
 
109
    x= functionMap.find(string(""));
 
110
    if (x == functionMap.end())
 
111
    {
 
112
      pthread_mutex_unlock(&lock);
 
113
      return false;
 
114
    }
 
115
  }
 
116
 
 
117
  if (gearman_client_clone(client, &((*x).second)) == NULL)
 
118
  {
 
119
    pthread_mutex_unlock(&lock);
 
120
    return false;
 
121
  }
 
122
 
 
123
  pthread_mutex_unlock(&lock);
 
124
  return true;
 
125
}