Class: Heliodor::DB
- Inherits:
-
Object
- Object
- Heliodor::DB
- Defined in:
- lib/heliodor/db.rb
Overview
Base class for accessing DBs and building queries
Instance Attribute Summary collapse
-
#file ⇒ String
The current value of file.
-
#tsafe ⇒ Bool
When true, uses mutexes for building queries.
Instance Method Summary collapse
-
#delete(table) ⇒ self
Deletes given table.
-
#initialize(file, tsafe = false) ⇒ DB
constructor
A new instance of DB.
- #inspect ⇒ Object
-
#query(table) ⇒ Heliodor::Query
Entry point for building queries.
-
#tables ⇒ Array<String>
Returns array of table names.
-
#write(dat) ⇒ self
Writes database to file.
Constructor Details
permalink #initialize(file, tsafe = false) ⇒ DB
Returns a new instance of DB.
7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 |
# File 'lib/heliodor/db.rb', line 7 def initialize(file, tsafe = false) @tsafe = tsafe @mutex = Mutex.new @dtable = { 'heliodor_data' => { 'name' => 'Heliodor', 'version' => Heliodor::VERSION, 'bson_version' => Gem.loaded_specs['bson'].version.version, 'last_write' => DateTime.now.strftime, 'ruby_version' => RUBY_VERSION, 'ruby_patchlevel' => RUBY_PATCHLEVEL.to_s, 'ruby_platform' => RUBY_PLATFORM } } @file = File.(file.to_s) if File.exist?(@file) Zlib::GzipReader.open(File.(@file)) do |f| bb = BSON::ByteBuffer.new(f.read) @dat = Hash.from_bson(bb) f.close end else Zlib::GzipWriter.open(File.(@file)) do |f| # File.open(@file, 'w') do |f| f.write(@dtable.to_bson.to_s) @dat = @dtable.clone f.close end end end |
Instance Attribute Details
permalink #file ⇒ String
Returns the current value of file.
4 5 6 |
# File 'lib/heliodor/db.rb', line 4 def file @file end |
permalink #tsafe ⇒ Bool
When true, uses mutexes for building queries
4 5 6 |
# File 'lib/heliodor/db.rb', line 4 def tsafe @tsafe end |
Instance Method Details
permalink #delete(table) ⇒ self
Deletes given table
53 54 55 56 57 58 59 60 61 62 63 |
# File 'lib/heliodor/db.rb', line 53 def delete(table) if @tsafe @mutex.synchronize do @dat.delete(table) write(dat) end else write(dat) @dat.delete(table) end end |
permalink #inspect ⇒ Object
[View source]
83 84 85 |
# File 'lib/heliodor/db.rb', line 83 def inspect %(#<Heliodor::DB:#{object_id.to_s(16)} @file='#{@file}'>) end |
permalink #query(table) ⇒ Heliodor::Query
Entry point for building queries
40 41 42 43 44 45 46 47 48 |
# File 'lib/heliodor/db.rb', line 40 def query(table) if @tsafe @mutex.synchronize do Heliodor::Query.new(self, table, @dat) end else Heliodor::Query.new(self, table, @dat) end end |
permalink #tables ⇒ Array<String>
Returns array of table names
67 68 69 |
# File 'lib/heliodor/db.rb', line 67 def tables Hash.from_bson(BSON::ByteBuffer.new(File.read(@file))).keys end |
permalink #write(dat) ⇒ self
Writes database to file
73 74 75 76 77 78 79 80 81 |
# File 'lib/heliodor/db.rb', line 73 def write(dat) @dat = dat File.truncate(@file, 0) if File.exist?(@file) Zlib::GzipWriter.open(File.(@file)) do |f| f.write(dat.merge(@dtable).to_bson.to_s) f.close end self end |