zoukankan      html  css  js  c++  java
  • 根据SQL Server排序规则创建顺序GUID

     public static class GuidUtil
        {
            private static readonly long EpochMilliseconds = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).Ticks / 10000L;
    
            /// <summary>
            /// Creates a sequential GUID according to SQL Server's ordering rules.
            /// </summary>
            public static Guid NewSequentialId()
            {
                // This code was not reviewed to guarantee uniqueness under most conditions, nor completely optimize for avoiding
                // page splits in SQL Server when doing inserts from multiple hosts, so do not re-use in production systems.
                var guidBytes = Guid.NewGuid().ToByteArray();
    
                // get the milliseconds since Jan 1 1970
                byte[] sequential = BitConverter.GetBytes((DateTime.Now.Ticks / 10000L) - EpochMilliseconds);
    
                // discard the 2 most significant bytes, as we only care about the milliseconds increasing, but the highest ones 
                // should be 0 for several thousand years to come (non-issue).
                if (BitConverter.IsLittleEndian)
                {
                    guidBytes[10] = sequential[5];
                    guidBytes[11] = sequential[4];
                    guidBytes[12] = sequential[3];
                    guidBytes[13] = sequential[2];
                    guidBytes[14] = sequential[1];
                    guidBytes[15] = sequential[0];
                }
                else
                {
                    Buffer.BlockCopy(sequential, 2, guidBytes, 10, 6);
                }
    
                return new Guid(guidBytes);
            }
        }
  • 相关阅读:
    1509 加长棒
    51Nod 1158 全是1的最大子矩阵
    P2953 [USACO09OPEN]牛的数字游戏Cow Digit Game
    P3384 【模板】树链剖分
    北京集训DAY3
    北京集训DAY2
    北京集训DAY1
    51Nod 1422 沙拉酱前缀 二分查找
    51Nod 1109 01组成的N的倍数
    51Nod 1043 幸运号码 数位DP
  • 原文地址:https://www.cnblogs.com/mycing/p/6063775.html
Copyright © 2011-2022 走看看