~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
/* -*- mode: c++; c-basic-offset: 2; indent-tabs-mode: nil; -*-
 *  vim:expandtab:shiftwidth=2:tabstop=2:smarttab:
 *
 *  Copyright (C) 2008 Sun Microsystems
 *
 *  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; version 2 of the License.
 *
 *  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 St, Fifth Floor, Boston, MA  02110-1301  USA
 */

#include <drizzled/server_includes.h>
#include <drizzled/definitions.h>
#include <drizzled/base.h>
#include <drizzled/handler.h>
#include <drizzled/handlerton.h>
#include <drizzled/session.h>
#include <drizzled/error.h>
#include <drizzled/gettext.h>

#include CSTDINT_H

/*
  While we have legacy_db_type, we have this array to
  check for dups and to find handlerton from legacy_db_type.
  Remove when legacy_db_type is finally gone
*/
st_plugin_int *hton2plugin[MAX_HA];

static handlerton *installed_htons[128];

static const LEX_STRING sys_table_aliases[]=
{
  { C_STRING_WITH_LEN("INNOBASE") },  { C_STRING_WITH_LEN("INNODB") },
  { C_STRING_WITH_LEN("HEAP") },      { C_STRING_WITH_LEN("MEMORY") },
  {NULL, 0}
};


handlerton *ha_resolve_by_legacy_type(Session *session,
                                      enum legacy_db_type db_type)
{
  plugin_ref plugin;
  switch (db_type) {
  case DB_TYPE_DEFAULT:
    return ha_default_handlerton(session);
  default:
    if (db_type > DB_TYPE_UNKNOWN && db_type < DB_TYPE_DEFAULT &&
        (plugin= ha_lock_engine(session, installed_htons[db_type])))
      return plugin_data(plugin, handlerton*);
    /* fall through */
  case DB_TYPE_UNKNOWN:
    return NULL;
  }
}


static plugin_ref ha_default_plugin(Session *session)
{
  if (session->variables.table_plugin)
    return session->variables.table_plugin;
  return my_plugin_lock(session, &global_system_variables.table_plugin);
}


/**
  Return the default storage engine handlerton for thread

  @param ha_default_handlerton(session)
  @param session         current thread

  @return
    pointer to handlerton
*/
handlerton *ha_default_handlerton(Session *session)
{
  plugin_ref plugin= ha_default_plugin(session);
  assert(plugin);
  handlerton *hton= plugin_data(plugin, handlerton*);
  assert(hton);
  return hton;
}


/**
  Return the storage engine handlerton for the supplied name

  @param session         current thread
  @param name        name of storage engine

  @return
    pointer to storage engine plugin handle
*/
plugin_ref ha_resolve_by_name(Session *session, const LEX_STRING *name)
{
  const LEX_STRING *table_alias;
  plugin_ref plugin;

redo:
  /* my_strnncoll is a macro and gcc doesn't do early expansion of macro */
  if (session && !my_charset_utf8_general_ci.coll->strnncoll(&my_charset_utf8_general_ci,
                           (const unsigned char *)name->str, name->length,
                           (const unsigned char *)STRING_WITH_LEN("DEFAULT"), 0))
    return ha_default_plugin(session);

  if ((plugin= my_plugin_lock_by_name(session, name, DRIZZLE_STORAGE_ENGINE_PLUGIN)))
  {
    handlerton *hton= plugin_data(plugin, handlerton *);
    if (!(hton->flags.test(HTON_BIT_NOT_USER_SELECTABLE)))
      return plugin;

    /*
      unlocking plugin immediately after locking is relatively low cost.
    */
    plugin_unlock(session, plugin);
  }

  /*
    We check for the historical aliases.
  */
  for (table_alias= sys_table_aliases; table_alias->str; table_alias+= 2)
  {
    if (!my_strnncoll(&my_charset_utf8_general_ci,
                      (const unsigned char *)name->str, name->length,
                      (const unsigned char *)table_alias->str,
                      table_alias->length))
    {
      name= table_alias + 1;
      goto redo;
    }
  }

  return NULL;
}


plugin_ref ha_lock_engine(Session *session, handlerton *hton)
{
  if (hton)
  {
    st_plugin_int **plugin= hton2plugin + hton->slot;

    return my_plugin_lock(session, &plugin);
  }
  return NULL;
}


/**
  Use other database handler if databasehandler is not compiled in.
*/
handlerton *ha_checktype(Session *session, enum legacy_db_type database_type,
                          bool no_substitute, bool report_error)
{
  handlerton *hton= ha_resolve_by_legacy_type(session, database_type);
  if (ha_storage_engine_is_enabled(hton))
    return hton;

  if (no_substitute)
  {
    if (report_error)
    {
      const char *engine_name= ha_resolve_storage_engine_name(hton);
      my_error(ER_FEATURE_DISABLED,MYF(0),engine_name,engine_name);
    }
    return NULL;
  }

  return ha_default_handlerton(session);
} /* ha_checktype */


