Class: MiqBerkeleyDB::MiqBdb
- Inherits:
-
Object
- Object
- MiqBerkeleyDB::MiqBdb
- Defined in:
- lib/db/MiqBdb/MiqBdb.rb
Instance Attribute Summary collapse
-
#db ⇒ Object
readonly
Returns the value of attribute db.
-
#header ⇒ Object
readonly
Returns the value of attribute header.
Instance Method Summary collapse
-
#close ⇒ Object
Close the database.
- #dump ⇒ Object
- #each ⇒ Object
- #each_key ⇒ Object
- #each_value ⇒ Object
-
#initialize(fileName = nil, fs = nil) ⇒ MiqBdb
constructor
A new instance of MiqBdb.
- #keys ⇒ Object
- #nbuckets ⇒ Object
- #nkeys ⇒ Object
- #npages ⇒ Object
- #nrecs ⇒ Object
-
#open(filename) ⇒ Object
Open the database.
- #pages ⇒ Object
- #pagesize ⇒ Object
- #read(len) ⇒ Object
- #readAllPages ⇒ Object
- #readPage(pagenum, buf = @read_buf) ⇒ Object
- #size ⇒ Object
- #values ⇒ Object
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
#db ⇒ Object (readonly)
Returns the value of attribute db.
131 132 133 |
# File 'lib/db/MiqBdb/MiqBdb.rb', line 131 def db @db end |
#header ⇒ Object (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
#close ⇒ Object
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 |
#dump ⇒ Object
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 |
#each ⇒ Object
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_key ⇒ Object
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_value ⇒ Object
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 |
#keys ⇒ Object
244 245 246 247 248 |
# File 'lib/db/MiqBdb/MiqBdb.rb', line 244 def keys keys = [] each_key { |k| keys << k } keys end |
#nbuckets ⇒ Object
184 185 186 |
# File 'lib/db/MiqBdb/MiqBdb.rb', line 184 def nbuckets @db.header.max_bucket + 1 end |
#nkeys ⇒ Object
188 189 190 |
# File 'lib/db/MiqBdb/MiqBdb.rb', line 188 def nkeys @header["key_count"] end |
#npages ⇒ Object
196 197 198 |
# File 'lib/db/MiqBdb/MiqBdb.rb', line 196 def npages @header["last_pgno"] + 1 end |
#nrecs ⇒ Object
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 |
#pages ⇒ Object
204 205 206 |
# File 'lib/db/MiqBdb/MiqBdb.rb', line 204 def pages @db.pages { |page| yield page } end |
#pagesize ⇒ Object
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 |
#readAllPages ⇒ Object
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 |
#size ⇒ Object
256 257 258 259 260 |
# File 'lib/db/MiqBdb/MiqBdb.rb', line 256 def size size = 0 each_key { |_k| size += 1 } size end |
#values ⇒ Object
250 251 252 253 254 |
# File 'lib/db/MiqBdb/MiqBdb.rb', line 250 def values values = [] each_value { |v| values << v } values end |