Drizzled Public API Documentation

function_map.cc
00001 /* Copyright (C) 2009 Sun Microsystems, Inc.
00002 
00003    This program is free software; you can redistribute it and/or modify
00004    it under the terms of the GNU General Public License as published by
00005    the Free Software Foundation; version 2 of the License.
00006 
00007    This program is distributed in the hope that it will be useful,
00008    but WITHOUT ANY WARRANTY; without even the implied warranty of
00009    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00010    GNU General Public License for more details.
00011 
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software
00014    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA */
00015 
00016 #include "function_map.h"
00017 #include <libgearman-1.0/client.h>
00018 #include <string.h>
00019 #include <stdlib.h>
00020 
00021 using namespace std;
00022 
00023 /* Constructor and destructor happen during module dlopen/dlclose. */
00024 static GearmanFunctionMap _functionMap;
00025 
00026 GearmanFunctionMap& GetFunctionMap(void)
00027 {
00028   return _functionMap;
00029 }
00030 
00031 GearmanFunctionMap::GearmanFunctionMap()
00032 {
00033   (void) pthread_mutex_init(&lock, NULL);
00034 }
00035 
00036 GearmanFunctionMap::~GearmanFunctionMap()
00037 {
00038   map<string, gearman_client_st>::iterator x;
00039 
00040   for (x= functionMap.begin(); x != functionMap.end(); x++)
00041     gearman_client_free(&((*x).second));
00042 
00043   (void) pthread_mutex_destroy(&lock);
00044 }
00045 
00046 bool GearmanFunctionMap::add(string function, string servers)
00047 {
00048   map<string, gearman_client_st>::iterator x;
00049   gearman_return_t ret;
00050 
00051   pthread_mutex_lock(&lock);
00052 
00053   x= functionMap.find(function);
00054   if (x == functionMap.end())
00055   {
00056     if (gearman_client_create(&(functionMap[function])) == NULL)
00057     {
00058       pthread_mutex_unlock(&lock);
00059       return false;
00060     }
00061   }
00062 
00063   gearman_client_remove_servers(&(functionMap[function]));
00064   ret= gearman_client_add_servers(&(functionMap[function]), servers.c_str());
00065   pthread_mutex_unlock(&lock);
00066   if (ret != GEARMAN_SUCCESS)
00067     return false;
00068 
00069   return true;
00070 }
00071 
00072 bool GearmanFunctionMap::get(string function, gearman_client_st *client)
00073 {
00074   map<string, gearman_client_st>::iterator x;
00075 
00076   pthread_mutex_lock(&lock);
00077 
00078   x= functionMap.find(function);
00079   if (x == functionMap.end())
00080   {
00081     x= functionMap.find(string(""));
00082     if (x == functionMap.end())
00083     {
00084       pthread_mutex_unlock(&lock);
00085       return false;
00086     }
00087   }
00088 
00089   /* Clone the object, the list of host:port pairs get cloned with it. */
00090   if (gearman_client_clone(client, &((*x).second)) == NULL)
00091   {
00092     pthread_mutex_unlock(&lock);
00093     return false;
00094   }
00095 
00096   pthread_mutex_unlock(&lock);
00097   return true;
00098 }