You are viewing a plain text version of this content. The canonical link for it is here.
Posted to user@couchdb.apache.org by Andrew Melo <an...@gmail.com> on 2012/07/19 23:09:58 UTC

Atomicity with _update handlers?

Hello all,

The documentation is a bit unclear on this, but basically I was
wondering what sort of atomicity guarantees exist for _update
handlers. Suppose I have documents that look like this

{ 'states' : [ { 'oldstate' : 'created', 'newstate' : 'new' } ] }

And an update handler that looks like this (probably not the exactly
right syntax)

function (doc, req) {
  // check that the requested old state equals the previous new state
  if doc.states[ len(doc.states) ]['newstate'] == req.query['oldstate'] {
     // update the document, adding a new state to the end of doc.states
  } else {
     // barf back to the user
  }
}

Is there a guarantee that concurrent requests to the same instance
can't end up with a garbled ordering of states?

Thanks,
Andrew

-- 
--
Andrew Melo

Re: Atomicity with _update handlers?

Posted by Robert Newson <rn...@apache.org>.
An update to a couchdb document is atomic, whether done directly or via an update handler. An update handler can fail with a 409 just the same as a direct update can. The only difference between the two update methods is where the logic runs. The update handler is merely returning the new document to couchdb, the actual update occurs after the function has returned (and can fail).

B.

On 19 Jul 2012, at 22:09, Andrew Melo wrote:

> Hello all,
> 
> The documentation is a bit unclear on this, but basically I was
> wondering what sort of atomicity guarantees exist for _update
> handlers. Suppose I have documents that look like this
> 
> { 'states' : [ { 'oldstate' : 'created', 'newstate' : 'new' } ] }
> 
> And an update handler that looks like this (probably not the exactly
> right syntax)
> 
> function (doc, req) {
>  // check that the requested old state equals the previous new state
>  if doc.states[ len(doc.states) ]['newstate'] == req.query['oldstate'] {
>     // update the document, adding a new state to the end of doc.states
>  } else {
>     // barf back to the user
>  }
> }
> 
> Is there a guarantee that concurrent requests to the same instance
> can't end up with a garbled ordering of states?
> 
> Thanks,
> Andrew
> 
> -- 
> --
> Andrew Melo


Re: Atomicity with _update handlers?

Posted by Mark Hahn <ma...@hahnca.com>.
>   what sort of atomicity guarantees exist for _update handlers.

None.  And they can fail with a 409 revision conflict