incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From mickael.bai...@free.fr
Subject Re: selecting a random subset of a view
Date Mon, 28 Jun 2010 19:28:22 GMT
It's an error to do this if I read the doc correctly, because shows and lists functions should
be idempotent. It's specified in the wiki.

Mickael

----- Mail Original -----
De: "Jan Prieser" <j.prieser@hotornot.de>
À: user@couchdb.apache.org
Envoyé: Lundi 28 Juin 2010 18h37:38 GMT +01:00 Amsterdam / Berlin / Berne / Rome / Stockholm
/ Vienne
Objet: Re: selecting a random subset of a view

hi Mickael,

i've had the same problem and used the lists-feature from couch.

my list-function looks like this:

function(head,req) {
   function shuffle(ary) {
     function randOrd(){ return Math.round(Math.random()) - 0.5; }
     ary.sort( randOrd );
   }
   body={};
   eval('body='+req.body);
   out = head;
   out.rows = [];
   if(out.total_rows > out.offset) {
     while (row = getRow()) {
         out.rows.push(row);
     }
   }
   shuffle(out.rows);
   if (body && body.rlimit) {
     out.rows = out.rows.slice(-body.rlimit);
   }
   return toJSON(out) + '\n';
}

maybe you could use a range with startkey and endkey, if the number of 
rows is to big. I didn't test the performance with bigger datasets.


Am 28.06.2010 15:29, schrieb mickael.bailly@free.fr:
> Hello couchers,
>
> how would you do to select a random subset of a view result (a simple view with map only).
>
> Example (I don't write the full view response array for clarity)
>
> When called normally, my view returns :
>
> {
> ...
> rows: [
> {id: aa1},
> {id: aa2},
> {id: aa3},
> {id: aa4},
> {id: aa5},
> {id: aa6},
> {id: aa7},
> {id: aa8},
> {id: aa9}
> ]
> }
>
> And I want only three of those rows, randomly chosen. So I launch the magic "get three
random rows" feature, and it gives me :
>
> {
> ...
> rows: [
> {id: aa5},
> {id: aa3},
> {id: aa6}
> ]
> }
>
> The second time I launch the same magic "get three random rows" I got:
> {
> ...
> rows: [
> {id: aa7},
> {id: aa1},
> {id: aa5}
> ]
> }
>
> Thanks for your advices
>
> Mickael

Mime
View raw message