drizzle
C++ MySQL-protocol-compatible database server
Loading...
Searching...
No Matches
function_map.cc
1/* Copyright (C) 2009 Sun Microsystems, Inc.
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 <config.h>
17
18#include "plugin/gearman_udf/function_map.h"
19
20#include <libgearman/gearman.h>
21#include <cstring>
22#include <cstdlib>
23
24using namespace std;
25
26/* Constructor and destructor happen during module dlopen/dlclose. */
27static GearmanFunctionMap _functionMap;
28
29GearmanFunctionMap& GetFunctionMap(void)
30{
31 return _functionMap;
32}
33
34GearmanFunctionMap::GearmanFunctionMap()
35{
36 (void) pthread_mutex_init(&lock, NULL);
37}
38
39GearmanFunctionMap::~GearmanFunctionMap()
40{
41 map<string, gearman_client_st>::iterator x;
42
43 for (x= functionMap.begin(); x != functionMap.end(); x++)
44 gearman_client_free(&((*x).second));
45
46 (void) pthread_mutex_destroy(&lock);
47}
48
49bool GearmanFunctionMap::add(string function, string servers)
50{
51 map<string, gearman_client_st>::iterator x;
52 gearman_return_t ret;
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 gearman_client_remove_servers(&(functionMap[function]));
67 ret= gearman_client_add_servers(&(functionMap[function]), servers.c_str());
68 pthread_mutex_unlock(&lock);
69 if (ret != GEARMAN_SUCCESS)
70 {
71 return false;
72 }
73
74 return true;
75}
76
77bool GearmanFunctionMap::get(string function, gearman_client_st *client)
78{
79 map<string, gearman_client_st>::iterator x;
80
81 if (pthread_mutex_lock(&lock) != 0)
82 {
83 return false;
84 }
85
86 x= functionMap.find(function);
87 if (x == functionMap.end())
88 {
89 x= functionMap.find(string(""));
90 if (x == functionMap.end())
91 {
92 pthread_mutex_unlock(&lock);
93 return false;
94 }
95 }
96
97 /* Clone the object, the list of host:port pairs get cloned with it. */
98 if (gearman_client_clone(client, &((*x).second)) == NULL)
99 {
100 pthread_mutex_unlock(&lock);
101 return false;
102 }
103
104 pthread_mutex_unlock(&lock);
105 return true;
106}
STL namespace.