How to insert auto increment / sequential number value in CouchDB docs?

I am playing around with couchDB for a while now and have the following scenario: I am using a problem tracker. The requirement is that each issue of the document (in addition to the _id document) has a unique numerical sequence number in order to reference it in a more appropriate way.

My first approach was to have a view that simply returns the number of unique documents currently stored. Increase this client side value by 1, assign it to my new problem and paste this. It turned out to be a bad idea when you add multiple issues with ajax calls, or add multiple clients at the same time. In the latter case, this would not have been possible even without communication between clients.

Ideally, I want the sequential number to be generated on the couch, which is not possible due to conflicting states in distributed systems.

Is there a good pattern that I can use (maybe client side) to approach this? I feel like this is a standard kind of use case (thinking account numbers, etc.).

Thanks in advance!

+3


source to share


2 answers


You can use a separate document that is empty, although it only consists of id

and rev

. The prefix is rev

always an integer, so you can use it as your auto incrementing number.

Just do a POST to your document, this will enlarge rev

and return it. Then you can use this generated value for your purpose.



Alternative way:

Create a separate document consisting of value

and lock

. Then do something like "IF lock = true THEN return ELSE set lock = true And increase the value by 1", then do a GET to fetch a new one, value

and finally set lock = false

.

+1


source


I agree with you that using a view that gives you the number of documents is not a great idea. And it is for this reason that couchdb uses uuid instead.

I am not aware of the sequential identifier function in couchdb, but I think it is fairly easy to write. I would think:



  • RPC (like with RabbitMQ) calls one service to avoid concurrency issues. Then you can save the last number in a special document on a specific non-circulated bag or elsewhere. It may not scale very well, but you write an issue tracker before it becomes an issue.
  • If you can resolve the missing numbers, set the uuid algorithm on your couch to sequential and you're at least good to the first buffer overflow. See more information at: http://couchdb.readthedocs.org/en/latest/config/misc.html#uuids-configuration
0


source







All Articles