Class: Innodb::DataDictionary

Inherits:
Object
  • Object
show all
Defined in:
lib/innodb/data_dictionary.rb

Overview

A class representing InnoDB’s data dictionary, which contains metadata about tables, columns, and indexes.

Defined Under Namespace

Classes: SYS_COLUMNS_PRIMARY, SYS_FIELDS_PRIMARY, SYS_INDEXES_PRIMARY, SYS_TABLES_ID, SYS_TABLES_PRIMARY

Constant Summary collapse

DATA_DICTIONARY_RECORD_DESCRIBERS =

A hash of hashes of table name and index name to describer class.

{
  :SYS_TABLES  => {
    :PRIMARY => SYS_TABLES_PRIMARY,
    :ID      => SYS_TABLES_ID
  },
  :SYS_COLUMNS => { :PRIMARY => SYS_COLUMNS_PRIMARY },
  :SYS_INDEXES => { :PRIMARY => SYS_INDEXES_PRIMARY },
  :SYS_FIELDS  => { :PRIMARY => SYS_FIELDS_PRIMARY },
}
MYSQL_TYPE =

A hash of MySQL’s internal type system to the stored values for those types, and the “external” SQL type.

{
# :DECIMAL      => { :value => 0,   :type => :DECIMAL     },
  :TINY         => { :value => 1,   :type => :TINYINT     },
  :SHORT        => { :value => 2,   :type => :SMALLINT    },
  :LONG         => { :value => 3,   :type => :INT         },
  :FLOAT        => { :value => 4,   :type => :FLOAT       },
  :DOUBLE       => { :value => 5,   :type => :DOUBLE      },
# :NULL         => { :value => 6,   :type => nil          },
  :TIMESTAMP    => { :value => 7,   :type => :TIMESTAMP   },
  :LONGLONG     => { :value => 8,   :type => :BIGINT      },
  :INT24        => { :value => 9,   :type => :MEDIUMINT   },
# :DATE         => { :value => 10,  :type => :DATE        },
  :TIME         => { :value => 11,  :type => :TIME        },
  :DATETIME     => { :value => 12,  :type => :DATETIME    },
  :YEAR         => { :value => 13,  :type => :YEAR        },
  :NEWDATE      => { :value => 14,  :type => :DATE        },
  :VARCHAR      => { :value => 15,  :type => :VARCHAR     },
  :BIT          => { :value => 16,  :type => :BIT         },
  :NEWDECIMAL   => { :value => 246, :type => :CHAR        },
# :ENUM         => { :value => 247, :type => :ENUM        },
# :SET          => { :value => 248, :type => :SET         },
  :TINY_BLOB    => { :value => 249, :type => :TINYBLOB    },
  :MEDIUM_BLOB  => { :value => 250, :type => :MEDIUMBLOB  },
  :LONG_BLOB    => { :value => 251, :type => :LONGBLOB    },
  :BLOB         => { :value => 252, :type => :BLOB        },
# :VAR_STRING   => { :value => 253, :type => :VARCHAR     },
  :STRING       => { :value => 254, :type => :CHAR        },
  :GEOMETRY     => { :value => 255, :type => :GEOMETRY    },
}
MYSQL_TYPE_BY_VALUE =

A hash of MYSQL_TYPE keys by value :value key.

MYSQL_TYPE.inject({}) { |h, (k, v)| h[v[:value]] = k; h }
COLUMN_MTYPE =

A hash of InnoDB’s internal type system to the values stored for each type.

{
  :VARCHAR    => 1,
  :CHAR       => 2,
  :FIXBINARY  => 3,
  :BINARY     => 4,
  :BLOB       => 5,
  :INT        => 6,
  :SYS_CHILD  => 7,
  :SYS        => 8,
  :FLOAT      => 9,
  :DOUBLE     => 10,
  :DECIMAL    => 11,
  :VARMYSQL   => 12,
  :MYSQL      => 13,
}
COLUMN_MTYPE_BY_VALUE =

