Class: ChillDB::Database
- Inherits:
-
Object
- Object
- ChillDB::Database
- Defined in:
- lib/chill.rb
Overview
A Database abstraction full of internal gizmos and a few external ones too. You can access your Database via KittensApp.database (following the ChillDB.goes :KittensApp convention)
The database object is mainly useful for maintenance. The #info method is neat for looking up stats on how the database is doing, and you can ask for a compaction, to remove old revisions and make database files smaller.
ChillDB::Database is mainly used internally and isn’t very useful for most chill apps.
Instance Attribute Summary collapse
-
#meta ⇒ Object
readonly
Returns the value of attribute meta.
-
#url ⇒ Object
readonly
Returns the value of attribute url.
Instance Method Summary collapse
-
#compact! ⇒ Object
Ask the CouchDB server to compact this database, effectively making a copy and moving all recent revisions and data across to the new file.
-
#http(resource, headers = {}) ⇒ Object
grab a RestClient http resource for this database - useful internally.
-
#info ⇒ Object
Gets a Hash of database configuration and status info from the server as a ChillDB::IndifferentHash.
-
#initialize(name, settings = {}) ⇒ Database
constructor
Initialize a new database reference.
-
#inspect ⇒ Object
pretty output for debugging things :).
Constructor Details
#initialize(name, settings = {}) ⇒ Database
Initialize a new database reference. This is used internally by ChillDB.goes, and shouldn’t be used directly
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 |
# File 'lib/chill.rb', line 228 def initialize name, settings = {} # :nodoc: @meta = {} # little place to store our things # a magical constant you can define to change the defaults for magical web hosting # where the web server wants to configure an app's chill connection details # dynamically without bothering the user with such things settings = ChillDBConnectionDefaults.merge(settings) if Kernel.const_defined? :ChillDBConnectionDefaults @url = URI::HTTP.build( host: settings[:host] || 'localhost', port: settings[:port] || 5984, userinfo: [settings[:user], settings[:pass]], path: settings[:path] || "/#{settings[:database_name_prefix]}#{URI.escape hyphenate(name)}/" ) # make this database if it doesn't exist yet $stderr.puts "New database created at #{@url}" if http('').put('').code == 201 end |
Instance Attribute Details
#meta ⇒ Object (readonly)
Returns the value of attribute meta.
224 225 226 |
# File 'lib/chill.rb', line 224 def @meta end |
#url ⇒ Object (readonly)
Returns the value of attribute url.
224 225 226 |
# File 'lib/chill.rb', line 224 def url @url end |
Instance Method Details
#compact! ⇒ Object
Ask the CouchDB server to compact this database, effectively making a copy and moving all recent revisions and data across to the new file. You can still keep using your app while a compact is running, and it shouldn’t affect performance much. When using CouchDB, compacting is important as Couch databases don’t remove any old deleted or updated documents until #compact! is called. This may seem a bit odd, but it is part of how couch can be so reliable and difficult to corrupt during power failures and extended server outages. Don’t worry about unless your database files are getting too big.
You can check to see if your couch server is currently compacting with the #info method
257 258 259 260 261 |
# File 'lib/chill.rb', line 257 def compact! request = http('_compact').post('') raise request.body unless request.code == 202 return self end |
#http(resource, headers = {}) ⇒ Object
grab a RestClient http resource for this database - useful internally
280 281 282 |
# File 'lib/chill.rb', line 280 def http resource, headers = {} # :nodoc: RestClient::Resource.new((@url + resource).to_s, headers: {accept: 'application/json', content_type: 'application/json'}.merge(headers)) { |r| r } end |
#info ⇒ Object
Gets a Hash of database configuration and status info from the server as a ChillDB::IndifferentHash. This contains all sorts of interesting information useful for maintenance.
266 267 268 269 |
# File 'lib/chill.rb', line 266 def info response = http('').get() IndifferentHash.new.replace(JSON.parse response.body) end |
#inspect ⇒ Object
pretty output for debugging things :)
272 273 274 |
# File 'lib/chill.rb', line 272 def inspect "#<ChillDB::Database: #{info.inspect} >" end |