couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Moritz Post <moritzp...@gmail.com>
Subject Re: Calculating the sum of attachment sizes
Date Thu, 10 Jun 2010 14:10:02 GMT
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 <elf2001@gmail.com> 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 <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
  • Unnamed multipart/alternative (inline, None, 0 bytes)
View raw message