| Class | Rddb::Database |
| In: |
lib/rddb/database.rb
|
| Parent: | Object |
The database.
| document_store | [R] | The document store. |
| logger | [RW] | A Logger for the database. |
| materializer | [R] | The materializer (defaults to ThreadedMaterializer) |
Initialize the database.
Options:
# File lib/rddb/database.rb, line 19
19: def initialize(options={})
20: @document_store = options[:document_store] || DocumentStore::RamDocumentStore.new
21: @materializer = (options[:materializer_class] || Materializer::ThreadedMaterializer).new(self)
22: @batch = false
23: database_listeners << @materializer
24: end
Add a document to the database. The document may either be a Hash or a Rddb::Document instance.
# File lib/rddb/database.rb, line 28
28: def <<(document)
29: case document
30: when Hash
31: # create a Document from the Hash and then call << again
32: self << Document.new(document)
33: when Document
34: document_store.store(document)
35: # TODO: this may be a bottleneck, allow index updating
36: document_store.write_indexes unless batch?
37: document_added(document)
38: document
39: else
40: raise ArgumentError, "The document must be either a Hash or a Document"
41: end
42: end
Get a document by it‘s ID.
# File lib/rddb/database.rb, line 62
62: def [](id)
63: document_store.find(id)
64: end
Batch process the given block, disabling materialization queue updating and index writing until the block has completed. All views that are materialized will be refreshed upon completion of the block and any document_store indexes will be written.
# File lib/rddb/database.rb, line 48
48: def batch(&block)
49: @batch = true
50: yield
51: refresh_views
52: document_store.write_indexes
53: @batch = false
54: end
Create the named view with the given filter code. Returns the newly created view object.
# File lib/rddb/database.rb, line 79
79: def create_view(name, options={}, &block)
80: returning View.new(self, name, options, &block) do |view|
81: views[name] = view
82: end
83: end
Listeners that will be invoked when a document is added to the database. Each object in this collection should have the method document_added(document)
# File lib/rddb/database.rb, line 108
108: def database_listeners
109: @database_listeners ||= []
110: end
Query the named view
# File lib/rddb/database.rb, line 72
72: def query(name, args={})
73: raise ArgumentError, "View '#{name}' does not exist." unless views.key?(name)
74: views[name].query(document_store, args)
75: end