顺序Guid发生器

时间:2009-11-17 21:36:41

标签: c# guid sequence

有没有办法获得Sql Server 2005+ Sequential Guid生成器的功能而不插入记录来回读或调用本机win dll调用?我看到有人用rpcrt4.dll的方式回答,但我不确定这是否可以在我的托管环境中进行生产。

编辑:使用@John Boker的回答我尝试将其转换为更多的GuidComb生成器,而不是依赖于最后生成的Guid而不是重新开始。对于种子,而不是从我使用的Guid.Empty开始

public SequentialGuid()
{
    var tempGuid = Guid.NewGuid();
    var bytes = tempGuid.ToByteArray();
    var time = DateTime.Now;
    bytes[3] = (byte) time.Year;
    bytes[2] = (byte) time.Month;
    bytes[1] = (byte) time.Day;
    bytes[0] = (byte) time.Hour;
    bytes[5] = (byte) time.Minute;
    bytes[4] = (byte) time.Second;
    CurrentGuid = new Guid(bytes);
}

我基于

的评论
// 3 - the least significant byte in Guid ByteArray 
        [for SQL Server ORDER BY clause]
// 10 - the most significant byte in Guid ByteArray 
        [for SQL Server ORDERY BY clause]
SqlOrderMap = new[] {3, 2, 1, 0, 5, 4, 7, 6, 9, 8, 15, 14, 13, 12, 11, 10};

这看起来像我想用DateTime为guid播种的方式,还是看起来我应该反过来并从SqlOrderMap索引的末尾开始向后工作?我不太关心他们是否会在创建初始guid时进行分页,因为它只会在应用程序回收期间发生。

11 个答案:

答案 0 :(得分:67)

您可以使用相同的Win32 API function that SQL Server uses

UuidCreateSequential

并应用一些位移以将值设置为big-endian顺序。

因为你想在C#中使用它:

private class NativeMethods
{
   [DllImport("rpcrt4.dll", SetLastError=true)]
   public static extern int UuidCreateSequential(out Guid guid);
}

public static Guid NewSequentialID()
{
   //Code is released into the public domain; no attribution required
   const int RPC_S_OK = 0;

   Guid guid;
   int result = NativeMethods.UuidCreateSequential(out guid);
   if (result != RPC_S_OK)
      return Guid.NewGuid();

   //Endian swap the UInt32, UInt16, and UInt16 into the big-endian order (RFC specified order) that SQL Server expects
   //See https://stackoverflow.com/a/47682820/12597
   //Short version: UuidCreateSequential writes out three numbers in litte, rather than big, endian order
   var s = guid.ToByteArray();
   var t = new byte[16];

   //Endian swap UInt32
   t[3] = s[0];
   t[2] = s[1];
   t[1] = s[2];
   t[0] = s[3];
   //Endian swap UInt16
   t[5] = s[4];
   t[4] = s[5];
   //Endian swap UInt16
   t[7] = s[6];
   t[6] = s[7];
   //The rest are already in the proper order
   t[8] = s[8];
   t[9] = s[9];
   t[10] = s[10];
   t[11] = s[11];
   t[12] = s[12];
   t[13] = s[13];
   t[14] = s[14];
   t[15] = s[15];

   return new Guid(t);
}

另见


Microsoft的UuidCreateSequential只是来自RFC 4122类型1 uuid的实现。

uuid有三个重要部分:

  • node(6个字节) - 计算机的MAC地址
  • timestamp(7个字节) - 自1582年10月15日00:00:00.00(格里高利改革为基督教历法之日)以来的100 ns间隔数
  • clockSequenceNumber(2个字节) - 计数器,以防你生成超过100ns的guid,或者你改变你的mac地址

