incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Adam Wolff <awo...@gmail.com>
Subject Re: Tracking view updates with CouchDB
Date Thu, 16 Apr 2009 18:24:37 GMT
[accidentally replied off-list. here's the off-list thread]

On Thu, Apr 16, 2009 at 10:19:06AM -0700, Adam Wolff wrote:
> Thanks for the reply Brian. The issue is that the key_doc_id isn't the
> same as the doc.key, so I can't get updated times by key when I'm just
> looking at a ref_doc. That is, this part would emit doc ids, not keys:
>
> >    if (doc.key_doc_id && doc.updated) {
> >      emit(key_doc_id, doc.updated);

Then you could emit ids everywhere, like this:

function(doc) {
  var type = doc['type'];
  switch(type) {
  case 'key_doc':
    if (doc.updated) {
      emit(doc._id, doc.updated);
    }
    break;
  case 'ref_doc':
    if (doc.key_doc_id && doc.updated) {
      emit(doc.key_doc_id, doc.updated);
    }
  }
}

The client does one query if necessary to convert key to _id, then another
query on [_id,null] to [_id,{}] to get related documents and timestamps.

Regards,

Brian.
On Thu, Apr 16, 2009 at 9:58 AM, Brian Candler <B.Candler@pobox.com> wrote:
> On Thu, Apr 16, 2009 at 09:21:59AM -0700, Adam Wolff wrote:
>> So we've got docs that look like this:
>> {
>>   type: "key_doc",
>>   _id : "xyzzy",
>>   key : "doc_key",
>>   updated : 1239896906303
>> }
>>
>> and docs that look like this:
>> {
>>   type: "ref_doc",
>>   key_doc_id : "xyzzy",
>>   updated : 1239897055080
>> }
>>
>> and we want a view that we query like this:
>>   startkey : ["doc_key",null], endkey : ["doc_key",{}]
>
> Not sure if this is exactly what you want, but maybe try something along
> these lines:
>
> // MAP
> function(doc) {
>  var type = doc['type'];
>  switch(type) {
>  case 'key_doc':
>    if (doc.key && doc.updated) {
>      emit(doc.key, doc.updated);
>    }
>    break;
>  case 'ref_doc':
>    if (doc.key_doc_id && doc.updated) {
>      emit(key_doc_id, doc.updated);
>    }
>  }
> }
>
> Then your startkey..endkey view will show all update timestamps referring to
> this document.
>
> Here are some other tricks you could try:
>
> * emit(..., -updated)
>
>  Then a limit=1 query should give you the (negated) most recent
>  timestamp
>
> * use a reduce function to find the maximum updated timestamp. I came across
>  an example recently of how to do that, but can't remember exactly where.
>  It also showed how to reduce both the max and min values simultaneously.
>
>> which reduces to the key_doc ids that were updated since the startkey
>> date, in order, e.g.
>>   ["xyzzy"] in this case
>
> Maybe you want to emit(updated, doc.key) instead, so you can query your view
> for all changes made after a particular time. Then it's easy on the client
> side to do a uniq on all the doc refs seen.
>
> Regards,
>
> Brian.
>

Mime
View raw message