Class: MiqBerkeleyDB::MiqBdb

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(fileName = nil, fs = nil) ⇒ MiqBdb

Returns a new instance of MiqBdb.



133
134
135
136
# File 'lib/db/MiqBdb/MiqBdb.rb', line 133

def initialize(fileName = nil, fs = nil)
  @fs = fs unless fs.nil?
  open(fileName) unless fileName.nil?
end

Instance Attribute Details

#dbObject (readonly)

Returns the value of attribute db.



131
132
133
# File 'lib/db/MiqBdb/MiqBdb.rb', line 131

def db
  @db
end

#headerObject (readonly)

Returns the value of attribute header.



131
132
133
# File 'lib/db/MiqBdb/MiqBdb.rb', line 131

def header
  @header
end

Instance Method Details

#closeObject

Close the database



173
174
175
176
177
# File 'lib/db/MiqBdb/MiqBdb.rb', line 173

def close
  @db.close
  @file.close
  @db = @header = @file = @filename = nil
end

#dumpObject



224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
# File 'lib/db/MiqBdb/MiqBdb.rb', line 224

def dump
  out = "File Header\n"
  out << "  lsn:             #{@header['lsn']}\n"
  out << "  pgno:            #{@header['pgno']}\n"
  out << "  magic:           0x#{'%08x' % @header['magic']}\n"
  out << "  version:         #{@header['version']}\n"
  out << "  pagesize:        #{@header['pagesize']}\n"
  out << "  encrypt_alg:     #{@header['encrypt_alg']}\n"
  out << "  page type:       #{MiqBdbPage.type2string(@header['p_type'])}\n"
  out << "  metaflags:       0x#{'%08x' % @header['metaflags']}\n"
  out << "  free:            #{@header['free']}\n"
  out << "  last_pgno:       #{@header['last_pgno']}\n"
  out << "  key_count:       #{@header['key_count']}\n"
  out << "  record_count:    #{@header['record_count']}\n"
  out << "  flags:           0x#{'%08x' % @header['flags']}\n"
  out << "  uid:             #{uid_format}\n"
  out << "\n"
  out
end

#eachObject



262
263
264
265
266
267
268
# File 'lib/db/MiqBdb/MiqBdb.rb', line 262

def each
  @db.pages do |page|
    page.pairs do |k, v|
      yield k, v
    end
  end
end

#each_keyObject



270
271
272
273
274
275
276
# File 'lib/db/MiqBdb/MiqBdb.rb', line 270

def each_key
  @db.pages do |page|
    page.keys do |k|
      yield k
    end
  end
end

#each_valueObject



278
279
280
281
282
283
284
# File 'lib/db/MiqBdb/MiqBdb.rb', line 278

def each_value
  @db.pages do |page|
    page.values do |v|
      yield v
    end
  end
end

#keysObject



244
245
246
247
248
# File 'lib/db/MiqBdb/MiqBdb.rb', line 244

def keys
  keys = []
  each_key { |k| keys << k }
  keys
end

#nbucketsObject



184
185
186
# File 'lib/db/MiqBdb/MiqBdb.rb', line 184

def nbuckets
  @db.header.max_bucket + 1
end

#nkeysObject



188
189
190
# File 'lib/db/MiqBdb/MiqBdb.rb', line 188

def nkeys
  @header["key_count"]
end

#npagesObject



196
197
198
# File 'lib/db/MiqBdb/MiqBdb.rb', line 196

def npages
  @header["last_pgno"] + 1
end

#nrecsObject



192
193
194
# File 'lib/db/MiqBdb/MiqBdb.rb', line 192

def nrecs
  @header["record_count"]
end

#open(filename) ⇒ Object

Open the database



139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/db/MiqBdb/MiqBdb.rb', line 139

def open(filename)
  # Get header & check.
  @filename = filename
  @file = fileOpen(@filename)
  @header = DBHEADER.decode(@file.read(SIZEOF_DBHEADER))

  # Holds the page buf if we read all pages
  @read_buf = nil

  # Check header version - simple warning if less than 8.
  if @header["version"] < 8
    msg = "MiqBerkeleyDB: Database header version is less than 8."
    if $log
      $log.warn(msg)
    else
      puts "WARNING: " + msg
    end
  end

  # We don't support encryption.
  raise "MiqBerkeleyDB: This database uses encryption." if @header["encrypt_alg"] != 0

  # Open db type (only hash for now, may expand to plugins in the future).
  @db = case @header["magic"]
        when DB_HASHMAGIC then   MiqBdbHashDatabase.new(self)
        when DB_BTREEMAGIC then  MiqBdbBtreeDatabase.new(self)
        when DB_QAMMAGIC then    raise "MiqBerkeleyDB: Database type is Queue"
        when DB_LOGMAGIC then    raise "MiqBerkeleyDB: Database type is Log"
        when DB_RENAMEMAGIC then raise "MiqBerkeleyDB: Database type is Rename"
        else                     raise "MiqBerkeleyDB: Database type #{@header['magic']} is not supported"
        end
end

#pagesObject



204
205
206
# File 'lib/db/MiqBdb/MiqBdb.rb', line 204

def pages
  @db.pages { |page| yield page }
end

#pagesizeObject



200
201
202
# File 'lib/db/MiqBdb/MiqBdb.rb', line 200

def pagesize
  @header["pagesize"]
end

#read(len) ⇒ Object



179
180
181
182
# File 'lib/db/MiqBdb/MiqBdb.rb', line 179

def read(len)
  raise "File not open" if @file.nil?
  @file.read(len)
end

#readAllPagesObject



219
220
221
222
# File 'lib/db/MiqBdb/MiqBdb.rb', line 219

def readAllPages
  @file.seek(0)
  @read_buf = @file.read(pagesize * npages)
end

#readPage(pagenum, buf = @read_buf) ⇒ Object



208
209
210
211
212
213
214
215
216
217
# File 'lib/db/MiqBdb/MiqBdb.rb', line 208

def readPage(pagenum, buf = @read_buf)
  where = (pagenum * pagesize)
  page = if buf.nil?
           @file.seek(where)
           @file.read(pagesize)
         else
           buf[where, pagesize]
         end
  page
end

#sizeObject



256
257
258
259
260
# File 'lib/db/MiqBdb/MiqBdb.rb', line 256

def size
  size = 0
  each_key { |_k| size += 1 }
  size
end

#valuesObject



250
251
252
253
254
# File 'lib/db/MiqBdb/MiqBdb.rb', line 250

def values
  values = []
  each_value { |v| values << v }
  values
end