A hash of COLUMN_MTYPE keys by value.

COLUMN_MTYPE.inject({}) { |h, (k, v)| h[v] = k; h }
COLUMN_PRTYPE_FLAG =

A hash of InnoDB “precise type” bitwise flags.

{
  :NOT_NULL          => 256,
  :UNSIGNED          => 512,
  :BINARY            => 1024,
  :LONG_TRUE_VARCHAR => 4096,
}
COLUMN_PRTYPE_FLAG_BY_VALUE =

A hash of COLUMN_PRTYPE keys by value.

COLUMN_PRTYPE_FLAG.inject({}) { |h, (k, v)| h[v] = k; h }
COLUMN_PRTYPE_MYSQL_TYPE_MASK =

The bitmask to extract the MySQL internal type from the InnoDB “precise type”.

0xFF
INDEX_TYPE_FLAG =

A hash of InnoDB’s index type flags.

{
  :CLUSTERED => 1,
  :UNIQUE    => 2,
  :UNIVERSAL => 4,
  :IBUF      => 8,
  :CORRUPT   => 16,
}
INDEX_TYPE_FLAG_BY_VALUE =

A hash of INDEX_TYPE_FLAG keys by value.

INDEX_TYPE_FLAG.inject({}) { |h, (k, v)| h[v] = k; h }

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(system_space) ⇒ DataDictionary

Returns a new instance of DataDictionary.



220
221
222
# File 'lib/innodb/data_dictionary.rb', line 220

def initialize(system_space)
  @system_space = system_space
end

Instance Attribute Details

#system_spaceObject (readonly)

Returns the value of attribute system_space.



218
219
220
# File 'lib/innodb/data_dictionary.rb', line 218

def system_space
  @system_space
end

Class Method Details

.mtype_prtype_to_data_type(mtype, prtype, len, prec) ⇒ Object

Return a full data type given an mtype and prtype, such as [“VARCHAR(10)”, :NOT_NULL] or [:INT, :UNSIGNED].



198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
# File 'lib/innodb/data_dictionary.rb', line 198

def self.mtype_prtype_to_data_type(mtype, prtype, len, prec)
  data_type = []

  if type = mtype_prtype_to_type_string(mtype, prtype, len, prec)
    data_type << type
  else
    raise "Unsupported type (mtype #{mtype}, prtype #{prtype})"
  end

  if prtype & COLUMN_PRTYPE_FLAG[:NOT_NULL] != 0
    data_type << :NOT_NULL
  end

  if prtype & COLUMN_PRTYPE_FLAG[:UNSIGNED] != 0
    data_type << :UNSIGNED
  end

  data_type
end

.mtype_prtype_to_type_string(mtype, prtype, len, prec) ⇒ Object

Return the “external” SQL type string (such as “VARCHAR” or “INT”) given the stored mtype and prtype from the InnoDB data dictionary. Note that not all types are extractable into fully defined SQL types due to the lossy nature of the MySQL-to-InnoDB interface regarding types.



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
# File 'lib/innodb/data_dictionary.rb', line 158

def self.mtype_prtype_to_type_string(mtype, prtype, len, prec)
  mysql_type = prtype & COLUMN_PRTYPE_MYSQL_TYPE_MASK
  internal_type = MYSQL_TYPE_BY_VALUE[mysql_type]
  external_type = MYSQL_TYPE[internal_type][:type]

  case external_type
  when :VARCHAR
    # One-argument: length.
    "%s(%i)" % [external_type, len]
  when :FLOAT, :DOUBLE
    # Two-argument: length and precision.
    "%s(%i,%i)" % [external_type, len, prec]
  when :CHAR
    if COLUMN_MTYPE_BY_VALUE[mtype] == :MYSQL
      # When the mtype is :MYSQL, the column is actually
      # stored as VARCHAR despite being a CHAR. This is
      # done for CHAR columns having multi-byte character
      # sets in order to limit size. Note that such data
      # are still space-padded to at least len.
      "VARCHAR(%i)" % [len]
    else
      "CHAR(%i)" % [len]
    end
  when :DECIMAL
    # The DECIMAL type is designated as DECIMAL(M,D)
    # however the M and D definitions are not stored
    # in the InnoDB data dictionary. We need to define
    # the column as something which will extract the
    # raw bytes in order to read the column, but we
    # can't figure out the right decimal type. The
    # len stored here is actually the on-disk storage
    # size.
    "CHAR(%i)" % [len]
  else
    external_type
  end
