~drizzle-trunk/drizzle/development

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/*
  Copyright (C) 2010 Stewart Smith

  This program is free software; you can redistribute it and/or
  modify it under the terms of the GNU General Public License
  as published by the Free Software Foundation; either version 2
  of the License, or (at your option) any later version.

  This program is distributed in the hope that it will be useful,
  but WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  GNU General Public License for more details.

  You should have received a copy of the GNU General Public License
  along with this program; if not, write to the Free Software
  Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
*/

#include <config.h>

#include "tableprototester.h"

#include <fcntl.h>

#include <string>
#include <map>
#include <fstream>

#include <drizzled/error.h>
#include <drizzled/charset.h>
#include <drizzled/internal/m_string.h>
#include <drizzled/internal/my_pthread.h>
#include <drizzled/message/table.h>
#include <drizzled/plugin/storage_engine.h>
#include <drizzled/table.h>


using namespace std;
using namespace google;
using namespace drizzled;

#define TABLEPROTOTESTER_EXT ".TBT"

static const char *TableProtoTesterCursor_exts[] = {
  NULL
};

class TableProtoTesterEngine : public drizzled::plugin::StorageEngine
{
public:
  TableProtoTesterEngine(const string &name_arg)
   : drizzled::plugin::StorageEngine(name_arg,
                                     HTON_NULL_IN_KEY |
                                     HTON_CAN_INDEX_BLOBS |
                                     HTON_SKIP_STORE_LOCK |
                                     HTON_AUTO_PART_KEY)
  {
    table_definition_ext= TABLEPROTOTESTER_EXT;
  }

  virtual Cursor *create(Table &table)
  {
    return new TableProtoTesterCursor(*this, table);
  }

  const char **bas_ext() const {
    return TableProtoTesterCursor_exts;
  }

  int doCreateTable(Session&,
                    Table&,
                    const drizzled::identifier::Table &identifier,
                    const drizzled::message::Table&);

  int doDropTable(Session&, const drizzled::identifier::Table &identifier);

  int doGetTableDefinition(Session &session,
                           const drizzled::identifier::Table &identifier,
                           drizzled::message::Table &table_proto);

  /* The following defines can be increased if necessary */
  uint32_t max_supported_keys()          const { return 64; }
  uint32_t max_supported_key_length()    const { return 1000; }
  uint32_t max_supported_key_part_length() const { return 1000; }

  uint32_t index_flags(enum  ha_key_alg) const
  {
    return (HA_READ_NEXT |
            HA_READ_PREV |
            HA_READ_RANGE |
            HA_READ_ORDER |
            HA_KEYREAD_ONLY);
  }

  bool doDoesTableExist(Session &session, const drizzled::identifier::Table &identifier);

  int doRenameTable(Session&, const drizzled::identifier::Table&, const drizzled::identifier::Table&)
  {
    return HA_ERR_NO_SUCH_TABLE;
  }

  void doGetTableIdentifiers(drizzled::CachedDirectory &directory,
                             const drizzled::identifier::Schema &schema_identifier,
                             drizzled::identifier::table::vector &set_of_identifiers);
};

void TableProtoTesterEngine::doGetTableIdentifiers(drizzled::CachedDirectory&,
                                                   const drizzled::identifier::Schema &schema_identifier,
                                                   drizzled::identifier::table::vector &set_of_identifiers)
{
  if (schema_identifier.compare("test"))
  {
    set_of_identifiers.push_back(identifier::Table(schema_identifier, "t1"));
    set_of_identifiers.push_back(identifier::Table(schema_identifier, "too_many_enum_values"));
    set_of_identifiers.push_back(identifier::Table(schema_identifier, "invalid_table_collation"));
  }
}

bool TableProtoTesterEngine::doDoesTableExist(Session&, const drizzled::identifier::Table &identifier)
{
  if (not identifier.getPath().compare("test/t1"))
    return true;
  if (not identifier.getPath().compare("test/too_many_enum_values"))
    return true;
  if (not identifier.getPath().compare("test/invalid_table_collation"))
    return true;

  return false;
}

TableProtoTesterCursor::TableProtoTesterCursor(drizzled::plugin::StorageEngine &engine_arg,
                                               Table &table_arg) :
  Cursor(engine_arg, table_arg)
{ }

int TableProtoTesterCursor::open(const char *, int, uint32_t)
{
  return 0;
}

int TableProtoTesterCursor::close(void)
{
  return 0;
}

int TableProtoTesterEngine::doCreateTable(Session&,
                                          Table&,
                                          const drizzled::identifier::Table&,
                                          const drizzled::message::Table&)
{
  return EEXIST;
}


int TableProtoTesterEngine::doDropTable(Session&, const drizzled::identifier::Table&)
{
  return HA_ERR_NO_SUCH_TABLE;
}

static void fill_table1(message::Table &table)
{
  message::Table::Field *field;
  message::Table::TableOptions *tableopts;

  table.set_name("t1");
  table.set_type(message::Table::INTERNAL);

  tableopts= table.mutable_options();
  tableopts->set_comment("Table without a StorageEngine message");

  {
    field= table.add_field();
    field->set_name("number");
    field->set_type(message::Table::Field::INTEGER);
  }

}

