~drizzle-trunk/drizzle/development

971.3.37 by Eric Day
Fixed copyright on gearman_udf plugin, was on Sun time. :)
1
/* Copyright (C) 2009 Sun Microsystems
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 "gman_do.h"
17
#include "function_map.h"
18
19
using namespace std;
20
971.3.36 by Eric Day
Solaris fixes for Gearman UDF.
21
extern "C"
22
{
23
  static void *_do_malloc(size_t size, void *arg)
24
  {
25
    Item_func_gman_do *item= (Item_func_gman_do *)arg;
26
    return item->realloc(size);
27
  }
28
}
971.3.33 by Eric Day
Gearman UDFs complete.
29
30
Item_func_gman_do::~Item_func_gman_do()
31
{
32
  if (options & GMAN_DO_OPTIONS_CLIENT)
33
    gearman_client_free(&client);
34
}
35
36
String *Item_func_gman_do::val_str(String *str)
37
{
38
  String *function;
39
  String *res;
40
  const char *unique;
41
  const char *workload;
42
  size_t workload_size;
43
  size_t result_size;
44
  gearman_return_t ret;
45
  char job_handle[GEARMAN_JOB_HANDLE_SIZE];
46
47
  if (arg_count < 1 || arg_count > 3 || !(function= args[0]->val_str(str)))
48
  {
49
    null_value= 1;
50
    return NULL;
51
  }
52
53
  if (arg_count > 1 && (res= args[1]->val_str(str)) != NULL)
54
  {
55
    workload= res->ptr();
56
    workload_size= res->length();
57
  }
58
  else
59
  {
60
    workload= NULL;
61
    workload_size= 0;
62
  }
63
64
  if (arg_count == 3 && (res= args[2]->val_str(str)) != NULL)
65
    unique= res->ptr();
66
  else
67
    unique= NULL;
68
69
  if (!(options & GMAN_DO_OPTIONS_CLIENT))
70
  {
71
    if (!GetFunctionMap().get(string(function->ptr()), &client))
72
    {
73
      null_value= 1;
74
      return NULL;
75
    }
76
77
    gearman_client_set_workload_malloc(&client, _do_malloc, this);
78
    options= (gman_do_options_t)(options | GMAN_DO_OPTIONS_CLIENT);
79
  }
80
81
  if (options & GMAN_DO_OPTIONS_BACKGROUND)
82
  {
83
    if (options & GMAN_DO_OPTIONS_HIGH)
84
    {
85
      ret= gearman_client_do_high_background(&client, function->ptr(), unique,
86
                                             workload, workload_size,
87
                                             job_handle);
88
    }
89
    else if (options & GMAN_DO_OPTIONS_LOW)
90
    {
91
      ret= gearman_client_do_low_background(&client, function->ptr(), unique,
92
                                            workload, workload_size,
93
                                            job_handle);
94
    }
95
    else
96
    {
97
      ret= gearman_client_do_background(&client, function->ptr(), unique,
98
                                        workload, workload_size, job_handle);
99
    }
100
101
    if (ret == GEARMAN_SUCCESS)
102
    {
103
      result_size= strlen(job_handle);
104
      buffer.realloc(result_size);
105
      buffer.length(result_size);
106
      memcpy(buffer.ptr(), job_handle, result_size);
107
    }
108
  }
109
  else
110
  {
111
    if (options & GMAN_DO_OPTIONS_HIGH)
112
    {
113
      (void) gearman_client_do_high(&client, function->ptr(), unique, workload,
114
                                    workload_size, &result_size, &ret);
115
    }
116
    else if (options & GMAN_DO_OPTIONS_LOW)
117
    {
118
      (void) gearman_client_do_low(&client, function->ptr(), unique, workload,
119
                                   workload_size, &result_size, &ret);
120
    }
121
    else
122
    {
123
      (void) gearman_client_do(&client, function->ptr(), unique, workload,
124
                               workload_size, &result_size, &ret);
125
    }
126
  }
127
128
  if (ret != GEARMAN_SUCCESS)
129
  {
130
    null_value= 1;
131
    return NULL;
132
  }
133
134
  null_value= 0;
135
  return &buffer;
136
}
137
138
void *Item_func_gman_do::realloc(size_t size)
139
{
140
  buffer.realloc(size);
141
  buffer.length(size);
142
  return buffer.ptr();
143
}