Since you won't likely be creating many blog posts at the same time, I'd
suggest to store them as a single list in one key, or a tail-linked list with a
single pointer to the head of the list. Either way, your access pattern is very
simple.
With the single list in one key, your first map function could look something
like this (assuming you stored it as JSON):
function(object, keyData, arg){
// Use arg to give the number of posts you want to return
var list = Riak.mapValuesJson(object)[0];
return list.slice(0,arg).map(function(v, i){ return ["posts", v, i]; });
}
Assuming you just have the keys in the list and not the bucket as well (and new
keys for posts get pushed onto the front of the array), this function will
return a list of keys that can be used in a subsequent map phase to get the
posts. Note we also pass keyData (the index in the original list) to the next
phase. That phase would look like this:
function(object, keyData, arg){
// Assuming the post is in JSON format
var data = Riak.mapValuesJson(object)[0];
return [[keyData, data]];
}
Then finally, we'd have a reduce phase that sorts the list based on that
original index.
function(values, arg){
return values.sort(function(a,b){ return a[0] - b[0]; });
}
If we were to take the other design, I'd use a link-walking query to capture
the results. Assuming the pointer to the head of your blog was at "blog/myblog":
http://localhost:8098/riak/blog/myblog/posts,_,1/_,next,1/_,next,1/_,next,1/_,next,1
...would return 5 posts. Because link-walking queries always return phases in
the order requested, your results would come back in the order of the blog.
Sean Cribbs <[email protected]>
Developer Advocate
Basho Technologies, Inc.
http://basho.com/
On Aug 10, 2010, at 6:59 AM, Nickolay Platonov wrote:
> Hello,
>
> I've examined couple of articles about schema design for Riak, but none of
> them
> cover the sorting topic.
>
> I have the following use case - a bucket holds a series of blog posts and I
> need
> to retrieve the 5 most recent ones. In relational world I would just sort on
> the
> "created" field, in the nosql world I'll use map/reduce.
>
> So, using a naive map/reduce function I will map the document's creation date
> and then
> sort by it. But this will scan the whole bucket and probably will be a very
> expensive operation.
>
> What will be a recommended solution for Riak?
>
> Thanks, Nickolay
> _______________________________________________
> riak-users mailing list
> [email protected]
> http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com
_______________________________________________
riak-users mailing list
[email protected]
http://lists.basho.com/mailman/listinfo/riak-users_lists.basho.com