Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 3106 invoked from network); 10 Jun 2009 10:26:04 -0000 Received: from hermes.apache.org (HELO mail.apache.org) (140.211.11.3) by minotaur.apache.org with SMTP; 10 Jun 2009 10:26:04 -0000 Received: (qmail 95834 invoked by uid 500); 10 Jun 2009 10:26:15 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 95763 invoked by uid 500); 10 Jun 2009 10:26:15 -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 95753 invoked by uid 99); 10 Jun 2009 10:26:15 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 10 Jun 2009 10:26:15 +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 bchesneau@gmail.com designates 72.14.220.152 as permitted sender) Received: from [72.14.220.152] (HELO fg-out-1718.google.com) (72.14.220.152) by apache.org (qpsmtpd/0.29) with ESMTP; Wed, 10 Jun 2009 10:26:03 +0000 Received: by fg-out-1718.google.com with SMTP id l27so1449966fgb.3 for ; Wed, 10 Jun 2009 03:25:42 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:date:message-id:subject :from:to:content-type:content-transfer-encoding; bh=6UcGhRzRcXu/5Nik+go9wTdphvrLubSX9T/IiZxrOQk=; b=ku14MTM/8RMWkNBTuYysdG39WAuVVL/BBfsF7kPpoMsEDouNkW6Fc30SHBacW2OTxj UBO5mMOgUIr7/GKTjVdu/bee2X21oDSKBo5Pe4Dxe+fTMlaVQ+4mnU+BUk3pwc7vDQla vZVC/ccHWfTnNMEyFE7EZqE3cZ8NLsyNwHgKI= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:date:message-id:subject:from:to:content-type :content-transfer-encoding; b=pDkueG3MAz7Map2xHfFUK+eT/YOp5OZfMVL1jPmP4mWVzk4IgP953bD7LmkmVVgx5C g7JX9VrBqLfn8Q0fxt3dGz8r+WcVNHa7/M9MnIRYr3pfJ1mbkC2S/YK5P/15V8i5z20J bBwxkceA6kCLeK7TmQOMyd4MBZSJ4Hc+zjst0= MIME-Version: 1.0 Received: by 10.86.51.10 with SMTP id y10mr999811fgy.9.1244629541949; Wed, 10 Jun 2009 03:25:41 -0700 (PDT) Date: Wed, 10 Jun 2009 12:25:41 +0200 Message-ID: Subject: calulate doc popularity with couchdb map/reduce From: Benoit Chesneau To: 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 Hi all, I'm trying to calculate popularity of a doc wiyth couchdb but for now have "reduce_overflow_error" error. What i do is saving each vote as a doc : { docid: id of document where vote happened, v : value of doc, d: date when docid have been created } Then I do map/reduce over it : map : function(doc) { if (doc.type =3D=3D "vote") { emit(doc.itemid, {d: doc.d, v: doc.v}); } } reduce : function(keys, values, rereduce) { // !code vendor/couchapp/date.js /* we use reddit algorithm to calculate scores http://blog.linkibol.com/post/How-to-Build-a-Popularity-Algorithm-You-can= -be-Proud-of.aspx http://news.ycombinator.com/item?id=3D231168 */ // all started at this date var scores =3D {}; var points =3D {}; if (!rereduce) { var B =3D new Date("Thu May 28 11:16:49 2009 +0200").valueOf(); for (var k in keys) { if (!scores[keys[k][0]]) { scores[keys[k][0]] =3D 0; points[keys[k][0]] =3D { A: new Date().setRFC3339(values[k].d).valueOf(), x: 0 } } var t =3D points[keys[k][0]].A - B; points[keys[k][0]].x +=3D values[k].v; if (points[keys[k][0]].x > 0) { y =3D 1; } else if (points[keys[k][0]].x =3D=3D 0) { y =3D 0; } else { y =3D -1; } z =3D (Math.abs(points[keys[k][0]].x) >=3D1 && points[keys[k][0]].x = || 1); scores[keys[k][0]] =3D Math.log(z) + (y*t)/45000; } lastkey =3D keys[keys.length-1][0]; } else { scores =3D values[0]; for(var v =3D 1; v < values.length; v++) { for(var s in values[v]) { if (scores[s]) scores[s] +=3D values[v][s]; else scores[s] =3D values[v][s]; } } } var top =3D []; for (k in scores) top[top.length] =3D [k, scores[k]]; top.sort(function(a, b) { return a[1] - b[1] }); for(var n =3D 20; n < top.length; n++) if(top[n][0] !=3D lastkey) scores[top[n][0]] =3D undefined; return scores; } Is this kind of reduce ok with couchdb ? The other solution would be saving scores without sorting them in a temporary db and make another map/reuce over it. Maybe I should go that way, but I wonder if somepone have a better idee ? - beno=EEt