亚洲乱码中文字幕综合,中国熟女仑乱hd,亚洲精品乱拍国产一区二区三区,一本大道卡一卡二卡三乱码全集资源,又粗又黄又硬又爽的免费视频

關(guān)于C#生成MongoDB中ObjectId的實(shí)現(xiàn)方法

 更新時(shí)間:2013年04月24日 09:46:16   作者:  
本篇文章小編為大家介紹,關(guān)于C#生成MongoDB中ObjectId的實(shí)現(xiàn)方法。需要的朋友參考下

ObjectId介紹
在MongoDB中,文檔(document)在集合(collection)中的存儲(chǔ)需要一個(gè)唯一的_id字段作為主鍵。這個(gè)_id默認(rèn)使用ObjectId來定義,因?yàn)镺bjectId定義的足夠短小,并盡最大可能的保持唯一性,同時(shí)能被快速的生成。

ObjectId 是一個(gè) 12 Bytes 的 BSON 類型,其包含
1.4 Bytes 自紀(jì)元時(shí)間開始的秒數(shù)
2.3 Bytes 機(jī)器描述符
3.2 Bytes 進(jìn)程ID
4.3 Bytes 隨機(jī)數(shù)

從定義可以看出,在同一秒內(nèi),在不同的機(jī)器上相同進(jìn)程ID條件下,非常有可能生成相同的ObjectId。
同時(shí)可以根據(jù)定義判斷出,在給定條件下,ObjectId本身即可描述生成的時(shí)間順序

ObjectId的存儲(chǔ)使用Byte數(shù)組,而其展現(xiàn)需將Byte數(shù)組轉(zhuǎn)換成字符串進(jìn)行顯示,所以通常我們看到的ObjectId都類似于:

ObjectId("507f191e810c19729de860ea")

C#定義ObjectId類

復(fù)制代碼 代碼如下:

View Code
   public class ObjectId
   {
     private string _string;

     public ObjectId()
     {
     }

     public ObjectId(string value)
       : this(DecodeHex(value))
     {
     }

     internal ObjectId(byte[] value)
     {
       Value = value;
     }

     public static ObjectId Empty
     {
       get { return new ObjectId("000000000000000000000000"); }
     }

     public byte[] Value { get; private set; }

     public static ObjectId NewObjectId()
     {
       return new ObjectId { Value = ObjectIdGenerator.Generate() };
     }

     public static bool TryParse(string value, out ObjectId objectId)
     {
       objectId = Empty;
       if (value == null || value.Length != 24)
       {
         return false;
       }

       try
       {
         objectId = new ObjectId(value);
         return true;
       }
       catch (FormatException)
       {
         return false;
       }
     }

     protected static byte[] DecodeHex(string value)
     {
       if (string.IsNullOrEmpty(value))
         throw new ArgumentNullException("value");

       var chars = value.ToCharArray();
       var numberChars = chars.Length;
       var bytes = new byte[numberChars / 2];

       for (var i = 0; i < numberChars; i += 2)
       {
         bytes[i / 2] = Convert.ToByte(new string(chars, i, 2), 16);
       }

       return bytes;
     }

     public override int GetHashCode()
     {
       return Value != null ? ToString().GetHashCode() : 0;
     }

     public override string ToString()
     {
       if (_string == null && Value != null)
       {
         _string = BitConverter.ToString(Value)
           .Replace("-", string.Empty)
           .ToLowerInvariant();
       }

       return _string;
     }

     public override bool Equals(object obj)
     {
       var other = obj as ObjectId;
       return Equals(other);
     }

     public bool Equals(ObjectId other)
     {
       return other != null && ToString() == other.ToString();
     }

     public static implicit operator string(ObjectId objectId)
     {
       return objectId == null ? null : objectId.ToString();
     }

     public static implicit operator ObjectId(string value)
     {
       return new ObjectId(value);
     }

     public static bool operator ==(ObjectId left, ObjectId right)
     {
       if (ReferenceEquals(left, right))
       {
         return true;
       }

       if (((object)left == null) || ((object)right == null))
       {
         return false;
       }

       return left.Equals(right);
     }

     public static bool operator !=(ObjectId left, ObjectId right)
     {
       return !(left == right);
     }
   }

C#實(shí)現(xiàn)ObjectId的生成器
復(fù)制代碼 代碼如下:

View Code
   internal static class ObjectIdGenerator
   {
     private static readonly DateTime Epoch =
       new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc);
     private static readonly object _innerLock = new object();
     private static int _counter;
     private static readonly byte[] _machineHash = GenerateHostHash();
     private static readonly byte[] _processId =
       BitConverter.GetBytes(GenerateProcessId());

     public static byte[] Generate()
     {
       var oid = new byte[12];
       var copyidx = 0;

       Array.Copy(BitConverter.GetBytes(GenerateTime()), 0, oid, copyidx, 4);
       copyidx += 4;

       Array.Copy(_machineHash, 0, oid, copyidx, 3);
       copyidx += 3;

       Array.Copy(_processId, 0, oid, copyidx, 2);
       copyidx += 2;

       Array.Copy(BitConverter.GetBytes(GenerateCounter()), 0, oid, copyidx, 3);

       return oid;
     }

     private static int GenerateTime()
     {
       var now = DateTime.UtcNow;
       var nowtime = new DateTime(Epoch.Year, Epoch.Month, Epoch.Day,
         now.Hour, now.Minute, now.Second, now.Millisecond);
       var diff = nowtime - Epoch;
       return Convert.ToInt32(Math.Floor(diff.TotalMilliseconds));
     }

     private static byte[] GenerateHostHash()
     {
       using (var md5 = MD5.Create())
       {
         var host = Dns.GetHostName();
         return md5.ComputeHash(Encoding.Default.GetBytes(host));
       }
     }

     private static int GenerateProcessId()
     {
       var process = Process.GetCurrentProcess();
       return process.Id;
     }

     private static int GenerateCounter()
     {
       lock (_innerLock)
       {
         return _counter++;
       }
     }
   }

使用舉例
復(fù)制代碼 代碼如下:

class Program
   {
     static void Main(string[] args)
     {
       Console.ForegroundColor = ConsoleColor.Red;

       ObjectId emptyOid = ObjectId.Empty;
       Console.WriteLine(emptyOid);

       Console.WriteLine();
       Console.ForegroundColor = ConsoleColor.Green;

       for (int i = 0; i < 10; i++)
       {
         ObjectId oid = ObjectId.NewObjectId();
         Console.WriteLine(oid);
       }

       Console.WriteLine();
       Console.ForegroundColor = ConsoleColor.Blue;

       ObjectId existingOid;
       ObjectId.TryParse("507f191e810c19729de860ea", out existingOid);
       Console.WriteLine(existingOid);

       Console.ReadKey();
     }
   }


 

相關(guān)文章

最新評(píng)論