static void fill_table_too_many_enum_values(message::Table &table)
{
  message::Table::Field *field;
  message::Table::TableOptions *tableopts;

  table.set_schema("test");
  table.set_name("too_many_enum_values");
  table.set_type(message::Table::STANDARD);
  table.mutable_engine()->set_name("tableprototester");
  table.set_creation_timestamp(0);
  table.set_update_timestamp(0);

  tableopts= table.mutable_options();
  tableopts->set_comment("Table with too many enum options");
  tableopts->set_collation("utf8_general_ci");
  tableopts->set_collation_id(45);

  {
    field= table.add_field();
    field->set_name("many_values");
    field->set_type(message::Table::Field::ENUM);

    message::Table::Field::EnumerationValues *field_options= field->mutable_enumeration_values();
    for(int i=0; i<70000; i++)
    {
      char enum_value[100];
      snprintf(enum_value, sizeof(enum_value), "a%d", i);
      field_options->add_field_value(enum_value);
    }
  }

}

static void fill_table_invalid_table_collation(message::Table &table)
{
  message::Table::Field *field;
  message::Table::TableOptions *tableopts;

  table.set_name("invalid_table_collation");
  table.set_type(message::Table::STANDARD);
  table.set_schema("test");
  table.set_creation_timestamp(0);
  table.set_update_timestamp(0);
  table.mutable_engine()->set_name("tableprototester");

  tableopts= table.mutable_options();
  tableopts->set_comment("Invalid table collation ");

  {
    field= table.add_field();
    field->set_name("number");
    field->set_type(message::Table::Field::INTEGER);
  }

  tableopts->set_collation("pi_pi_pi");
  tableopts->set_collation_id(123456);

}

int TableProtoTesterEngine::doGetTableDefinition(Session&,
                                                 const drizzled::identifier::Table &identifier,
                                                 drizzled::message::Table &table_proto)
{
  if (not identifier.getPath().compare("test/t1"))
  {
    fill_table1(table_proto);
    return EEXIST;
  }
  else if (not identifier.getPath().compare("test/too_many_enum_values"))
  {
    fill_table_too_many_enum_values(table_proto);
    return EEXIST;
  }
  else if (not identifier.getPath().compare("test/invalid_table_collation"))
  {
    fill_table_invalid_table_collation(table_proto);
    return EEXIST;
  }
  return ENOENT;
}

const char *TableProtoTesterCursor::index_type(uint32_t)
{
  return("BTREE");
}

int TableProtoTesterCursor::doInsertRecord(unsigned char *)
{
  return(getTable()->next_number_field ? update_auto_increment() : 0);
}

int TableProtoTesterCursor::doStartTableScan(bool)
{
  return(0);
}


int TableProtoTesterCursor::rnd_next(unsigned char *)
{
  return(HA_ERR_END_OF_FILE);
}


int TableProtoTesterCursor::rnd_pos(unsigned char *, unsigned char *)
{
  assert(0);
  return(0);
}


void TableProtoTesterCursor::position(const unsigned char *)
{
  assert(0);
  return;
}


int TableProtoTesterCursor::info(uint32_t flag)
{
  memset(&stats, 0, sizeof(stats));
  if (flag & HA_STATUS_AUTO)
    stats.auto_increment_value= 1;
  return(0);
}


int TableProtoTesterCursor::index_read_map(unsigned char *, const unsigned char *,
                                 key_part_map, enum ha_rkey_function)
{
  return(HA_ERR_END_OF_FILE);
}


int TableProtoTesterCursor::index_read_idx_map(unsigned char *, uint32_t, const unsigned char *,
                                     key_part_map, enum ha_rkey_function)
{
  return(HA_ERR_END_OF_FILE);
}


int TableProtoTesterCursor::index_read_last_map(unsigned char *, const unsigned char *, key_part_map)
{
  return(HA_ERR_END_OF_FILE);
}


int TableProtoTesterCursor::index_next(unsigned char *)
{
  return(HA_ERR_END_OF_FILE);
}


int TableProtoTesterCursor::index_prev(unsigned char *)
{
  return(HA_ERR_END_OF_FILE);
}


int TableProtoTesterCursor::index_first(unsigned char *)
{
  return(HA_ERR_END_OF_FILE);
}


int TableProtoTesterCursor::index_last(unsigned char *)
{
  return(HA_ERR_END_OF_FILE);
}

static drizzled::plugin::StorageEngine *tableprototester_engine= NULL;

static int tableprototester_init(drizzled::module::Context &context)
{

  tableprototester_engine= new TableProtoTesterEngine("TABLEPROTOTESTER");
  context.add(tableprototester_engine);

  return 0;
}

DRIZZLE_DECLARE_PLUGIN
{
  DRIZZLE_VERSION_ID,
  "TABLEPROTOTESTER",
  "1.0",
  "Stewart Smith",
  "Used to test rest of server with various table proto messages",
  PLUGIN_LICENSE_GPL,
  tableprototester_init,     /* Plugin Init */
  NULL,               /* depends */
  NULL                /* config options   */
}
DRIZZLE_DECLARE_PLUGIN_END;