Frage

I am following a tutorial on making a simple app with Node.js and Geddy. For each new entry it sets the entry id with geddy.string.uuid(10) and doesn't seem to run any other checks. My concern is when creating a new entry this could generate the same ID as a previous entry and the save script in the tutorial will overwrite/update an entry if the id already exists.

I know this would be a rare occurrence if it could happen, but I feel it is not responsible to leave this to chance if it can. I know in PHP I would be running a check of some sort to make sure it is unique, and just wanted to know if maybe Geddy was doing this already for me being the tutorial seems to not worry about it?

Thank you.

War es hilfreich?

Lösung

There is no real chance of a uuid occurring twice.

Technically, there is a one in 340,282,366,920,938,463,463,374,607,431,768,211,456 (16 to the 32nd power) chance that there will be a duplicate. Geddy's uuid function (like most others) uses 32 hex digits (128 bits) of random bits. See http://en.wikipedia.org/wiki/Universally_unique_identifier#Random_UUID_probability_of_duplicates. Basically, you're not going to get a duplicate.

As a side note, it's interesting that you're passing 10 uuid here: geddy.string.uuid(10). In the latest version of getty string.js, uuid does not take an argument, so the 10 will be ignored.

148   this.uuid = function () {
149     var chars = _UUID_CHARS, uuid = [], rnd=0, r;
150     for (var i = 0; i < 36; i++) {
151       if (i==8 || i==13 ||  i==18 || i==23) {
152         uuid[i] = '-';
153       } else {
154         if (rnd <= 0x02) rnd = 0x2000000 + (Math.random()*0x1000000)|0;
155         r = rnd & 0xf;
156         rnd = rnd >> 4;
157         uuid[i] = chars[(i == 19) ? (r & 0x3) | 0x8 : r];
158       }
159     }
160     return uuid.join('');
161   };

If the 10 in your version of geddy is limiting the number of hex digits to 10, then you only have a 1 in 1,099,511,627,776 (1 trillion) chance of a collision. That's still quite unlikely, though it could be brute forced to find a duplicate id.

Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top