Class: Innodb::List

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

Overview

An abstract InnoDB “free list” or FLST (renamed to just “list” here as it frequently is used for structures that aren’t free lists). This class must be sub-classed to provide an appropriate #object_from_address method.

Direct Known Subclasses

History, Inode, UndoPage, Xdes

Defined Under Namespace

Classes: History, Inode, ListCursor, UndoPage, Xdes

Constant Summary collapse

ADDRESS_SIZE =

An “address”, which consists of a page number and byte offset within the page. This points to the list “node” pointers (prev and next) of the node, not necessarily the node object.

4 + 2
NODE_SIZE =

A list node consists of two addresses: the “previous” node address, and the “next” node address.

2 * ADDRESS_SIZE
BASE_NODE_SIZE =

A list base node consists of a list length followed by two addresses: the “first” node address, and the “last” node address.

4 + (2 * ADDRESS_SIZE)

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(space, base) ⇒ List

Returns a new instance of List.



58
59
60
61
# File 'lib/innodb/list.rb', line 58

def initialize(space, base)
  @space  = space
  @base   = base
end

Instance Attribute Details

#baseObject (readonly)

Returns the value of attribute base.



64
65
66
# File 'lib/innodb/list.rb', line 64

def base
  @base
end

#spaceObject (readonly)

Returns the value of attribute space.



63
64
65
# File 'lib/innodb/list.rb', line 63

def space
  @space
end

Class Method Details

.get_address(cursor) ⇒ Object

Read a node address from a cursor. Return nil if the address is an end or “NULL” pointer (the page number is UINT32_MAX), or the address if valid.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/innodb/list.rb', line 14

def self.get_address(cursor)
  page    = cursor.name("page") {
    Innodb::Page.maybe_undefined(cursor.get_uint32)
  }
  offset  = cursor.name("offset") { cursor.get_uint16 }
  if page
    {
      :page     => page,
      :offset   => offset,
    }
  end
end

.get_base_node(cursor) ⇒ Object

Read a base node, consisting of a list length followed by two addresses (:first and :last) from a cursor. Either address may be nil. An empty list has a :length of 0 and :first and :last are nil. A list with only a single item will have a :length of 1 and :first and :last will point to the same address.



50
51
52
53
54
55
56
# File 'lib/innodb/list.rb', line 50

def self.get_base_node(cursor)
  {
    :length => cursor.name("length") { cursor.get_uint32 },
    :first  => cursor.name("first") { get_address(cursor) },
    :last   => cursor.name("last")  { get_address(cursor) },
  }
end

.get_node(cursor) ⇒ Object

Read a node, consisting of two consecutive addresses (:prev and :next) from a cursor. Either address may be nil, indicating the end of the linked list.



34
35
36
37
38
39
# File 'lib/innodb/list.rb', line 34

def self.get_node(cursor)
  {
    :prev => cursor.name("prev") { get_address(cursor) },
    :next => cursor.name("next") { get_address(cursor) },
  }
end

Instance Method Details

#eachObject

Iterate through all nodes in the list.



123
124
125
126
127
128
129
130
131
132
# File 'lib/innodb/list.rb', line 123

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

  c = list_cursor
  while e = c.next
    yield e
  end
end

#firstObject

Return the first object in the list using the list base node “first” address pointer.



99
100
101
# File 'lib/innodb/list.rb', line 99

def first
  object_from_address(@base[:first])
end

#include?(item) ⇒ Boolean

Return whether the given item is present in the list. This depends on the item and the items in the list implementing some sufficient == method. This is implemented rather inefficiently by constructing an array and leaning on Array#include? to do the real work.

Returns:

  • (Boolean)


118
119
120
# File 'lib/innodb/list.rb', line 118

def include?(item)
  each.to_a.include? item
end

#lastObject

Return the first object in the list using the list base node “last” address pointer.



105
106
107
# File 'lib/innodb/list.rb', line 105

def last
  object_from_address(@base[:last])
end

#lengthObject

Return the number of items in the list.



93
94
95
# File 'lib/innodb/list.rb', line 93

def length
  @base[:length]
end

#list_cursor(node = nil) ⇒ Object

Return a list cursor for the list.



110
111
112
# File 'lib/innodb/list.rb', line 110

def list_cursor(node=nil)
  ListCursor.new(self, node)
end

#next(object) ⇒ Object

Return the object pointed to by the “next” address pointer of the provided object.



84
85
86
87
88
89
90
# File 'lib/innodb/list.rb', line 84

def next(object)
  unless object.respond_to? :next_address
    raise "Class #{object.class} does not respond to next_address"
  end

  object_from_address(object.next_address)
end

#object_from_address(address) ⇒ Object

Abstract #object_from_address method which must be implemented by sub-classes in order to return a useful object given an object address.



68
69
70
# File 'lib/innodb/list.rb', line 68

def object_from_address(address)
  raise "#{self.class} must implement object_from_address"
end

#prev(object) ⇒ Object

Return the object pointed to by the “previous” address pointer of the provided object.



74
75
76
77
78
79
80
# File 'lib/innodb/list.rb', line 74

def prev(object)
  unless object.respond_to? :prev_address
    raise "Class #{object.class} does not respond to prev_address"
  end

  object_from_address(object.prev_address)
end