incubator-couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From Jan Lehnardt <...@apache.org>
Subject Re: Tree structure: how to calculate branch node sum, using results from a view that calculates leaf node sums.
Date Thu, 03 Sep 2009 12:53:19 GMT

On 3 Sep 2009, at 14:35, Metin Akat wrote:

> I am writing an accounting application.
> Generally, so far my design is as follows:
>
> 1. Account document.
>
> 2. Transaction document. It has kind of the usual structure...
> "debit", "credit", "qty", "value" etc.
> Here "debit" and "credit" are two account UUIDs
>
> I have a view with a reduce, that emits all the transactions for each
> account and calculates the account balance.
> Something quite similar to what is described here:
> http://books.couchdb.org/relax/receipts/banking
>
> The accounts are a tree structure. Every account can have child
> accounts. And transactions are only executed against
> accounts that have no children (leaf nodes).
> So far I have no problems in calculating the balance of the leaf  
> nodes.
>
> Every node in the tree has a property called "lineage" that is a list
> with its "path" in the tree.
>
> So what I'm trying to do now is to calculate the balance of the branch
> nodes across the tree.
>
> One way is to do it client side (fetch all the leafs (view collation)
> that belong to some branch and calculate the balance in my
> application).
> This won't be a great problem, because I don't really believe that
> I'll have more than several hundred leafs altogether.
> Though I'm still interested to find out if there is some "cool" way to
> do it with couchdb.

query your reduce view with group_level=1 (or 2 or 3 .. depending on  
how many level you want to descend into your tree). Your "path" to the  
root node should be an array as in: ["toplevel", "firstlevel",  
"secondlevel"] as the key of your view.

Cheers
Jan
--



Mime
View raw message