~drizzle-trunk/drizzle/development

« back to all changes in this revision

Viewing changes to plugin/gearman_udf/function_map.cc

  • Committer: brian
  • Date: 2008-06-25 05:29:13 UTC
  • Revision ID: brian@localhost.localdomain-20080625052913-6upwo0jsrl4lnapl
clean slate

Show diffs side-by-side

added added

removed removed

Lines of Context:
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 "function_map.h"
17
 
#include <libgearman/client.h>
18
 
#include <string.h>
19
 
#include <stdlib.h>
20
 
 
21
 
using namespace std;
22
 
 
23
 
/* Constructor and destructor happen during module dlopen/dlclose. */
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;
49
 
  gearman_return_t ret;
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
 
 
63
 
  gearman_client_remove_servers(&(functionMap[function]));
64
 
  ret= gearman_client_add_servers(&(functionMap[function]), servers.c_str());
65
 
  pthread_mutex_unlock(&lock);
66
 
  if (ret != GEARMAN_SUCCESS)
67
 
    return false;
68
 
 
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
 
 
89
 
  /* Clone the object, the list of host:port pairs get cloned with it. */
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
 
}