Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

couchdb change notifications

Tags:

couchdb

What does the changes API actually do? does it list the last revision of each document + deleted documents?

or to put it this way:

Can the change notifications feature of couchdb be used to list all the documents that match a query?

For example, if I have the filter function:

search = function(doc,req)
{
  return (doc.min && doc.max && doc.min < req.query.q && doc.max > req.query.q)
}

will I get all the documents that match doc.min < somevalue < doc.max here?

http :// server / database / _changes ? filter=doctype/search & q=somevalue

For my test database it appears to be so, but what if I have a large database?

like image 358
Alumni Avatar asked Nov 28 '25 20:11

Alumni


1 Answers

Every write to the database is given what is known as a seqnum. (or sequence number) A log of those writes is stored with the document _id, _rev, amongst other information about the write. (See this section of the CouchDB book online.) A newly-created document gets the next seqnum (old seqnum + 1). A document update, on the other hand, also appends a new seqnum but also removes the document's old one from from the log. If you list all documents ordered by seqnum, you get a timeline of the evolution of the data.

Calling the _changes API retrieves that list. And since every revision is saved in it's entirity (not just a delta of changes) then you can reconstruct everything that has changed in that database since a particular seqnum.

Running a compaction removes old document revisions, but does not affect the seqnum or _changes data. This is because _changes shows only the latest (live) revisions of the documents.

like image 123
Dominic Barnes Avatar answered Dec 01 '25 00:12

Dominic Barnes



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!