StrokeDB persistable incremental views

Posted by yrashk

This weekend StrokeDB got so called “persistable incremental views”. What is this?

Well, lets start from View concept. It is basically a map-reduce filter with map and reduce functions defined in Ruby.

By default, it maps all documents and lets you reduce them (lets say we want to find users with age > 21):

 
   my_view = View.create!(:name => "my view").reduce_with {|doc| doc.is_a?(User) && doc.age > 21 }
 

Or, you can specify your own map block (if you need to create new documents set to be reduced):

 
   my_view = View.create!(:name => "my view").map_with do |doc|
                     new_doc = Document.create!(:doc => doc)
   end.reduce_with {|doc| doc.doc.is_a?(User) && doc.doc.age > 21 }
 

To get results, simply use

 
  my_view.emit.to_a # or my_view.emit.documents, that's the same
 

Okay, that’s simple. We map documents to documents and then reducing them using some criteria. Also I would like to mention that Views could be argument-polymorphic. If you’ll define your map and reduce blocks having more than one argument, you can emit results using some parameters:

 
   my_view = View.create!(:name => "my view").reduce_with {|doc,age| doc.is_a?(User) && doc.age > age }
   my_view.emit(21).to_a
 

I think that’s simple and nice :)

Now incremental views come in. When you call my_view.emit View emits first “view cut” which is a set of documents map/reduced for the whole database. Now, you can use this view cut to get new view updates:
   
     first_cut = my_view.emit 
     # ... work with database, add some new documents, update old documents
     next_cut = first_cut.emit
   

next_cut view cut will contain only newly created/updated documents — so, you get updates incrementally.

Now, what about persistency declared above? That’s really simple — View and ViewCut are documents themselves — so you can easily save them and reuse later!

P.S. Currently Views are pretty slow — but things will change hopefully

P.P.S. Incremental views are really, really young in StrokeDB so I can’t promise that they are bug-free. Also API isn’t stable by any means (yet!).

Get StrokeDB

Comments

Leave a response

  1. Thom ParkinFebruary 11, 2008 @ 03:05 PM

    That is absolutely brilliant! I am very new to Ruby (by way of Rails) and continue to be amazed at the elegance of this marvelous language.