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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
|
/*
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/internal/my_pthread.h"
#include <fcntl.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 "libinnodb_version_func.h"
#include "libinnodb_datadict_dump_func.h"
#include "embedded_innodb-1.0/innodb.h"
#include "embedded_innodb_engine.h"
#include <drizzled/field.h>
using namespace std;
using namespace google;
using namespace drizzled;
int read_row_from_innodb(ib_crsr_t cursor, ib_tpl_t tuple, Table* table);
#define EMBEDDED_INNODB_EXT ".EID"
const char INNODB_TABLE_DEFINITIONS_TABLE[]= "data_dictionary/innodb_table_definitions";
static const char *EmbeddedInnoDBCursor_exts[] = {
NULL
};
class EmbeddedInnoDBEngine : public drizzled::plugin::StorageEngine
{
public:
EmbeddedInnoDBEngine(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 |
HTON_HAS_DATA_DICTIONARY)
{
table_definition_ext= EMBEDDED_INNODB_EXT;
}
virtual Cursor *create(TableShare &table,
drizzled::memory::Root *mem_root)
{
return new (mem_root) EmbeddedInnoDBCursor(*this, table);
}
const char **bas_ext() const {
return EmbeddedInnoDBCursor_exts;
}
int doCreateTable(Session*,
const char *,
Table&,
drizzled::message::Table&);
int doDropTable(Session&, const string table_name);
int doGetTableDefinition(Session& session,
const char* path,
const char *db,
const char *table_name,
const bool is_tmp,
drizzled::message::Table *table_proto);
void doGetTableNames(drizzled::CachedDirectory &,
string& database_name, set<string>& set_of_names);
/* 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);
}
};
EmbeddedInnoDBCursor::EmbeddedInnoDBCursor(drizzled::plugin::StorageEngine &engine_arg,
TableShare &table_arg)
:Cursor(engine_arg, table_arg)
{ }
int EmbeddedInnoDBCursor::open(const char *name, int, uint32_t)
{
ib_err_t err= ib_cursor_open_table(name+2, NULL, &cursor);
assert (err == DB_SUCCESS);
return(0);
}
int EmbeddedInnoDBCursor::close(void)
{
ib_cursor_close(cursor);
return 0;
}
static int create_table_add_field(ib_tbl_sch_t schema,
const message::Table::Field &field,
ib_err_t *err)
{
ib_col_attr_t column_attr= IB_COL_NONE;
if (field.has_constraints() && ! field.constraints().is_nullable())
column_attr= IB_COL_NOT_NULL;
switch (field.type())
{
case message::Table::Field::VARCHAR:
*err= ib_table_schema_add_col(schema, field.name().c_str(), IB_VARCHAR,
column_attr, 0,
field.string_options().length());
break;
case message::Table::Field::INTEGER:
*err= ib_table_schema_add_col(schema, field.name().c_str(), IB_INT,
column_attr, 0, 4);
break;
case message::Table::Field::BIGINT:
*err= ib_table_schema_add_col(schema, field.name().c_str(), IB_INT,
column_attr, 0, 8);
break;
default:
my_error(ER_CHECK_NOT_IMPLEMENTED, MYF(0), "Column Type");
return(HA_ERR_UNSUPPORTED);
}
return 0;
}
static ib_err_t store_table_message(ib_trx_t transaction, const char* table_name, drizzled::message::Table& table_message)
{
ib_crsr_t cursor;
ib_tpl_t message_tuple;
ib_err_t err;
string serialized_message;
err= ib_cursor_open_table(INNODB_TABLE_DEFINITIONS_TABLE, transaction, &cursor);
if (err != DB_SUCCESS)
return err;
message_tuple= ib_clust_read_tuple_create(cursor);
err= ib_col_set_value(message_tuple, 0, table_name, strlen(table_name));
if (err != DB_SUCCESS)
goto cleanup;
table_message.SerializeToString(&serialized_message);
err= ib_col_set_value(message_tuple, 1, serialized_message.c_str(),
serialized_message.length());
if (err != DB_SUCCESS)
goto cleanup;
err= ib_cursor_insert_row(cursor, message_tuple);
cleanup:
ib_tuple_delete(message_tuple);
ib_cursor_close(cursor);
return err;
}
int EmbeddedInnoDBEngine::doCreateTable(Session* session, const char *path,
Table& table_obj,
drizzled::message::Table& table_message)
{
ib_tbl_sch_t innodb_table_schema= NULL;
// ib_idx_sch_t innodb_pkey= NULL;
ib_trx_t innodb_schema_transaction;
ib_id_t innodb_table_id;
ib_err_t innodb_err= DB_SUCCESS;
(void)session;
(void)table_obj;
innodb_err= ib_table_schema_create(path+2, &innodb_table_schema, IB_TBL_COMPACT, 0);
if (innodb_err != DB_SUCCESS)
{
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
ER_CANT_CREATE_TABLE,
_("Cannot create table %s. InnoDB Error %d (%s)\n"),
path, innodb_err, ib_strerror(innodb_err));
return HA_ERR_GENERIC;
}
for (int colnr= 0; colnr < table_message.field_size() ; colnr++)
{
const message::Table::Field field = table_message.field(colnr);
int field_err= create_table_add_field(innodb_table_schema, field,
&innodb_err);
if (innodb_err != DB_SUCCESS || field_err != 0)
ib_table_schema_delete(innodb_table_schema); /* cleanup */
if (innodb_err != DB_SUCCESS)
{
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
ER_CANT_CREATE_TABLE,
_("Cannot create field %s on table %s."
" InnoDB Error %d (%s)\n"),
field.name().c_str(), path,
innodb_err, ib_strerror(innodb_err));
return HA_ERR_GENERIC;
}
if (field_err != 0)
return field_err;
}
for (int indexnr= 0; indexnr < table_message.indexes_size() ; indexnr++)
{
message::Table::Index *index = table_message.mutable_indexes(indexnr);
ib_idx_sch_t innodb_index;
ib_table_schema_add_index(innodb_table_schema, index->name().c_str(),
&innodb_index);
if (index->is_primary())
ib_index_schema_set_clustered(innodb_index);
if (index->is_unique())
ib_index_schema_set_unique(innodb_index);
if (index->type() == message::Table::Index::UNKNOWN_INDEX)
index->set_type(message::Table::Index::BTREE);
for (int partnr= 0; partnr < index->index_part_size(); partnr++)
{
/* TODO: Index prefix lengths */
const message::Table::Index::IndexPart part= index->index_part(partnr);
ib_index_schema_add_col(innodb_index, table_message.field(part.fieldnr()).name().c_str(), 0);
}
}
innodb_schema_transaction= ib_trx_begin(IB_TRX_REPEATABLE_READ);
ib_schema_lock_exclusive(innodb_schema_transaction);
innodb_err= ib_table_create(innodb_schema_transaction, innodb_table_schema,
&innodb_table_id);
if (innodb_err != DB_SUCCESS)
{
ib_trx_rollback(innodb_schema_transaction);
ib_table_schema_delete(innodb_table_schema);
if (innodb_err == DB_TABLE_IS_BEING_USED)
return EEXIST;
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
ER_CANT_CREATE_TABLE,
_("Cannot create table %s. InnoDB Error %d (%s)\n"),
path, innodb_err, ib_strerror(innodb_err));
return HA_ERR_GENERIC;
}
innodb_err= store_table_message(innodb_schema_transaction, path+2,
table_message);
if (innodb_err == DB_SUCCESS)
innodb_err= ib_trx_commit(innodb_schema_transaction);
else
innodb_err= ib_trx_rollback(innodb_schema_transaction);
ib_table_schema_delete(innodb_table_schema);
if (innodb_err != DB_SUCCESS)
{
push_warning_printf(session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
ER_CANT_CREATE_TABLE,
_("Cannot create table %s. InnoDB Error %d (%s)\n"),
path, innodb_err, ib_strerror(innodb_err));
return HA_ERR_GENERIC;
}
return 0;
}
static int delete_table_message_from_innodb(ib_trx_t transaction, const char* table_name)
{
ib_crsr_t cursor;
ib_tpl_t search_tuple;
int res;
ib_err_t err;
ib_cursor_open_table(INNODB_TABLE_DEFINITIONS_TABLE, transaction, &cursor);
search_tuple= ib_clust_search_tuple_create(cursor);
ib_col_set_value(search_tuple, 0, table_name, strlen(table_name));
// ib_cursor_set_match_mode(cursor, IB_EXACT_MATCH);
err= ib_cursor_moveto(cursor, search_tuple, IB_CUR_GE, &res);
if (err == DB_RECORD_NOT_FOUND || res != 0)
goto rollback;
err= ib_cursor_delete_row(cursor);
assert (err == DB_SUCCESS);
rollback:
ib_cursor_close(cursor);
ib_schema_unlock(transaction);
ib_tuple_delete(search_tuple);
return err;
}
int EmbeddedInnoDBEngine::doDropTable(Session& session, const string table_name)
{
ib_trx_t innodb_schema_transaction;
ib_err_t innodb_err;
innodb_schema_transaction= ib_trx_begin(IB_TRX_REPEATABLE_READ);
if (delete_table_message_from_innodb(innodb_schema_transaction, table_name.c_str()+2) != DB_SUCCESS)
{
ib_trx_rollback(innodb_schema_transaction);
return HA_ERR_GENERIC;
}
innodb_err= ib_table_drop(table_name.c_str()+2);
if (innodb_err == DB_TABLE_NOT_FOUND)
{
ib_trx_rollback(innodb_schema_transaction);
return ENOENT;
}
else if (innodb_err != DB_SUCCESS)
{
ib_trx_rollback(innodb_schema_transaction);
push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
ER_CANT_DELETE_FILE,
_("Cannot DROP table %s. InnoDB Error %d (%s)\n"),
table_name.c_str(),
innodb_err, ib_strerror(innodb_err));
return HA_ERR_GENERIC;
}
innodb_err= ib_trx_commit(innodb_schema_transaction);
if (innodb_err != DB_SUCCESS)
{
ib_trx_rollback(innodb_schema_transaction);
push_warning_printf(&session, DRIZZLE_ERROR::WARN_LEVEL_ERROR,
ER_CANT_DELETE_FILE,
_("Cannot DROP table %s. InnoDB Error %d (%s)\n"),
table_name.c_str(),
innodb_err, ib_strerror(innodb_err));
return HA_ERR_GENERIC;
}
return 0;
}
void EmbeddedInnoDBEngine::doGetTableNames(drizzled::CachedDirectory &,
string& database_name,
set<string>& set_of_names)
{
ib_trx_t transaction;
ib_crsr_t cursor;
string search_string(database_name);
search_string.append("/");
transaction = ib_trx_begin(IB_TRX_REPEATABLE_READ);
ib_schema_lock_exclusive(transaction);
int err= ib_cursor_open_table("SYS_TABLES", transaction, &cursor);
assert(err == DB_SUCCESS);
ib_cursor_first(cursor);
ib_tpl_t read_tuple;
read_tuple= ib_clust_read_tuple_create(cursor);
/*
What we really want to do here is not the ib_cursor_first() as above
but instead using a search tuple to find the first instance of
"database_name/" in SYS_TABLES.NAME.
There is currently a bug in libinnodb (1.0.3.5325) that means this ends in
a failed assert() rather than speeedy bliss.
It should be fixed in the next release.
See: http://forums.innodb.com/read.php?8,1090,1094#msg-1094
we want something like:
ib_col_set_value(read_tuple, 0,
search_string.c_str(), search_string.length());
err = ib_cursor_moveto(cursor, read_tuple, IB_CUR_GE, &res);
*/
while (err == DB_SUCCESS)
{
err= ib_cursor_read_row(cursor, read_tuple);
const char *table_name;
int table_name_len;
ib_col_meta_t column_metadata;
table_name= (const char*)ib_col_get_value(read_tuple, 0);
table_name_len= ib_col_get_meta(read_tuple, 0, &column_metadata);
if (search_string.compare(0, search_string.length(),
table_name, search_string.length()) == 0)
{
const char *just_table_name= strchr(table_name, '/');
assert(just_table_name);
just_table_name++; /* skip over '/' */
set_of_names.insert(just_table_name);
}
err= ib_cursor_next(cursor);
read_tuple= ib_tuple_clear(read_tuple);
}
ib_tuple_delete(read_tuple);
ib_cursor_close(cursor);
ib_trx_commit(transaction);
}
static int read_table_message_from_innodb(const char* table_name, drizzled::message::Table *table_message)
{
ib_trx_t transaction;
ib_tpl_t search_tuple;
ib_tpl_t read_tuple;
ib_crsr_t cursor;
const char *message;
ib_ulint_t message_len;
ib_col_meta_t col_meta;
int res;
ib_err_t err;
transaction= ib_trx_begin(IB_TRX_REPEATABLE_READ);
ib_schema_lock_exclusive(transaction);
ib_cursor_open_table(INNODB_TABLE_DEFINITIONS_TABLE, transaction, &cursor);
search_tuple= ib_clust_search_tuple_create(cursor);
read_tuple= ib_clust_read_tuple_create(cursor);
ib_col_set_value(search_tuple, 0, table_name, strlen(table_name));
// ib_cursor_set_match_mode(cursor, IB_EXACT_MATCH);
err= ib_cursor_moveto(cursor, search_tuple, IB_CUR_GE, &res);
if (err == DB_RECORD_NOT_FOUND || res != 0)
goto rollback;
err= ib_cursor_read_row(cursor, read_tuple);
if (err == DB_RECORD_NOT_FOUND || res != 0)
goto rollback;
message= (const char*)ib_col_get_value(read_tuple, 1);
message_len= ib_col_get_meta(read_tuple, 1, &col_meta);
if (table_message->ParseFromArray(message, message_len) == false)
goto rollback;
ib_tuple_delete(search_tuple);
ib_tuple_delete(read_tuple);
ib_cursor_close(cursor);
ib_trx_commit(transaction);
return 0;
rollback:
ib_tuple_delete(search_tuple);
ib_tuple_delete(read_tuple);
ib_cursor_close(cursor);
ib_schema_unlock(transaction);
ib_trx_rollback(transaction);
if (strcmp(table_name, INNODB_TABLE_DEFINITIONS_TABLE) == 0)
{
message::Table::StorageEngine *engine= table_message->mutable_engine();
engine->set_name("InnoDB");
table_message->set_name("innodb_table_message");
table_message->set_type(message::Table::STANDARD);
message::Table::Field *field= table_message->add_field();
field->set_name("table_name");
field->set_type(message::Table::Field::VARCHAR);
message::Table::Field::StringFieldOptions *stropt= field->mutable_string_options();
stropt->set_length(IB_MAX_TABLE_NAME_LEN);
stropt->set_collation(my_charset_bin.csname);
stropt->set_collation_id(my_charset_bin.number);
field= table_message->add_field();
field->set_name("message");
field->set_type(message::Table::Field::BLOB);
message::Table::Index *index= table_message->add_indexes();
index->set_name("PRIMARY");
index->set_is_primary(true);
index->set_is_unique(true);
index->set_type(message::Table::Index::BTREE);
index->set_key_length(IB_MAX_TABLE_NAME_LEN);
message::Table::Index::IndexPart *part= index->add_index_part();
part->set_fieldnr(0);
part->set_compare_length(IB_MAX_TABLE_NAME_LEN);
return 0;
}
return -1;
}
int EmbeddedInnoDBEngine::doGetTableDefinition(Session&,
const char* path,
const char *,
const char *,
const bool,
drizzled::message::Table *table)
{
ib_crsr_t innodb_cursor= NULL;
if (ib_cursor_open_table(path+2, NULL, &innodb_cursor) != DB_SUCCESS)
return ENOENT;
ib_cursor_close(innodb_cursor);
if (table)
{
read_table_message_from_innodb(path+2, table);
}
return EEXIST;
}
const char *EmbeddedInnoDBCursor::index_type(uint32_t)
{
return("BTREE");
}
int EmbeddedInnoDBCursor::write_row(unsigned char *)
{
if (table->next_number_field)
update_auto_increment();
ib_err_t err;
int colnr= 0;
int ret= 0;
transaction= ib_trx_begin(IB_TRX_REPEATABLE_READ);
tuple= ib_clust_read_tuple_create(cursor);
ib_cursor_attach_trx(cursor, transaction);
ib_cursor_first(cursor);
for (Field **field= table->field; *field; field++, colnr++)
{
if ((**field).type() == DRIZZLE_TYPE_VARCHAR)
{
/* To get around the length bytes (1 or 2) at (**field).ptr
we can use Field_varstring::val_str to a String
to get a pointer to the real string without copying it.
*/
String str;
(**field).setReadSet();
(**field).val_str(&str);
err= ib_col_set_value(tuple, colnr, str.ptr(), str.length());
}
else
{
err= ib_col_set_value(tuple, colnr, (*field)->ptr, (*field)->data_length());
}
assert (err == DB_SUCCESS);
}
err= ib_cursor_insert_row(cursor, tuple);
if (err == DB_DUPLICATE_KEY)
ret= HA_ERR_FOUND_DUPP_KEY;
ib_tuple_clear(tuple);
ib_tuple_delete(tuple);
ib_cursor_reset(cursor);
ib_trx_commit(transaction);
return ret;
}
int EmbeddedInnoDBCursor::delete_row(const unsigned char *)
{
ib_cursor_prev(cursor);
ib_cursor_delete_row(cursor);
ib_cursor_next(cursor);
return 0;
}
int EmbeddedInnoDBCursor::rnd_init(bool)
{
transaction= ib_trx_begin(IB_TRX_REPEATABLE_READ);
ib_cursor_attach_trx(cursor, transaction);
tuple= ib_clust_read_tuple_create(cursor);
ib_cursor_first(cursor);
return(0);
}
int read_row_from_innodb(ib_crsr_t cursor, ib_tpl_t tuple, Table* table)
{
ib_err_t err;
err= ib_cursor_read_row(cursor, tuple);
if (err != DB_SUCCESS) // FIXME
return HA_ERR_END_OF_FILE;
int colnr= 0;
for (Field **field=table->field ; *field ; field++, colnr++)
{
if (! (**field).isReadSet())
continue;
(**field).setWriteSet();
uint32_t length= ib_col_get_len(tuple, colnr);
if (length == IB_SQL_NULL)
(**field).set_null();
else
(**field).set_notnull();
if ((**field).type() == DRIZZLE_TYPE_VARCHAR)
{
(*field)->store((const char*)ib_col_get_value(tuple, colnr),
length,
&my_charset_bin);
}
else
{
ib_col_copy_value(tuple, colnr, (*field)->ptr, (*field)->data_length());
}
}
ib_tuple_clear(tuple);
return 0;
}
int EmbeddedInnoDBCursor::rnd_next(unsigned char *)
{
ib_err_t err;
int ret;
ret= read_row_from_innodb(cursor, tuple, table);
err= ib_cursor_next(cursor);
return ret;
}
int EmbeddedInnoDBCursor::rnd_end()
{
ib_tuple_delete(tuple);
ib_cursor_reset(cursor);
ib_trx_commit(transaction);
return 0;
}
int EmbeddedInnoDBCursor::rnd_pos(unsigned char *, unsigned char *)
{
assert(0);
return(0);
}
void EmbeddedInnoDBCursor::position(const unsigned char *)
{
assert(0);
return;
}
double EmbeddedInnoDBCursor::scan_time()
{
return 0.1;
}
int EmbeddedInnoDBCursor::info(uint32_t flag)
{
stats.records= 2;
if (flag & HA_STATUS_AUTO)
stats.auto_increment_value= 1;
return(0);
}
int EmbeddedInnoDBCursor::index_init(uint32_t keynr, bool)
{
active_index= keynr;
transaction= ib_trx_begin(IB_TRX_REPEATABLE_READ);
ib_cursor_attach_trx(cursor, transaction);
if (active_index == 0)
{
tuple= ib_clust_read_tuple_create(cursor);
}
else
{
/* Open 2ndary index */
}
return 0;
}
int EmbeddedInnoDBCursor::index_read(unsigned char *buf,
const unsigned char *key_ptr,
uint32_t key_len,
drizzled::ha_rkey_function find_flag)
{
ib_tpl_t search_tuple;
int res;
ib_err_t err;
int ret;
(void)buf;
(void)find_flag;
search_tuple= ib_clust_search_tuple_create(cursor);
ib_col_set_value(search_tuple, 0, key_ptr, key_len);
err= ib_cursor_moveto(cursor, search_tuple, IB_CUR_GE, &res);
if (err == DB_RECORD_NOT_FOUND || res != 0)
return HA_ERR_END_OF_FILE;
ret= read_row_from_innodb(cursor, tuple, table);
err= ib_cursor_next(cursor);
return 0;
}
int EmbeddedInnoDBCursor::index_next(unsigned char *)
{
int ret= HA_ERR_END_OF_FILE;
ib_err_t err;
if (active_index == 0)
{
ret= read_row_from_innodb(cursor, tuple, table);
err= ib_cursor_next(cursor);
}
return ret;
}
int EmbeddedInnoDBCursor::index_end()
{
active_index= MAX_KEY;
return rnd_end();
}
int EmbeddedInnoDBCursor::index_prev(unsigned char *)
{
int ret= HA_ERR_END_OF_FILE;
ib_err_t err;
if (active_index == 0)
{
ret= read_row_from_innodb(cursor, tuple, table);
err= ib_cursor_prev(cursor);
}
return ret;
}
int EmbeddedInnoDBCursor::index_first(unsigned char *)
{
int ret= HA_ERR_END_OF_FILE;
ib_err_t err;
ib_cursor_first(cursor);
if (active_index == 0)
{
ret= read_row_from_innodb(cursor, tuple, table);
err= ib_cursor_next(cursor);
}
return ret;
}
int EmbeddedInnoDBCursor::index_last(unsigned char *)
{
int ret= HA_ERR_END_OF_FILE;
ib_err_t err;
ib_cursor_last(cursor);
if (active_index == 0)
{
ret= read_row_from_innodb(cursor, tuple, table);
err= ib_cursor_prev(cursor);
}
return ret;
}
static int create_table_message_table()
{
ib_tbl_sch_t schema;
ib_idx_sch_t index_schema;
ib_trx_t transaction;
ib_id_t table_id;
ib_database_create("data_dictionary");
ib_table_schema_create(INNODB_TABLE_DEFINITIONS_TABLE, &schema,
IB_TBL_COMPACT, 0);
ib_table_schema_add_col(schema, "table_name", IB_VARCHAR, IB_COL_NONE, 0,
IB_MAX_TABLE_NAME_LEN);
ib_table_schema_add_col(schema, "message", IB_BLOB, IB_COL_NONE, 0, 0);
ib_table_schema_add_index(schema, "PRIMARY_KEY", &index_schema);
ib_index_schema_add_col(index_schema, "table_name", 0);
ib_index_schema_set_clustered(index_schema);
transaction= ib_trx_begin(IB_TRX_REPEATABLE_READ);
ib_schema_lock_exclusive(transaction);
ib_table_create(transaction, schema, &table_id);
ib_trx_commit(transaction);
ib_table_schema_delete(schema);
return 0;
}
static drizzled::plugin::StorageEngine *embedded_innodb_engine= NULL;
static char default_innodb_data_file_path[]= "ibdata1:10M:autoextend";
static char* innodb_data_file_path= NULL;
static int64_t innodb_log_file_size;
static int64_t innodb_log_files_in_group;
static int embedded_innodb_init(drizzled::plugin::Registry ®istry)
{
ib_err_t err;
err= ib_init();
if (err != DB_SUCCESS)
goto innodb_error;
if (innodb_data_file_path == NULL)
innodb_data_file_path= default_innodb_data_file_path;
err= ib_cfg_set_text("data_file_path", innodb_data_file_path);
if (err != DB_SUCCESS)
goto innodb_error;
err= ib_cfg_set_int("log_file_size", innodb_log_file_size);
if (err != DB_SUCCESS)
goto innodb_error;
err= ib_cfg_set_int("log_files_in_group", innodb_log_files_in_group);
if (err != DB_SUCCESS)
goto innodb_error;
err= ib_startup("barracuda");
if (err != DB_SUCCESS)
goto innodb_error;
create_table_message_table();
embedded_innodb_engine= new EmbeddedInnoDBEngine("InnoDB");
registry.add(embedded_innodb_engine);
libinnodb_version_func_initialize(registry);
libinnodb_datadict_dump_func_initialize(registry);
return 0;
innodb_error:
fprintf(stderr, _("Error starting Embedded InnoDB %d (%s)\n"),
err, ib_strerror(err));
return -1;
}
static int embedded_innodb_fini(drizzled::plugin::Registry ®istry)
{
int err;
registry.remove(embedded_innodb_engine);
delete embedded_innodb_engine;
libinnodb_version_func_finalize(registry);
libinnodb_datadict_dump_func_finalize(registry);
err= ib_shutdown(IB_SHUTDOWN_NORMAL);
if (err != DB_SUCCESS)
{
fprintf(stderr,"Error %d shutting down Embedded InnoDB!", err);
return err;
}
return 0;
}
static DRIZZLE_SYSVAR_STR(data_file_path, innodb_data_file_path,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"Path to individual files and their sizes.",
NULL, NULL, NULL);
static DRIZZLE_SYSVAR_LONGLONG(log_file_size, innodb_log_file_size,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"Size of each log file in a log group.",
NULL, NULL, 5*1024*1024L, 1*1024*1024L, INT64_MAX, 1024*1024L);
static DRIZZLE_SYSVAR_LONGLONG(log_files_in_group, innodb_log_files_in_group,
PLUGIN_VAR_RQCMDARG | PLUGIN_VAR_READONLY,
"Number of log files in the log group. InnoDB writes to the files in a circular fashion. Value 3 is recommended here.",
NULL, NULL, 2, 2, 100, 0);
static DRIZZLE_SessionVAR_ULONG(lock_wait_timeout, PLUGIN_VAR_RQCMDARG,
"Placeholder: to be compatible with InnoDB plugin.",
NULL, NULL, 50, 1, 1024 * 1024 * 1024, 0);
static drizzle_sys_var* innobase_system_variables[]= {
DRIZZLE_SYSVAR(data_file_path),
DRIZZLE_SYSVAR(lock_wait_timeout),
DRIZZLE_SYSVAR(log_file_size),
DRIZZLE_SYSVAR(log_files_in_group),
NULL
};
DRIZZLE_DECLARE_PLUGIN
{
DRIZZLE_VERSION_ID,
"INNODB",
"1.0",
"Stewart Smith",
"Used to test rest of server with various table proto messages",
PLUGIN_LICENSE_GPL,
embedded_innodb_init, /* Plugin Init */
embedded_innodb_fini, /* Plugin Deinit */
innobase_system_variables, /* system variables */
NULL /* config options */
}
DRIZZLE_DECLARE_PLUGIN_END;
|