From user-return-4439-apmail-couchdb-user-archive=couchdb.apache.org@couchdb.apache.org Thu Apr 16 18:25:08 2009 Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 46907 invoked from network); 16 Apr 2009 18:25:07 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 16 Apr 2009 18:25:07 -0000 Received: (qmail 21686 invoked by uid 500); 16 Apr 2009 18:25:06 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 21587 invoked by uid 500); 16 Apr 2009 18:25:05 -0000 Mailing-List: contact user-help@couchdb.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: user@couchdb.apache.org Delivered-To: mailing list user@couchdb.apache.org Received: (qmail 21577 invoked by uid 99); 16 Apr 2009 18:25:05 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 16 Apr 2009 18:25:05 +0000 X-ASF-Spam-Status: No, hits=-0.0 required=10.0 tests=SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of awolff@gmail.com designates 74.125.92.24 as permitted sender) Received: from [74.125.92.24] (HELO qw-out-2122.google.com) (74.125.92.24) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 16 Apr 2009 18:24:59 +0000 Received: by qw-out-2122.google.com with SMTP id 8so400011qwh.29 for ; Thu, 16 Apr 2009 11:24:38 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:in-reply-to:references :date:message-id:subject:from:to:cc:content-type :content-transfer-encoding; bh=LvbCAkd16SrXB0ErDGbVpEEJxHgDq6qEWJcxHPK79nE=; b=UwdFFIEGyLKdPagx+ydB48zEgiJtS4pSHZn9e6mApDGfWIyk6Q+OyxplE+oelAkhEG RaFAMt8yioRRY3K9samJ7uVKRCPP9fJiIRohMUYWCbDqvWvvgDu3YjbsA1uU3nVzs7iW zkdaxnS8g4mopfWFOpkCqEIwH+DBZ8GDtoB2Q= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :cc:content-type:content-transfer-encoding; b=xBoZjBQkNuMmTwOuEO+TdaLiJHpoN0FyXsvk3BGP24t0SGQzb/eI/XzF6sM6C9cRvK mdKqY6tWnh2uAQsvKxjhf/LZfNPlVCDzoFFO+8MnGly0Os/BXBEpoG39uzbBgfmqfVR9 56jK1QffI3WqD0qVNPv3fIenvlzLB/aX5ZvJU= MIME-Version: 1.0 Received: by 10.224.19.131 with SMTP id a3mr2190818qab.23.1239906277917; Thu, 16 Apr 2009 11:24:37 -0700 (PDT) In-Reply-To: <20090416165842.GA432@uk.tiscali.com> References: <20090416165842.GA432@uk.tiscali.com> Date: Thu, 16 Apr 2009 11:24:37 -0700 Message-ID: Subject: Re: Tracking view updates with CouchDB From: Adam Wolff To: Brian Candler Cc: user@couchdb.apache.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable X-Virus-Checked: Checked by ClamAV on apache.org [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 =3D 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 wrote: > On Thu, Apr 16, 2009 at 09:21:59AM -0700, Adam Wolff wrote: >> So we've got docs that look like this: >> { >> =A0 type: "key_doc", >> =A0 _id : "xyzzy", >> =A0 key : "doc_key", >> =A0 updated : 1239896906303 >> } >> >> and docs that look like this: >> { >> =A0 type: "ref_doc", >> =A0 key_doc_id : "xyzzy", >> =A0 updated : 1239897055080 >> } >> >> and we want a view that we query like this: >> =A0 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) { > =A0var type =3D doc['type']; > =A0switch(type) { > =A0case 'key_doc': > =A0 =A0if (doc.key && doc.updated) { > =A0 =A0 =A0emit(doc.key, doc.updated); > =A0 =A0} > =A0 =A0break; > =A0case 'ref_doc': > =A0 =A0if (doc.key_doc_id && doc.updated) { > =A0 =A0 =A0emit(key_doc_id, doc.updated); > =A0 =A0} > =A0} > } > > Then your startkey..endkey view will show all update timestamps referring= to > this document. > > Here are some other tricks you could try: > > * emit(..., -updated) > > =A0Then a limit=3D1 query should give you the (negated) most recent > =A0timestamp > > * use a reduce function to find the maximum updated timestamp. I came acr= oss > =A0an example recently of how to do that, but can't remember exactly wher= e. > =A0It also showed how to reduce both the max and min values simultaneousl= y. > >> which reduces to the key_doc ids that were updated since the startkey >> date, in order, e.g. >> =A0 ["xyzzy"] in this case > > Maybe you want to emit(updated, doc.key) instead, so you can query your v= iew > for all changes made after a particular time. Then it's easy on the clien= t > side to do a uniq on all the doc refs seen. > > Regards, > > Brian. >