0

Helo Folks,

I am working on a view in couchdb. And, in the 'extract' list function, I'm trying to filter out some information using that view (myView). From the client that connects to couchdb, I want to do 1 major thing - show the results from the 'extract' list function. But, there are multiple other things that I want to perform on the results returned from the 'extract' function. One simple operation out of all the other operations is 'sum'. But, there are many other features like calculating median/standard deviation etc on the results of the 'extract' list function.

{
   "_id": "_design/myDesigndoc",
   "lists": {
       "extract": "function(head, req){ ...*extract some info the view*: **myView** ...}",
       "sum" : "function(head,req) {...**sum up all the values returned from the 'extract' function above**...}"
    },

    "views": {
        "myView" : { "map" : "..." },
    }
}

So, I'm stuck at one point:-

As the whole design doc is a Json and the function bodies are javascript, is there a way to call the 'extract' list function in other list functions like 'sum', 'median', 'standard deviation' etc ?

Reason I want to do this:-

All the other list functions: 'sum', 'standard deviation' etc expect the return value of 'extract' function as input. So, just making redundant copies of the code of extract function in other list functions is the last thing I would want to do.

Is there an alternate way to solve this:-

Yes, there is a way. I had thought that I'll use another view functions than 'myView' for all these functionalities and write the same 'map' function as that in 'myView' but, all these views will have separate 'reduce' functions for calculating 'sum', 'standard dev' etc. But, the calculation of those views caused a lot of resource usage because those many views were getting created each time.

Could you guys provide a better solution than this?

Thanks

GodMan
  • 2,561
  • 2
  • 24
  • 40
  • It sounds to me like you should abstract the logic of `extract` and `sum` into functions that you can include via [CommonJS](http://wiki.apache.org/couchdb/CommonJS_Modules) and call in your _list function. – Dominic Barnes May 20 '13 at 13:34

1 Answers1

0

My first thought was to implement the views again with reduce functions to do the calculations but you say this is too resource intensive.. I wonder how often are the views used and if there is a heap of changes between accesses?

If they are just used to produce some statistics for reports or something and are rarely accessed that when they do there is a heap of changes it needs to make to the view indexes maybe you could look at running a script that regularly retrieves the views so it keeps the views up to date so when they are accessed they still respond relatively quickly.

This is something we have done with our all of our views in our production environment and it works quite well, I guess it depends on your infrastructure and how much data you are pumping through.

Something else to consider I am not sure if there is any difference/benefit to doing so but maybe the built in reduce functions may offer better performance than your self created ones

http://wiki.apache.org/couchdb/Built-In_Reduce_Functions

Dan675
  • 1,697
  • 15
  • 15