How can I create a partially unique and consistent field in a couchdb document?

I am very new to couchdb and I wonder how I can create an ID that looks like.

Employee:DBX-**0001**-SP

number 0001 must be unique and sequential. How can I achieve something like this in couchdb? I searched everything and I cannot find a simple solution.

It would be better if I could generate the serial part in couchdb and not on the client side to avoid collisions during replication.

Currently, the solution that I have is that I will bring the document I saved, which looks like this {"_id": "EmployeeAutoIncrement", value: 1} after extraction, I increase the value and send it back to the server if those are successful, then I return a new value with an extra value and use it as an Auto Increment value to participate in the Employee ID: DBX- AUTO_INCREMENT_VALUE_HERE -SP

The problem is that if two people access EmployeeAutoIncrement at the same time, and they both update it, it will not cause conflicts? In addition, if one person makes a request, and they go offline, then they return to the network and then do not conflict?

+5
source share
2 answers

All requirements cannot be met on the client side when using multiple clients, some of which can be autonomous.

Here is the process that leads to a monotonically increasing id:

  • Each client saves a record with a unique identifier. The entry should include a flag designating the entry as temporary.
  • Create an external process that listens for changes to feed for entries marked as temporary. Changes to the output channels are recorded in the "application temporal order".
  • The external process should create a new record with the correct identifier, marked as permanent. Since only this process creates β€œpersistent” records, it can read and write the value of EmployeeAutoIncrement without collisions.
  • An external process may delete a temporary entry.

The database will double the number of records, so it will grow faster and will need to be compacted earlier if this is a problem. Any submissions / requests for employee records should check the constant flag if the query is executed while the external process adds a new record.

+2
source

This can be done (at least), although I recommend that you think about your design options and why you want to do this in a distributed database - this is probably best done on the client, where you can control serialization with a sequence generator,

If you want to do this at least partially on the server, you will need to implement the so-called CRDT counter, as indicated in the following document:

http://hal.upmc.fr/docs/00/55/55/88/PDF/techreport.pdf

Here you can find the Ruby implementation of some of these ideas:

https://github.com/aphyr/meangirls

and the simplest counter implementation (the one you need) for Couch, and the set:

https://github.com/drsm79/couch-crdt

The latter, written in Python, will do almost what you want if you follow the pattern, as shown in the following example:

https://github.com/drsm79/couch-crdt/blob/master/examples/counter.py

which will give you your monotonous sequence. From there, create your _id document.

JavaScript and PouchDB translation left as an exercise for the reader.

+1
source

Source: https://habr.com/ru/post/1270953/


All Articles