Map Reduce for leveldb (via levelup)
Incremental map-reduces and real-time results.
Waat?
An "incremental map reduce" means when you update one key, only a relevant protion of the data needs to be recalculated.
"real-time results" means that you can listen to the database, and recieve change notifications on the fly! a la level-live-stream
If you just want something very simple, like mapping the date a blog post is created to the blog, then level-index may be enough.
Example
create a simple map-reduce
var LevelUp =var SubLevel =var MapReduce =var db =var mapDb =})
map-reduce
uses level-trigger to make map reduces durable.
querying results.
//get all the results in a specific group//start:[...] implies end:.. to be the end of that group.mapDb//get all the results in under a group.mapDb//get all the top levelmapDb
complex aggregations
map-reduce with multiple levels of aggregation.
suppose we are building a database of all the street-food in the world. the data looks like this:
country: USA | Germany | Cambodia etc...state: CA | NY | '' etc...city: Oakland | New York | Berlin | Phnom Penh etc...type: taco | chili-dog | doner | noodles etc...
We will aggregate to counts per-region, that look like this:
//say: under the key USA'taco': 23497'chili-dog': 5643etc...
first we'll map the raw data to ([country, state, city],type)
tuples.
then we'll count up all the instances of a particular type in that region!
var LevelUp =var SubLevel =var MapReduce =var db =var mapDb =
then query it like this:
mapDb
retrive a specific result
pass db.get
an array, and you can retrive a specific value, by group.
var userMapping =db"userPoints"{value = JSON;var date = valuecreated;;}{return Numberacc + Numbervalue;}0;{userMapping;}
License
MIT