-
Notifications
You must be signed in to change notification settings - Fork 475
/
api0api.cc
3457 lines (2781 loc) · 85.2 KB
/
api0api.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
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
998
999
1000
/*****************************************************************************
Copyright (c) 2008, 2022, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is also distributed with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have included with MySQL.
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, version 2.0, 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, Suite 500, Boston, MA 02110-1335 USA
*****************************************************************************/
/**************************************************//**
@file api/api0api.cc
InnoDB Native API
2008-08-01 Created Sunny Bains
3/20/2011 Jimmy Yang extracted from Embedded InnoDB
*******************************************************/
#include "ha_prototypes.h"
#include "api0api.h"
#include "api0misc.h"
#include "srv0start.h"
#include "dict0dict.h"
#include "btr0pcur.h"
#include "row0ins.h"
#include "row0upd.h"
#include "row0vers.h"
#include "trx0roll.h"
#include "dict0crea.h"
#include "row0merge.h"
#include "pars0pars.h"
#include "lock0types.h"
#include "row0sel.h"
#include "lock0lock.h"
#include "rem0cmp.h"
#include "dict0priv.h"
#include "trx0roll.h"
#include "row0trunc.h"
/** configure variable for binlog option with InnoDB APIs */
my_bool ib_binlog_enabled = FALSE;
/** configure variable for MDL option with InnoDB APIs */
my_bool ib_mdl_enabled = FALSE;
/** configure variable for disable rowlock with InnoDB APIs */
my_bool ib_disable_row_lock = FALSE;
/** configure variable for Transaction isolation levels */
ulong ib_trx_level_setting = IB_TRX_READ_UNCOMMITTED;
/** configure variable for background commit interval in seconds */
ulong ib_bk_commit_interval = 0;
/** InnoDB tuple types. */
enum ib_tuple_type_t{
TPL_TYPE_ROW, /*!< Data row tuple */
TPL_TYPE_KEY /*!< Index key tuple */
};
/** Query types supported. */
enum ib_qry_type_t{
QRY_NON, /*!< None/Sentinel */
QRY_INS, /*!< Insert operation */
QRY_UPD, /*!< Update operation */
QRY_SEL /*!< Select operation */
};
/** Query graph types. */
struct ib_qry_grph_t {
que_fork_t* ins; /*!< Innobase SQL query graph used
in inserts */
que_fork_t* upd; /*!< Innobase SQL query graph used
in updates or deletes */
que_fork_t* sel; /*!< dummy query graph used in
selects */
};
/** Query node types. */
struct ib_qry_node_t {
ins_node_t* ins; /*!< Innobase SQL insert node
used to perform inserts to the table */
upd_node_t* upd; /*!< Innobase SQL update node
used to perform updates and deletes */
sel_node_t* sel; /*!< Innobase SQL select node
used to perform selects on the table */
};
/** Query processing fields. */
struct ib_qry_proc_t {
ib_qry_node_t node; /*!< Query node*/
ib_qry_grph_t grph; /*!< Query graph */
};
/** Cursor instance for traversing tables/indexes. This will eventually
become row_prebuilt_t. */
struct ib_cursor_t {
mem_heap_t* heap; /*!< Instance heap */
mem_heap_t* query_heap; /*!< Heap to use for query graphs */
ib_qry_proc_t q_proc; /*!< Query processing info */
ib_match_mode_t match_mode; /*!< ib_cursor_moveto match mode */
row_prebuilt_t* prebuilt; /*!< For reading rows */
bool valid_trx; /*!< Valid transaction attached */
};
/** InnoDB table columns used during table and index schema creation. */
struct ib_col_t {
const char* name; /*!< Name of column */
ib_col_type_t ib_col_type; /*!< Main type of the column */
ulint len; /*!< Length of the column */
ib_col_attr_t ib_col_attr; /*!< Column attributes */
};
/** InnoDB index columns used during index and index schema creation. */
struct ib_key_col_t {
const char* name; /*!< Name of column */
ulint prefix_len; /*!< Column index prefix len or 0 */
};
struct ib_table_def_t;
/** InnoDB index schema used during index creation */
struct ib_index_def_t {
mem_heap_t* heap; /*!< Heap used to build this and all
its columns in the list */
const char* name; /*!< Index name */
dict_table_t* table; /*!< Parent InnoDB table */
ib_table_def_t* schema; /*!< Parent table schema that owns
this instance */
ibool clustered; /*!< True if clustered index */
ibool unique; /*!< True if unique index */
ib_vector_t* cols; /*!< Vector of columns */
trx_t* usr_trx; /*!< User transacton covering the
DDL operations */
};
/** InnoDB table schema used during table creation */
struct ib_table_def_t {
mem_heap_t* heap; /*!< Heap used to build this and all
its columns in the list */
const char* name; /*!< Table name */
ib_tbl_fmt_t ib_tbl_fmt; /*!< Row format */
ulint page_size; /*!< Page size */
ib_vector_t* cols; /*!< Vector of columns */
ib_vector_t* indexes; /*!< Vector of indexes */
dict_table_t* table; /* Table read from or NULL */
};
/** InnoDB tuple used for key operations. */
struct ib_tuple_t {
mem_heap_t* heap; /*!< Heap used to build
this and for copying
the column values. */
ib_tuple_type_t type; /*!< Tuple discriminitor. */
const dict_index_t* index; /*!< Index for tuple can be either
secondary or cluster index. */
dtuple_t* ptr; /*!< The internal tuple
instance */
};
/** The following counter is used to convey information to InnoDB
about server activity: in case of normal DML ops it is not
sensible to call srv_active_wake_master_thread after each
operation, we only do it every INNOBASE_WAKE_INTERVAL'th step. */
#define INNOBASE_WAKE_INTERVAL 32
/*****************************************************************//**
Check whether the InnoDB persistent cursor is positioned.
@return IB_TRUE if positioned */
UNIV_INLINE
ib_bool_t
ib_btr_cursor_is_positioned(
/*========================*/
btr_pcur_t* pcur) /*!< in: InnoDB persistent cursor */
{
return(pcur->old_stored
&& (pcur->pos_state == BTR_PCUR_IS_POSITIONED
|| pcur->pos_state == BTR_PCUR_WAS_POSITIONED));
}
/********************************************************************//**
Open a table using the table id, if found then increment table ref count.
@return table instance if found */
static
dict_table_t*
ib_open_table_by_id(
/*================*/
ib_id_u64_t tid, /*!< in: table id to lookup */
ib_bool_t locked) /*!< in: TRUE if own dict mutex */
{
dict_table_t* table;
table_id_t table_id;
table_id = tid;
if (!locked) {
dict_mutex_enter_for_mysql();
}
table = dict_table_open_on_id(table_id, TRUE, DICT_TABLE_OP_NORMAL);
if (table != NULL && table->file_unreadable) {
table = NULL;
}
if (!locked) {
dict_mutex_exit_for_mysql();
}
return(table);
}
/********************************************************************//**
Open a table using the table name, if found then increment table ref count.
@return table instance if found */
static
dict_table_t*
ib_open_table_by_name(
/*==================*/
const char* name) /*!< in: table name to lookup */
{
dict_table_t* table;
table = dict_table_open_on_name(name, FALSE, FALSE,
DICT_ERR_IGNORE_NONE);
if (table != NULL && table->file_unreadable) {
table = NULL;
}
return(table);
}
/********************************************************************//**
Find table using table name.
@return table instance if found */
static
dict_table_t*
ib_lookup_table_by_name(
/*====================*/
const char* name) /*!< in: table name to lookup */
{
dict_table_t* table;
table = dict_table_get_low(name);
if (table != NULL && table->file_unreadable) {
table = NULL;
}
return(table);
}
/********************************************************************//**
Increments innobase_active_counter and every INNOBASE_WAKE_INTERVALth
time calls srv_active_wake_master_thread. This function should be used
when a single database operation may introduce a small need for
server utility activity, like checkpointing. */
UNIV_INLINE
void
ib_wake_master_thread(void)
/*=======================*/
{
static ulint ib_signal_counter = 0;
++ib_signal_counter;
if ((ib_signal_counter % INNOBASE_WAKE_INTERVAL) == 0) {
srv_active_wake_master_thread();
}
}
/*****************************************************************//**
Read the columns from a rec into a tuple. */
static
void
ib_read_tuple(
/*==========*/
const rec_t* rec, /*!< in: Record to read */
ib_bool_t page_format, /*!< in: IB_TRUE if compressed format */
ib_tuple_t* tuple, /*!< in: tuple to read into */
void** rec_buf, /*!< in/out: row buffer */
ulint* len) /*!< in/out: buffer len */
{
ulint i;
void* ptr;
rec_t* copy;
ulint rec_meta_data;
ulint n_index_fields;
ulint offsets_[REC_OFFS_NORMAL_SIZE];
ulint* offsets = offsets_;
dtuple_t* dtuple = tuple->ptr;
const dict_index_t* index = tuple->index;
ulint offset_size;
rec_offs_init(offsets_);
offsets = rec_get_offsets(
rec, index, offsets, ULINT_UNDEFINED, &tuple->heap);
rec_meta_data = rec_get_info_bits(rec, page_format);
dtuple_set_info_bits(dtuple, rec_meta_data);
offset_size = rec_offs_size(offsets);
if (rec_buf && *rec_buf) {
if (*len < offset_size) {
free(*rec_buf);
*rec_buf = malloc(offset_size);
*len = offset_size;
}
ptr = *rec_buf;
} else {
/* Make a copy of the rec. */
ptr = mem_heap_alloc(tuple->heap, offset_size);
}
copy = rec_copy(ptr, rec, offsets);
n_index_fields = ut_min(
rec_offs_n_fields(offsets), dtuple_get_n_fields(dtuple));
for (i = 0; i < n_index_fields; ++i) {
ulint len;
const byte* data;
dfield_t* dfield;
if (tuple->type == TPL_TYPE_ROW) {
const dict_col_t* col;
ulint col_no;
const dict_field_t* index_field;
index_field = dict_index_get_nth_field(index, i);
col = dict_field_get_col(index_field);
col_no = dict_col_get_no(col);
dfield = dtuple_get_nth_field(dtuple, col_no);
} else {
dfield = dtuple_get_nth_field(dtuple, i);
}
data = rec_get_nth_field(copy, offsets, i, &len);
/* Fetch and copy any externally stored column. */
if (rec_offs_nth_extern(offsets, i)) {
const page_size_t page_size(
dict_table_page_size(index->table));
data = btr_rec_copy_externally_stored_field(
copy, offsets, page_size, i, &len,
tuple->heap);
ut_a(len != UNIV_SQL_NULL);
/* Reset the data length if it fails to copy
externally stored column */
if (data == NULL) {
/* data == NULL means that the externally
stored field was not written yet.
This record should only be seen by
TRX_ISO_READ_UNCOMMITTED transactions. */
ut_ad(ib_cfg_trx_level() ==
IB_TRX_READ_UNCOMMITTED);
len = 0;
}
}
dfield_set_data(dfield, data, len);
}
}
/*****************************************************************//**
Create an InnoDB key tuple.
@return tuple instance created, or NULL */
static
ib_tpl_t
ib_key_tuple_new_low(
/*=================*/
const dict_index_t* index, /*!< in: index for which tuple
required */
ulint n_cols, /*!< in: no. of user defined cols */
mem_heap_t* heap) /*!< in: memory heap */
{
ib_tuple_t* tuple;
ulint i;
ulint n_cmp_cols;
tuple = static_cast<ib_tuple_t*>(
mem_heap_alloc(heap, sizeof(*tuple)));
if (tuple == NULL) {
mem_heap_free(heap);
return(NULL);
}
tuple->heap = heap;
tuple->index = index;
tuple->type = TPL_TYPE_KEY;
/* Is it a generated clustered index ? */
if (n_cols == 0) {
++n_cols;
}
tuple->ptr = dtuple_create(heap, n_cols);
/* Copy types and set to SQL_NULL. */
dict_index_copy_types(tuple->ptr, index, n_cols);
for (i = 0; i < n_cols; i++) {
dfield_t* dfield;
dfield = dtuple_get_nth_field(tuple->ptr, i);
dfield_set_null(dfield);
}
n_cmp_cols = dict_index_get_n_ordering_defined_by_user(index);
dtuple_set_n_fields_cmp(tuple->ptr, n_cmp_cols);
return((ib_tpl_t) tuple);
}
/*****************************************************************//**
Create an InnoDB key tuple.
@return tuple instance created, or NULL */
static
ib_tpl_t
ib_key_tuple_new(
/*=============*/
const dict_index_t* index, /*!< in: index of tuple */
ulint n_cols) /*!< in: no. of user defined cols */
{
mem_heap_t* heap;
heap = mem_heap_create(64);
if (heap == NULL) {
return(NULL);
}
return(ib_key_tuple_new_low(index, n_cols, heap));
}
/*****************************************************************//**
Create an InnoDB row tuple.
@return tuple instance, or NULL */
static
ib_tpl_t
ib_row_tuple_new_low(
/*=================*/
const dict_index_t* index, /*!< in: index of tuple */
ulint n_cols, /*!< in: no. of cols in tuple */
mem_heap_t* heap) /*!< in: memory heap */
{
ib_tuple_t* tuple;
tuple = static_cast<ib_tuple_t*>(mem_heap_alloc(heap, sizeof(*tuple)));
if (tuple == NULL) {
mem_heap_free(heap);
return(NULL);
}
tuple->heap = heap;
tuple->index = index;
tuple->type = TPL_TYPE_ROW;
tuple->ptr = dtuple_create(heap, n_cols);
/* Copy types and set to SQL_NULL. */
dict_table_copy_types(tuple->ptr, index->table);
return((ib_tpl_t) tuple);
}
/*****************************************************************//**
Create an InnoDB row tuple.
@return tuple instance, or NULL */
static
ib_tpl_t
ib_row_tuple_new(
/*=============*/
const dict_index_t* index, /*!< in: index of tuple */
ulint n_cols) /*!< in: no. of cols in tuple */
{
mem_heap_t* heap;
heap = mem_heap_create(64);
if (heap == NULL) {
return(NULL);
}
return(ib_row_tuple_new_low(index, n_cols, heap));
}
/*****************************************************************//**
Begin a transaction.
@return innobase txn handle */
ib_err_t
ib_trx_start(
/*=========*/
ib_trx_t ib_trx, /*!< in: transaction to restart */
ib_trx_level_t ib_trx_level, /*!< in: trx isolation level */
ib_bool_t read_write, /*!< in: true if read write
transaction */
ib_bool_t auto_commit, /*!< in: auto commit after each
single DML */
void* thd) /*!< in: THD */
{
ib_err_t err = DB_SUCCESS;
trx_t* trx = (trx_t*) ib_trx;
ut_a(ib_trx_level <= IB_TRX_SERIALIZABLE);
trx->api_trx = true;
trx->api_auto_commit = auto_commit;
trx->read_write = read_write;
trx_start_if_not_started(trx, read_write);
trx->isolation_level = ib_trx_level;
/* FIXME: This is a place holder, we should add an arg that comes
from the client. */
trx->mysql_thd = static_cast<THD*>(thd);
return(err);
}
/*****************************************************************//**
Begin a transaction. This will allocate a new transaction handle.
put the transaction in the active state.
@return innobase txn handle */
ib_trx_t
ib_trx_begin(
/*=========*/
ib_trx_level_t ib_trx_level, /*!< in: trx isolation level */
ib_bool_t read_write, /*!< in: true if read write
transaction */
ib_bool_t auto_commit) /*!< in: auto commit after each
single DML */
{
trx_t* trx;
ib_bool_t started;
trx = trx_allocate_for_mysql();
started = ib_trx_start(static_cast<ib_trx_t>(trx), ib_trx_level,
read_write, auto_commit, NULL);
ut_a(started);
return(static_cast<ib_trx_t>(trx));
}
/*****************************************************************//**
Check if transaction is read_only
@return transaction read_only status */
ib_u32_t
ib_trx_read_only(
/*=============*/
ib_trx_t ib_trx) /*!< in: trx handle */
{
trx_t* trx = (trx_t*) ib_trx;
return(trx->read_only);
}
/*****************************************************************//**
Get a trx start time.
@return trx start_time */
ib_u64_t
ib_trx_get_start_time(
/*==================*/
ib_trx_t ib_trx) /*!< in: transaction */
{
trx_t* trx = (trx_t*) ib_trx;
return(static_cast<ib_u64_t>(trx->start_time));
}
/*****************************************************************//**
Release the resources of the transaction.
@return DB_SUCCESS or err code */
ib_err_t
ib_trx_release(
/*===========*/
ib_trx_t ib_trx) /*!< in: trx handle */
{
trx_t* trx = (trx_t*) ib_trx;
ut_ad(trx != NULL);
trx_free_for_mysql(trx);
return(DB_SUCCESS);
}
/*****************************************************************//**
Commit a transaction. This function will also release the schema
latches too.
@return DB_SUCCESS or err code */
ib_err_t
ib_trx_commit(
/*==========*/
ib_trx_t ib_trx) /*!< in: trx handle */
{
ib_err_t err = DB_SUCCESS;
trx_t* trx = reinterpret_cast<trx_t*>(ib_trx);
if (!trx_is_started(trx)) {
return(err);
}
trx_commit(trx);
return(DB_SUCCESS);
}
/*****************************************************************//**
Rollback a transaction. This function will also release the schema
latches too.
@return DB_SUCCESS or err code */
ib_err_t
ib_trx_rollback(
/*============*/
ib_trx_t ib_trx) /*!< in: trx handle */
{
ib_err_t err;
trx_t* trx = (trx_t*) ib_trx;
err = static_cast<ib_err_t>(trx_rollback_for_mysql(trx));
/* It should always succeed */
ut_a(err == DB_SUCCESS);
return(err);
}
#ifdef _WIN32
/*****************************************************************//**
Convert a string to lower case. */
static
void
ib_to_lower_case(
/*=============*/
char* ptr) /*!< string to convert to lower case */
{
while (*ptr) {
*ptr = tolower(*ptr);
++ptr;
}
}
#endif /* _WIN32 */
/*****************************************************************//**
Normalizes a table name string. A normalized name consists of the
database name catenated to '/' and table name. An example:
test/mytable. On Windows normalization puts both the database name and the
table name always to lower case. This function can be called for system
tables and they don't have a database component. For tables that don't have
a database component, we don't normalize them to lower case on Windows.
The assumption is that they are system tables that reside in the system
table space. */
static
void
ib_normalize_table_name(
/*====================*/
char* norm_name, /*!< out: normalized name as a
null-terminated string */
const char* name) /*!< in: table name string */
{
const char* ptr = name;
/* Scan name from the end */
ptr += ut_strlen(name) - 1;
/* Find the start of the table name. */
while (ptr >= name && *ptr != '\\' && *ptr != '/' && ptr > name) {
--ptr;
}
/* For system tables there is no '/' or dbname. */
ut_a(ptr >= name);
if (ptr > name) {
const char* db_name;
const char* table_name;
table_name = ptr + 1;
--ptr;
while (ptr >= name && *ptr != '\\' && *ptr != '/') {
ptr--;
}
db_name = ptr + 1;
memcpy(norm_name, db_name,
ut_strlen(name) + 1 - (db_name - name));
norm_name[table_name - db_name - 1] = '/';
#ifdef _WIN32
ib_to_lower_case(norm_name);
#endif
} else {
ut_strcpy(norm_name, name);
}
}
/*****************************************************************//**
Get a table id. The caller must have acquired the dictionary mutex.
@return DB_SUCCESS if found */
static
ib_err_t
ib_table_get_id_low(
/*================*/
const char* table_name, /*!< in: table to find */
ib_id_u64_t* table_id) /*!< out: table id if found */
{
dict_table_t* table;
ib_err_t err = DB_TABLE_NOT_FOUND;
*table_id = 0;
table = ib_lookup_table_by_name(table_name);
if (table != NULL) {
*table_id = (table->id);
err = DB_SUCCESS;
}
return(err);
}
/*****************************************************************//**
Create an internal cursor instance.
@return DB_SUCCESS or err code */
static
ib_err_t
ib_create_cursor(
/*=============*/
ib_crsr_t* ib_crsr, /*!< out: InnoDB cursor */
dict_table_t* table, /*!< in: table instance */
dict_index_t* index, /*!< in: index to use */
trx_t* trx) /*!< in: transaction */
{
mem_heap_t* heap;
ib_cursor_t* cursor;
ib_err_t err = DB_SUCCESS;
heap = mem_heap_create(sizeof(*cursor) * 2);
if (heap != NULL) {
row_prebuilt_t* prebuilt;
cursor = static_cast<ib_cursor_t*>(
mem_heap_zalloc(heap, sizeof(*cursor)));
cursor->heap = heap;
cursor->query_heap = mem_heap_create(64);
if (cursor->query_heap == NULL) {
mem_heap_free(heap);
return(DB_OUT_OF_MEMORY);
}
cursor->prebuilt = row_create_prebuilt(table, 0);
prebuilt = cursor->prebuilt;
prebuilt->trx = trx;
cursor->valid_trx = TRUE;
prebuilt->table = table;
prebuilt->select_lock_type = LOCK_NONE;
prebuilt->innodb_api = TRUE;
prebuilt->index = index;
ut_a(prebuilt->index != NULL);
if (prebuilt->trx != NULL) {
++prebuilt->trx->n_mysql_tables_in_use;
prebuilt->index_usable =
row_merge_is_index_usable(
prebuilt->trx, prebuilt->index);
/* Assign a read view if the transaction does
not have it yet */
trx_assign_read_view(prebuilt->trx);
}
*ib_crsr = (ib_crsr_t) cursor;
} else {
err = DB_OUT_OF_MEMORY;
}
return(err);
}
/*****************************************************************//**
Create an internal cursor instance, and set prebuilt->index to index
with supplied index_id.
@return DB_SUCCESS or err code */
static
ib_err_t
ib_create_cursor_with_index_id(
/*===========================*/
ib_crsr_t* ib_crsr, /*!< out: InnoDB cursor */
dict_table_t* table, /*!< in: table instance */
ib_id_u64_t index_id, /*!< in: index id or 0 */
trx_t* trx) /*!< in: transaction */
{
dict_index_t* index;
if (index_id != 0) {
mutex_enter(&dict_sys->mutex);
index = dict_index_find_on_id_low(index_id);
mutex_exit(&dict_sys->mutex);
} else {
index = dict_table_get_first_index(table);
}
return(ib_create_cursor(ib_crsr, table, index, trx));
}
/*****************************************************************//**
Open an InnoDB table and return a cursor handle to it.
@return DB_SUCCESS or err code */
ib_err_t
ib_cursor_open_table_using_id(
/*==========================*/
ib_id_u64_t table_id, /*!< in: table id of table to open */
ib_trx_t ib_trx, /*!< in: Current transaction handle
can be NULL */
ib_crsr_t* ib_crsr) /*!< out,own: InnoDB cursor */
{
ib_err_t err;
dict_table_t* table;
const ib_bool_t locked
= ib_trx && ib_schema_lock_is_exclusive(ib_trx);
table = ib_open_table_by_id(table_id, locked);
if (table == NULL) {
return(DB_TABLE_NOT_FOUND);
}
err = ib_create_cursor_with_index_id(ib_crsr, table, 0,
(trx_t*) ib_trx);
return(err);
}
/*****************************************************************//**
Open an InnoDB secondary index cursor and return a cursor handle to it.
@return DB_SUCCESS or err code */
ib_err_t
ib_cursor_open_index_using_name(
/*============================*/
ib_crsr_t ib_open_crsr, /*!< in: open/active cursor */
const char* index_name, /*!< in: secondary index name */
ib_crsr_t* ib_crsr, /*!< out,own: InnoDB index cursor */
int* idx_type, /*!< out: index is cluster index */
ib_id_u64_t* idx_id) /*!< out: index id */
{
dict_table_t* table;
dict_index_t* index;
index_id_t index_id = 0;
ib_err_t err = DB_TABLE_NOT_FOUND;
ib_cursor_t* cursor = (ib_cursor_t*) ib_open_crsr;
*idx_type = 0;
*idx_id = 0;
*ib_crsr = NULL;
/* We want to increment the ref count, so we do a redundant search. */
table = dict_table_open_on_id(cursor->prebuilt->table->id,
FALSE, DICT_TABLE_OP_NORMAL);
ut_a(table != NULL);
/* The first index is always the cluster index. */
index = dict_table_get_first_index(table);
/* Traverse the user defined indexes. */
while (index != NULL) {
if (innobase_strcasecmp(index->name, index_name) == 0) {
index_id = index->id;
*idx_type = index->type;
*idx_id = index_id;
break;
}
index = UT_LIST_GET_NEXT(indexes, index);
}
if (!index_id) {
dict_table_close(table, FALSE, FALSE);
return(DB_ERROR);
}
if (index_id > 0) {
ut_ad(index->id == index_id);
err = ib_create_cursor(
ib_crsr, table, index, cursor->prebuilt->trx);
}
if (*ib_crsr != NULL) {
const ib_cursor_t* cursor;
cursor = *(ib_cursor_t**) ib_crsr;
if (cursor->prebuilt->index == NULL) {
err = ib_cursor_close(*ib_crsr);
ut_a(err == DB_SUCCESS);
*ib_crsr = NULL;
}
}
return(err);
}
/*****************************************************************//**
Open an InnoDB table and return a cursor handle to it.
@return DB_SUCCESS or err code */
ib_err_t
ib_cursor_open_table(
/*=================*/
const char* name, /*!< in: table name */
ib_trx_t ib_trx, /*!< in: Current transaction handle
can be NULL */
ib_crsr_t* ib_crsr) /*!< out,own: InnoDB cursor */
{
ib_err_t err;
dict_table_t* table;
char* normalized_name;
normalized_name = static_cast<char*>(ut_malloc_nokey(ut_strlen(name)
+ 1));
ib_normalize_table_name(normalized_name, name);
if (ib_trx != NULL) {
if (!ib_schema_lock_is_exclusive(ib_trx)) {
table = ib_open_table_by_name(normalized_name);
} else {