Class: Innodb::System

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

Overview

A class representing an entire InnoDB system, having a system tablespace and any number of attached single-table tablespaces.

Constant Summary collapse

SYSTEM_SPACE_ID =

The space ID of the system space, always 0.

0

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(system_space_file) ⇒ System

Returns a new instance of System.



18
19
20
21
22
23
24
25
26
27
# File 'lib/innodb/system.rb', line 18

def initialize(system_space_file)
  @spaces = {}
  @config = {
    :datadir => File.dirname(system_space_file),
  }

  add_space_file(system_space_file)

  @data_dictionary = Innodb::DataDictionary.new(system_space)
end

Instance Attribute Details

#configObject (readonly)

A hash of configuration options by configuration key.



7
8
9
# File 'lib/innodb/system.rb', line 7

def config
  @config
end

#data_dictionaryObject (readonly)

The Innodb::DataDictionary for this system.



13
14
15
# File 'lib/innodb/system.rb', line 13

def data_dictionary
  @data_dictionary
end

#spacesObject (readonly)

A hash of spaces by space ID.



10
11
12
# File 'lib/innodb/system.rb', line 10

def spaces
  @spaces
end

Instance Method Details

#add_space(space) ⇒ Object

Add an already-constructed Innodb::Space object.



35
36
37
38
39
40
41
# File 'lib/innodb/system.rb', line 35

def add_space(space)
  unless space.is_a?(Innodb::Space)
    raise "Object was not an Innodb::Space"
  end

  spaces[space.space_id.to_i] = space
end

#add_space_file(space_file) ⇒ Object

Add a space by filename.



44
45
46
47
48
# File 'lib/innodb/system.rb', line 44

def add_space_file(space_file)
  space = Innodb::Space.new(space_file)
  space.innodb_system = self
  add_space(space)
end

#add_table(table_name) ⇒ Object

Add a space by table name, constructing an appropriate filename from the provided table name.



52
53
54
# File 'lib/innodb/system.rb', line 52

def add_table(table_name)
  add_space_file("%s/%s.ibd" % [config[:datadir], table_name])
end

#each_column_name_by_table_name(table_name) ⇒ Object

Iterate through all column names by table name.



97
98
99
100
101
102
103
104
105
106
107
# File 'lib/innodb/system.rb', line 97

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

  data_dictionary.each_column_by_table_name(table_name) do |record|
    yield record["NAME"]
  end

  nil
end

#each_index_field_name_by_index_name(table_name, index_name) ⇒ Object

Iterate through all field names in a given index by table name and index name.



124
125
126
127
128
129
130
131
132
133
134
135
# File 'lib/innodb/system.rb', line 124

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

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

  nil
end

#each_index_name_by_table_name(table_name) ⇒ Object

Iterate through all index names by table name.



110
111
112
113
114
115
116
117
118
119
120
# File 'lib/innodb/system.rb', line 110

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

  data_dictionary.each_index_by_table_name(table_name) do |record|
    yield record["NAME"]
  end

  nil
end

#each_table_nameObject

Iterate through all table names.



84
85
86
87
88
89
90
91
92
93
94
# File 'lib/innodb/system.rb', line 84

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

  data_dictionary.each_table do |record|
    yield record["NAME"]
  end

  nil
end

#historyObject



169
170
171
# File 'lib/innodb/system.rb', line 169

def history
  Innodb::History.new(self)
end

#index_by_name(table_name, index_name) ⇒ Object

Return an Innodb::Index object given a table name and index name.



159
160
161
162
163
164
165
166
167
# File 'lib/innodb/system.rb', line 159

def index_by_name(table_name, index_name)
  index_record = data_dictionary.index_by_name(table_name, index_name)

  index_space = space(index_record["SPACE"])
  describer = data_dictionary.record_describer_by_index_name(table_name, index_name)
  index = index_space.index(index_record["PAGE_NO"], describer)

  index
end

#index_name_by_id(index_id) ⇒ Object

Return the index name given an index ID.



145
146
147
148
149
# File 'lib/innodb/system.rb', line 145

def index_name_by_id(index_id)
  if index_record = data_dictionary.index_by_id(index_id)
    index_record["NAME"]
  end
end

#space(space_id) ⇒ Object

Return an Innodb::Space object for a given space ID, looking up and adding the single-table space if necessary.



58
59
60
61
62
63
64
65
66
67
68
# File 'lib/innodb/system.rb', line 58

def space(space_id)
  return spaces[space_id] if spaces[space_id]

  unless table_record = data_dictionary.table_by_space_id(space_id)
    raise "Table with space ID #{space_id} not found"
  end

  add_table(table_record["NAME"])

  spaces[space_id]
end

#space_by_table_name(table_name) ⇒ Object

Return an Innodb::Space object by table name.



71
72
73
74
75
76
77
78
79
80
81
# File 'lib/innodb/system.rb', line 71

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

  if table_record["SPACE"] == 0
    return nil
  end

  space(table_record["SPACE"])
end

#system_spaceObject

A helper to get the system space.



30
31
32
# File 'lib/innodb/system.rb', line 30

def system_space
  spaces[SYSTEM_SPACE_ID]
end

#table_and_index_name_by_id(index_id) ⇒ Object

Return an array of the table name and index name given an index ID.



152
153
154
155
156
# File 'lib/innodb/system.rb', line 152

def table_and_index_name_by_id(index_id)
  if index_record = data_dictionary.index_by_id(index_id)
    [table_name_by_id(index_record["TABLE_ID"]), index_record["NAME"]]
  end
end

#table_name_by_id(table_id) ⇒ Object

Return the table name given a table ID.



138
139
140
141
142
# File 'lib/innodb/system.rb', line 138

def table_name_by_id(table_id)
  if table_record = data_dictionary.table_by_id(table_id)
    table_record["NAME"]
  end
end