~clint-fewbar/ubuntu/natty/drizzle/beta1-fixes

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
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
/*
  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 <drizzled/table.h>
#include <drizzled/error.h>
#include <drizzled/plugin/transactional_storage_engine.h>
#include <string>
#include <map>
#include <fstream>
#include <drizzled/message/table.pb.h>
#include "drizzled/internal/m_string.h"

#include "drizzled/global_charset_info.h"

#include <boost/unordered_map.hpp>

using namespace std;
using namespace drizzled;

typedef boost::unordered_map<std::string, message::Table, util::insensitive_hash, util::insensitive_equal_to> TableMessageMap;
typedef boost::unordered_map<std::string, message::Table, util::insensitive_hash, util::insensitive_equal_to>::iterator TableMessageMapIterator;

string engine_state;
typedef multimap<string, string> state_multimap;
typedef multimap<string, string>::value_type state_pair;
typedef multimap<string, string>::iterator state_multimap_iter;
state_multimap engine_state_transitions;
state_multimap cursor_state_transitions;

void load_engine_state_transitions(state_multimap &states);
void load_cursor_state_transitions(state_multimap &states);

/* This is a hack to make store_lock kinda work */
drizzled::THR_LOCK share_lock;

static inline void ENGINE_NEW_STATE(const string &new_state)
{
  state_multimap_iter cur= engine_state_transitions.find(engine_state);
  if (engine_state_transitions.count(engine_state) == 0)
  {
    cerr << "ERROR: Invalid engine state: " << engine_state << endl
         << "This should *NEVER* happen."
         << endl
         << "i.e. you've really screwed it up and you should be ashamed of "
         << "yourself." << endl;
    assert(engine_state_transitions.count(engine_state));
  }

  for(cur= engine_state_transitions.lower_bound(engine_state);
      cur != engine_state_transitions.upper_bound(engine_state);
      cur++)
  {
    if (new_state.compare((*cur).second) == 0)
      break;
  }

  if (cur == engine_state_transitions.end()
      || new_state.compare((*cur).second))
  {
    cerr << "ERROR: Invalid Storage Engine state transition!" << endl
         << "Cannot go from " << engine_state << " to " << new_state << endl;
    assert(false);
  }

  engine_state= new_state;

  cerr << "\tENGINE STATE : " << engine_state << endl;
}

static const string engine_name("STORAGE_ENGINE_API_TESTER");

class SEAPITesterCursor : public drizzled::Cursor
{
public:
  SEAPITesterCursor(drizzled::plugin::StorageEngine &engine_arg,
                    drizzled::Table &table_arg)
    : Cursor(engine_arg, table_arg)
    { cursor_state= "Cursor()"; }
  ~SEAPITesterCursor()
  {}

  int close() { return 0; }
  int rnd_next(unsigned char*) { CURSOR_NEW_STATE("::rnd_next()"); return HA_ERR_END_OF_FILE; }
  int rnd_pos(unsigned char*, unsigned char*) { CURSOR_NEW_STATE("::rnd_pos()"); return -1; }
  void position(const unsigned char*) { CURSOR_NEW_STATE("::position()"); return; }
  int info(uint32_t) { return 0; }
  void get_auto_increment(uint64_t, uint64_t, uint64_t, uint64_t*, uint64_t*) {}
  int doStartTableScan(bool) { CURSOR_NEW_STATE("::doStartTableScan()"); return HA_ERR_END_OF_FILE; }

  int open(const char*, int, uint32_t)
    { CURSOR_NEW_STATE("::open()"); lock.init(&share_lock); return 0;}

  drizzled::THR_LOCK_DATA lock;        /* MySQL lock */

  THR_LOCK_DATA **store_lock(Session *,
                                     THR_LOCK_DATA **to,
                             enum thr_lock_type);

  int doInsertRecord(unsigned char *)
  {
    CURSOR_NEW_STATE("::doInsertRecord()");
    CURSOR_NEW_STATE("::store_lock()");
    return 0;
  }

private:
  string cursor_state;
  void CURSOR_NEW_STATE(const string &new_state);
};

THR_LOCK_DATA **SEAPITesterCursor::store_lock(Session *session,
                                              THR_LOCK_DATA **to,
                                              enum thr_lock_type lock_type)

{
  CURSOR_NEW_STATE("::store_lock()");

  if (lock_type != TL_IGNORE && lock.type == TL_UNLOCK)
  {
    /*
      Here is where we get into the guts of a row level lock.
      If TL_UNLOCK is set
      If we are not doing a LOCK Table or DISCARD/IMPORT
      TABLESPACE, then allow multiple writers
    */

    if ((lock_type >= TL_WRITE_CONCURRENT_INSERT &&
         lock_type <= TL_WRITE)
        && !session_tablespace_op(session))
      lock_type = TL_WRITE_ALLOW_WRITE;

    /*
      In queries of type INSERT INTO t1 SELECT ... FROM t2 ...
      MySQL would use the lock TL_READ_NO_INSERT on t2, and that
      would conflict with TL_WRITE_ALLOW_WRITE, blocking all inserts
      to t2. Convert the lock to a normal read lock to allow
      concurrent inserts to t2.
    */

    if (lock_type == TL_READ_NO_INSERT)
      lock_type = TL_READ;

    lock.type=lock_type;
  }

  *to++= &lock;

  return(to);
}

