Class: MiqSqlite3DB::MiqSqlite3

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

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

Returns a new instance of MiqSqlite3.



228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
# File 'lib/db/MiqSqlite/MiqSqlite3.rb', line 228

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

  if  @pageSize < 512 || @pageSize > SQLITE_MAX_PAGE_SIZE || ((@pageSize - 1) & @pageSize) != 0
    #        @pageSize = 0
    #        sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize);
    @maxEmbedFrac = 64   # 25.0%
    @minEmbedFrac = 32   # 12.5%
    @minLeafFrac  = 32   # 12.5%
    @nReserve     = 0
  else
    @nReserve     = @header.unused_space
    @maxEmbedFrac = @header.max_payload_node_fraction
    @minEmbedFrac = @header.min_payload_node_fraction
    @minLeafFrac  = @header.min_payload_leaf_fraction
    @pageSizeFixed = true
  end

  @usableSize = @pageSize - @nReserve
  #      assert( (pBt->pageSize & 7)==0 );  /* 8-byte alignment of pageSize */

  #########################################################################
  # /* maxLocal is the maximum amount of payload to store locally for
  # ** a cell.  Make sure it is small enough so that at least minFanout
  # ** cells can will fit on one page.  We assume a 10-byte page header.
  # ** Besides the payload, the cell must store:
  # **     2-byte pointer to the cell
  # **     4-byte child pointer
  # **     9-byte nKey value
  # **     4-byte nData value
  # **     4-byte overflow page pointer
  # ** So a cell consists of a 2-byte poiner, a header which is as much as
  # ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow
  # ** page pointer.
  #########################################################################
  @maxLocal = (@usableSize - 12) * @maxEmbedFrac / 255 - 23
  @minLocal = (@usableSize - 12) * @minEmbedFrac / 255 - 23
  @maxLeaf  = @usableSize - 35
  @minLeaf  = (@usableSize - 12) * @minLeafFrac / 255 - 23
end

Instance Attribute Details

#maxLeafObject (readonly)

Returns the value of attribute maxLeaf.



226
227
228
# File 'lib/db/MiqSqlite/MiqSqlite3.rb', line 226

def maxLeaf
  @maxLeaf
end

#maxLocalObject (readonly)

Returns the value of attribute maxLocal.



226
227
228
# File 'lib/db/MiqSqlite/MiqSqlite3.rb', line 226

def maxLocal
  @maxLocal
end

#minLeafObject (readonly)

Returns the value of attribute minLeaf.



226
227
228
# File 'lib/db/MiqSqlite/MiqSqlite3.rb', line 226

def minLeaf
  @minLeaf
end

#minLocalObject (readonly)

Returns the value of attribute minLocal.



226
227
228
# File 'lib/db/MiqSqlite/MiqSqlite3.rb', line 226

def minLocal
  @minLocal
end

#npagesObject (readonly)

Returns the value of attribute npages.



226
227
228
# File 'lib/db/MiqSqlite/MiqSqlite3.rb', line 226

def npages
  @npages
end

#pageSizeObject (readonly)

Returns the value of attribute pageSize.



226
227
228
# File 'lib/db/MiqSqlite/MiqSqlite3.rb', line 226

def pageSize
  @pageSize
end

#usableSizeObject (readonly)

Returns the value of attribute usableSize.



226
227
228
# File 'lib/db/MiqSqlite/MiqSqlite3.rb', line 226

def usableSize
  @usableSize
end

Instance Method Details

#closeObject

Close the database



284
285
286
287
288
# File 'lib/db/MiqSqlite/MiqSqlite3.rb', line 284

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

#each_pageObject



290
291
292
293
294
# File 'lib/db/MiqSqlite/MiqSqlite3.rb', line 290

def each_page
  for pagenum in 1..npages
    yield MiqSqlite3Page.getPage(self, pagenum)
  end
end

#getTable(name) ⇒ Object



310
311
312
# File 'lib/db/MiqSqlite/MiqSqlite3.rb', line 310

def getTable(name)
  MiqSqlite3Table.getTable(self, name)
end

#open(filename) ⇒ Object

Open the database



271
272
273
274
275
276
277
278
279
280
281
# File 'lib/db/MiqSqlite/MiqSqlite3.rb', line 271

def open(filename)
  # Get header & check.
  @filename = filename
  @file     = fileOpen(@filename)
  @header   = OpenStruct.new(DBHEADER.decode(@file.read(SIZEOF_DBHEADER)))
  @pageSize = @header.page_size
  @npages   = fileSize / @pageSize

  raise "#{@filename} is not a SQLite3 Database."                                 if @header.magic != "SQLite format 3\000"
  raise "#{@filename} is corrupt -- has incomplete pages (pagesize=#{@pageSize})" if (@pageSize * @npages) != fileSize
end

#readPage(pagenum) ⇒ Object



296
297
298
299
300
# File 'lib/db/MiqSqlite/MiqSqlite3.rb', line 296

def readPage(pagenum)
  where = (pagenum - 1) * @pageSize
  @file.seek(where)
  @file.read(@pageSize)
end

#sizeObject



302
303
304
# File 'lib/db/MiqSqlite/MiqSqlite3.rb', line 302

def size
  fileSize
end

#table_namesObject



306
307
308
# File 'lib/db/MiqSqlite/MiqSqlite3.rb', line 306

def table_names
  MiqSqlite3Table.table_names(self)
end