Class: MiqBerkeleyDB::MiqBdbPage

Inherits:
Object
  • Object
show all
Defined in:
lib/db/MiqBdb/MiqBdbPage.rb

Constant Summary collapse

PGNO_INVALID =

Page numbers.

0
PGNO_BASE_MD =
0
P_INVALID =

Page types

0
P_DUPLICATE =

Invalid page type

1
P_HASH =

Duplicate. DEPRECATED in 3.1

2
P_IBTREE =

Hash

3
P_IRECNO =

Btree internal

4
P_LBTREE =

Recno internal

5
P_LRECNO =

Btree leaf

6
P_OVERFLOW =

Recno leaf

7
P_HASHMETA =

Overflow

8
P_BTREEMETA =

Hash metadata page

9
P_QAMMETA =

Btree metadata page

10
P_QAMDATA =

Queue metadata page

11
P_LDUP =

Queue data page

12
HEADER =

Page Header.

BinaryStruct.new([
  # 00-07: Log sequence number (LSN)
  'L', 'lsn_file',        #   0-3: LSN File
  'L', 'lsn_offset',      #   4-7: LSN Offset
  'L', 'pgno',            # 08-11: Current page number
  'L', 'prev_pgno',       # 12-15: Previous page number
  'L', 'next_pgno',       # 16-19: Next page number
  'S', 'entries',         # 20-21: Number of items on the page
  'S', 'hf_offset',       # 22-23: High free byte page offset
  'C', 'level',           #    24: Btree tree level
  'C', 'p_type'           #    25: Page type
])
SIZEOF_HEADER =
HEADER.size
SIZEOF_PAGE =

With many compilers sizeof(PAGE) == 28, while SIZEOF_PAGE == 26. We add in other things directly after the page header and need the SIZEOF_PAGE. When giving the sizeof(), many compilers will pad it out to the next 4-byte boundary.

26

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(buf, db) ⇒ MiqBdbPage

Returns a new instance of MiqBdbPage.



80
81
82
83
84
85
86
87
# File 'lib/db/MiqBdb/MiqBdbPage.rb', line 80

def initialize(buf, db)
  raise "Nil buffer." if buf.nil?
  @db         = db
  @buf        = buf
  @pagesize   = buf.size
  @header     = HEADER.decode(@buf[0, SIZEOF_HEADER])
  @data       = buf[SIZEOF_HEADER..-1]
end

Instance Attribute Details

#bufObject (readonly)

Returns the value of attribute buf.



69
70
71
# File 'lib/db/MiqBdb/MiqBdbPage.rb', line 69

def buf
  @buf
end

#dataObject (readonly)

Returns the value of attribute data.



69
70
71
# File 'lib/db/MiqBdb/MiqBdbPage.rb', line 69

def data
  @data
end

#headerObject (readonly)

Returns the value of attribute header.



69
70
71
# File 'lib/db/MiqBdb/MiqBdbPage.rb', line 69

def header
  @header
end

#pagesizeObject (readonly)

Returns the value of attribute pagesize.



69
70
71
# File 'lib/db/MiqBdb/MiqBdbPage.rb', line 69

def pagesize
  @pagesize
end

Class Method Details

.getPage(db, pagenum) ⇒ Object



71
72
73
74
75
76
77
78
# File 'lib/db/MiqBdb/MiqBdbPage.rb', line 71

def self.getPage(db, pagenum)
  return nil if pagenum == PGNO_INVALID
  #     return nil if pagenum >= db.npages

  buf = db.bdb.readPage(pagenum)
  return nil if buf.nil? || buf[25, 1].ord == P_INVALID
  MiqBdbPage.new(buf, db)
end

.type2string(t) ⇒ Object

Off-page duplicate leaf



26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
# File 'lib/db/MiqBdb/MiqBdbPage.rb', line 26

def self.type2string(t)
  case t
  when P_INVALID then   "invalid"
  when P_DUPLICATE then "duplicate (deprecated)"
  when P_HASH then      "hash"
  when P_IBTREE then    "btree internal"
  when P_IRECNO then    "recno internal"
  when P_LBTREE then    "btree leaf"
  when P_LRECNO then    "recno leaf"
  when P_OVERFLOW then  "overflow"
  when P_HASHMETA then  "hash metadata"
  when P_BTREEMETA then "btree metadata"
  when P_QAMMETA then   "queue metadata"
  when P_QAMDATA then   "queue data"
  when P_LDUP then      "offpage duplicate leaf"
  else                  "unknown value of #{t}"
  end
end

Instance Method Details

#currentObject



89
90
91
# File 'lib/db/MiqBdb/MiqBdbPage.rb', line 89

def current
  @header['pgno']
end

#dumpObject

Dump page statistics like db_dump.



130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
# File 'lib/db/MiqBdb/MiqBdbPage.rb', line 130

def dump
  out  = ""
  out << "Page #{current}\n"
  out << "  type:            #{MiqBdbPage.type2string(ptype)}\n"
  out << "  prev:            #{prev}\n"
  out << "  next:            #{@header['next_pgno']}\n"
  out << "  log seq num:     file=#{@header['lsn_file']}  offset=#{@header['lsn_offset']}\n"
  out << "  level:           #{level}\n"

  if @header['p_type'] == P_OVERFLOW
    out << "  ref cnt:         #{nentries}\n"
    out << "  len:             #{offset}\n"
  else
    out << "  entries:         #{nentries}\n"
    out << "  offset:          #{offset}\n"
  end
  out << "  data size:       #{@data.size}\n"
  out << "  data:            "

  @data.bytes.take(20).each do |c|
    out << sprintf("%.2x ", c)
  end
  out << "..." if @data.size > 20

  out << "\n\n"
  out
end

#keysObject



117
118
119
# File 'lib/db/MiqBdb/MiqBdbPage.rb', line 117

def keys
  @db.keys(self) { |k| yield k }
end

#levelObject



109
110
111
# File 'lib/db/MiqBdb/MiqBdbPage.rb', line 109

def level
  @header['level']
end

#nentriesObject



101
102
103
# File 'lib/db/MiqBdb/MiqBdbPage.rb', line 101

def nentries
  @header['entries']
end

#nextObject



93
94
95
# File 'lib/db/MiqBdb/MiqBdbPage.rb', line 93

def next
  @header['next_pgno']
end

#offsetObject



105
106
107
# File 'lib/db/MiqBdb/MiqBdbPage.rb', line 105

def offset
  @header['hf_offset']
end

#pairsObject



125
126
127
# File 'lib/db/MiqBdb/MiqBdbPage.rb', line 125

def pairs
  @db.pairs(self) { |k, v| yield k, v }
end

#prevObject



97
98
99
# File 'lib/db/MiqBdb/MiqBdbPage.rb', line 97

def prev
  @header['prev_pgno']
end

#ptypeObject



113
114
115
# File 'lib/db/MiqBdb/MiqBdbPage.rb', line 113

def ptype
  @header['p_type']
end

#valuesObject



121
122
123
# File 'lib/db/MiqBdb/MiqBdbPage.rb', line 121

def values
  @db.values(self) { |v| yield v }
end