user3614014
user3614014

Reputation: 673

Changing the UUID algorithm in CouchDB

This question is referencing couchdb version 1.5.0

For the purposes of my environment, I want to be able to effectively order the documents in a database for later retrieval based on some sort of time index. I read online that there are four algorithms for uuid creation in couchdb.

Documentation which shows the algorithms that can be used in uuid generation are listed in here: http://couchdb.readthedocs.org/en/latest/config/misc.html#uuids-configuration

I suspect the default uuid algorithm setting is "sequential", when issuing a http://couchdb:5984/_uuids?count=50 and I see that for a large chunk of them (if not all) the first 26 digits are identical and the last 6 are unique.

I tried changing the algorithm to one of the utc options (either "utc_random" or "utc_id" will work) where the first 16 bits is the time in microseconds from Unix epoch in hex.

The method I used to change the UUID algorithm was to go into the local.ini file and append the following stanza (it wasn't in there previously):

[uuids] algorithm = utc_random

I then restart the couchdb service

service couchdb restart

When I query again for a batch of UUIDS I still get what appears to be a result indicating that the uuids being generated are still in the sequential algorithm. (when I query for 50 uuids a second time after this, the "sequential" algorithm behavior is noticed)

Am I doing something wrong? Any help that can be offered would be greatly appreciated!

Update

After trying the curl command suggested by Akshat Jiwan Sharma I went delving back into the configuration files and found a default.ini file. This is the file where the uuids parameter appears to be set and I replaced 'sequential' with 'utc_random', restarted the couchdb service and everything works as expected. To find the configuration files for couchdb, I used a simple command line couchdb -c Thank you again for all your help, you guys are awesome!

Upvotes: 3

Views: 1874

Answers (2)

Akshat Jiwan Sharma
Akshat Jiwan Sharma

Reputation: 16060

There are a couple of things you can check.

  1. Have you added another configuration file? You can chain config files in couchdb and if you do only the changes from the last file in the chain will be used.

  2. Make a call to the http://localhost:5984/_config/uuids to get the info about the uuid algorithm that is used by couchdb. You should get a result like

{"algorithm":"utc_random","max_count":"1000"}

and you can verify whether couchdb has picked up your configuration or not.

Also you can change configuration files via a rest api. This will help in debugging as you don't need to restart couchdb again. The benefit of this method is that only the relevant configuration file will be changed.

here is how you can do it

curl -X PUT http://localhost:5984/_config/uuids/algorithm -d '"utc_random"'

Be aware though if you have a typo in the parameter you want to change like for example ramdom instead of random (I actually typed that !) couch db will throw a runtime exception and you have have to resort to manually changing the config files.

Upvotes: 3

wallacer
wallacer

Reputation: 13213

As a slightly different approach to your problem, you can set your own _id in each document before you write them. I would suggest that approach over trusting a CouchDB auto-generation algorithm to get your ordering right.

For example, you could use a UUID generator in your client system, and prepend your index to the generated UUID. In python I would use the UUID library. You should be able to find a UUID generator for virtually every language I would think.

_id would look like < indexValue > _ < UUID >

As long as it's a new doc, you can choose any _id value you want.

I've often used deterministic doc ID's for doc's that may be related as well. Imagine that I may have a "Customer" doc with some customer information, and I also store a "Customer History" doc with a history of that customer's transactions. I might give my customer doc the form

Customer_14567ad352f-351979ffe12 and then make his history doc

Customer_14567ad352f-351979ffe12_History

Another approach, since you said "I want to be able to effectively order the documents in a database for later retrieval based on some sort of time index"

That is what views are used for. So you could write a view to organize the docs by the time index you'd like. The most simple possible view to do so would be:

function(doc)
{
    emit( doc.TimeIndex, null );
}

Upvotes: 2

Related Questions