Class: MiqBerkeleyDB::MiqBdbHashDatabase

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

Constant Summary collapse

DB_HASH_DUP =

Hash constants.

0x01
DB_HASH_SUBDB =

Duplicates.

0x02
DB_HASH_DUPSORT =

Subdatabases.

0x04
HASH_HEADER =

Duplicates are sorted.

BinaryStruct.new([
  'L',    'max_bucket',   # 72-75: ID of Maximum bucket in use.
  'L',    'high_mask',    # 76-79: Modulo mask into table.
  'L',    'low_mask',     # 80-83: Modulo mask into table lower half.
  'L',    'ffactor',      # 84-87: Fill factor.
  'L',    'nelem',        # 88-91: Number of keys in hash table.
  'L',    'h_charkey',    # 92-95: Value of hash(CHARKEY).
  'a128', 'spares',       # 96-223: Spare pages for overflow.
  'a236', 'unused',       # 224-459: Unused space.
  'L',    'crypto_magic', # 460-463: Crypto magic number.
  'a12',  'trash',        # 464-475: Trash space - Do not use.
  'a16',  'iv',           # 476-495: Crypto IV.
  'a20',  'chksum',       # 496-511: Page chksum.
  # NOTE: There is a discrepency between the last two members.
  # Offset notes show these as 20 and 16 bytes, when they are
  # in fact 16 and 20 bytes respectively.
])
SIZEOF_HASH_HEADER =
HASH_HEADER.size
H_KEYDATA =

Each index references a group of bytes on the page.

1
H_DUPLICATE =

Key/data item.

2
H_OFFPAGE =

Duplicate key/data item.

3
H_OFFDUP =

Overflow key/data item.

4
OFFSET_LEN =

Overflow page of duplicates.

2

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(bdb) ⇒ MiqBdbHashDatabase

Returns a new instance of MiqBdbHashDatabase.



44
45
46
47
48
49
# File 'lib/db/MiqBdb/MiqBdbHash.rb', line 44

def initialize(bdb)
  # Read pointer is positioned to hash header.
  @bdb = bdb
  @header = HASH_HEADER.decode(@bdb.read(SIZEOF_HASH_HEADER))
  @header['spares'] = @header['spares'].unpack('L*')
end

Instance Attribute Details

#bdbObject (readonly)

Returns the value of attribute bdb.



42
43
44
# File 'lib/db/MiqBdb/MiqBdbHash.rb', line 42

def bdb
  @bdb
end

#headerObject (readonly)

Returns the value of attribute header.



42
43
44
# File 'lib/db/MiqBdb/MiqBdbHash.rb', line 42

def header
  @header
end

Class Method Details

.log2(num) ⇒ Object



134
135
136
137
138
139
140
141
142
143
# File 'lib/db/MiqBdb/MiqBdbHash.rb', line 134

def self.log2(num)
  limit = 1
  i = 0
  while limit < num
    limit <<= 1
    i += 1
  end

  i
end

Instance Method Details

#closeObject



51
52
53
# File 'lib/db/MiqBdb/MiqBdbHash.rb', line 51

def close
  @bdb = @header = nil
end

#dumpObject



105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
# File 'lib/db/MiqBdb/MiqBdbHash.rb', line 105

def dump
  out  = ""
  out << "Hash Database Header\n"
  out << "  nkeys:           #{@header['nelem']}\n"
  out << "  high_mask:       0x#{'%01x' % @header['high_mask']}\n"
  out << "  low_mask:        0x#{'%01x' % @header['low_mask']}\n"
  out << "  ffactor:         #{@header['ffactor']}\n"
  out << "  h_charkey:       0x#{'%08x' % @header['h_charkey']}\n"

  out << "  spare points:    "
  @header['spares'].each { |s| out << s.to_s << " " }
  out << "\n"

  out << "\n"
  out
end

#keys(page) ⇒ Object



74
75
76
77
78
79
80
81
82
# File 'lib/db/MiqBdb/MiqBdbHash.rb', line 74

def keys(page)
  i = 0
  while i < page.nentries
    key  = entryData(i,   page)

    yield key
    i += 2  # skip value
  end
end

#nkeysObject



59
60
61
# File 'lib/db/MiqBdb/MiqBdbHash.rb', line 59

def nkeys
  @header['nelem']
end

#npagesObject



55
56
57
# File 'lib/db/MiqBdb/MiqBdbHash.rb', line 55

def npages
  @bdb.npages
end

#pagesObject



63
64
65
66
67
68
69
70
71
72
# File 'lib/db/MiqBdb/MiqBdbHash.rb', line 63

def pages
  0.upto(@header['max_bucket']) do |b|
    pagenum = bucket2page(b)
    while page = MiqBdbPage.getPage(self, pagenum)
      yield page

      pagenum = page.next
    end
  end
end

#pairs(page) ⇒ Object



94
95
96
97
98
99
100
101
102
103
# File 'lib/db/MiqBdb/MiqBdbHash.rb', line 94

def pairs(page)
  i = 0
  while i < page.nentries
    key  = entryData(i,   page)
    value = entryData(i + 1, page)

    yield key, value
    i += 2
  end
end

#values(page) ⇒ Object



84
85
86
87
88
89
90
91
92
# File 'lib/db/MiqBdb/MiqBdbHash.rb', line 84

def values(page)
  i = 0
  while i < page.nentries
    value = entryData(i + 1, page)

    yield value
    i += 2  # skip key
  end
end