end

Instance Method Details

#_make_column_description(type, record) ⇒ Object

Produce a Innodb::RecordDescriber-compatible column description given a type (:key, :row) and data dictionary SYS_COLUMNS record.



558
559
560
561
562
563
564
565
566
567
568
569
# File 'lib/innodb/data_dictionary.rb', line 558

def _make_column_description(type, record)
  {
    :type => type,
    :name => record["NAME"],
    :description => self.class.mtype_prtype_to_data_type(
      record["MTYPE"],
      record["PRTYPE"],
      record["LEN"],
      record["PREC"]
    )
  }
end

#clustered_index_name_by_table_name(table_name) ⇒ Object

Return the name of the clustered index (usually “PRIMARY”, but not always) for a given table name.



544
545
546
547
548
549
550
551
552
553
554
# File 'lib/innodb/data_dictionary.rb', line 544

def clustered_index_name_by_table_name(table_name)
  unless table_record = table_by_name(table_name)
    raise "Table #{table_name} not found"
  end

  if index_record = object_by_two_fields(:each_index,
                                         "TABLE_ID", table_record["ID"],
                                         "TYPE", 3)
    index_record["NAME"]
  end
end

#column_by_name(table_name, column_name) ⇒ Object

Lookup a column by table name and column name.



380
381
382
383
384
385
386
387
388
# File 'lib/innodb/data_dictionary.rb', line 380

def column_by_name(table_name, column_name)
  unless table = table_by_name(table_name)
    return nil
  end

  object_by_two_fields(:each_column,
                       "TABLE_ID", table["ID"],
                       "NAME", column_name)
end

#data_dictionary_index(table_name, index_name) ⇒ Object

Return an Innodb::Index object initialized to the internal data dictionary index with an appropriate record describer so that records can be recursed.



234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
# File 'lib/innodb/data_dictionary.rb', line 234

def data_dictionary_index(table_name, index_name)
  unless table_entry = data_dictionary_indexes[table_name]
    raise "Unknown data dictionary table #{table_name}"
  end

  unless index_root_page = table_entry[index_name]
    raise "Unknown data dictionary index #{table_name}.#{index_name}"
  end

  # If we have a record describer for this index, load it.
  record_describer = DATA_DICTIONARY_RECORD_DESCRIBERS[table_name] &&
                     DATA_DICTIONARY_RECORD_DESCRIBERS[table_name][index_name]

  system_space.index(index_root_page, record_describer.new)
end

#data_dictionary_indexesObject

A helper method to reach inside the system space and retrieve the data dictionary index locations from the data dictionary header.



227
228
229
# File 'lib/innodb/data_dictionary.rb', line 227

def data_dictionary_indexes
  system_space.data_dictionary_page.data_dictionary_header[:indexes]
end

#each_columnObject

Iterate through the records in the SYS_COLUMNS data dictionary table.



311
312
313
314
315
316
317
318
319
320
321
# File 'lib/innodb/data_dictionary.rb', line 311

def each_column
  unless block_given?
    return enum_for(:each_column)
  end

  each_record_from_data_dictionary_index(:SYS_COLUMNS, :PRIMARY) do |record|
    yield record.fields
  end

  nil
end

#each_column_by_table_id(table_id) ⇒ Object

Iterate through all columns in a table by table ID.



481
482
483
484
485
486
487
488
489
490
491
# File 'lib/innodb/data_dictionary.rb', line 481

