couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Paul Davis <paul.joseph.da...@gmail.com>
Subject Re: Reduce parameters?
Date Thu, 27 Jan 2011 23:57:12 GMT
On Thu, Jan 27, 2011 at 6:48 PM, Joe Freeman <joe.freeman@bitroot.com> wrote:
> Hi CouchDB users,
>
> I'm trying to explain my problem as clearly as possible. Please bear with me...
>
> I have a view with a map function that is producing key-value pairs like this:
>
> ["document1","part1.1",1294696806874] -> {"content": "part 1.1, revision 3"}
> ["document1","part1.1",1294696793572] -> {"content": "part 1.1, revision 2"}
> ["document1","part1.1",1294696769516] -> {"content": "part 1.1, revision 1"}
> ["document1","part1.2",1294696816974] -> {"content": "part 1.2, revision 2"}
> ["document1","part1.2",1294696761684] -> {"content": "part 1.2, revision 1"}
> ["document1","part1.3",1294696709610] -> {"content": "part 1.3, revision 1"}
> ["document2","part2.1",1294696812168] -> {"content": "part 2.1, revision 3"}
> ["document2","part2.1",1294696802362] -> {"content": "part 2.1, revision 2"}
> ["document2","part2.1",1294696743154] -> {"content": "part 2.1, revision 1"}
> ["document2","part2.2",1294696819313] -> {"content": "part 2.2, revision 1"}
>
> (Key is on the left of the arrow, value on the right.)
>
> These results list different revisions of parts within a document. The
> key is split into three parts: document ID, part ID and the timestamp
> of the revision.
>
> If I add a reduce function:
>
> function(keys, values, rereduce) {
>  return values[0];
> }

I think you'll want to make sure and sort here. IIRC there are a
couple edge cases you can run into if you don't. But that might've
been without groups...

> And specify a group_level of two, I can get the latest revisions for
> all the parts. I can also specify a startkey and endkey of
> '["document1"]' and '["document1",{}]' to get all the latest revisions
> for a specific document.
>
> For example, the output would be:
>
> ["document1","part1.1",1294696806874] -> {"content": "part 1.1, revision 3"}
> ["document1","part1.2",1294696816974] -> {"content": "part 1.2, revision 2"}
> ["document1","part1.3",1294696709610] -> {"content": "part 1.3, revision 1"}
>
> However, I was hoping to progress this by being able to specify a
> timestamp in the past, and have the view return *the latest revision
> for each part that existed at that point in time*.
>
> The closest I have got so far is being able to get *the latest
> revision for a specific part* (but not for all parts within a
> document). I can do this by specifying a startkey and endkey of
> '["document1","part1.1", 0]' and
> '["document1","part1.1",1294696800000]' - which would return "part
> 1.1, revision 2" (from above).
>
> An approach that I had in mind was somehow being able to pass a
> parameter to the reduce function so that it was able to iterate over
> all the values and find the latest revision after a specified time
> (SOME_PARAMETER). Perhaps something like this (untested):
>
> function(keys, values, rereduce) {
>  var latestIndex, latestTime;
>  values.forEach(function(index, value) {
>    if (latestTime < value.timestamp && value.timestamp < SOME_PARAMETER)
{
>      latestTime = value.timestamp;
>      latestIndex = index;
>    }
>  });
>  return values[latestIndex];
> }
>
> But I'm guessing that's not possible? And probably terribly inefficient.
>
> Thanks for reading. Any suggestions?
>

You're best bet would be to make another view that you can use to get
what you want. For instance, if you want the revisions that existed
for a given document before a certain time just create a second view
that doesn't include the part in the key and then you can grab per
document all revisions for a given document before (or after) a given
timestamp.

Mime
View raw message