From: Michael D. <mik...@us...> - 2004-10-11 23:59:15
|
Update of /cvsroot/nhibernate/nhibernate/src/NHibernate/Id In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv5623/NHibernate/Id Modified Files: IdentifierGeneratorFactory.cs Added Files: GuidCombGenerator.cs Log Message: added guid.comb generator from a jira submitted. --- NEW FILE: GuidCombGenerator.cs --- using System; using NHibernate.Engine; namespace NHibernate.Id { /// <summary> /// Generates <see cref="System.Guid"/> values using a strategy suggested Jimmy Nilsson's /// <a href="http://www.informit.com/articles/article.asp?p=25862">article</a> /// on <a href="http://www.informit.com">informit.com</a>. /// </summary> /// <remarks> /// This code was contributed by Donald Mull. /// </remarks> public class GuidCombGenerator : IIdentifierGenerator { #region IIdentifierGenerator Members public object Generate(ISessionImplementor session, object obj) { return GenerateComb(); } private Guid GenerateComb() { byte[] guidArray = System.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 = new TimeSpan(now.Ticks - (new DateTime(now.Year, now.Month, now.Day).Ticks)); // 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); } #endregion } } Index: IdentifierGeneratorFactory.cs =================================================================== RCS file: /cvsroot/nhibernate/nhibernate/src/NHibernate/Id/IdentifierGeneratorFactory.cs,v retrieving revision 1.10 retrieving revision 1.11 diff -C2 -d -r1.10 -r1.11 *** IdentifierGeneratorFactory.cs 30 Apr 2004 14:06:46 -0000 1.10 --- IdentifierGeneratorFactory.cs 11 Oct 2004 23:57:54 -0000 1.11 *************** *** 46,59 **** static IdentifierGeneratorFactory() { ! idgenerators.Add("uuid.hex", typeof(UUIDHexGenerator)); ! idgenerators.Add("uuid.string", typeof(UUIDStringGenerator)); ! idgenerators.Add("hilo", typeof(TableHiLoGenerator)); ! idgenerators.Add("assigned", typeof(Assigned)); ! idgenerators.Add("identity", typeof(IdentityGenerator)); ! idgenerators.Add("sequence", typeof(SequenceGenerator)); ! idgenerators.Add("seqhilo", typeof(SequenceHiLoGenerator)); ! idgenerators.Add("vm", typeof(CounterGenerator)); ! idgenerators.Add("foreign", typeof(ForeignGenerator)); ! idgenerators.Add("guid", typeof(GuidGenerator)); } --- 46,60 ---- static IdentifierGeneratorFactory() { ! idgenerators.Add( "uuid.hex", typeof(UUIDHexGenerator) ); ! idgenerators.Add( "uuid.string", typeof(UUIDStringGenerator) ); ! idgenerators.Add( "hilo", typeof(TableHiLoGenerator) ); ! idgenerators.Add( "assigned", typeof(Assigned) ); ! idgenerators.Add( "identity", typeof(IdentityGenerator) ); ! idgenerators.Add( "sequence", typeof(SequenceGenerator) ); ! idgenerators.Add( "seqhilo", typeof(SequenceHiLoGenerator) ); ! idgenerators.Add( "vm", typeof(CounterGenerator) ); ! idgenerators.Add( "foreign", typeof(ForeignGenerator) ); ! idgenerators.Add( "guid", typeof(GuidGenerator) ); ! idgenerators.Add( "guid.comb", typeof(GuidCombGenerator) ); } |