~drizzle-trunk/drizzle/development

1999.6.1 by kalebral at gmail
update Copyright strings to a more common format to help with creating the master debian copyright file
1
/* Copyright (C) 2009 Sun Microsystems, Inc.
971.3.33 by Eric Day
Gearman UDFs complete.
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"
1619 by Monty Taylor
Added include.
17
#include <libgearman/client.h>
971.3.33 by Eric Day
Gearman UDFs complete.
18
#include <string.h>
19
#include <stdlib.h>
20
21
using namespace std;
22
971.3.39 by Eric Day
A little cleanup and some comments.
23
/* Constructor and destructor happen during module dlopen/dlclose. */
971.3.33 by Eric Day
Gearman UDFs complete.
24
static GearmanFunctionMap _functionMap;
25
26
GearmanFunctionMap& GetFunctionMap(void)
27
{
28
  return _functionMap;
29
}
30
31
GearmanFunctionMap::GearmanFunctionMap()
32
{
33
  (void) pthread_mutex_init(&lock, NULL);
34
}
35
36
GearmanFunctionMap::~GearmanFunctionMap()
37
{
38
  map<string, gearman_client_st>::iterator x;
39
40
  for (x= functionMap.begin(); x != functionMap.end(); x++)
41
    gearman_client_free(&((*x).second));
42
43
  (void) pthread_mutex_destroy(&lock);
44
}
45
46
bool GearmanFunctionMap::add(string function, string servers)
47
{
48
  map<string, gearman_client_st>::iterator x;
1144.1.1 by Eric Day
Updated Gearman UDF to work with gearmand-0.10 release.
49
  gearman_return_t ret;
971.3.33 by Eric Day
Gearman UDFs complete.
50
51
  pthread_mutex_lock(&lock);
52
53
  x= functionMap.find(function);
54
  if (x == functionMap.end())
55
  {
56
    if (gearman_client_create(&(functionMap[function])) == NULL)
57
    {
58
      pthread_mutex_unlock(&lock);
59
      return false;
60
    }
61
  }
62
1144.1.1 by Eric Day
Updated Gearman UDF to work with gearmand-0.10 release.
63
  gearman_client_remove_servers(&(functionMap[function]));
64
  ret= gearman_client_add_servers(&(functionMap[function]), servers.c_str());
971.3.33 by Eric Day
Gearman UDFs complete.
65
  pthread_mutex_unlock(&lock);
1144.1.1 by Eric Day
Updated Gearman UDF to work with gearmand-0.10 release.
66
  if (ret != GEARMAN_SUCCESS)
67
    return false;
68
971.3.33 by Eric Day
Gearman UDFs complete.
69
  return true;
70
}
71
72
bool GearmanFunctionMap::get(string function, gearman_client_st *client)
73
{
74
  map<string, gearman_client_st>::iterator x;
75
76
  pthread_mutex_lock(&lock);
77
78
  x= functionMap.find(function);
79
  if (x == functionMap.end())
80
  {
81
    x= functionMap.find(string(""));
82
    if (x == functionMap.end())
83
    {
84
      pthread_mutex_unlock(&lock);
85
      return false;
86
    }
87
  }
88
971.3.39 by Eric Day
A little cleanup and some comments.
89
  /* Clone the object, the list of host:port pairs get cloned with it. */
971.3.33 by Eric Day
Gearman UDFs complete.
90
  if (gearman_client_clone(client, &((*x).second)) == NULL)
91
  {
92
    pthread_mutex_unlock(&lock);
93
    return false;
94
  }
95
96
  pthread_mutex_unlock(&lock);
97
  return true;
98
}