couchdb-user mailing list archives

Site index · List index
Message view « Date » · « Thread »
Top « Date » · « Thread »
From "Paul Davis" <>
Subject Re: view reduce and updating documents
Date Tue, 16 Dec 2008 20:30:15 GMT
This article is pretty good at explaining the update mechanism:

On Tue, Dec 16, 2008 at 3:18 PM, Dusty Leary <> wrote:
> Hi,
> I'm wondering about the 'reduce' part of views and how updating a
> document is handled.
> I think I understand how arbitrary views work, and how they can be
> stored in a B-tree, and an update is 'cheap'...
> I suppose it's:
> A)  Drop old documents from views when the document is updated.
> B) Re-run the view function on all new documents when the view is re-read.
> First, is this correct?

The right idea, but the actual implementation combines these steps.
There are some properties of the btree implementation that show up in
a fairly elegant but non-obvious way. What's technically happening is
that CouchDB iterates over the update_seq data to find documents that
have changed. If a document is deleted, then the output of the map can
be thought of as {deleted_doc_id, []} or, zero results for that docid.
Updated and created docs are actually passed to the js function for

The results are then just written to the btree. The append only nature
of btrees means that a process can write to the file while not
clobbering the old version of the btree which other people may be
reading. Readers will pick up the newer btree version the next time
the read the file header.

> Second:  In the face of a reduce function, how does this work?  It
> seems like in the general case, you need to rebuild the entire view,
> since you can't "remove" the effects of a single document from the
> reduce.
> Unless maybe the reduce function is rerun on every view?
> Or, partial reduce intermediates are cached, maybe in a tree?

Another facet of the append only btree and the nifty way that damien
added storing partial reduce results. I only have a vague idea of how
this works mostly coming from the link I posted. I'm pretty sure the
interpretation is that you basically have a 'shadow' tree that uses
the btree leaf nodes. Then when your map changes a leaf node and you
go to read the reduce tree, it detects any new leaf nodes (append
only!) and re-runs the reduce up to the root. Then again, I haven't
actually groked the code on this so I could be way off. :D

> -Dusty

Paul Davis

View raw message