Class: Innodb::Field

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

Overview

A single field in an InnoDB record (within an INDEX page). This class provides essential information to parse records, including the length of the fixed-width and variable-width portion of the field.

Constant Summary collapse

EXTERN_FIELD_SIZE =

Size of a reference to data stored externally to the page.

20

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(position, data_type, *properties) ⇒ Field

Returns a new instance of Field.



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

def initialize(position, data_type, *properties)
  @position = position
  @type = Innodb::FieldType.new(data_type.to_s, properties)
end

Instance Attribute Details

#positionObject (readonly)

Returns the value of attribute position.



8
9
10
# File 'lib/innodb/field.rb', line 8

def position
  @position
end

#typeObject (readonly)

Returns the value of attribute type.



8
9
10
# File 'lib/innodb/field.rb', line 8

def type
  @type
end

Instance Method Details

#extern?(record) ⇒ Boolean

Return whether a part of this field is stored externally (off-page).

Returns:

  • (Boolean)


24
25
26
# File 'lib/innodb/field.rb', line 24

def extern?(record)
  record[:header][:field_externs][position]
end

#length(record) ⇒ Object

Return the actual length of this variable-length field.



29
30
31
32
33
34
35
36
# File 'lib/innodb/field.rb', line 29

def length(record)
  if type.variable?
    len = record[:header][:field_lengths][position]
  else
    len = type.length
  end
  extern?(record) ? len - EXTERN_FIELD_SIZE : len
end

#null?(record) ⇒ Boolean

Return whether this field is NULL.

Returns:

  • (Boolean)


19
20
21
# File 'lib/innodb/field.rb', line 19

def null?(record)
  type.nullable? && record[:header][:field_nulls][position]
end

#read(record, cursor) ⇒ Object

Read an InnoDB encoded data field.



39
40
41
42
# File 'lib/innodb/field.rb', line 39

def read(record, cursor)
  return :NULL if null?(record)
  cursor.name(type.name) { type.reader.read(cursor, length(record)) }
end

#read_extern(record, cursor) ⇒ Object

Read an InnoDB external pointer field.



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

def read_extern(record, cursor)
  return nil if not extern?(record)
  cursor.name(type.name) { type.reader.read_extern(cursor) }
end