View changes #1

wants to merge 15 commits into


None yet

1 participant

benoitc commented Mar 23, 2012

Pull request to track the view changes branch.

benoitc added some commits Feb 22, 2012
@benoitc benoitc Add `couch_mrview:view_changes_since` function.
This functions fetch all changes in a view since last update like the
`couch_db:changes_since` function for the databases. It returns an error
if the option `seq_indexed` hasn't been set to `true` in the design

        "_id": "_design/somedoc",
        "options": {
            "seq_indexed": "true"
        "views": {
            "someview": {
                "map": "function(doc) { if (doc.field) emit(doc.field, null); }"

This is done by adding an index to the view group (a view group is the
global index associated to a design doc basically). This index associate
each sequences & documents ID to a view . `{{ViewId, Seq}, DocId} is
stored in the seq_btree, DocId is the value.

example of usage:

    (refuge@> F = fun(KV, Acc) -> {ok, [KV|Acc]} end.
    (refuge@> couch_mrview:view_changes_since(Db, DDoc,
<<"all">>, 9990, F).
    [info] [<0.220.0>] Opening index for db: testdb idx: _design/test
    (refuge@> couch_mrview:view_changes_since(Db, DDoc,
<<"all">>, 10000, F).
    (refuge@> couch_mrview:view_changes_since(Db, DDoc,
<<"all">>, 10000, F).
@benoitc benoitc `_view` filter is now hanled by couch_mrview:view_changes_since/7
Rather than passing all doc sent y couch_db:changes_since/5 to a
transformed view function we are now using the more efficient
`couch_mrview:view_changes_since/7` wich use the seq btree in the view

Also add the parameters `view_filter=DName/FilterName` which allows to
filter any view changes using the fiter function `FilterName` from the
design document `DName`:

   GET http://<node>/<dbname>/_changes?filter=_view&view=<viewname>&view_filter<filtername>

Where variables are:

- <node>: URL of the CouchDB node
- <dbname>: Name of the database
- <viewname>: DesignId/ViewNameThe name of the view in the views properties
  from the design document DesignId`
- <filtername>: DesignId1/FilterName The name of the filter in
  the filters properties from the design document DesignId1

The difference with the old behaviour is that you really accessing the
view index instead of passing each map function to all the changed docs
wich is a way more efficient (you do'nt open a system process, serialize
the function, pass it to the process, wait for return, test it and
eventually return a change). You can also filter the results. Other
advantages are that the views are refreshed in real time when the
database content changes and the view changes are only triggered if the
index is updated.
@benoitc benoitc Get view changes using a view query.
Add the possibility to the function `couch_mrview:view_changes_since` to
accept some **view query parameters** like `key`, `start_key` &
`end_key`. Instead of fetching all changes in a view from last updated
it will fetch changes only from the results of the query.  It returns an
error  if the option `seq_indexed` hasn't been set to `true` in the
view options:

            "_id": "_design/somedoc",
            "views": {
                "someview": {
                    "map": "function(doc) { if (doc.field)
emit(doc.field, null); }",
                    "options": {
                        "seq_indexed": "true"

This is done by adding an index (b-tree) to each view. This index
associate the view key with the doc sequence. `{{Key, Seq}, {DocId, Value}}`
is stored in this btree.
@benoitc benoitc add support for `include_deleted: true` option in a design document.
This option allows a view to map deleted documents.

Remember that documents deleted with the DELETE HTTP verb will look like
{_id: id, _rev: rev, _deleted: true} to the indexer. If you want to
store extra data on the deleted document you can use _bulk_docs or
updating a document with the member '_deleted: true' using the HTTP verb
@benoitc benoitc fix _changes test
The `_view` filter now need a view group indexed by sequences.
@benoitc benoitc fix imports fce9e24
@benoitc benoitc change order 37efe00
@benoitc benoitc fix include: 950bde5
@benoitc benoitc Revert "fix include:"
This reverts commit ed0076d3fdbf2b2cf28acb204f48fe07db65440f.
@benoitc @benoitc benoitc fix purge 93e1987
@benoitc benoitc filter fields in included doc when returned from a _changes
With this changes you can only return a specified list of fields in the
included docs by passing the list of them to an optionnal `fields`
parameter. For now nested fields are ignored.

    $ curl -XPUT localhost:5984/testdb
    $ curl -XPUT localhost:5984/testdb/test -d'{"f1": 1, "f2": 2, "f3":
    $ curl -XPUT localhost:5984/testdb/test1 -d'{"f1": 1, "f2": 4, "f3":
    $ curl -XPUT localhost:5984/testdb/test2 -d'{"f1": 1, "f2": 7, "f3":
    $ curl "http://localhost:5984/testdb/_changes?include_docs=true"
    $ curl

    $ curl
benoitc added some commits May 16, 2012
@benoitc benoitc add a test for changes eventsource feed. 16db793
@benoitc benoitc fix whitespaces 4c9bafc
@benoitc benoitc make sure the changes test works on all browsers 56bb344
@benoitc benoitc Merge branch 'master' into view_changes
benoitc commented Jun 22, 2012

applied in las head

@benoitc benoitc closed this Jun 22, 2012
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment