Return-Path: Delivered-To: apmail-incubator-couchdb-user-archive@locus.apache.org Received: (qmail 99472 invoked from network); 30 Apr 2008 13:26:24 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.2) by minotaur.apache.org with SMTP; 30 Apr 2008 13:26:24 -0000 Received: (qmail 92300 invoked by uid 500); 30 Apr 2008 13:26:25 -0000 Delivered-To: apmail-incubator-couchdb-user-archive@incubator.apache.org Received: (qmail 92269 invoked by uid 500); 30 Apr 2008 13:26:25 -0000 Mailing-List: contact couchdb-user-help@incubator.apache.org; run by ezmlm Precedence: bulk List-Help: List-Unsubscribe: List-Post: List-Id: Reply-To: couchdb-user@incubator.apache.org Delivered-To: mailing list couchdb-user@incubator.apache.org Received: (qmail 92258 invoked by uid 99); 30 Apr 2008 13:26:25 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 30 Apr 2008 06:26:25 -0700 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: local policy) Received: from [212.100.226.88] (HELO mail.c360uk.com) (212.100.226.88) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 30 Apr 2008 13:25:40 +0000 Received: from localhost ([127.0.0.1]) by mail.c360uk.com (Kerio MailServer 5.7.9) for couchdb-user@incubator.apache.org; Wed, 30 Apr 2008 14:25:50 +0100 Message-ID: <4818735F.60705@tangentlabs.co.uk> Date: Wed, 30 Apr 2008 14:25:51 +0100 From: Jonathan Moss Organization: Tangent Labs User-Agent: Thunderbird 2.0.0.12 (X11/20080227) MIME-Version: 1.0 To: couchdb-user@incubator.apache.org Subject: Re: efficiency of temporary views References: <75bda7a00804300406x2758ce72qbf653992b6ae7c14@mail.gmail.com> <495F96C8-9C0E-43E0-A083-0901A5649058@apache.org> <48186336.2000500@tangentlabs.co.uk> In-Reply-To: X-Enigmail-Version: 0.95.0 Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: 7bit X-Virus-Checked: Checked by ClamAV on apache.org Jan Lehnardt wrote: > Hi, > On Apr 30, 2008, at 14:16, Jonathan Moss wrote: >> Greetings all, >> >> I have been lurking for a couple of weeks now and am about to embark on >> little project using couch DB and have a couple of questions. >> >> I am trying to come up with a flexible way to model an object hierarchy. >> My current thoughts run something like >> >> >> { >> _id: xxx >> _rev: yyy >> type: something >> n.e.other: foo >> parents: ["xxx","xxx"] >> children: ["yyy","yyy"] >> } >> >> so a simple view (pseudo-code) to get all the children of an object >> (with id = 123) would be: >> >> function(doc): >> if(doc.parents contains "123"){ >> map(doc._id,doc); >> } >> } >> >> Obviously this kind of view cannot be persisted as the value if id would >> need to change for every document in the DB. >> >> Would this be terribly in-efficient as it would have to be a temporary >> view or am I missing a trick? >> Could I do something cunning with the key >> field in the map function and the start/end_key get params? > > Yeah :) > Do: > > function(doc) { > for(var idx in doc.parents) { > map(doc.parents[idx], doc); > } > } > > and then you can use the startkey parameters to get all docs > that have the parent "123". The Same for children and and > anything else you need. > > >> This also extends to the question of how to deal with running >> getChildren and only returning those of a specific 'type' e.g. >> >> if(doc.type = 'atype' && doc.parents contains "123"){ >> ... >> } > > map([doc.parent[idx], doc.type], doc); > > You can have complex JSON structures as the key that > allows you to collate by more than one attribute. > > Don't be shy of adding more views :) > > Beware though, at the moment you store the full doc > in the view, effectively doubling data. If that is okay > for you application and amount of data, never mind. > If you want to be a tad more conservative, store NULL > as the map value and retrieve only the document ids > from the view and the document data with subsequent > requests. Trade-offs and all that. > > > Cheers > Jan > -- > > Thanks Jan, That certainly makes it easier. I think I understand your warning regarding doubling of the data. Essentially a results of a view are pre-calculated when the view is save? So using the id's to then retrieve the related object would be a lot more efficient in terms of storage. The trade off being multiple requests to get the actual objects? Would it be possible to retrieve all the documents in one request. If the id's where continuous it would be easy to use start_key and end_key but what if they are dis-continuous? Thanks again, Jon