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