def each_column_by_table_id(table_id)
  unless block_given?
    return enum_for(:each_column_by_table_id, table_id)
  end

  each_column do |record|
    yield record if record["TABLE_ID"] == table_id
  end

  nil
end

#each_column_by_table_name(table_name) ⇒ Object

Iterate through all columns in a table by table name.



494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
# File 'lib/innodb/data_dictionary.rb', line 494

def each_column_by_table_name(table_name)
  unless block_given?
    return enum_for(:each_column_by_table_name, table_name)
  end

  unless table = table_by_name(table_name)
    raise "Table #{table_name} not found"
  end

  each_column_by_table_id(table["ID"]) do |record|
    yield record
  end

  nil
end

#each_column_description_by_index_name(table_name, index_name) ⇒ Object

Iterate through Innodb::RecordDescriber-compatible column descriptions for a given index by table name and index name.



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
# File 'lib/innodb/data_dictionary.rb', line 573

def each_column_description_by_index_name(table_name, index_name)
  unless block_given?
    return enum_for(:each_column_description_by_index_name, table_name, index_name)
  end

  unless index = index_by_name(table_name, index_name)
    raise "Index #{index_name} for table #{table_name} not found"
  end

  columns_in_index = {}
  each_column_in_index_by_name(table_name, index_name) do |record|
    columns_in_index[record["NAME"]] = 1
    yield _make_column_description(:key, record)
  end

  if index["TYPE"] & INDEX_TYPE_FLAG[:CLUSTERED] != 0
    each_column_by_table_name(table_name) do |record|
      unless columns_in_index.include?(record["NAME"])
        yield _make_column_description(:row, record)
      end
    end
  else
    clustered_index_name = clustered_index_name_by_table_name(table_name)

    each_column_in_index_by_name(table_name, clustered_index_name) do |record|
      yield _make_column_description(:row, record)
    end
  end

  nil
end

#each_column_in_index_by_name(table_name, index_name) ⇒ Object

Iterate through all columns in an index by table name and index name.



511
512
513
514
515
516
517
518
519
520
521
# File 'lib/innodb/data_dictionary.rb', line 511

def each_column_in_index_by_name(table_name, index_name)
  unless block_given?
    return enum_for(:each_column_in_index_by_name, table_name, index_name)
  end

  each_field_by_index_name(table_name, index_name) do |record|
    yield column_by_name(table_name, record["COL_NAME"])
  end

  nil
end

#each_column_not_in_index_by_name(table_name, index_name) ⇒ Object

Iterate through all columns not in an index by table name and index name. This is useful when building index descriptions for secondary indexes.



525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
# File 'lib/innodb/data_dictionary.rb', line 525

def each_column_not_in_index_by_name(table_name, index_name)
  unless block_given?
    return enum_for(:each_column_not_in_index_by_name, table_name, index_name)
  end

  columns_in_index = {}
  each_column_in_index_by_name(table_name, index_name) do |record|
    columns_in_index[record["NAME"]] = 1
  end

  each_column_by_table_name(table_name) do |record|
    yield record unless columns_in_index.include?(record["NAME"])
  end

  nil
end

#each_data_dictionary_indexObject

Iterate through all data dictionary indexes, yielding the table name, index name, and the index itself as an Innodb::Index.



268
269
270
271
272
273
274
275
276
277
278
279
280
281
# File 'lib/innodb/data_dictionary.rb', line 268

def each_data_dictionary_index
  unless block_given?
    return enum_for(:each_data_dictionary_index)
  end

  data_dictionary_indexes.each do |table_name, indexes|
    indexes.each do |index_name, root_page_number|
      yield table_name, index_name,
        data_dictionary_index(table_name, index_name)
    end
  end

  nil
end

#each_data_dictionary_index_root_page_numberObject

Iterate through all data dictionary indexes, yielding the table name, index name, and root page number.



252
253
254
255
256
257
258
259
260
261
262
263
264
# File 'lib/innodb/data_dictionary.rb', line 252

