Problem: update relevant Views on document save.
Step 1. Run all the views with no prefiltering.
- Store keeps a simple list (set) of registered views.
- When Store#save! is called, it accesses this document and iterates over the listed views and updates them one by one.
- When view is defined it registers itself in a views list with Store#register_view(view).
Step 2. Some prefiltering.
When there are several hundreds of views in the system, running all the views for update can be very slow. The most common case: view should be run for a single meta (or possibly, a little set of metas). We may optimize this by defining a list of necessary metas for a particular view:
View.new("has_many_comments", :only => [Comment, SmallComment]) { }
- Store#register_view(view, [Meta1, Meta2,...]) is called then
- Store contains a hash map: { :rest => [list of views without :only option], meta1 => [list of views to consume meta1], meta2 => [...] }
- If View defines an :only option, it doesn't go to the :rest list, but is added to all the lists indexed by meta.
- When a document with several metas should be indexed only :rest views are updated and all the views under the selected metas in the store's index.
Example
View.new("generic") { }
View.new("comments", :only => ["comment"]) { }
View.new("comments_and_articles", :only => ["comment", "article"]) { }
View.new("sponsored", :only => ["sponsored_articles"]) { }
a = (Article + SponsoredArticle).create!(:title => "This is a sponsored article")
c = Comment.create!(:article => a, :text => "Hello")
In this situation "a" will be added to "generic" view, "comments_and_articles" view and "sponsored" view.
Comment "c" will be added to "generic", "comments" and "comments_and_articles" views.
nsurl can be attached to a meta name: "comment:http://some.url/"
(default is the same nsurl viewdoc has)
Step 3. Immediate and lazy view updates.
- Store may be configured to update some of the views in different manners (immediately, asynchronously, on schedule etc).
- It is a good idea to separate view definition and it's update strategy.
Possible syntax:
immediate = ImmediateViewUpdater.new
immediate << "view_name", "view_name2"
# this reindexes already registered views & is used to correctly index the views, which are added later.
store.attach_view_updater(immediate)
# Config builder DSL for the code above:
Config.view_updaters["immediate"] = ["view1", "view2"]
Config.view_updaters["nightly"] = ["fulltext", "weird_stats"]
Updaters incapsulate different update strategies. All the views, which are not defined in a particular updater are handled by a default ImmediateUpdater.
Create your profile
Help contribute to this project by taking a few moments to create your personal profile. Create your profile ยป
StrokeDB is an embeddable distributed document database written in Ruby
0.0.3—81% complete
Completed 26 of 32 tickets