Class: Innodb::Space
- Inherits:
-
Object
- Object
- Innodb::Space
- Defined in:
- lib/innodb/space.rb
Defined Under Namespace
Classes: DataFile
Constant Summary collapse
- DEFAULT_PAGE_SIZE =
InnoDB’s default page size is 16KiB.
16 * 1024
- DEFAULT_EXTENT_SIZE =
The default extent size is 1 MiB defined originally as 64 pages.
64 * DEFAULT_PAGE_SIZE
- SYSTEM_SPACE_PAGE_MAP =
A map of InnoDB system space fixed-allocation pages. This can be used to check whether a space is a system space or not, as non-system spaces will not match this pattern.
{ 0 => :FSP_HDR, 1 => :IBUF_BITMAP, 2 => :INODE, 3 => :SYS, 4 => :INDEX, 5 => :TRX_SYS, 6 => :SYS, 7 => :SYS, }.freeze
- XDES_LISTS =
%i[ free free_frag full_frag ].freeze
- INODE_LISTS =
An array of Innodb::Inode list names.
%i[ full_inodes free_inodes ].freeze
Instance Attribute Summary collapse
-
#innodb_system ⇒ Object
The Innodb::System to which this space belongs, if any.
-
#page_size ⇒ Object
readonly
The size (in bytes) of each page in the space.
-
#pages ⇒ Object
readonly
The number of pages in the space.
-
#record_describer ⇒ Object
An object which can be used to describe records found in pages within this space.
-
#size ⇒ Object
readonly
The size (in bytes) of the space.
-
#system_page_size ⇒ Object
readonly
The system default page size (in bytes), equivalent to UNIV_PAGE_SIZE.
Instance Method Summary collapse
- #checked_page_class!(page, expected_class) ⇒ Object
-
#data_dictionary_page ⇒ Object
Get the Innodb::Page::SysDataDictionaryHeader page for a system space.
- #data_file_for_offset(offset) ⇒ Object
-
#doublewrite_page?(page_number) ⇒ Boolean
Return true if a page is in the doublewrite buffer.
-
#each_doublewrite_page_number(&block) ⇒ Object
Iterate through the page numbers in the doublewrite buffer.
-
#each_index ⇒ Object
Iterate through all indexes in the space.
-
#each_index_root_page_number ⇒ Object
Iterate through all root page numbers for indexes in the space.
-
#each_inode(&block) ⇒ Object
Iterate through Innodb::Inode objects in the space.
-
#each_inode_list ⇒ Object
Iterate through Innodb::Inode lists in the space.
-
#each_page(start_page = 0) ⇒ Object
Iterate through all pages in a space, returning the page number and an Innodb::Page object for each one.
-
#each_page_status(start_page = 0) ⇒ Object
Iterate through all pages, yielding the page number, page object, and page status.
-
#each_page_type_region(start_page = 0) {|region| ... } ⇒ Object
Iterate through unique regions in the space by page type.
-
#each_xdes ⇒ Object
Iterate through all extent descriptors for the space, returning an Innodb::Xdes object for each one.
-
#each_xdes_list ⇒ Object
Iterate through Innodb::Xdes lists in the space.
-
#each_xdes_page ⇒ Object
Iterate through all extent descriptor pages, returning an Innodb::Page object for each one.
-
#each_xdes_page_number(&block) ⇒ Object
An array of all FSP/XDES page numbers for the space.
-
#extent_size ⇒ Object
The size (in bytes) of an extent.
-
#fsp ⇒ Object
Get (and cache) the FSP header from the FSP_HDR page.
-
#fsp_flags ⇒ Object
The FSP header flags, decoded.
-
#ibuf_bitmap_page_for_page(page_number) ⇒ Object
The IBUF_BITMAP page which will contain the bitmap entry for a given page.
-
#index(root_page_number, record_describer = nil) ⇒ Object
Get an Innodb::Index object for a specific index by root page number.
-
#initialize(filenames) ⇒ Space
constructor
Open a space file, optionally providing the page size to use.
-
#inode(fseg_id) ⇒ Object
Return an Inode by fseg_id.
- #inspect ⇒ Object
-
#list(name) ⇒ Object
Get an Innodb::List object for a specific list by list name.
-
#name ⇒ Object
Return a string which can uniquely identify this space.
-
#page(page_number) ⇒ Object
Get an Innodb::Page object for a specific page by page number.
-
#page_data(page_number) ⇒ Object
Get the raw byte buffer for a specific page by page number.
-
#page_fsp_hdr ⇒ Object
Return the page number for the space’s FSP_HDR page.
-
#page_sys_data_dictionary ⇒ Object
Return the page number for the space’s SYS data dictionary header.
-
#page_trx_sys ⇒ Object
Return the page number for the space’s TRX_SYS page.
-
#pages_per_bookkeeping_page ⇒ Object
The number of pages per FSP_HDR/XDES/IBUF_BITMAP page.
-
#pages_per_extent ⇒ Object
The number of pages per extent.
-
#raw_fsp_header_flags ⇒ Object
Read the FSP header “flags” field by byte offset within the space file.
-
#read_at_offset(offset, size) ⇒ Object
Get the raw byte buffer of size bytes at offset in the file.
- #rseg_page?(page_number) ⇒ Boolean
- #space_id ⇒ Object
-
#system_space? ⇒ Boolean
Determine whether this space looks like a system space.
-
#trx_sys ⇒ Object
Get the Innodb::Page::TrxSys page for a system space.
-
#type_for_page(page, page_status) ⇒ Object
A helper to produce a printable page type.
-
#xdes_entry_for_page(page_number) ⇒ Object
The XDES entry offset for a given page within its FSP_HDR/XDES page’s XDES array.
-
#xdes_for_page(page_number) ⇒ Object
Return the Innodb::Xdes entry which represents a given page.
-
#xdes_page_for_page(page_number) ⇒ Object
The FSP_HDR/XDES page which will contain the XDES entry for a given page.
Constructor Details
#initialize(filenames) ⇒ Space
Open a space file, optionally providing the page size to use. Pages that aren’t 16 KiB may not be supported well.
60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 |
# File 'lib/innodb/space.rb', line 60 def initialize(filenames) filenames = [filenames] unless filenames.is_a?(Array) @data_files = [] @size = 0 filenames.each do |filename| file = DataFile.new(filename, @size) @size += file.size @data_files << file end @system_page_size = fsp_flags.system_page_size @page_size = fsp_flags.page_size @compressed = fsp_flags.compressed @pages = (@size / @page_size) @innodb_system = nil @record_describer = nil end |
Instance Attribute Details
#innodb_system ⇒ Object
The Innodb::System to which this space belongs, if any.
81 82 83 |
# File 'lib/innodb/space.rb', line 81 def innodb_system @innodb_system end |
#page_size ⇒ Object (readonly)
The size (in bytes) of each page in the space.
91 92 93 |
# File 'lib/innodb/space.rb', line 91 def page_size @page_size end |
#pages ⇒ Object (readonly)
The number of pages in the space.
97 98 99 |
# File 'lib/innodb/space.rb', line 97 def pages @pages end |
#record_describer ⇒ Object
An object which can be used to describe records found in pages within this space.
85 86 87 |
# File 'lib/innodb/space.rb', line 85 def record_describer @record_describer end |
#size ⇒ Object (readonly)
The size (in bytes) of the space
94 95 96 |
# File 'lib/innodb/space.rb', line 94 def size @size end |
#system_page_size ⇒ Object (readonly)
The system default page size (in bytes), equivalent to UNIV_PAGE_SIZE.
88 89 90 |
# File 'lib/innodb/space.rb', line 88 def system_page_size @system_page_size end |
Instance Method Details
#checked_page_class!(page, expected_class) ⇒ Object
279 280 281 282 283 |
# File 'lib/innodb/space.rb', line 279 def checked_page_class!(page, expected_class) return page if page.instance_of?(expected_class) raise "Page #{page.offset} is not the correct type, found: #{page.class}, expected: #{expected_class}" end |
#data_dictionary_page ⇒ Object
Get the Innodb::Page::SysDataDictionaryHeader page for a system space.
309 310 311 312 313 |
# File 'lib/innodb/space.rb', line 309 def data_dictionary_page raise "Data Dictionary is only available in system spaces" unless system_space? checked_page_class!(page(page_sys_data_dictionary), Innodb::Page::SysDataDictionaryHeader) end |
#data_file_for_offset(offset) ⇒ Object
220 221 222 223 224 225 226 227 |
# File 'lib/innodb/space.rb', line 220 def data_file_for_offset(offset) @data_files.each do |file| return file if offset < file.size offset -= file.size end nil end |
#doublewrite_page?(page_number) ⇒ Boolean
Return true if a page is in the doublewrite buffer.
395 396 397 398 399 400 |
# File 'lib/innodb/space.rb', line 395 def doublewrite_page?(page_number) return false unless system_space? @doublewrite_pages ||= each_doublewrite_page_number.to_a @doublewrite_pages.include?(page_number) end |
#each_doublewrite_page_number(&block) ⇒ Object
Iterate through the page numbers in the doublewrite buffer.
385 386 387 388 389 390 391 392 |
# File 'lib/innodb/space.rb', line 385 def each_doublewrite_page_number(&block) return nil unless system_space? return enum_for(:each_doublewrite_page_number) unless block_given? trx_sys.doublewrite[:page_info][0][:page_number].each do |start_page| (start_page...(start_page + pages_per_extent)).each(&block) end end |
#each_index ⇒ Object
Iterate through all indexes in the space.
348 349 350 351 352 353 354 355 356 |
# File 'lib/innodb/space.rb', line 348 def each_index return enum_for(:each_index) unless block_given? each_index_root_page_number do |page_number| yield index(page_number) end nil end |
#each_index_root_page_number ⇒ Object
Iterate through all root page numbers for indexes in the space.
326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 |
# File 'lib/innodb/space.rb', line 326 def each_index_root_page_number return enum_for(:each_index_root_page_number) unless block_given? if innodb_system # Retrieve the index root page numbers from the data dictionary. innodb_system.data_dictionary.each_index_by_space_id(space_id) do |record| yield record["PAGE_NO"] end else # Guess that the index root pages will be present starting at page 3, # and walk forward until we find a non-root page. This should work fine # for IBD files, if they haven't added indexes online. (3...@pages).each do |page_number| page = page(page_number) yield page_number if page.is_a?(Innodb::Page::Index) && page.root? end end nil end |
#each_inode(&block) ⇒ Object
Iterate through Innodb::Inode objects in the space.
368 369 370 371 372 373 374 375 376 |
# File 'lib/innodb/space.rb', line 368 def each_inode(&block) return enum_for(:each_inode) unless block_given? each_inode_list do |_name, list| list.each do |page| page.each_allocated_inode(&block) end end end |
#each_inode_list ⇒ Object
Iterate through Innodb::Inode lists in the space.
359 360 361 362 363 364 365 |
# File 'lib/innodb/space.rb', line 359 def each_inode_list return enum_for(:each_inode_list) unless block_given? INODE_LISTS.each do |name| yield name, list(name) end end |
#each_page(start_page = 0) ⇒ Object
Iterate through all pages in a space, returning the page number and an Innodb::Page object for each one.
404 405 406 407 408 409 410 411 |
# File 'lib/innodb/space.rb', line 404 def each_page(start_page = 0) return enum_for(:each_page, start_page) unless block_given? (start_page...@pages).each do |page_number| current_page = page(page_number) yield page_number, current_page if current_page end end |
#each_page_status(start_page = 0) ⇒ Object
Iterate through all pages, yielding the page number, page object, and page status.
456 457 458 459 460 461 462 463 464 465 466 467 468 469 |
# File 'lib/innodb/space.rb', line 456 def each_page_status(start_page = 0) return enum_for(:each_page_status, start_page) unless block_given? each_xdes do |xdes| xdes.each_page_status do |page_number, page_status| next if page_number < start_page next if page_number >= @pages if (this_page = page(page_number)) yield page_number, this_page, page_status end end end end |
#each_page_type_region(start_page = 0) {|region| ... } ⇒ Object
Iterate through unique regions in the space by page type. This is useful to achieve an overall view of the space.
478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 |
# File 'lib/innodb/space.rb', line 478 def each_page_type_region(start_page = 0) return enum_for(:each_page_type_region, start_page) unless block_given? region = nil each_page_status(start_page) do |page_number, page, page_status| page_type = type_for_page(page, page_status) if region && region[:type] == page_type region[:end] = page_number region[:count] += 1 else yield region if region region = { start: page_number, end: page_number, type: page_type, count: 1, } end end yield region if region end |
#each_xdes ⇒ Object
Iterate through all extent descriptors for the space, returning an Innodb::Xdes object for each one.
442 443 444 445 446 447 448 449 450 451 452 |
# File 'lib/innodb/space.rb', line 442 def each_xdes return enum_for(:each_xdes) unless block_given? each_xdes_page do |xdes_page| xdes_page.each_xdes do |xdes| # Only return initialized XDES entries; :state will be nil for extents # that have not been allocated yet. yield xdes if xdes.xdes[:state] end end end |
#each_xdes_list ⇒ Object
Iterate through Innodb::Xdes lists in the space.
414 415 416 417 418 419 420 |
# File 'lib/innodb/space.rb', line 414 def each_xdes_list return enum_for(:each_xdes_list) unless block_given? XDES_LISTS.each do |name| yield name, list(name) end end |
#each_xdes_page ⇒ Object
Iterate through all extent descriptor pages, returning an Innodb::Page object for each one.
431 432 433 434 435 436 437 438 |
# File 'lib/innodb/space.rb', line 431 def each_xdes_page return enum_for(:each_xdes_page) unless block_given? each_xdes_page_number do |page_number| current_page = page(page_number) yield current_page if current_page&.extent_descriptor? end end |
#each_xdes_page_number(&block) ⇒ Object
An array of all FSP/XDES page numbers for the space.
423 424 425 426 427 |
# File 'lib/innodb/space.rb', line 423 def each_xdes_page_number(&block) return enum_for(:each_xdes_page_number) unless block_given? 0.step(pages - 1, pages_per_bookkeeping_page).each(&block) end |
#extent_size ⇒ Object
The size (in bytes) of an extent.
187 188 189 |
# File 'lib/innodb/space.rb', line 187 def extent_size pages_per_extent * page_size end |
#fsp ⇒ Object
Get (and cache) the FSP header from the FSP_HDR page.
271 272 273 |
# File 'lib/innodb/space.rb', line 271 def fsp @fsp ||= page(page_fsp_hdr).fsp_header end |
#fsp_flags ⇒ Object
The FSP header flags, decoded. If the page size has not been initialized, reach into the raw bytes of the FSP_HDR page and attempt to decode the flags field that way.
149 150 151 152 153 |
# File 'lib/innodb/space.rb', line 149 def fsp_flags return fsp.flags if @page_size raw_fsp_header_flags end |
#ibuf_bitmap_page_for_page(page_number) ⇒ Object
The IBUF_BITMAP page which will contain the bitmap entry for a given page.
203 204 205 |
# File 'lib/innodb/space.rb', line 203 def ibuf_bitmap_page_for_page(page_number) page_number - (page_number % pages_per_bookkeeping_page) + 1 end |
#index(root_page_number, record_describer = nil) ⇒ Object
Get an Innodb::Index object for a specific index by root page number.
321 322 323 |
# File 'lib/innodb/space.rb', line 321 def index(root_page_number, record_describer = nil) Innodb::Index.new(self, root_page_number, record_describer || @record_describer) end |
#inode(fseg_id) ⇒ Object
Return an Inode by fseg_id. Iterates through the inode list, but it normally is fairly small, so should be relatively efficient.
380 381 382 |
# File 'lib/innodb/space.rb', line 380 def inode(fseg_id) each_inode.select { |inode| inode.fseg_id == fseg_id }.first end |
#inspect ⇒ Object
106 107 108 109 110 111 112 113 |
# File 'lib/innodb/space.rb', line 106 def inspect "<%s file=%s, page_size=%i, pages=%i>" % [ self.class.name, name.inspect, page_size, pages, ] end |
#list(name) ⇒ Object
Get an Innodb::List object for a specific list by list name.
316 317 318 |
# File 'lib/innodb/space.rb', line 316 def list(name) fsp[name] if XDES_LISTS.include?(name) || INODE_LISTS.include?(name) end |
#name ⇒ Object
Return a string which can uniquely identify this space. Be careful not to do anything which could instantiate a BufferCursor so that we can use this method in cursor initialization.
102 103 104 |
# File 'lib/innodb/space.rb', line 102 def name @name ||= @data_files.map(&:name).join(",") end |
#page(page_number) ⇒ Object
Get an Innodb::Page object for a specific page by page number.
244 245 246 247 |
# File 'lib/innodb/space.rb', line 244 def page(page_number) data = page_data(page_number) Innodb::Page.parse(self, data, page_number) if data end |
#page_data(page_number) ⇒ Object
Get the raw byte buffer for a specific page by page number.
239 240 241 |
# File 'lib/innodb/space.rb', line 239 def page_data(page_number) read_at_offset(page_number * page_size, page_size) end |
#page_fsp_hdr ⇒ Object
Return the page number for the space’s FSP_HDR page.
266 267 268 |
# File 'lib/innodb/space.rb', line 266 def page_fsp_hdr 0 end |
#page_sys_data_dictionary ⇒ Object
Return the page number for the space’s SYS data dictionary header.
304 305 306 |
# File 'lib/innodb/space.rb', line 304 def page_sys_data_dictionary 7 end |
#page_trx_sys ⇒ Object
Return the page number for the space’s TRX_SYS page.
286 287 288 |
# File 'lib/innodb/space.rb', line 286 def page_trx_sys 5 end |
#pages_per_bookkeeping_page ⇒ Object
The number of pages per FSP_HDR/XDES/IBUF_BITMAP page. This is crudely mapped to the page size, and works for pages down to 1KiB.
193 194 195 |
# File 'lib/innodb/space.rb', line 193 def pages_per_bookkeeping_page page_size end |
#pages_per_extent ⇒ Object
The number of pages per extent.
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 |
# File 'lib/innodb/space.rb', line 156 def pages_per_extent # Note that uncompressed tables and compressed tables using the same page # size will have a different number of pages per "extent" because InnoDB # compression uses the FSP_EXTENT_SIZE define (which is then based on the # UNIV_PAGE_SIZE define, which may be based on the innodb_page_size system # variable) for compressed tables rather than something based on the actual # compressed page size. # # For this reason, an "extent" differs in size as follows (the maximum page # size supported for compressed tables is the innodb_page_size): # # innodb_page_size | innodb compression | # page size | extent size | pages | page size | extent size | pages | # 16384 | 1 MiB | 64 | 16384 | 1 MiB | 64 | # | 8192 | 512 KiB | 64 | # | 4096 | 256 KiB | 64 | # | 2048 | 128 KiB | 64 | # | 1024 | 64 KiB | 64 | # 8192 | 1 MiB | 128 | 8192 | 1 MiB | 128 | # | 4096 | 512 KiB | 128 | # | 2048 | 256 KiB | 128 | # | 1024 | 128 KiB | 128 | # 4096 | 1 MiB | 256 | 4096 | 1 MiB | 256 | # | 2048 | 512 KiB | 256 | # | 1024 | 256 KiB | 256 | # DEFAULT_EXTENT_SIZE / system_page_size end |
#raw_fsp_header_flags ⇒ Object
Read the FSP header “flags” field by byte offset within the space file. This is useful in order to initialize the page size, as we can’t properly read the FSP_HDR page before we know its size.
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 |
# File 'lib/innodb/space.rb', line 118 def raw_fsp_header_flags # A simple sanity check. The FIL header should be initialized in page 0, # to offset 0 and page type :FSP_HDR (8). page_offset = BinData::Uint32be.read(read_at_offset(4, 4)).to_i page_type = BinData::Uint16be.read(read_at_offset(24, 2)).to_i unless page_offset.zero? && Innodb::Page::PAGE_TYPE_BY_VALUE[page_type] == :FSP_HDR raise "Something is very wrong; Page 0 does not seem to be type FSP_HDR; got page type %i but expected %i" % [ page_type, Innodb::Page::PAGE_TYPE[:FSP_HDR][:value], ] end # Another sanity check. The Space ID should be the same in both the FIL # and FSP headers. fil_space = BinData::Uint32be.read(read_at_offset(34, 4)).to_i fsp_space = BinData::Uint32be.read(read_at_offset(38, 4)).to_i unless fil_space == fsp_space raise "Something is very wrong; FIL and FSP header Space IDs do not match: FIL is %i but FSP is %i" % [ fil_space, fsp_space, ] end # Well, we're as sure as we can be. Read the flags field and decode it. flags_value = BinData::Uint32be.read(read_at_offset(54, 4)) Innodb::Page::FspHdrXdes.decode_flags(flags_value) end |
#read_at_offset(offset, size) ⇒ Object
Get the raw byte buffer of size bytes at offset in the file.
230 231 232 233 234 235 236 |
# File 'lib/innodb/space.rb', line 230 def read_at_offset(offset, size) return nil unless offset < @size && (offset + size) <= @size data_file = data_file_for_offset(offset) data_file.file.seek(offset - data_file.offset) data_file.file.read(size) end |
#rseg_page?(page_number) ⇒ Boolean
297 298 299 300 301 |
# File 'lib/innodb/space.rb', line 297 def rseg_page?(page_number) return false unless trx_sys trx_sys.rsegs.any? { |rseg| rseg.space_id.zero? && rseg.page_number == page_number } end |
#space_id ⇒ Object
275 276 277 |
# File 'lib/innodb/space.rb', line 275 def space_id fsp[:space_id] end |
#system_space? ⇒ Boolean
Determine whether this space looks like a system space. If the initial pages in the space match the SYSTEM_SPACE_PAGE_MAP, it is likely to be a system space.
252 253 254 255 256 257 258 259 260 261 262 263 |
# File 'lib/innodb/space.rb', line 252 def system_space? SYSTEM_SPACE_PAGE_MAP.each do |page_number, type| # We can't use page() here, because system_space? need to be used # in the Innodb::Page::Sys.parse to determine what type of page # is being looked at. Using page() would cause us to keep recurse # infinitely. Use Innodb::Page.new instead to load the page as # simply as possible. test_page = Innodb::Page.new(self, page_data(page_number)) return false unless test_page.type == type end true end |
#trx_sys ⇒ Object
Get the Innodb::Page::TrxSys page for a system space.
291 292 293 294 295 |
# File 'lib/innodb/space.rb', line 291 def trx_sys raise "Transaction System is only available in system spaces" unless system_space? checked_page_class!(page(page_trx_sys), Innodb::Page::TrxSys) end |
#type_for_page(page, page_status) ⇒ Object
A helper to produce a printable page type.
472 473 474 |
# File 'lib/innodb/space.rb', line 472 def type_for_page(page, page_status) page_status[:free] ? "FREE (#{page.type})" : page.type end |
#xdes_entry_for_page(page_number) ⇒ Object
The XDES entry offset for a given page within its FSP_HDR/XDES page’s XDES array.
209 210 211 212 |
# File 'lib/innodb/space.rb', line 209 def xdes_entry_for_page(page_number) relative_page_number = page_number - xdes_page_for_page(page_number) relative_page_number / pages_per_extent end |
#xdes_for_page(page_number) ⇒ Object
Return the Innodb::Xdes entry which represents a given page.
215 216 217 218 |
# File 'lib/innodb/space.rb', line 215 def xdes_for_page(page_number) xdes_array = page(xdes_page_for_page(page_number)).each_xdes.to_a xdes_array[xdes_entry_for_page(page_number)] end |
#xdes_page_for_page(page_number) ⇒ Object
The FSP_HDR/XDES page which will contain the XDES entry for a given page.
198 199 200 |
# File 'lib/innodb/space.rb', line 198 def xdes_page_for_page(page_number) page_number - (page_number % pages_per_bookkeeping_page) end |