Drizzled Public API Documentation

benchmarkudf.cc
1 /* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
2  * vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
3  *
4  * Copyright (C) 2008 MySQL AB
5  *
6  * This program is free software; you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  * GNU General Public License for more details.
14  *
15  * You should have received a copy of the GNU General Public License
16  * along with this program; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 #include <config.h>
21 #include <drizzled/error.h>
22 #include <drizzled/internal/m_string.h>
23 #include <drizzled/plugin/function.h>
24 #include <drizzled/session.h>
25 
26 using namespace std;
27 using namespace drizzled;
28 
30 {
31 public:
33  int64_t val_int();
34  virtual void print(String *str);
35 
36  const char *func_name() const
37  {
38  return "benchmark";
39  }
40 
41  void fix_length_and_dec()
42  {
43  max_length= 1;
44  maybe_null= false;
45  }
46 
48  {
49  return (n == 2);
50  }
51 };
52 
53 
54 /* This function is just used to test speed of different functions */
56 {
57  assert(fixed == true);
58 
59  char buff[MAX_FIELD_WIDTH];
60  String tmp(buff,sizeof(buff), &my_charset_bin);
61  type::Decimal tmp_decimal;
62  uint64_t loop_count;
63 
64  loop_count= (uint64_t) args[0]->val_int();
65 
66  if (args[0]->null_value ||
67  (args[0]->unsigned_flag == false && (((int64_t) loop_count) < 0)))
68  {
69  if (args[0]->null_value == false)
70  {
71  internal::int64_t10_to_str((int64_t)loop_count, buff, -10);
72  push_warning_printf(&getSession(), DRIZZLE_ERROR::WARN_LEVEL_ERROR,
73  ER_WRONG_VALUE_FOR_TYPE, ER(ER_WRONG_VALUE_FOR_TYPE),
74  "count", buff, "benchmark");
75  }
76 
77  null_value= true;
78  return 0;
79  }
80 
81  null_value= false;
82 
83  uint64_t loop;
84  for (loop= 0 ; loop < loop_count && not getSession().getKilled(); loop++)
85  {
86  switch (args[1]->result_type())
87  {
88  case REAL_RESULT:
89  (void) args[1]->val_real();
90  break;
91  case INT_RESULT:
92  (void) args[1]->val_int();
93  break;
94  case STRING_RESULT:
95  (void) args[1]->val_str(&tmp);
96  break;
97  case DECIMAL_RESULT:
98  (void) args[1]->val_decimal(&tmp_decimal);
99  break;
100  case ROW_RESULT:
101  default:
102  // This case should never be chosen
103  assert(0);
104  return 0;
105  }
106  }
107  return 0;
108 }
109 
111 {
112  str->append(STRING_WITH_LEN("benchmark("));
113  args[0]->print(str);
114  str->append(',');
115  args[1]->print(str);
116  str->append(')');
117 }
118 
119 plugin::Create_function<BenchmarkFunction> *benchmarkudf= NULL;
120 
121 static int initialize(module::Context &context)
122 {
123  benchmarkudf= new plugin::Create_function<BenchmarkFunction>("benchmark");
124  context.add(benchmarkudf);
125  return 0;
126 }
127 
128 DRIZZLE_DECLARE_PLUGIN
129 {
130  DRIZZLE_VERSION_ID,
131  "benchmark",
132  "1.0",
133  "Devananda van der Veen",
134  N_("BENCHMARK function"),
135  PLUGIN_LICENSE_GPL,
136  initialize,
137  NULL,
138  NULL
139 }
140 DRIZZLE_DECLARE_PLUGIN_END;