编程知识 cdmana.com

Implementation of snowflake global unique ID algorithm C

public class SnowFlake
    {
        /// <summary>
        ///  Total number of machines 
        /// </summary>
        private static readonly int WorkBits = 5;
        /// <summary>
        ///  Automatically increasing the number of digits of serial number 
        /// </summary>
        private static readonly int SequenceBits = 16;
        /// <summary>
        ///  Maximum serial number  0 Start  
        /// </summary>
        private static readonly long SequenceMax = 1L << SequenceBits;
        /// <summary>
        ///  Server maximum 
        /// </summary>
        private static readonly long WorkMax = 1L << WorkBits;
        /// <summary>
        ///  Current machine number  0-WorkMax
        /// </summary>
        private static long CurrentWorkId = 0;
        /// <summary>
        ///  Lock object 
        /// </summary>
        private readonly object objlock = new object();
        /// <summary>
        ///  Record last timestamp 
        /// </summary>
        private static long LastTime;
        /// <summary>
        ///  Current serial number  0 Start 
        /// </summary>
        private static long CurrentSequence = -1;
        public SnowFlake(int _CurrentWorkId)
        {
            CurrentWorkId = _CurrentWorkId;
            Console.WriteLine(SequenceMax);
        }
        /// <summary>
        ///  Custom timestamp 
        /// </summary>
        /// <returns></returns>
        private long GetTime()
        {
            return (long)(DateTime.UtcNow - new DateTime(2020, 1, 1, 0, 0, 0, DateTimeKind.Utc)).TotalSeconds;
        }


        /// <summary>
        ///  obtain ID
        /// </summary>
        /// <returns></returns>
        public long NextId()
        {
            lock (objlock)
            {
                if (CurrentWorkId >= WorkMax)
                {
                    throw new Exception($" The machine code cannot be greater than the maximum machine code value {WorkMax}");
                }
                var time = GetTime();
                // The serial number increases automatically in the same second 
                if (time == LastTime)
                {
                    // Serial number exceeds maximum   Waiting for the next point in time 
                    if (CurrentSequence >= SequenceMax)
                    {
                        // Waiting for the next point in time 
                        while (time == LastTime)
                        {
                            time = GetTime();
                        }
                        CurrentSequence = -1;
                        LastTime = time;
                    }
                }
                else
                {
                    // Different seconds   Self increasing again 
                    CurrentSequence = -1;
                    LastTime = time;
                }
                CurrentSequence++;

                //Console.WriteLine(DateTime.UtcNow.ToString("yyyy-MM-dd HH:mm:ss fff"));
                //Console.WriteLine(CurrentSequence);

                var num = (WorkBits + SequenceBits);
                return time << num | CurrentWorkId << SequenceBits | CurrentSequence;
            }
        }

       
    }

The above code is for personal understanding , If there is any deficiency , Please correct me. .

The actual algorithm is 1 individual 0bit+41bit Time stamp +10bit Machine code +12bit Serial number ; What you end up with is 64bit Binary system That is, it can support int64 Maximum , It can support single machine per second 400 many W Of ID Generate ;

but js Maximum Number The type is 53bit, And common systems can't achieve so much concurrency , So I refer to Mr. Liao Xuefeng's opinion It is amended as follows

32bit Time stamp in seconds of ( Now the result of calculation is 31bit)+5bit Machine code +16bit The serial number of common 53bit, And the timestamp is not used 1970 Default algorithm , A custom year is used , The above code can support maximum 2^5=32 A machine Each machine 2^16=65535 individual ID Generate .

SnowFlake Other understanding of the algorithm reference  https://www.cnblogs.com/firstdream/p/9055771.html,

see int Of bit You can use  Convert.ToString(num, 2); see

<< Left offset To the right 0 

| Or algorithm  

 

版权声明
本文为[CaiFengJian]所创,转载请带上原文链接,感谢

Scroll to Top