From user-return-20413-apmail-couchdb-user-archive=couchdb.apache.org@couchdb.apache.org Wed Apr 11 20:12:08 2012 Return-Path: X-Original-To: apmail-couchdb-user-archive@www.apache.org Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: from mail.apache.org (hermes.apache.org [140.211.11.3]) by minotaur.apache.org (Postfix) with SMTP id 5A0F49834 for ; Wed, 11 Apr 2012 20:12:08 +0000 (UTC) Received: (qmail 77016 invoked by uid 500); 11 Apr 2012 20:12:06 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 76981 invoked by uid 500); 11 Apr 2012 20:12:06 -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 76973 invoked by uid 99); 11 Apr 2012 20:12:06 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 11 Apr 2012 20:12:06 +0000 X-ASF-Spam-Status: No, hits=-0.7 required=5.0 tests=RCVD_IN_DNSWL_LOW,SPF_PASS X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of matthieu.rakotojaona@gmail.com designates 209.85.212.176 as permitted sender) Received: from [209.85.212.176] (HELO mail-wi0-f176.google.com) (209.85.212.176) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 11 Apr 2012 20:12:01 +0000 Received: by wibhm17 with SMTP id hm17so3682540wib.5 for ; Wed, 11 Apr 2012 13:11:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20120113; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; bh=nKh34Fx4GXWU5EW2cdeRpxSUKg0TqlddRr+f4UcMZDE=; b=YbHtLKE5s4wS7Dz6qY/WiU6PQlIMwgS1i5PV2Q8oAlQPgIOp8y/mvCwDKBYb73Lg68 MAf4oSYFA9b5Qpo+i8DMXmWTqLUQITLhSkxqqUfnXDwPipO0ku9Y3lxoG/CWJE1EzBD2 TO1wqdNq0j9HuWyy7dNJQd7UWt7EqzXDJajlOmSg6YlLmcyqYrxnwnJKGHlW7WwvPMyB QAv9DAWMOA1jRDH0o52s1j5P0uDIp3+bCkL0CxrsY2n4GtNe438CaUNVCv65VQymNNkc duTrsofaoJmI0ycp2HCUF116vpX3pTbyZQLLYdff2Z2uzVTIlEnb8iUUlwHpeqO9TXvU u6nA== Received: by 10.180.102.129 with SMTP id fo1mr19140821wib.6.1334175100190; Wed, 11 Apr 2012 13:11:40 -0700 (PDT) MIME-Version: 1.0 Received: by 10.223.144.133 with HTTP; Wed, 11 Apr 2012 13:11:19 -0700 (PDT) In-Reply-To: References: <4A256A62-EC51-4FBB-98B3-1638BBEA674D@utt.fr> From: Matthieu Rakotojaona Date: Wed, 11 Apr 2012 22:11:19 +0200 Message-ID: Subject: Re: View cache invalidation To: user@couchdb.apache.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable X-Virus-Checked: Checked by ClamAV on apache.org On Wed, Apr 11, 2012 at 9:24 PM, Julien Dreux wrote: > function(doc) { > =C2=A0if(doc.businessHours) { > =C2=A0 =C2=A0var daysArr =3D new Array('s','m','t','w','h','f','a'); > =C2=A0 =C2=A0var day =3D daysArr[new Date().getDay()]; > =C2=A0 =C2=A0emit(parseInt(doc.businessHours[day][0].indexOf('0') =3D=3D = 0 ? > doc.businessHours[day][0].substring(1) : doc.businessHours[day][0]), doc)= }; > } > > Thank you for your help. > * > * > *JULIEN DREUX* > jdreux@justlexit.com > 514 812-8084 > www.justlexit.com > As said before, views (both map and reduce) MUST be absolutely context-independent. There is only ONE thing a map function can depend on : the document being processed. It cannot even rely on other documents, or the version of your couchdb, or whatever. If you need this information, include it in your doc. I get from your view definition that you would like to ask your database for the opening hours of your businesses for today, right ? I will suppose you do so. A correct way to implement way would be like that : - for each day in the 'businessHours' array of the doc, emit a pair. The key should be the day from the array, the value would be the opening hour as processed in your emit (removing the leading 0 if it exists) - You would then have multiple key/value pairs by document. - At query time, use 'key=3D' as an argument, like 'key=3D"s"'. - Bonus : do not emit the doc in a map. If you need it, you can add 'include_docs=3Dtrue' to the arguments at querying time, and you will be able to access it along the data you emitted when defining the view. This will prevent you from storing the doc twice. Proposed (untested) implementation : function(doc){ var daysArr =3D new Array('s','m','t','w','h','f','a'); var i=3D0; for (i =3D 0; i < daysArr.length; i++){ var day =3D daysArr[i]; var openingHour =3D doc.businessHours[day][0].indexOf('0') =3D=3D 0 ? doc.businessHours[day][0].substring(1) : doc.businessHours[day][0]); emit(day, openingHour); } } Query would look like this for today (wednesday) : GET /path/to/view?key=3D"w"&include_docs=3Dtrue --=20 Matthieu RAKOTOJAONA