-
Notifications
You must be signed in to change notification settings - Fork 2
/
ha_smalldb.cc
601 lines (480 loc) · 13.8 KB
/
ha_smalldb.cc
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
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
/* Copyright (c) 2004, 2011, Oracle and/or its affiliates. All rights reserved.
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 */
#ifdef USE_PRAGMA_IMPLEMENTATION
#pragma implementation // gcc: Class implementation
#endif
#include "sql_priv.h"
#include "sql_class.h" // MYSQL_HANDLERTON_INTERFACE_VERSION
#include "ha_smalldb.h"
#include "probes_mysql.h"
#include "sql_plugin.h"
static handler *smalldb_create_handler(handlerton *hton,
TABLE_SHARE *table,
MEM_ROOT *mem_root);
handlerton *smalldb_hton;
/* Interface to mysqld, to check system tables supported by SE */
static const char* smalldb_system_database();
static bool smalldb_is_supported_system_table(const char *db,
const char *table_name,
bool is_sql_layer_system_table);
static HASH smalldb_open_tables;
mysql_mutex_t smalldb_mutex;
static uchar* smalldb_get_key(SMALLDB_SHARE *share, size_t *length,
my_bool not_used __attribute__((unused)))
{
*length=share->table_name_length;
return (uchar*) share->table_name;
}
#ifdef HAVE_PSI_INTERFACE
static PSI_mutex_key ex_key_mutex_smalldb, ex_key_mutex_SMALLDB_SHARE_mutex;
static PSI_mutex_info all_smalldb_mutexes[]=
{
{ &ex_key_mutex_smalldb, "smalldb", PSI_FLAG_GLOBAL},
{ &ex_key_mutex_SMALLDB_SHARE_mutex, "SMALLDB_SHARE::mutex", 0}
};
static void init_smalldb_psi_keys()
{
const char* category= "smalldb";
int count;
if (PSI_server == NULL)
return;
count= array_elements(all_smalldb_mutexes);
PSI_server->register_mutex(category, all_smalldb_mutexes, count);
}
#endif
static int smalldb_init_func(void *p)
{
DBUG_ENTER("smalldb_init_func");
#ifdef HAVE_PSI_INTERFACE
init_smalldb_psi_keys();
#endif
smalldb_hton= (handlerton *)p;
mysql_mutex_init(ex_key_mutex_smalldb, &smalldb_mutex, MY_MUTEX_INIT_FAST);
(void) my_hash_init(&smalldb_open_tables,system_charset_info,32,0,0,
(my_hash_get_key) smalldb_get_key,0,0);
smalldb_hton->state= SHOW_OPTION_YES;
smalldb_hton->create= smalldb_create_handler;
smalldb_hton->flags= HTON_CAN_RECREATE;
smalldb_hton->system_database= smalldb_system_database;
smalldb_hton->is_supported_system_table= smalldb_is_supported_system_table;
DBUG_RETURN(0);
}
static int smalldb_done_func(void *p)
{
int error= 0;
DBUG_ENTER("smalldb_done_func");
if (smalldb_open_tables.records)
error= 1;
my_hash_free(&smalldb_open_tables);
mysql_mutex_destroy(&smalldb_mutex);
DBUG_RETURN(error);
}
static SMALLDB_SHARE *get_share(const char *table_name, TABLE *table)
{
SMALLDB_SHARE *share;
uint length;
char *tmp_name;
mysql_mutex_lock(&smalldb_mutex);
length=(uint) strlen(table_name);
if (!(share=(SMALLDB_SHARE*) my_hash_search(&smalldb_open_tables,
(uchar*) table_name,
length)))
{
if (!(share=(SMALLDB_SHARE *)
my_multi_malloc(MYF(MY_WME | MY_ZEROFILL),
&share, sizeof(*share),
&tmp_name, length+1,
NullS)))
{
mysql_mutex_unlock(&smalldb_mutex);
return NULL;
}
share->use_count=0;
share->table_name_length=length;
share->table_name=tmp_name;
strmov(share->table_name,table_name);
if (my_hash_insert(&smalldb_open_tables, (uchar*) share))
goto error;
thr_lock_init(&share->lock);
mysql_mutex_init(ex_key_mutex_SMALLDB_SHARE_mutex,
&share->mutex, MY_MUTEX_INIT_FAST);
}
share->use_count++;
mysql_mutex_unlock(&smalldb_mutex);
return share;
error:
mysql_mutex_destroy(&share->mutex);
my_free(share);
return NULL;
}
static int free_share(SMALLDB_SHARE *share)
{
mysql_mutex_lock(&smalldb_mutex);
if (!--share->use_count)
{
my_hash_delete(&smalldb_open_tables, (uchar*) share);
thr_lock_delete(&share->lock);
mysql_mutex_destroy(&share->mutex);
my_free(share);
}
mysql_mutex_unlock(&smalldb_mutex);
return 0;
}
static handler* smalldb_create_handler(handlerton *hton,
TABLE_SHARE *table,
MEM_ROOT *mem_root)
{
return new (mem_root) ha_smalldb(hton, table);
}
ha_smalldb::ha_smalldb(handlerton *hton, TABLE_SHARE *table_arg)
:handler(hton, table_arg)
{
first=cur=prev=next=NULL;
row_count=cur_pos=0;
}
static const char *ha_smalldb_exts[] = {
NullS
};
const char **ha_smalldb::bas_ext() const
{
return ha_smalldb_exts;
}
const char* ha_smalldb_system_database= NULL;
const char* smalldb_system_database()
{
return ha_smalldb_system_database;
}
static st_system_tablename ha_smalldb_system_tables[]= {
{(const char*)NULL, (const char*)NULL}
};
static bool smalldb_is_supported_system_table(const char *db,
const char *table_name,
bool is_sql_layer_system_table)
{
st_system_tablename *systab;
// Does this SE support "ALL" SQL layer system tables ?
if (is_sql_layer_system_table)
return false;
// Check if this is SE layer system tables
systab= ha_smalldb_system_tables;
while (systab && systab->db)
{
if (systab->db == db &&
strcmp(systab->tablename, table_name) == 0)
return true;
systab++;
}
return false;
}
int ha_smalldb::open(const char *name, int mode, uint test_if_locked)
{
DBUG_ENTER("ha_smalldb::open");
if (!(share = get_share(name, table)))
DBUG_RETURN(1);
thr_lock_data_init(&share->lock,&lock,NULL);
DBUG_RETURN(0);
}
int ha_smalldb::close(void)
{
DBUG_ENTER("ha_smalldb::close");
DBUG_RETURN(free_share(share));
}
int ha_smalldb::write_row(uchar *buf)
{
DBUG_ENTER("ha_smalldb::write_row");
node* n=new node();
n->data = (uchar*) malloc(sizeof(uchar)*table->s->reclength);
memcpy(n->data,buf,table->s->reclength);
append_node(n);
row_count++;
DBUG_RETURN(0);
}
int ha_smalldb::update_row(const uchar *old_data, uchar *new_data)
{
DBUG_ENTER("ha_smalldb::update_row");
DBUG_RETURN(HA_ERR_WRONG_COMMAND);
}
int ha_smalldb::delete_row(const uchar *buf)
{
DBUG_ENTER("ha_smalldb::delete_row");
if (cur!=first){
free(cur);
prev->next=next;
}else{
free(cur);
cur=NULL;
first=next;
}
row_count--;
DBUG_RETURN(0);
}
int ha_smalldb::index_read_map(uchar *buf, const uchar *key,
key_part_map keypart_map __attribute__((unused)),
enum ha_rkey_function find_flag
__attribute__((unused)))
{
int rc;
DBUG_ENTER("ha_smalldb::index_read");
MYSQL_INDEX_READ_ROW_START(table_share->db.str, table_share->table_name.str);
rc= HA_ERR_WRONG_COMMAND;
MYSQL_INDEX_READ_ROW_DONE(rc);
DBUG_RETURN(rc);
}
int ha_smalldb::index_next(uchar *buf)
{
int rc;
DBUG_ENTER("ha_smalldb::index_next");
MYSQL_INDEX_READ_ROW_START(table_share->db.str, table_share->table_name.str);
rc= HA_ERR_WRONG_COMMAND;
MYSQL_INDEX_READ_ROW_DONE(rc);
DBUG_RETURN(rc);
}
int ha_smalldb::index_prev(uchar *buf)
{
int rc;
DBUG_ENTER("ha_smalldb::index_prev");
MYSQL_INDEX_READ_ROW_START(table_share->db.str, table_share->table_name.str);
rc= HA_ERR_WRONG_COMMAND;
MYSQL_INDEX_READ_ROW_DONE(rc);
DBUG_RETURN(rc);
}
int ha_smalldb::index_first(uchar *buf)
{
int rc;
DBUG_ENTER("ha_smalldb::index_first");
MYSQL_INDEX_READ_ROW_START(table_share->db.str, table_share->table_name.str);
rc= HA_ERR_WRONG_COMMAND;
MYSQL_INDEX_READ_ROW_DONE(rc);
DBUG_RETURN(rc);
}
int ha_smalldb::index_last(uchar *buf)
{
int rc;
DBUG_ENTER("ha_smalldb::index_last");
MYSQL_INDEX_READ_ROW_START(table_share->db.str, table_share->table_name.str);
rc= HA_ERR_WRONG_COMMAND;
MYSQL_INDEX_READ_ROW_DONE(rc);
DBUG_RETURN(rc);
}
int ha_smalldb::rnd_init(bool scan)
{
DBUG_ENTER("ha_smalldb::rnd_init");
cur=NULL;
prev=NULL;
next=first;
cur_pos=0;
DBUG_RETURN(0);
}
int ha_smalldb::rnd_end()
{
DBUG_ENTER("ha_smalldb::rnd_end");
DBUG_RETURN(0);
}
int ha_smalldb::rnd_next(uchar *buf)
{
int rc;
DBUG_ENTER("ha_smalldb::rnd_next");
MYSQL_READ_ROW_START(table_share->db.str, table_share->table_name.str,
TRUE);
if (next!=NULL){
prev=cur;
cur=next;
next=cur->next;
memcpy(buf,cur->data,table->s->reclength);
cur_pos++;
rc=0;
}else{
rc= HA_ERR_END_OF_FILE;
}
MYSQL_READ_ROW_DONE(rc);
DBUG_RETURN(rc);
}
void ha_smalldb::position(const uchar *record)
{
DBUG_ENTER("ha_smalldb::position");
DBUG_VOID_RETURN;
}
int ha_smalldb::rnd_pos(uchar *buf, uchar *pos)
{
int rc;
DBUG_ENTER("ha_smalldb::rnd_pos");
MYSQL_READ_ROW_START(table_share->db.str, table_share->table_name.str,
TRUE);
rc= HA_ERR_WRONG_COMMAND;
MYSQL_READ_ROW_DONE(rc);
DBUG_RETURN(rc);
}
int ha_smalldb::info(uint flag)
{
DBUG_ENTER("ha_smalldb::info");
DBUG_RETURN(0);
}
int ha_smalldb::extra(enum ha_extra_function operation)
{
DBUG_ENTER("ha_smalldb::extra");
DBUG_RETURN(0);
}
int ha_smalldb::delete_all_rows()
{
DBUG_ENTER("ha_smalldb::delete_all_rows");
DBUG_RETURN(HA_ERR_WRONG_COMMAND);
}
int ha_smalldb::truncate()
{
DBUG_ENTER("ha_smalldb::truncate");
DBUG_RETURN(HA_ERR_WRONG_COMMAND);
}
int ha_smalldb::external_lock(THD *thd, int lock_type)
{
DBUG_ENTER("ha_smalldb::external_lock");
DBUG_RETURN(0);
}
THR_LOCK_DATA **ha_smalldb::store_lock(THD *thd,
THR_LOCK_DATA **to,
enum thr_lock_type lock_type)
{
if (lock_type != TL_IGNORE && lock.type == TL_UNLOCK)
lock.type=lock_type;
*to++= &lock;
return to;
}
int ha_smalldb::delete_table(const char *name)
{
DBUG_ENTER("ha_smalldb::delete_table");
/* This is not implemented but we want someone to be able that it works. */
DBUG_RETURN(0);
}
int ha_smalldb::rename_table(const char * from, const char * to)
{
DBUG_ENTER("ha_smalldb::rename_table ");
DBUG_RETURN(HA_ERR_WRONG_COMMAND);
}
ha_rows ha_smalldb::records_in_range(uint inx, key_range *min_key,
key_range *max_key)
{
DBUG_ENTER("ha_smalldb::records_in_range");
DBUG_RETURN(10); // low number to force index usage
}
int ha_smalldb::create(const char *name, TABLE *table_arg,
HA_CREATE_INFO *create_info)
{
DBUG_ENTER("ha_smalldb::create");
/*
This is not implemented but we want someone to be able to see that it
works.
*/
DBUG_RETURN(0);
}
void append_node(node* n){
if (first==NULL){
first=n;
}else{
node* iter=first;
while (iter->next!=NULL){iter=iter->next;}
iter->next=n;
}
}
struct st_mysql_storage_engine smalldb_storage_engine=
{ MYSQL_HANDLERTON_INTERFACE_VERSION };
static ulong srv_enum_var= 0;
static ulong srv_ulong_var= 0;
static double srv_double_var= 0;
const char *enum_var_names[]=
{
"e1", "e2", NullS
};
TYPELIB enum_var_typelib=
{
array_elements(enum_var_names) - 1, "enum_var_typelib",
enum_var_names, NULL
};
static MYSQL_SYSVAR_ENUM(
enum_var, // name
srv_enum_var, // varname
PLUGIN_VAR_RQCMDARG, // opt
"Sample ENUM system variable.", // comment
NULL, // check
NULL, // update
0, // def
&enum_var_typelib); // typelib
static MYSQL_SYSVAR_ULONG(
ulong_var,
srv_ulong_var,
PLUGIN_VAR_RQCMDARG,
"0..1000",
NULL,
NULL,
8,
0,
1000,
0);
static MYSQL_SYSVAR_DOUBLE(
double_var,
srv_double_var,
PLUGIN_VAR_RQCMDARG,
"0.500000..1000.500000",
NULL,
NULL,
8.5,
0.5,
1000.5,
0); // reserved always 0
static MYSQL_THDVAR_DOUBLE(
double_thdvar,
PLUGIN_VAR_RQCMDARG,
"0.500000..1000.500000",
NULL,
NULL,
8.5,
0.5,
1000.5,
0);
static struct st_mysql_sys_var* smalldb_system_variables[]= {
MYSQL_SYSVAR(enum_var),
MYSQL_SYSVAR(ulong_var),
MYSQL_SYSVAR(double_var),
MYSQL_SYSVAR(double_thdvar),
NULL
};
static int show_func_smalldb(MYSQL_THD thd, struct st_mysql_show_var *var,
char *buf)
{
var->type= SHOW_CHAR;
var->value= buf; // it's of SHOW_VAR_FUNC_BUFF_SIZE bytes
my_snprintf(buf, SHOW_VAR_FUNC_BUFF_SIZE,
"enum_var is %lu, ulong_var is %lu, "
"double_var is %f, %.6b", // %b is a MySQL extension
srv_enum_var, srv_ulong_var, srv_double_var, "really");
return 0;
}
static struct st_mysql_show_var func_status[]=
{
{"smalldb_func_smalldb", (char *)show_func_smalldb, SHOW_FUNC},
{0,0,SHOW_UNDEF}
};
mysql_declare_plugin(smalldb)
{
MYSQL_STORAGE_ENGINE_PLUGIN,
&smalldb_storage_engine,
"SMALLDB",
"Aleksey B",
"smalldb storage engine",
PLUGIN_LICENSE_GPL,
smalldb_init_func, /* Plugin Init */
smalldb_done_func, /* Plugin Deinit */
0x0001 /* 0.1 */,
func_status, /* status variables */
smalldb_system_variables, /* system variables */
NULL, /* config options */
0, /* flags */
}
mysql_declare_plugin_end;