void SEAPITesterCursor::CURSOR_NEW_STATE(const string &new_state)
{
  state_multimap_iter cur= cursor_state_transitions.find(cursor_state);
  if (cursor_state_transitions.count(cursor_state) == 0)
  {
    cerr << "ERROR: Invalid Cursor state: " << cursor_state << endl
         << "This should *NEVER* happen."
         << endl
         << "i.e. you've really screwed it up and you should be ashamed of "
         << "yourself." << endl;
    assert(cursor_state_transitions.count(cursor_state));
  }

  for(cur= cursor_state_transitions.lower_bound(cursor_state);
      cur != cursor_state_transitions.upper_bound(cursor_state);
      cur++)
  {
    if (new_state.compare((*cur).second) == 0)
      break;
  }

  if (cur == cursor_state_transitions.end()
      || new_state.compare((*cur).second))
  {
    cerr << "ERROR: Invalid Cursor state transition!" << endl
         << "Cannot go from " << cursor_state << " to " << new_state << endl;
    assert(false);
  }

  cursor_state= new_state;

  cerr << "\t\tCursor STATE : " << cursor_state << endl;
}

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


class SEAPITester : public drizzled::plugin::TransactionalStorageEngine
{
public:
  SEAPITester(const string &name_arg)
    : drizzled::plugin::TransactionalStorageEngine(name_arg,
//                                                   HTON_SKIP_STORE_LOCK |
                                                   HTON_HAS_DOES_TRANSACTIONS)
  {
    ENGINE_NEW_STATE("::SEAPITester()");
  }

  ~SEAPITester()
  {
    ENGINE_NEW_STATE("::~SEAPITester()");
  }

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

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

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

  int doDropTable(Session&, const TableIdentifier &identifier);

  int doRenameTable(drizzled::Session&,
                    const drizzled::TableIdentifier&,
                    const drizzled::TableIdentifier&)
    { return ENOENT; }

  int doGetTableDefinition(Session& ,
                           const TableIdentifier &,
                           drizzled::message::Table &);

  bool doDoesTableExist(Session&, const TableIdentifier &identifier);

  void doGetTableIdentifiers(drizzled::CachedDirectory &,
                             const drizzled::SchemaIdentifier &,
                             drizzled::TableIdentifier::vector &);

  virtual int doStartTransaction(Session *session,
                                 start_transaction_option_t options);
  virtual void doStartStatement(Session *session);
  virtual void doEndStatement(Session *session);

  virtual int doSetSavepoint(Session*,
                             drizzled::NamedSavepoint &)
    { return 0; }
  virtual int doRollbackToSavepoint(Session*,
                                     drizzled::NamedSavepoint &)
    { return 0; }
  virtual int doReleaseSavepoint(Session*,
                                 drizzled::NamedSavepoint &)
    { return 0; }
  virtual int doCommit(Session*, bool);

  virtual int doRollback(Session*, bool);


private:
  TableMessageMap table_messages;
};

bool SEAPITester::doDoesTableExist(Session&, const TableIdentifier &identifier)
{
  return table_messages.find(identifier.getPath()) != table_messages.end();
}

void SEAPITester::doGetTableIdentifiers(drizzled::CachedDirectory &,
                                        const drizzled::SchemaIdentifier &,
                                        drizzled::TableIdentifier::vector &)
{
}

int SEAPITester::doCreateTable(Session&,
                               Table&,
                               const drizzled::TableIdentifier &identifier,
                               drizzled::message::Table& create_proto)
{
  ENGINE_NEW_STATE("::doCreateTable()");

  if (table_messages.find(identifier.getPath()) != table_messages.end())
  {
    ENGINE_NEW_STATE("::SEAPITester()");
    return EEXIST;
  }

  table_messages.insert(make_pair(identifier.getPath(), create_proto));
  ENGINE_NEW_STATE("::SEAPITester()");
  return 0;
}

int SEAPITester::doDropTable(Session&, const TableIdentifier &identifier)
{
  if (table_messages.find(identifier.getPath()) == table_messages.end())
    return ENOENT;

  table_messages.erase(identifier.getPath());
  assert(table_messages.find(identifier.getPath()) == table_messages.end());
  return 0;
}

int SEAPITester::doGetTableDefinition(Session& ,
                                      const TableIdentifier &identifier,
                                      drizzled::message::Table &table)
{
  TableMessageMapIterator iter= table_messages.find(identifier.getPath());
  if (iter == table_messages.end())
  {
    return ENOENT;
  }

  table= (*iter).second;

  return EEXIST;
}

int SEAPITester::doStartTransaction(Session *,
                                    start_transaction_option_t )
{
  ENGINE_NEW_STATE("BEGIN");

  return 0;
}

void SEAPITester::doStartStatement(Session *)
{
  ENGINE_NEW_STATE("START STATEMENT");
}

void SEAPITester::doEndStatement(Session *)
{
  ENGINE_NEW_STATE("END STATEMENT");
}

int SEAPITester::doCommit(Session*, bool)
{
  ENGINE_NEW_STATE("COMMIT");
  ENGINE_NEW_STATE("::SEAPITester()");
  return 0;
}

int SEAPITester::doRollback(Session*, bool)
{
  ENGINE_NEW_STATE("ROLLBACK");
  ENGINE_NEW_STATE("::SEAPITester()");
  return 0;
}

static int seapi_tester_init(drizzled::module::Context &context)
{
  load_engine_state_transitions(engine_state_transitions);
  load_cursor_state_transitions(cursor_state_transitions);
  engine_state= "INIT";

  thr_lock_init(&share_lock); /* HACK for store_lock */

  context.add(new SEAPITester(engine_name));
  return 0;
}

DRIZZLE_DECLARE_PLUGIN
{
  DRIZZLE_VERSION_ID,
  "SEAPITESTER",
  "1.0",
  "Stewart Smith",
  "Test the Storage Engine API callls are in correct order",
  PLUGIN_LICENSE_GPL,
  seapi_tester_init,     /* Plugin Init */
  NULL, /* system variables */
  NULL                /* config options   */
}
DRIZZLE_DECLARE_PLUGIN_END;