Class: Innodb::List
- Inherits:
-
Object
- Object
- Innodb::List
- Defined in:
- lib/innodb/list.rb
Defined Under Namespace
Classes: BaseNode, History, Inode, ListCursor, Node, 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
-
#base ⇒ Object
readonly
Returns the value of attribute base.
-
#space ⇒ Object
readonly
Returns the value of attribute space.
Class Method Summary collapse
-
.get_address(cursor) ⇒ Object
Read a node address from a cursor.
-
.get_base_node(cursor) ⇒ Object
Read a base node, consisting of a list length followed by two addresses (:first and :last) from a cursor.
-
.get_node(cursor) ⇒ Object
Read a node, consisting of two consecutive addresses (:prev and :next) from a cursor.
Instance Method Summary collapse
-
#each(&block) ⇒ Object
Iterate through all nodes in the list.
-
#empty? ⇒ Boolean
Is the list currently empty?.
-
#first ⇒ Object
Return the first object in the list using the list base node “first” address pointer.
-
#include?(item) ⇒ Boolean
Return whether the given item is present in the list.
-
#initialize(space, base) ⇒ List
constructor
A new instance of List.
-
#last ⇒ Object
Return the first object in the list using the list base node “last” address pointer.
-
#length ⇒ Object
Return the number of items in the list.
-
#list_cursor(node = :min, direction = :forward) ⇒ Object
Return a list cursor for the list.
-
#next(object) ⇒ Object
Return the object pointed to by the “next” address pointer of the provided object.
-
#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.
-
#prev(object) ⇒ Object
Return the object pointed to by the “previous” address pointer of the provided object.
Constructor Details
#initialize(space, base) ⇒ List
Returns a new instance of List.
71 72 73 74 |
# File 'lib/innodb/list.rb', line 71 def initialize(space, base) @space = space @base = base end |
Instance Attribute Details
#base ⇒ Object (readonly)
Returns the value of attribute base.
69 70 71 |
# File 'lib/innodb/list.rb', line 69 def base @base end |
#space ⇒ Object (readonly)
Returns the value of attribute space.
68 69 70 |
# File 'lib/innodb/list.rb', line 68 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.
30 31 32 33 34 35 |
# File 'lib/innodb/list.rb', line 30 def self.get_address(cursor) page = cursor.name("page") { Innodb::Page.maybe_undefined(cursor.read_uint32) } offset = cursor.name("offset") { cursor.read_uint16 } Innodb::Page::Address.new(page: page, offset: offset) if page 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.
60 61 62 63 64 65 66 |
# File 'lib/innodb/list.rb', line 60 def self.get_base_node(cursor) BaseNode.new( length: cursor.name("length") { cursor.read_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.
44 45 46 47 48 49 |
# File 'lib/innodb/list.rb', line 44 def self.get_node(cursor) Node.new( prev: cursor.name("prev") { get_address(cursor) }, next: cursor.name("next") { get_address(cursor) } ) end |
Instance Method Details
#each(&block) ⇒ Object
Iterate through all nodes in the list.
134 135 136 137 138 |
# File 'lib/innodb/list.rb', line 134 def each(&block) return enum_for(:each) unless block_given? list_cursor.each_node(&block) end |
#empty? ⇒ Boolean
Is the list currently empty?
104 105 106 |
# File 'lib/innodb/list.rb', line 104 def empty? length.zero? end |
#first ⇒ Object
Return the first object in the list using the list base node “first” address pointer.
110 111 112 |
# File 'lib/innodb/list.rb', line 110 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.
129 130 131 |
# File 'lib/innodb/list.rb', line 129 def include?(item) each.to_a.include?(item) end |
#last ⇒ Object
Return the first object in the list using the list base node “last” address pointer.
116 117 118 |
# File 'lib/innodb/list.rb', line 116 def last object_from_address(@base.last) end |
#length ⇒ Object
Return the number of items in the list.
99 100 101 |
# File 'lib/innodb/list.rb', line 99 def length @base.length end |
#list_cursor(node = :min, direction = :forward) ⇒ Object
Return a list cursor for the list.
121 122 123 |
# File 'lib/innodb/list.rb', line 121 def list_cursor(node = :min, direction = :forward) ListCursor.new(self, node, direction) end |
#next(object) ⇒ Object
Return the object pointed to by the “next” address pointer of the provided object.
92 93 94 95 96 |
# File 'lib/innodb/list.rb', line 92 def next(object) raise "Class #{object.class} does not respond to next_address" unless object.respond_to?(:next_address) 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.
78 79 80 |
# File 'lib/innodb/list.rb', line 78 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.
84 85 86 87 88 |
# File 'lib/innodb/list.rb', line 84 def prev(object) raise "Class #{object.class} does not respond to prev_address" unless object.respond_to?(:prev_address) object_from_address(object.prev_address) end |