基本算法是:

  1. 获得系统范围的锁定
  2. 从持久存储(注册表/文件)中读取最后nodetimestampclockSequenceNumber
  3. 获取当前node(即MAC地址)
  4. 获取当前timestamp
    • a)如果保存的状态不可用或已损坏,或者mac地址已更改,则生成随机clockSequenceNumber
    • b)如果状态可用,但当前timestamp与保存的时间戳相同或更旧,请递增clockSequenceNumber
  5. nodetimestampclockSequenceNumber保存回持久存储空间
  6. 发布全局锁定
  7. 根据rfc
  8. 格式化guid结构

    还有一个4位版本号和2位变种,它们也需要与数据进行AND运算:

    guid = new Guid(
          timestamp & 0xFFFFFFFF,  //timestamp low
          (timestamp >> 32) & 0xFFFF, //timestamp mid
          ((timestamp >> 40) & 0x0FFF), | (1 << 12) //timestamp high and version (version 1)
          (clockSequenceNumber & 0x3F) | (0x80), //clock sequence number and reserved
          node[0], node[1], node[2], node[3], node[4], node[5], node[6]);
    
      

    注意:完全未经测试;我只是从RFC中看到它。

         
        
    • 可能必须更改字节顺序(Here is byte order for sql server
    •   
    • 您可能想要创建自己的版本,例如版本6(版本1-5定义)。这样你就可以保证是普遍独特的
    •   

答案 1 :(得分:22)

这个人提出了制作顺序guid的东西,这是一个链接

http://developmenttips.blogspot.com/2008/03/generate-sequential-guids-for-sql.html

相关代码:

public class SequentialGuid {
    Guid _CurrentGuid;
    public Guid CurrentGuid {
        get {
            return _CurrentGuid;
        }
    }

    public SequentialGuid() {
        _CurrentGuid = Guid.NewGuid();
    }

    public SequentialGuid(Guid previousGuid) {
        _CurrentGuid = previousGuid;
    }

    public static SequentialGuid operator++(SequentialGuid sequentialGuid) {
        byte[] bytes = sequentialGuid._CurrentGuid.ToByteArray();
        for (int mapIndex = 0; mapIndex < 16; mapIndex++) {
            int bytesIndex = SqlOrderMap[mapIndex];
            bytes[bytesIndex]++;
            if (bytes[bytesIndex] != 0) {
                break; // No need to increment more significant bytes
            }
        }
        sequentialGuid._CurrentGuid = new Guid(bytes);
        return sequentialGuid;
    }

    private static int[] _SqlOrderMap = null;
    private static int[] SqlOrderMap {
        get {
            if (_SqlOrderMap == null) {
                _SqlOrderMap = new int[16] {
                    3, 2, 1, 0, 5, 4, 7, 6, 9, 8, 15, 14, 13, 12, 11, 10
                };
                // 3 - the least significant byte in Guid ByteArray [for SQL Server ORDER BY clause]
                // 10 - the most significant byte in Guid ByteArray [for SQL Server ORDERY BY clause]
            }
            return _SqlOrderMap;
        }
    }
}

答案 2 :(得分:17)

Here是NHibernate实现Guid.Comb算法的方式:

private Guid GenerateComb()
{
    byte[] guidArray = Guid.NewGuid().ToByteArray();

    DateTime baseDate = new DateTime(1900, 1, 1);
    DateTime now = DateTime.Now;

    // Get the days and milliseconds which will be used to build the byte string 
    TimeSpan days = new TimeSpan(now.Ticks - baseDate.Ticks);
    TimeSpan msecs = now.TimeOfDay;

    // Convert to a byte array 
    // Note that SQL Server is accurate to 1/300th of a millisecond so we divide by 3.333333 
    byte[] daysArray = BitConverter.GetBytes(days.Days);
    byte[] msecsArray = BitConverter.GetBytes((long) (msecs.TotalMilliseconds / 3.333333));

    // Reverse the bytes to match SQL Servers ordering 
    Array.Reverse(daysArray);
    Array.Reverse(msecsArray);

    // Copy the bytes into the guid 
    Array.Copy(daysArray, daysArray.Length - 2, guidArray, guidArray.Length - 6, 2);
    Array.Copy(msecsArray, msecsArray.Length - 4, guidArray, guidArray.Length - 4, 4);

    return new Guid(guidArray);
}

答案 3 :(得分:7)

可以找到经常更新(每毫秒至少3次)的顺序guid here。它是使用常规C#代码创建的(没有本机代码调用)。

答案 4 :(得分:4)

与其他建议比较可能很有趣:

EntityFramework Core还实现了sequentialGuidValueGenerator。 它们为每个值生成randoms guids,并且仅根据时间戳和线程安全增量更改最重要的字节,以便在SQL Server中进行排序。

source link

这会导致值非常不同,但时间戳可排序。

答案 5 :(得分:3)

我的解决方案(在VB中但很容易转换)。它将最重要的(对于SQL Server排序)GUID的前8个字节更改为DateTime.UtcNow.Ticks,并且还有额外的代码来帮助解决多次获取相同的Ticks的问题,如果您比系统更快地调用新的GUID时钟更新。

Private ReadOnly _toSeqGuidLock As New Object()
''' <summary>
''' Replaces the most significant eight bytes of the GUID (according to SQL Server ordering) with the current UTC-timestamp.
''' </summary>
''' <remarks>Thread-Safe</remarks>
<System.Runtime.CompilerServices.Extension()> _
Public Function ToSeqGuid(ByVal guid As Guid) As Guid

    Static lastTicks As Int64 = -1

    Dim ticks = DateTime.UtcNow.Ticks

    SyncLock _toSeqGuidLock

        If ticks <= lastTicks Then
            ticks = lastTicks + 1
        End If

        lastTicks = ticks

    End SyncLock

    Dim ticksBytes = BitConverter.GetBytes(ticks)

    Array.Reverse(ticksBytes)

    Dim guidBytes = guid.ToByteArray()

    Array.Copy(ticksBytes, 0, guidBytes, 10, 6)
    Array.Copy(ticksBytes, 6, guidBytes, 8, 2)

    Return New Guid(guidBytes)

End Function

答案 6 :(得分:3)

C#版本

    public static Guid ToSeqGuid()
    {
        Int64 lastTicks = -1;
        long ticks = System.DateTime.UtcNow.Ticks;

        if (ticks <= lastTicks)
        {
            ticks = lastTicks + 1;
        }

        lastTicks = ticks;

        byte[] ticksBytes = BitConverter.GetBytes(ticks);

        Array.Reverse(ticksBytes);

        Guid myGuid = new Guid();
        byte[] guidBytes = myGuid.ToByteArray();

        Array.Copy(ticksBytes, 0, guidBytes, 10, 6);
        Array.Copy(ticksBytes, 6, guidBytes, 8, 2);

        Guid newGuid = new Guid(guidBytes);

        string filepath = @"C:\temp\TheNewGuids.txt";
        using (StreamWriter writer = new StreamWriter(filepath, true))
        {
            writer.WriteLine("GUID Created =  " + newGuid.ToString());
        }

        return newGuid;

    }

}

}

