Unique creation of a fixed-length identifier

Well, I was looking for ways to generate UIDs in java code (most of them are also suitable for stackoverflow). It is best to use the java UUID to create unique identifiers as it uses a timestamp. But my problem is that it is 128-bit, and I need a shorter string, for example, 14 or 15 characters. So, I developed the following code for this.

Date date = new Date(); Long luid = (Long) date.getTime(); String suid = luid.toString(); System.out.println(suid+": "+suid.length() + " characters"); Random rn = new Random(); Integer long1 = rn.nextInt(9); Integer long2 = rn.nextInt(13); String newstr = suid.substring(0, long2) + " " + long1 + " " + suid.subtring(long2); System.out.println("New string in spaced format: "+newstr); System.out.println("New string in proper format: "+newstr.replaceAll(" ", "")); 

Please note that I am just showing a line with a formatted and correctly formatted one to compare only with the original line.

Will it guarantee a 100% unique identifier every time? Or do you see any possibility of repeating numbers? Also, instead of inserting a random number into a random position that β€œcould” create repeated numbers, I could do this either at the beginning or at the end. This is necessary to fill in the required length of my UID. Although, perhaps this will not work if you need a UID of less than 13 characters.

Any thoughts?

+4
source share
2 answers

This will not work if it is a distributed system, of course, but what about the following:

 private AtomicLong uniqueId = new AtomicLong(0); ... // get a unique current-time-millis value long now; long prev; do { prev = uniqueId.get(); now = System.currentTimeMillis(); // make sure now is moving ahead and unique if (now <= prev) { now = prev + 1; } // loop if someone else has updated the id } while (!uniqueId.compareAndSet(prev, now)); // shuffle it long result = shuffleBits(now); System.out.println("Result is " + Long.toHexString(result)); public static long shuffleBits(long val) { long result = 0; result |= (val & 0xFF00000000000000L) >> 56; result |= (val & 0x00FF000000000000L) >> 40; result |= (val & 0x0000FF0000000000L) >> 24; result |= (val & 0x000000FF00000000L) >> 8; result |= (val & 0x00000000FF000000L) << 8; result |= (val & 0x0000000000FF0000L) << 24; result |= (val & 0x000000000000FF00L) << 40; result |= (val & 0x00000000000000FFL) << 56; return result; } 

Bit shuffling can be improved to generate more changes in the values ​​of each iteration. You mentioned that you do not want the numbers to be consecutive, but you did not specify the requirement of complete randomness.

Of course, not as good as a UUID , but faster than a database operation.

+3
source

An easy way is to use database sequences, if available. If this is not the case, you can simulate them as follows:

  • Create a table with a column that will indicate the maximum value (initially 0). Some applications create multiple lines where each line controls a specific unique identifier, but all you really need is a single line. For this example, suppose the table structure is as follows: as follows:

     ID_TABLE ID_NAME VARCHAR(40); -- Or whatever type is appropriate ID_COLUMN INTEGER; -- Or whatever type is appropriate 
  • Each process reserves rows by doing the following:

     a. Begin Txn; b. Update ID_TABLE set ID_VALUE = ID_VALUE + <n> where ID_NAME = <name>; c. Select ID_VALUE from ID_TABLE where ID_NAME = <name>; d. Commit Txn; 

    If all of this completes successfully, you just reserved the range (val - n + 1) through val, where val is the return value from stage c. above.

  • Each process issues identifiers from the range that it has reserved. If the process is multithreaded, it must provide synchronization to ensure each value is issued no more than once. When he has exhausted his sentence of values, he returns to step 2 and saves more values. Note that not all reserved values ​​are guaranteed. If a process terminates without using all the values ​​that it has reserved unused values ​​are lost and never used.

+1
source

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


All Articles