Class Rddb::Database
In: lib/rddb/database.rb
Parent: Object

The database.

Methods

<<   []   batch   batch?   count   create_view   database_listeners   new   query   refresh_views   views  

Attributes

document_store  [R]  The document store.
logger  [RW]  A Logger for the database.
materializer  [R]  The materializer (defaults to ThreadedMaterializer)

Public Class methods

Initialize the database.

Options:

  • :document_store: A DocumentStore instance
  • :materializer_class: The Materializer class

[Source]

    # 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

Public Instance methods

Add a document to the database. The document may either be a Hash or a Rddb::Document instance.

[Source]

    # 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.

[Source]

    # 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.

[Source]

    # 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

Return true if the database is currently in batch write mode.

[Source]

    # File lib/rddb/database.rb, line 57
57:     def batch?
58:       @batch
59:     end

Return the total count of documents in the database

[Source]

    # File lib/rddb/database.rb, line 67
67:     def count
68:       document_store.count
69:     end

Create the named view with the given filter code. Returns the newly created view object.

[Source]

    # 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)

[Source]

     # File lib/rddb/database.rb, line 108
108:     def database_listeners
109:       @database_listeners ||= []
110:     end

Query the named view

[Source]

    # 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

Refresh all materialized views.

[Source]

    # File lib/rddb/database.rb, line 86
86:     def refresh_views
87:       @materializer.refresh_views
88:     end

Get named views.

[Source]

    # File lib/rddb/database.rb, line 91
91:     def views
92:       @views ||= {}
93:     end

[Validate]