From user-return-11172-apmail-couchdb-user-archive=couchdb.apache.org@couchdb.apache.org Mon Jun 28 20:52:24 2010 Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 57621 invoked from network); 28 Jun 2010 20:52:23 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 28 Jun 2010 20:52:23 -0000 Received: (qmail 91968 invoked by uid 500); 28 Jun 2010 20:52:22 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 91919 invoked by uid 500); 28 Jun 2010 20:52:21 -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 91911 invoked by uid 99); 28 Jun 2010 20:52:21 -0000 Received: from nike.apache.org (HELO nike.apache.org) (192.87.106.230) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 28 Jun 2010 20:52:21 +0000 X-ASF-Spam-Status: No, hits=0.0 required=10.0 tests=FREEMAIL_FROM,RCVD_IN_DNSWL_NONE,SPF_PASS,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: apache.org Received-SPF: pass (nike.apache.org: local policy) Received: from [212.27.42.2] (HELO smtp2-g21.free.fr) (212.27.42.2) by apache.org (qpsmtpd/0.29) with ESMTP; Mon, 28 Jun 2010 20:52:14 +0000 Received: from zimbra5-e1.priv.proxad.net (unknown [172.20.243.155]) by smtp2-g21.free.fr (Postfix) with ESMTP id B1DAC4B001D for ; Mon, 28 Jun 2010 22:51:49 +0200 (CEST) Date: Mon, 28 Jun 2010 22:51:48 +0200 (CEST) From: mickael.bailly@free.fr To: user@couchdb.apache.org Message-ID: <1337936121.1779181277758308298.JavaMail.root@zimbra5-e1.priv.proxad.net> In-Reply-To: <50802023.1778881277758206066.JavaMail.root@zimbra5-e1.priv.proxad.net> Subject: Re: selecting a random subset of a view MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Originating-IP: [88.161.108.151] X-Mailer: Zimbra 5.0 (ZimbraWebClient - SAF3 (Mac)/5.0.15_GA_2815.UBUNTU8_64) X-Authenticated-User: mickael.bailly@free.fr X-Virus-Checked: Checked by ClamAV on apache.org I find it a really great idea. If it can be set per design doc, of course. Mickael ----- Mail Original ----- De: "J Chris Anderson" =C3=80: user@couchdb.apache.org Envoy=C3=A9: Lundi 28 Juin 2010 21h34:16 GMT +01:00 Amsterdam / Berlin / Be= rne / Rome / Stockholm / Vienne Objet: Re: selecting a random subset of a view On Jun 28, 2010, at 12:28 PM, mickael.bailly@free.fr wrote: > It's an error to do this if I read the doc correctly, because shows and l= ists functions should be idempotent. It's specified in the wiki. >=20 It would be fine to have a config option to suppress the Etags on show and = list. Then you'd have no risk of improperly caching the output here. For th= e time being, you can probably configure an http proxy to ignore and strip = the etags. Chris > Mickael >=20 > ----- Mail Original ----- > De: "Jan Prieser" > =C3=80: user@couchdb.apache.org > Envoy=C3=A9: Lundi 28 Juin 2010 18h37:38 GMT +01:00 Amsterdam / Berlin / = Berne / Rome / Stockholm / Vienne > Objet: Re: selecting a random subset of a view >=20 > hi Mickael, >=20 > i've had the same problem and used the lists-feature from couch. >=20 > my list-function looks like this: >=20 > function(head,req) { > function shuffle(ary) { > function randOrd(){ return Math.round(Math.random()) - 0.5; } > ary.sort( randOrd ); > } > body=3D{}; > eval('body=3D'+req.body); > out =3D head; > out.rows =3D []; > if(out.total_rows > out.offset) { > while (row =3D getRow()) { > out.rows.push(row); > } > } > shuffle(out.rows); > if (body && body.rlimit) { > out.rows =3D out.rows.slice(-body.rlimit); > } > return toJSON(out) + '\n'; > } >=20 > maybe you could use a range with startkey and endkey, if the number of=20 > rows is to big. I didn't test the performance with bigger datasets. >=20 >=20 > Am 28.06.2010 15:29, schrieb mickael.bailly@free.fr: >> Hello couchers, >>=20 >> how would you do to select a random subset of a view result (a simple vi= ew with map only). >>=20 >> Example (I don't write the full view response array for clarity) >>=20 >> When called normally, my view returns : >>=20 >> { >> ... >> rows: [ >> {id: aa1}, >> {id: aa2}, >> {id: aa3}, >> {id: aa4}, >> {id: aa5}, >> {id: aa6}, >> {id: aa7}, >> {id: aa8}, >> {id: aa9} >> ] >> } >>=20 >> And I want only three of those rows, randomly chosen. So I launch the ma= gic "get three random rows" feature, and it gives me : >>=20 >> { >> ... >> rows: [ >> {id: aa5}, >> {id: aa3}, >> {id: aa6} >> ] >> } >>=20 >> The second time I launch the same magic "get three random rows" I got: >> { >> ... >> rows: [ >> {id: aa7}, >> {id: aa1}, >> {id: aa5} >> ] >> } >>=20 >> Thanks for your advices >>=20 >> Mickael