handler *get_new_handler(TABLE_SHARE *share, MEM_ROOT *alloc,
                         handlerton *db_type)
{
  handler *file;

  if (db_type && db_type->state == SHOW_OPTION_YES && db_type->create)
  {
    if ((file= db_type->create(db_type, share, alloc)))
      file->init();
    return(file);
  }
  /*
    Try the default table type
    Here the call to current_session() is ok as we call this function a lot of
    times but we enter this branch very seldom.
  */
  return(get_new_handler(share, alloc, ha_default_handlerton(current_session)));
}


int ha_finalize_handlerton(st_plugin_int *plugin)
{
  handlerton *hton= (handlerton *)plugin->data;

  switch (hton->state)
  {
  case SHOW_OPTION_NO:
  case SHOW_OPTION_DISABLED:
    break;
  case SHOW_OPTION_YES:
    if (installed_htons[hton->db_type] == hton)
      installed_htons[hton->db_type]= NULL;
    break;
  };

  if (hton && plugin->plugin->deinit)
    (void)plugin->plugin->deinit(hton);

  free((unsigned char*)hton);

  return(0);
}


int ha_initialize_handlerton(st_plugin_int *plugin)
{
  handlerton *hton;

  hton= (handlerton *)malloc(sizeof(handlerton));
  memset(hton, 0, sizeof(handlerton));

  /* Historical Requirement */
  plugin->data= hton; // shortcut for the future
  if (plugin->plugin->init)
  {
    if (plugin->plugin->init(hton))
    {
      errmsg_printf(ERRMSG_LVL_ERROR, _("Plugin '%s' init function returned error."),
                      plugin->name.str);
      goto err;
    }
  }

  hton->name= plugin->name.str;

  /*
    the switch below and hton->state should be removed when
    command-line options for plugins will be implemented
  */
  switch (hton->state) {
  case SHOW_OPTION_NO:
    break;
  case SHOW_OPTION_YES:
    {
      uint32_t tmp;
      /* now check the db_type for conflict */
      if (hton->db_type <= DB_TYPE_UNKNOWN ||
          hton->db_type >= DB_TYPE_DEFAULT ||
          installed_htons[hton->db_type])
      {
        int idx= (int) DB_TYPE_FIRST_DYNAMIC;

        while (idx < (int) DB_TYPE_DEFAULT && installed_htons[idx])
          idx++;

        if (idx == (int) DB_TYPE_DEFAULT)
        {
          errmsg_printf(ERRMSG_LVL_WARN, _("Too many storage engines!"));
          return(1);
        }
        if (hton->db_type != DB_TYPE_UNKNOWN)
          errmsg_printf(ERRMSG_LVL_WARN,
                        _("Storage engine '%s' has conflicting typecode. Assigning value %d."),
                        plugin->plugin->name, idx);
        hton->db_type= (enum legacy_db_type) idx;
      }
      installed_htons[hton->db_type]= hton;
      tmp= hton->savepoint_offset;
      hton->savepoint_offset= savepoint_alloc_size;
      savepoint_alloc_size+= tmp;
      hton->slot= total_ha++;
      hton2plugin[hton->slot]=plugin;
      if (hton->prepare)
        total_ha_2pc++;
      break;
    }
    /* fall through */
  default:
    hton->state= SHOW_OPTION_DISABLED;
    break;
  }

  /*
    This is entirely for legacy. We will create a new "disk based" hton and a
    "memory" hton which will be configurable longterm. We should be able to
    remove partition and myisammrg.
  */
  if (strcmp(plugin->plugin->name, "MEMORY") == 0)
    heap_hton= hton;

  if (strcmp(plugin->plugin->name, "MyISAM") == 0)
    myisam_hton= hton;

  plugin->state= PLUGIN_IS_READY;

  return(0);
err:
  return(1);
}

enum legacy_db_type ha_legacy_type(const handlerton *db_type)
{
  return (db_type == NULL) ? DB_TYPE_UNKNOWN : db_type->db_type;
}

const char *ha_resolve_storage_engine_name(const handlerton *db_type)
{
  return db_type == NULL ? "UNKNOWN" : hton2plugin[db_type->slot]->name.str;
}

bool ha_check_storage_engine_flag(const handlerton *db_type, const hton_flag_bits flag)
{
  return db_type == NULL ? false : db_type->flags.test(static_cast<size_t>(flag));
}

bool ha_storage_engine_is_enabled(const handlerton *db_type)
{
  return (db_type && db_type->create) ?
         (db_type->state == SHOW_OPTION_YES) : false;
}

LEX_STRING *ha_storage_engine_name(const handlerton *hton)
{
  return &hton2plugin[hton->slot]->name;
}