Class: Innodb::Space
- Inherits:
-
Object
- Object
- Innodb::Space
- Defined in:
- lib/innodb/space.rb
Overview
An InnoDB space file, which can be either a multi-table ibdataN file or a single-table “innodb_file_per_table” .ibd file.
Constant Summary collapse
- DEFAULT_PAGE_SIZE =
InnoDB’s default page size is 16KiB.
16384- 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, }
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
-
#data_dictionary_page ⇒ Object
Get the Innodb::Page::SysDataDictionaryHeader page for a system space.
-
#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 ⇒ 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 FSP_HDR/XDES pages, returning an Innodb::Page object for each one.
-
#each_xdes_page_number ⇒ 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(file) ⇒ Space
constructor
Open a space file, optionally providing the page size to use.
-
#inode_lists ⇒ Object
An array of Innodb::Inode list names.
- #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_lists ⇒ Object
An array of Innodb::Xdes list names.
-
#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(file) ⇒ Space
Open a space file, optionally providing the page size to use. Pages that aren’t 16 KiB may not be supported well.
25 26 27 28 29 30 31 32 33 34 35 36 |
# File 'lib/innodb/space.rb', line 25 def initialize(file) @file = File.open(file) @size = @file.stat.size @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.
39 40 41 |
# File 'lib/innodb/space.rb', line 39 def innodb_system @innodb_system end |
#page_size ⇒ Object (readonly)
The size (in bytes) of each page in the space.
49 50 51 |
# File 'lib/innodb/space.rb', line 49 def page_size @page_size end |
#pages ⇒ Object (readonly)
The number of pages in the space.
55 56 57 |
# File 'lib/innodb/space.rb', line 55 def pages @pages end |
#record_describer ⇒ Object
An object which can be used to describe records found in pages within this space.
43 44 45 |
# File 'lib/innodb/space.rb', line 43 def record_describer @record_describer end |
#size ⇒ Object (readonly)
The size (in bytes) of the space
52 53 54 |
# File 'lib/innodb/space.rb', line 52 def size @size end |
#system_page_size ⇒ Object (readonly)
The system default page size (in bytes), equivalent to UNIV_PAGE_SIZE.
46 47 48 |
# File 'lib/innodb/space.rb', line 46 def system_page_size @system_page_size end |
Instance Method Details
#data_dictionary_page ⇒ Object
Get the Innodb::Page::SysDataDictionaryHeader page for a system space.
262 263 264 |
# File 'lib/innodb/space.rb', line 262 def data_dictionary_page page(page_sys_data_dictionary) if system_space? end |
#each_index ⇒ Object
Iterate through all indexes in the space.
306 307 308 309 310 311 312 313 314 315 316 |
# File 'lib/innodb/space.rb', line 306 def each_index unless block_given? return enum_for(:each_index) end 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.
280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 |
# File 'lib/innodb/space.rb', line 280 def each_index_root_page_number unless block_given? return enum_for(:each_index_root_page_number) end 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) if page.type == :INDEX && page.root? yield page_number end end end nil end |
#each_inode ⇒ Object
Iterate through Innodb::Inode objects in the space.
335 336 337 338 339 340 341 342 343 344 345 346 347 |
# File 'lib/innodb/space.rb', line 335 def each_inode unless block_given? return enum_for(:each_inode) end each_inode_list.each do |name, list| list.each do |page| page.each_allocated_inode do |inode| yield inode end end end end |
#each_inode_list ⇒ Object
Iterate through Innodb::Inode lists in the space.
324 325 326 327 328 329 330 331 332 |
# File 'lib/innodb/space.rb', line 324 def each_inode_list unless block_given? return enum_for(:each_inode_list) end 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.
351 352 353 354 355 356 357 358 359 360 |
# File 'lib/innodb/space.rb', line 351 def each_page(start_page=0) unless block_given? return enum_for(:each_page, start_page) end (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.
420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 |
# File 'lib/innodb/space.rb', line 420 def each_page_status(start_page=0) unless block_given? return enum_for(:each_page_with_status, start_page) end 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.
444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 |
# File 'lib/innodb/space.rb', line 444 def each_page_type_region(start_page=0) unless block_given? return enum_for(:each_page_type_region, start_page) end 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.
404 405 406 407 408 409 410 411 412 413 414 415 416 |
# File 'lib/innodb/space.rb', line 404 def each_xdes unless block_given? return enum_for(:each_xdes) end 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.
368 369 370 371 372 373 374 375 376 |
# File 'lib/innodb/space.rb', line 368 def each_xdes_list unless block_given? return enum_for(:each_xdes_list) end xdes_lists.each do |name| yield name, list(name) end end |
#each_xdes_page ⇒ Object
Iterate through all FSP_HDR/XDES pages, returning an Innodb::Page object for each one.
391 392 393 394 395 396 397 398 399 400 |
# File 'lib/innodb/space.rb', line 391 def each_xdes_page unless block_given? return enum_for(:each_xdes_page) end each_xdes_page_number do |page_number| current_page = page(page_number) yield current_page if current_page and [:FSP_HDR, :XDES].include?(current_page.type) end end |
#each_xdes_page_number ⇒ Object
An array of all FSP/XDES page numbers for the space.
379 380 381 382 383 384 385 386 387 |
# File 'lib/innodb/space.rb', line 379 def each_xdes_page_number unless block_given? return enum_for(:each_xdes_page_number) end 0.step(pages - 1, pages_per_bookkeeping_page).each do |n| yield n end end |
#extent_size ⇒ Object
The size (in bytes) of an extent.
154 155 156 |
# File 'lib/innodb/space.rb', line 154 def extent_size pages_per_extent * page_size end |
#fsp ⇒ Object
Get (and cache) the FSP header from the FSP_HDR page.
228 229 230 |
# File 'lib/innodb/space.rb', line 228 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.
114 115 116 117 118 119 120 |
# File 'lib/innodb/space.rb', line 114 def fsp_flags if @page_size return fsp[:flags] else raw_fsp_header_flags end end |
#ibuf_bitmap_page_for_page(page_number) ⇒ Object
The IBUF_BITMAP page which will contain the bitmap entry for a given page.
170 171 172 |
# File 'lib/innodb/space.rb', line 170 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.
274 275 276 277 |
# File 'lib/innodb/space.rb', line 274 def index(root_page_number, record_describer=nil) Innodb::Index.new(self, root_page_number, record_describer || @record_describer) end |
#inode_lists ⇒ Object
An array of Innodb::Inode list names.
319 320 321 |
# File 'lib/innodb/space.rb', line 319 def inode_lists [:full_inodes, :free_inodes] end |
#inspect ⇒ Object
71 72 73 74 75 76 77 78 |
# File 'lib/innodb/space.rb', line 71 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.
267 268 269 270 271 |
# File 'lib/innodb/space.rb', line 267 def list(name) if xdes_lists.include?(name) || inode_lists.include?(name) fsp[name] end 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.
60 61 62 63 64 65 66 67 68 69 |
# File 'lib/innodb/space.rb', line 60 def name return @name if @name prefix = "" if File.extname(@file.path) == ".ibd" prefix = File.basename(File.dirname(@file.path)) + "/" end @name = prefix + File.basename(@file.path) end |
#page(page_number) ⇒ Object
Get an Innodb::Page object for a specific page by page number.
202 203 204 |
# File 'lib/innodb/space.rb', line 202 def page(page_number) Innodb::Page.parse(self, page_data(page_number)) end |
#page_data(page_number) ⇒ Object
Get the raw byte buffer for a specific page by page number.
194 195 196 197 198 199 |
# File 'lib/innodb/space.rb', line 194 def page_data(page_number) offset = page_number.to_i * page_size return nil unless offset < @size return nil unless (offset + page_size) <= @size read_at_offset(offset, page_size) end |
#page_fsp_hdr ⇒ Object
Return the page number for the space’s FSP_HDR page.
223 224 225 |
# File 'lib/innodb/space.rb', line 223 def page_fsp_hdr 0 end |
#page_sys_data_dictionary ⇒ Object
Return the page number for the space’s SYS data dictionary header.
257 258 259 |
# File 'lib/innodb/space.rb', line 257 def page_sys_data_dictionary 7 end |
#page_trx_sys ⇒ Object
Return the page number for the space’s TRX_SYS page.
237 238 239 |
# File 'lib/innodb/space.rb', line 237 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.
160 161 162 |
# File 'lib/innodb/space.rb', line 160 def pages_per_bookkeeping_page page_size end |
#pages_per_extent ⇒ Object
The number of pages per extent.
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 |
# File 'lib/innodb/space.rb', line 123 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 | # 1048576 / 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.
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 |
# File 'lib/innodb/space.rb', line 83 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 == 0 && 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 don't 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.
188 189 190 191 |
# File 'lib/innodb/space.rb', line 188 def read_at_offset(offset, size) @file.seek(offset) @file.read(size) end |
#rseg_page?(page_number) ⇒ Boolean
246 247 248 249 250 251 252 253 254 |
# File 'lib/innodb/space.rb', line 246 def rseg_page?(page_number) if trx_sys rseg_match = trx_sys.rsegs.select { |rseg| rseg[:space_id] == 0 && rseg[:page_number] == page_number } ! rseg_match.empty? end end |
#space_id ⇒ Object
232 233 234 |
# File 'lib/innodb/space.rb', line 232 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.
209 210 211 212 213 214 215 216 217 218 219 220 |
# File 'lib/innodb/space.rb', line 209 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.
242 243 244 |
# File 'lib/innodb/space.rb', line 242 def trx_sys page(page_trx_sys) if system_space? end |
#type_for_page(page, page_status) ⇒ Object
A helper to produce a printable page type.
438 439 440 |
# File 'lib/innodb/space.rb', line 438 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.
176 177 178 179 |
# File 'lib/innodb/space.rb', line 176 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.
182 183 184 185 |
# File 'lib/innodb/space.rb', line 182 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_lists ⇒ Object
An array of Innodb::Xdes list names.
363 364 365 |
# File 'lib/innodb/space.rb', line 363 def xdes_lists [:free, :free_frag, :full_frag] end |
#xdes_page_for_page(page_number) ⇒ Object
The FSP_HDR/XDES page which will contain the XDES entry for a given page.
165 166 167 |
# File 'lib/innodb/space.rb', line 165 def xdes_page_for_page(page_number) page_number - (page_number % pages_per_bookkeeping_page) end |