答案 7 :(得分:3)

我刚刚tf.constant()取了the NHibernate based answer并将其作为扩展函数:

using System;

namespace Atlas.Core.Kernel.Extensions
{
  public static class Guids
  {
    public static Guid Comb(this Guid source)
    {
      byte[] guidArray = source.ToByteArray();

      DateTime baseDate = new DateTime(1900, 1, 1);
      DateTime now = DateTime.Now;

      // Get the days and milliseconds which will be used to build the byte string 
      TimeSpan days = new TimeSpan(now.Ticks - baseDate.Ticks);
      TimeSpan msecs = now.TimeOfDay;

      // Convert to a byte array 
      // Note that SQL Server is accurate to 1/300th of a millisecond so we divide by 3.333333 
      byte[] daysArray = BitConverter.GetBytes(days.Days);
      byte[] msecsArray = BitConverter.GetBytes((long)(msecs.TotalMilliseconds / 3.333333));

      // Reverse the bytes to match SQL Servers ordering 
      Array.Reverse(daysArray);
      Array.Reverse(msecsArray);

      // Copy the bytes into the guid 
      Array.Copy(daysArray, daysArray.Length - 2, guidArray, guidArray.Length - 6, 2);
      Array.Copy(msecsArray, msecsArray.Length - 4, guidArray, guidArray.Length - 4, 4);

      return new Guid(guidArray);
    }
  }
}

答案 8 :(得分:2)

据我所知,NHibernate有一个名为GuidCombGenerator的特殊生成器。你可以看一下。

答案 9 :(得分:2)

不是特别guid,但我现在通常使用Snowflake风格的顺序id生成器。 guid具有相同的优点,同时具有比顺序guid更好的聚簇索引兼容性。

Flakey for .NET Core

IdGen for .NET Framework

答案 10 :(得分:2)

我刚看到这个问题......我碰巧是一个用于生成COMB风格GUID的小型开源.NET库的作者。

该库支持原始方法(与SQL Server&#39; s datetime类型兼容)和使用Unix时间戳的方法,后者具有更高的时间精度。它还包括一个适用于PostgrSQL的变体:

https://github.com/richardtallent/RT.Comb