Hello,
I would like to build an application on CouchDB, which allocates free
documents to the users. Each document would have a status, which
indicates whether it's available for assigning, or not.
Because of the great number of new users wanting available documents, I
think the default model of writing a view, which lists available
documents, and picking one from it (for efficiency limiting the number
of return values to one and picking that) won't work. The contention
will make applications do a lot of conflicts, which slows down (or even
make it impossible) the new assignments, and is a lot of unnecessary
load for both the server and the client applications.
So I would need a method, which would pick the first document from the
database which has "available" in its status field, and immediately
(atomically) set it to "unavailable", and return the document ID to the
caller.
I've found two posts, which do something similar (server side changes to
documents):
http://blog.couch.io/post/410290711/atomic-increments-in-couchdb
http://wiki.apache.org/couchdb/Document_Update_Handlers
I'm very new to CouchDB, and I can't yet to see whether these can be
adapted to the above task (these two are tied to a given document).
So in short I would need: a server-side function, which traverses on all
entries (uses a view for efficiency?), picks the first one which has
status: available, sets that to unavailable and returns the docID to the
client. And of course all of this atomically, meaning no two calls to
this method should give back the same docID.
Is this doable somehow with CouchDB?
Thanks,