def each_data_dictionary_index_root_page_number
  unless block_given?
    return enum_for(:each_data_dictionary_index_root_page_number)
  end

  data_dictionary_indexes.each do |table_name, indexes|
    indexes.each do |index_name, root_page_number|
      yield table_name, index_name, root_page_number
    end
  end

  nil
end

#each_fieldObject

Iterate through the records in the SYS_FIELDS data dictionary table.



337
338
339
340
341
342
343
344
345
346
347
# File 'lib/innodb/data_dictionary.rb', line 337

def each_field
  unless block_given?
    return enum_for(:each_field)
  end

  each_record_from_data_dictionary_index(:SYS_FIELDS, :PRIMARY) do |record|
    yield record.fields
  end

  nil
end

#each_field_by_index_id(index_id) ⇒ Object

Iterate through all fields in an index by index ID.



451
452
453
454
455
456
457
458
459
460
461
# File 'lib/innodb/data_dictionary.rb', line 451

def each_field_by_index_id(index_id)
  unless block_given?
    return enum_for(:each_field_by_index_id, index_id)
  end

  each_field do |record|
    yield record if record["INDEX_ID"] == index_id
  end

  nil
end

#each_field_by_index_name(table_name, index_name) ⇒ Object

Iterate through all fields in an index by index name.



464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
# File 'lib/innodb/data_dictionary.rb', line 464

def each_field_by_index_name(table_name, index_name)
  unless block_given?
    return enum_for(:each_field_by_name, table_name, index_name)
  end

  unless index = index_by_name(table_name, index_name)
    raise "Index #{index_name} for table #{table_name} not found"
  end

  each_field_by_index_id(index["ID"]) do |record|
    yield record
  end

  nil
end

#each_indexObject

Iterate through the records in the SYS_INDEXES dictionary table.



324
325
326
327
328
329
330
331
332
333
334
# File 'lib/innodb/data_dictionary.rb', line 324

def each_index
  unless block_given?
    return enum_for(:each_index)
  end

  each_record_from_data_dictionary_index(:SYS_INDEXES, :PRIMARY) do |record|
    yield record.fields
  end

  nil
end

#each_index_by_space_id(space_id) ⇒ Object

Iterate through indexes by space ID.



408
409
410
411
412
413
414
415
416
417
418
# File 'lib/innodb/data_dictionary.rb', line 408

def each_index_by_space_id(space_id)
  unless block_given?
    return enum_for(:each_index_by_space_id, space_id)
  end

  each_index do |record|
    yield record if record["SPACE"] == space_id
  end

  nil
end

#each_index_by_table_id(table_id) ⇒ Object

Iterate through all indexes in a table by table ID.



421
422
423
424
425
426
427
428
429
430
431
# File 'lib/innodb/data_dictionary.rb', line 421

def each_index_by_table_id(table_id)
  unless block_given?
    return enum_for(:each_index_by_table_id, table_id)
  end

  each_index do |record|
    yield record if record["TABLE_ID"] == table_id
  end

  nil
end

#each_index_by_table_name(table_name) ⇒ Object

Iterate through all indexes in a table by table name.



434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
# File 'lib/innodb/data_dictionary.rb', line 434

def each_index_by_table_name(table_name)
  unless block_given?
    return enum_for(:each_index_by_table_name, table_name)
  end

  unless table = table_by_name(table_name)
    raise "Table #{table_name} not found"
  end

  each_index_by_table_id(table["ID"]) do |record|
    yield record
  end

  nil
end

#each_record_from_data_dictionary_index(table, index) ⇒ Object

Iterate through records from a data dictionary index yielding each record as a Innodb::Record object.



285
286
287
288
289
290
291
292
293
294
295
# File 'lib/innodb/data_dictionary.rb', line 285

def each_record_from_data_dictionary_index(table, index)
  unless block_given?
    return enum_for(:each_index, table, index)
  end

  data_dictionary_index(table, index).each_record do |record|
    yield record
  end

  nil
end

#each_tableObject

