Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 49038 invoked from network); 10 Jun 2010 13:34:11 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 10 Jun 2010 13:34:11 -0000 Received: (qmail 44060 invoked by uid 500); 10 Jun 2010 13:34:09 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 43922 invoked by uid 500); 10 Jun 2010 13:34:09 -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 43914 invoked by uid 99); 10 Jun 2010 13:34:09 -0000 Received: from athena.apache.org (HELO athena.apache.org) (140.211.11.136) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 10 Jun 2010 13:34:09 +0000 X-ASF-Spam-Status: No, hits=2.8 required=10.0 tests=AWL,FREEMAIL_ENVFROM_END_DIGIT,FREEMAIL_FROM,SPF_PASS,T_TO_NO_BRKTS_FREEMAIL X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of elf2001@gmail.com designates 209.85.161.52 as permitted sender) Received: from [209.85.161.52] (HELO mail-fx0-f52.google.com) (209.85.161.52) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 10 Jun 2010 13:34:02 +0000 Received: by fxm4 with SMTP id 4so1557198fxm.11 for ; Thu, 10 Jun 2010 06:33:41 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:mime-version:received:received:in-reply-to :references:date:message-id:subject:from:to:content-type :content-transfer-encoding; bh=RRMTXaMHFUGidOHK/8L3qwYZ8aJZqLva0cADMHEIduo=; b=c8zvZ/9GFRKEZDujNxru/OClmiGgGnwVOk9L9+xkXUcFoejhLr39vJ1dvALsSAQJDH DfwnSSqDs5pSZFeYHcIClKEjHQPqPWnGKTF31Ia3EickLYNRnKkcQKx3TRnu7Uokx6aO WWbPu+lLwxH6NdnwPJOpplMOgO+Ng28R8QETU= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:date:message-id:subject:from:to :content-type:content-transfer-encoding; b=a3q175T2Lj2+aSlj4wrEJ24K9GdzxYCurg7rwEyRDJ9EQCjbyrVheWTFzoIgLJBwKd 4OvwP/iiNO/xobloC/Hhaj+MZ979fuNL/4kCQVssROllru3oG7O1dAikU/qMPRgKu9Eb ao36wtsaSRI7bOGefcIE+NHzFDX4yS0U/wZTQ= MIME-Version: 1.0 Received: by 10.204.83.101 with SMTP id e37mr168017bkl.48.1276176821215; Thu, 10 Jun 2010 06:33:41 -0700 (PDT) Received: by 10.204.98.12 with HTTP; Thu, 10 Jun 2010 06:33:41 -0700 (PDT) In-Reply-To: References: Date: Thu, 10 Jun 2010 16:33:41 +0300 Message-ID: Subject: Re: Calculating the sum of attachment sizes From: Elf To: user@couchdb.apache.org Content-Type: text/plain; charset=ISO-8859-1 Content-Transfer-Encoding: quoted-printable You may emit some of your group definitions as keys and size as value. Like view: fun(doc) { // attachments size calculations emit([doc.user, doc.folder], attachments_size) } then, reduce will be like fun(keys, values) { return sum(values); } And then, you may query view with argument group =3D true and group_level =3D 1 for calculate user-level space usage, or group_level =3D 2 for folder-level space usage. 2010/6/10 Moritz Post : > Hi CouchDB > > I want to calculate the sum of all attachments of a particular subset of > documents. Here is my approach: > > =3D=3D Map =3D=3D > > function(doc) { > =A0var localSum =3D 0; > =A0for ( var i in doc._attachments) { > =A0 =A0localSum +=3D doc._attachments[i].length > =A0} > =A0emit(doc._id, localSum); > } > > =3D=3D Reduce =3D=3D > > function(key, values) { > =A0return sum(values) > } > > This works fine when i run this code from within futon without providing = any > subset of docs. It returns a single result { null, > } > > But what i do in practice is using a POST on the view to provide the ids = of > documents of which totalsum i am interested in (eg 2 docs here): > > curl -d > '{"keys":["a813cded7cfa807c6c48ae1d84004d45","a813cded7cfa807c6c48ae1d840= 01823"]}' > -X POST > http://localhost:5984/mydb/_design/mydesign/_view/getAttachmentSize?group= =3Dtrue > > Here is the problem: I have to use group=3Dtrue when calling the view wit= h > POST parameters because it is a multi-key fetch (otherwise the couch > complains). In this results i get a separate result entry for each docume= nt > since the grouping keys are obviously different for each doc._id. So i ge= t > something like: > > "a813cded7cfa807c6c48ae1d84004d45", 68346 > "a813cded7cfa807c6c48ae1d84001823", 23755 > > but not the aggregated sum. > > I have to use the doc._id in the emit function though because otherwise t= he > filtering via the POST parameter would not limit the sum to the document > subset i specified. > > So i hope one understands the problem and is able to point me in the righ= t > direction. > > If their is any better way to calculate the sum of all attachments sizes = for > a subset of documents i would also be very interested. > > Thanks in advance > Moritz Post > --=20 ---------------- Best regards Elf mailto:elf2001@gmail.com