Drizzled Public API Documentation

gman_do.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 <config.h>
00017 #include "gman_do.h"
00018 #include "function_map.h"
00019 
00020 using namespace std;
00021 using namespace drizzled;
00022 
00023 extern "C"
00024 {
00025   static void *_do_malloc(size_t size, void *arg)
00026   {
00027     Item_func_gman_do *item= (Item_func_gman_do *)arg;
00028     return item->realloc(size);
00029   }
00030 }
00031 
00032 Item_func_gman_do::~Item_func_gman_do()
00033 {
00034   if (options & GMAN_DO_OPTIONS_CLIENT)
00035     gearman_client_free(&client);
00036 }
00037 
00038 String *Item_func_gman_do::val_str(String *str)
00039 {
00040   String *function;
00041   String *res;
00042   const char *unique;
00043   const char *workload;
00044   size_t workload_size;
00045   size_t result_size;
00046   gearman_return_t ret;
00047   char job_handle[GEARMAN_JOB_HANDLE_SIZE];
00048 
00049   if (arg_count < 1 || arg_count > 3 || !(function= args[0]->val_str(str)))
00050   {
00051     null_value= 1;
00052     return NULL;
00053   }
00054 
00055   if (arg_count > 1 && (res= args[1]->val_str(str)) != NULL)
00056   {
00057     workload= res->ptr();
00058     workload_size= res->length();
00059   }
00060   else
00061   {
00062     workload= NULL;
00063     workload_size= 0;
00064   }
00065 
00066   if (arg_count == 3 && (res= args[2]->val_str(str)) != NULL)
00067     unique= res->ptr();
00068   else
00069     unique= NULL;
00070 
00071   if (!(options & GMAN_DO_OPTIONS_CLIENT))
00072   {
00073     if (!GetFunctionMap().get(string(function->ptr()), &client))
00074     {
00075       null_value= 1;
00076       return NULL;
00077     }
00078 
00079     gearman_client_set_workload_malloc_fn(&client, _do_malloc, this);
00080     options= (gman_do_options_t)(options | GMAN_DO_OPTIONS_CLIENT);
00081   }
00082 
00083   if (options & GMAN_DO_OPTIONS_BACKGROUND)
00084   {
00085     if (options & GMAN_DO_OPTIONS_HIGH)
00086     {
00087       ret= gearman_client_do_high_background(&client, function->ptr(), unique,
00088                                              workload, workload_size,
00089                                              job_handle);
00090     }
00091     else if (options & GMAN_DO_OPTIONS_LOW)
00092     {
00093       ret= gearman_client_do_low_background(&client, function->ptr(), unique,
00094                                             workload, workload_size,
00095                                             job_handle);
00096     }
00097     else
00098     {
00099       ret= gearman_client_do_background(&client, function->ptr(), unique,
00100                                         workload, workload_size, job_handle);
00101     }
00102 
00103     if (ret == GEARMAN_SUCCESS)
00104     {
00105       result_size= strlen(job_handle);
00106       buffer.realloc(result_size);
00107       buffer.length(result_size);
00108       memcpy(buffer.ptr(), job_handle, result_size);
00109     }
00110   }
00111   else
00112   {
00113     if (options & GMAN_DO_OPTIONS_HIGH)
00114     {
00115       (void) gearman_client_do_high(&client, function->ptr(), unique, workload,
00116                                     workload_size, &result_size, &ret);
00117     }
00118     else if (options & GMAN_DO_OPTIONS_LOW)
00119     {
00120       (void) gearman_client_do_low(&client, function->ptr(), unique, workload,
00121                                    workload_size, &result_size, &ret);
00122     }
00123     else
00124     {
00125       (void) gearman_client_do(&client, function->ptr(), unique, workload,
00126                                workload_size, &result_size, &ret);
00127     }
00128   }
00129 
00130   if (ret != GEARMAN_SUCCESS)
00131   {
00132     null_value= 1;
00133     return NULL;
00134   }
00135 
00136   null_value= 0;
00137   return &buffer;
00138 }
00139 
00140 void *Item_func_gman_do::realloc(size_t size)
00141 {
00142   buffer.realloc(size);
00143   buffer.length(size);
00144   return buffer.ptr();
00145 }