Iterate through the records in the SYS_TABLES data dictionary table.



298
299
300
301
302
303
304
305
306
307
308
# File 'lib/innodb/data_dictionary.rb', line 298

def each_table
  unless block_given?
    return enum_for(:each_table)
  end

  each_record_from_data_dictionary_index(:SYS_TABLES, :PRIMARY) do |record|
    yield record.fields
  end

  nil
end

#index_by_id(index_id) ⇒ Object

Lookup an index by index ID.



391
392
393
394
# File 'lib/innodb/data_dictionary.rb', line 391

def index_by_id(index_id)
  object_by_field(:each_index,
                  "ID", index_id)
end

#index_by_name(table_name, index_name) ⇒ Object

Lookup an index by table name and index name.



397
398
399
400
401
402
403
404
405
# File 'lib/innodb/data_dictionary.rb', line 397

def index_by_name(table_name, index_name)
  unless table = table_by_name(table_name)
    return nil
  end

  object_by_two_fields(:each_index,
                       "TABLE_ID", table["ID"],
                       "NAME", index_name)
end

#object_by_field(method, field, value) ⇒ Object

A helper to iterate the method provided and return the first record where the record’s field matches the provided value.



351
352
353
# File 'lib/innodb/data_dictionary.rb', line 351

def object_by_field(method, field, value)
  send(method).select { |o| o[field] == value }.first
end

#object_by_two_fields(method, f1, v1, f2, v2) ⇒ Object

A helper to iterate the method provided and return the first record where the record’s fields f1 and f2 match the provided values v1 and v2.



357
358
359
# File 'lib/innodb/data_dictionary.rb', line 357

def object_by_two_fields(method, f1, v1, f2, v2)
  send(method).select { |o| o[f1] == v1 && o[f2] == v2 }.first
end

#record_describer_by_index_id(index_id) ⇒ Object

Return an Innodb::RecordDescriber object describing the records in a given index by index ID.



634
635
636
637
638
639
640
641
642
643
644
# File 'lib/innodb/data_dictionary.rb', line 634

def record_describer_by_index_id(index_id)
  unless index = index_by_id(index_id)
    raise "Index #{index_id} not found"
  end

  unless table = table_by_id(index["TABLE_ID"])
    raise "Table #{INDEX["TABLE_ID"]} not found"
  end

  record_describer_by_index_name(table["NAME"], index["NAME"])
end

#record_describer_by_index_name(table_name, index_name) ⇒ Object

Return an Innodb::RecordDescriber object describing records for a given index by table name and index name.



607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
# File 'lib/innodb/data_dictionary.rb', line 607

def record_describer_by_index_name(table_name, index_name)
  unless index = index_by_name(table_name, index_name)
    raise "Index #{index_name} for table #{table_name} not found"
  end

  describer = Innodb::RecordDescriber.new

  if index["TYPE"] & INDEX_TYPE_FLAG[:CLUSTERED] != 0
    describer.type :clustered
  else
    describer.type :secondary
  end

  each_column_description_by_index_name(table_name, index_name) do |column|
    case column[:type]
    when :key
      describer.key column[:name], *column[:description]
    when :row
      describer.row column[:name], *column[:description]
    end
  end

  describer
end

#table_by_id(table_id) ⇒ Object

Lookup a table by table ID.



362
363
364
365
# File 'lib/innodb/data_dictionary.rb', line 362

def table_by_id(table_id)
  object_by_field(:each_table,
                  "ID", table_id)
end

#table_by_name(table_name) ⇒ Object

Lookup a table by table name.



368
369
370
371
# File 'lib/innodb/data_dictionary.rb', line 368

def table_by_name(table_name)
  object_by_field(:each_table,
                  "NAME", table_name)
end

#table_by_space_id(space_id) ⇒ Object

Lookup a table by space ID.



374
375
376
377
# File 'lib/innodb/data_dictionary.rb', line 374

def table_by_space_id(space_id)
  object_by_field(:each_table,
                  "SPACE", space_id)
end