Return-Path: Delivered-To: apmail-couchdb-user-archive@www.apache.org Received: (qmail 61214 invoked from network); 10 Jun 2010 14:10:31 -0000 Received: from unknown (HELO mail.apache.org) (140.211.11.3) by 140.211.11.9 with SMTP; 10 Jun 2010 14:10:31 -0000 Received: (qmail 14729 invoked by uid 500); 10 Jun 2010 14:10:30 -0000 Delivered-To: apmail-couchdb-user-archive@couchdb.apache.org Received: (qmail 14545 invoked by uid 500); 10 Jun 2010 14:10:30 -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 14537 invoked by uid 99); 10 Jun 2010 14:10:30 -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 14:10:29 +0000 X-ASF-Spam-Status: No, hits=3.1 required=10.0 tests=AWL,FREEMAIL_FROM,FREEMAIL_REPLY,HTML_MESSAGE,NORMAL_HTTP_TO_IP,SPF_PASS,T_TO_NO_BRKTS_FREEMAIL,WEIRD_PORT X-Spam-Check-By: apache.org Received-SPF: pass (athena.apache.org: domain of moritzpost@gmail.com designates 209.85.214.180 as permitted sender) Received: from [209.85.214.180] (HELO mail-iw0-f180.google.com) (209.85.214.180) by apache.org (qpsmtpd/0.29) with ESMTP; Thu, 10 Jun 2010 14:10:23 +0000 Received: by iwn38 with SMTP id 38so2166044iwn.11 for ; Thu, 10 Jun 2010 07:10:03 -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; bh=n/aP67QNXruScmG+rTjtnRIuQq+0IAfI8u4eEDOHzyU=; b=TNYtN4ZBnYLKFWYtQbONjpxijC+cqpg5Okin20cBQ/YoET/psgBI6ve2z65EMw6h5x 1aXm39o7BMON/lVDStFJ/HEx1r60uu23UbUNnHCXI6aKCDY9LNz7Z6krLe6HvCoTwic3 sEpyL1MZUCA6q6flLTn3WII/O5Qaw0IHmkiQA= 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; b=SgtYdoJSJTpZ4G/ZREI2JgZk+WducSnodRqyTFOPQQPJwIJZfvmBa2STcgpTFZklbI j9Qwms/+oQudRAwLyF4g3tpGBrWJkQ5kJsRmFTU97ORaXfYYuASMyha5T+4TiVdQdSuh BYIuiA/ZFmU++x7j3JwMWaTo8ezniITLFSw04= MIME-Version: 1.0 Received: by 10.231.141.26 with SMTP id k26mr156905ibu.163.1276179002738; Thu, 10 Jun 2010 07:10:02 -0700 (PDT) Received: by 10.231.143.18 with HTTP; Thu, 10 Jun 2010 07:10:02 -0700 (PDT) In-Reply-To: References: Date: Thu, 10 Jun 2010 16:10:02 +0200 Message-ID: Subject: Re: Calculating the sum of attachment sizes From: Moritz Post To: user@couchdb.apache.org Content-Type: multipart/alternative; boundary=0016e65097f020c9af0488ad9443 --0016e65097f020c9af0488ad9443 Content-Type: text/plain; charset=ISO-8859-1 Hi Elf Thanks for answering. I have considered your advice by introducing a preceding element in my emit key which is is the same in every document. and when i use the group_level = 1 which than only takes the first element in the key list into account the aggregation of the sizes works with group=true. That is in futon and a GET request. Back in the real world where i use POST to add the filter elements it does not work. I issue the following command: curl -d '{"keys":[["leaflet","d947fce9711cb624e2df06e081004b1f"],["leaflet","d947fce9711cb624e2df06e081002c2b"]]}' -X POST http://192.168.87.1:5984/mydb/_design/mydb/_view/getAttachmentSize?group=true&group_level=1 You can see that in included a first parameter "leaflet" which i can use to group by when setting the group_level to only 1. (I might add that this does not feel good. Adding some dummy element in my query is awkward.) Anyhow... What i get as result is still: {"rows":[ {"key":["leaflet","d947fce9711cb624e2df06e081004b1f"],"value":23755}, {"key":["leaflet","d947fce9711cb624e2df06e081002c2b"],"value":31984} ]} You see that the elements are not grouped by the first level key. Also the CURL command does not return and i need to hit ctrl+c manually to get back to the shell. The log says 200 though. So is there anything else i need to consider when using group in a POST scenario? Thanks Moritz Post On Thu, Jun 10, 2010 at 3:33 PM, Elf wrote: > 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 = true and > group_level = 1 for calculate user-level space usage, or group_level = > 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: > > > > == Map == > > > > function(doc) { > > var localSum = 0; > > for ( var i in doc._attachments) { > > localSum += doc._attachments[i].length > > } > > emit(doc._id, localSum); > > } > > > > == Reduce == > > > > function(key, values) { > > return 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","a813cded7cfa807c6c48ae1d84001823"]}' > > -X POST > > > http://localhost:5984/mydb/_design/mydesign/_view/getAttachmentSize?group=true > > > > Here is the problem: I have to use group=true when calling the view with > > POST parameters because it is a multi-key fetch (otherwise the couch > > complains). In this results i get a separate result entry for each > document > > since the grouping keys are obviously different for each doc._id. So i > get > > 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 > the > > 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 > right > > 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 > > > > > > -- > ---------------- > Best regards > Elf > mailto:elf2001@gmail.com > --0016e65097f020c9af0488ad9443--