couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Elf <elf2...@gmail.com>
Subject Re: Calculating the sum of attachment sizes
Date Thu, 10 Jun 2010 13:33:41 GMT
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 <moritzpost@gmail.com>:
> 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,
> <totalSumOfAllAttachments>}
>
> 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

Mime
View raw message