Generating 8-character only UUIDs

后端 未结 8 1513
[愿得一人]
[愿得一人] 2020-11-27 03:22

UUID libraries generate 32-character UUIDs.

I want to generate 8-character only UUIDs, is it possible?

8条回答
  •  夕颜
    夕颜 (楼主)
    2020-11-27 03:53

    Actually I want timestamp based shorter unique identifier, hence tried the below program.

    It is guessable with nanosecond + ( endians.length * endians.length ) combinations.

    public class TimStampShorterUUID {
    
        private static final Character [] endians = 
               {'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 
                'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 
                'u', 'v', 'w', 'x', 'y', 'z', 
                'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 
                'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R', 'S', 'T', 
                'U', 'V', 'W', 'X', 'Y', 'Z',
                '0', '1', '2', '3', '4', '5', '6', '7', '8', '9'
                };
    
       private static ThreadLocal threadLocal =  new ThreadLocal();
    
       private static AtomicLong iterator = new AtomicLong(-1);
    
    
        public static String generateShorterTxnId() {
            // Keep this as secure random when we want more secure, in distributed systems
            int firstLetter = ThreadLocalRandom.current().nextInt(0, (endians.length));
    
            //Sometimes your randomness and timestamp will be same value,
            //when multiple threads are trying at the same nano second
            //time hence to differentiate it, utilize the threads requesting
            //for this value, the possible unique thread numbers == endians.length
            Character secondLetter = threadLocal.get();
            if (secondLetter == null) {
                synchronized (threadLocal) {
                    if (secondLetter == null) {
                        threadLocal.set(endians[(int) (iterator.incrementAndGet() % endians.length)]);
                    }
                }
                secondLetter = threadLocal.get();
            }
            return "" + endians[firstLetter] + secondLetter + System.nanoTime();
        }
    
    
        public static void main(String[] args) {
    
            Map uniqueKeysTestMap = new ConcurrentHashMap<>();
    
            Thread t1 = new Thread() {  
                @Override
                public void run() {
                    while(true) {
                        String time = generateShorterTxnId();
                        String result = uniqueKeysTestMap.put(time, "");
                        if(result != null) {
                            System.out.println("failed! - " + time);
                        }
                    }
                }       
            };
    
            Thread t2 = new Thread() {  
                @Override
                public void run() {
                    while(true) {
                        String time = generateShorterTxnId();
                        String result = uniqueKeysTestMap.put(time, "");
                        if(result != null) {
                            System.out.println("failed! - " + time);
                        }
                    }
                }       
            };
    
            Thread t3 = new Thread() {  
                @Override
                public void run() {
                    while(true) {
                        String time = generateShorterTxnId();
                        String result = uniqueKeysTestMap.put(time, "");
                        if(result != null) {
                            System.out.println("failed! - " + time);
                        }
                    }
                }       
            };
    
            Thread t4 = new Thread() {  
                @Override
                public void run() {
                    while(true) {
                        String time = generateShorterTxnId();
                        String result = uniqueKeysTestMap.put(time, "");
                        if(result != null) {
                            System.out.println("failed! - " + time);
                        }
                    }
                }       
            };
    
            Thread t5 = new Thread() {  
                @Override
                public void run() {
                    while(true) {
                        String time = generateShorterTxnId();
                        String result = uniqueKeysTestMap.put(time, "");
                        if(result != null) {
                            System.out.println("failed! - " + time);
                        }
                    }
                }
            };
    
            Thread t6 = new Thread() {  
                @Override
                public void run() {
                    while(true) {
                        String time = generateShorterTxnId();
                        String result = uniqueKeysTestMap.put(time, "");
                        if(result != null) {
                            System.out.println("failed! - " + time);
                        }
                    }
                }   
            };
    
            Thread t7 = new Thread() {  
                @Override
                public void run() {
                    while(true) {
                        String time = generateShorterTxnId();
                        String result = uniqueKeysTestMap.put(time, "");
                        if(result != null) {
                            System.out.println("failed! - " + time);
                        }
                    }
                }
            };
    
            t1.start();
            t2.start();
            t3.start();
            t4.start();
            t5.start();
            t6.start();
            t7.start();
        }
    }
    

    UPDATE: This code will work on single JVM, but we should think on distributed JVM, hence i am thinking two solutions one with DB and another one without DB.

    with DB

    Company name (shortname 3 chars) ---- Random_Number ---- Key specific redis COUNTER
    (3 char) ------------------------------------------------ (2 char) ---------------- (11 char)

    without DB

    IPADDRESS ---- THREAD_NUMBER ---- INCR_NUMBER ---- epoch milliseconds
    (5 chars) ----------------- (2char) ----------------------- (2 char) ----------------- (6 char)

    will update you once coding is done.

提交回复
热议问题