|
| 1 | +/* Copyright 2010-2013 10gen Inc. |
| 2 | +* |
| 3 | +* Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +* you may not use this file except in compliance with the License. |
| 5 | +* You may obtain a copy of the License at |
| 6 | +* |
| 7 | +* http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +* |
| 9 | +* Unless required by applicable law or agreed to in writing, software |
| 10 | +* distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +* See the License for the specific language governing permissions and |
| 13 | +* limitations under the License. |
| 14 | +*/ |
| 15 | + |
| 16 | +using System; |
| 17 | +using System.Diagnostics; |
| 18 | +using System.Runtime.CompilerServices; |
| 19 | +using System.Security; |
| 20 | +using System.Security.Cryptography; |
| 21 | +using System.Text; |
| 22 | +using System.Threading; |
| 23 | + |
| 24 | +namespace MongoDB.Bson.Serialization.IdGenerators |
| 25 | +{ |
| 26 | + /// <summary> |
| 27 | + /// A GUID generator that generates GUIDs in ascending order. To enable |
| 28 | + /// an index to make use of the ascending nature make sure to use |
| 29 | + /// <see cref="GuidRepresentation.Standard">GuidRepresentation.Standard</see> |
| 30 | + /// as the storage representation. |
| 31 | + /// Internally the GUID is of the form |
| 32 | + /// 8 bytes: Ticks from DateTime.UtcNow.Ticks |
| 33 | + /// 3 bytes: hash of machine name |
| 34 | + /// 2 bytes: low order bytes of process Id |
| 35 | + /// 3 bytes: increment |
| 36 | + /// </summary> |
| 37 | + public class AscendingGuidGenerator : IIdGenerator |
| 38 | + { |
| 39 | + // private static fields |
| 40 | + private static readonly AscendingGuidGenerator __instance = new AscendingGuidGenerator(); |
| 41 | + private static readonly byte[] __machineProcessId; |
| 42 | + private static int __increment; |
| 43 | + |
| 44 | + // static constructor |
| 45 | + static AscendingGuidGenerator() |
| 46 | + { |
| 47 | + var machineHash = GetMachineHash(); |
| 48 | + short processId; |
| 49 | + try |
| 50 | + { |
| 51 | + // use low order two bytes only |
| 52 | + processId = (short)GetCurrentProcessId(); |
| 53 | + } |
| 54 | + catch (SecurityException) |
| 55 | + { |
| 56 | + processId = 0; |
| 57 | + } |
| 58 | + |
| 59 | + __machineProcessId = new byte[5] |
| 60 | + { |
| 61 | + machineHash[0], |
| 62 | + machineHash[1], |
| 63 | + machineHash[2], |
| 64 | + (byte)(processId >> 8), |
| 65 | + (byte)(processId) |
| 66 | + }; |
| 67 | + } |
| 68 | + |
| 69 | + // public static properties |
| 70 | + |
| 71 | + /// <summary> |
| 72 | + /// Gets an instance of AscendingGuidGenerator. |
| 73 | + /// </summary> |
| 74 | + public static AscendingGuidGenerator Instance |
| 75 | + { |
| 76 | + get { return __instance; } |
| 77 | + } |
| 78 | + |
| 79 | + // public methods |
| 80 | + |
| 81 | + /// <summary> |
| 82 | + /// Generates an ascending Guid for a document. Consecutive invocations |
| 83 | + /// should generate Guids that are ascending from a MongoDB perspective |
| 84 | + /// </summary> |
| 85 | + /// <param name="container">The container of the document (will be a |
| 86 | + /// MongoCollection when called from the driver). </param> |
| 87 | + /// <param name="document">The document it was generated for.</param> |
| 88 | + /// <returns>A Guid.</returns> |
| 89 | + public object GenerateId(object container, object document) |
| 90 | + { |
| 91 | + var increment = Interlocked.Increment(ref __increment) & 0x00ffffff; |
| 92 | + return GenerateId(DateTime.UtcNow.Ticks, __machineProcessId, increment); |
| 93 | + } |
| 94 | + |
| 95 | + /// <summary> |
| 96 | + /// Generates a Guid for a document. Note - this is purely used for |
| 97 | + /// unit testing |
| 98 | + /// </summary> |
| 99 | + /// <param name="tickCount">The time portion of the Guid</param> |
| 100 | + /// <param name="machineProcessId">A 5 byte array with the first 3 bytes |
| 101 | + /// representing a machine id and the next 2 representing a process |
| 102 | + /// id</param> |
| 103 | + /// <param name="increment">The increment portion of the Guid. Used |
| 104 | + /// to distinguish between 2 Guids that have the timestamp. Note |
| 105 | + /// only the least significant 3 bytes are used.</param> |
| 106 | + /// <returns>A Guid.</returns> |
| 107 | + public object GenerateId( |
| 108 | + long tickCount, |
| 109 | + byte[] machineProcessId, |
| 110 | + int increment) |
| 111 | + { |
| 112 | + var a = (int)(tickCount >> 32); |
| 113 | + var b = (short)(tickCount >> 16); |
| 114 | + var c = (short)(tickCount); |
| 115 | + var d = new byte[8]; |
| 116 | + Array.Copy(machineProcessId, d, 5); |
| 117 | + d[5] = (byte)(increment >> 16); |
| 118 | + d[6] = (byte)(increment >> 8); |
| 119 | + d[7] = (byte)(increment); |
| 120 | + return new Guid (a, b, c, d); |
| 121 | + } |
| 122 | + |
| 123 | + /// <summary> |
| 124 | + /// Tests whether an id is empty. |
| 125 | + /// </summary> |
| 126 | + /// <param name="id">The id to test.</param> |
| 127 | + /// <returns>True if the Id is empty. False otherwise</returns> |
| 128 | + public bool IsEmpty(object id) |
| 129 | + { |
| 130 | + return id == null || (Guid)id == Guid.Empty; |
| 131 | + } |
| 132 | + |
| 133 | + // private static methods |
| 134 | + /// <summary> |
| 135 | + /// Gets the current process id. This method exists because of how |
| 136 | + /// CAS operates on the call stack, checking for permissions before |
| 137 | + /// executing the method. Hence, if we inlined this call, the calling |
| 138 | + /// method would not execute before throwing an exception requiring the |
| 139 | + /// try/catch at an even higher level that we don't necessarily control. |
| 140 | + /// </summary> |
| 141 | + [MethodImpl(MethodImplOptions.NoInlining)] |
| 142 | + private static int GetCurrentProcessId() |
| 143 | + { |
| 144 | + return Process.GetCurrentProcess().Id; |
| 145 | + } |
| 146 | + |
| 147 | + private static byte[] GetMachineHash() |
| 148 | + { |
| 149 | + // use instead of Dns.HostName so it will work offline |
| 150 | + var hostName = Environment.MachineName; |
| 151 | + var sha1 = SHA1.Create(); |
| 152 | + return sha1.ComputeHash(Encoding.UTF8.GetBytes(hostName)); |
| 153 | + } |
| 154 | + } |
| 155 | +} |
0 commit comments