Skip to content

Commit

Permalink
Move OidGenerator to Uitl. Oids can be generated via Oid.NewOid()
Browse files Browse the repository at this point in the history
  • Loading branch information
lanwin committed May 25, 2010
1 parent bbc0782 commit d5675b6
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 106 deletions.
2 changes: 1 addition & 1 deletion source/MongoDB/MongoDB.csproj
Expand Up @@ -301,7 +301,7 @@
<Compile Include="MongoServerEndPoint.cs" />
<Compile Include="Oid.cs" />
<Compile Include="MongoRegex.cs" />
<Compile Include="OidGenerator.cs" />
<Compile Include="Util\OidGenerator.cs" />
<Compile Include="Code.cs" />
<Compile Include="CodeWScope.cs" />
<Compile Include="Binary.cs" />
Expand Down
105 changes: 0 additions & 105 deletions source/MongoDB/OidGenerator.cs

This file was deleted.

112 changes: 112 additions & 0 deletions source/MongoDB/Util/OidGenerator.cs
@@ -0,0 +1,112 @@
using System;
using System.Diagnostics;
using System.Net;
using System.Security.Cryptography;
using System.Text;
using MongoDB.Bson;

namespace MongoDB
{
/// <summary>
/// </summary>
internal class OidGenerator
{
private readonly object inclock = new object();
private int inc;
private byte[] machineHash;
private byte[] procID;

/// <summary>
/// Initializes a new instance of the <see cref = "OidGenerator" /> class.
/// </summary>
public OidGenerator()
{
GenerateConstants();
}

/// <summary>
/// Generates this instance.
/// </summary>
/// <returns></returns>
public Oid Generate()
{
//FIXME Endian issues with this code.
//.Net runs in native endian mode which is usually little endian.
//Big endian machines don't need the reversing (Linux+PPC, XNA on XBox)
var oid = new byte[12];
var copyidx = 0;

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

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

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

var inc = BitConverter.GetBytes(GenerateInc());
Array.Reverse(inc);
Array.Copy(inc, 1, oid, copyidx, 3);

return new Oid(oid);
}

/// <summary>
/// Generates the time.
/// </summary>
/// <returns></returns>
private int GenerateTime()
{
var now = DateTime.UtcNow;
//DateTime nowtime = new DateTime(epoch.Year, epoch.Month, epoch.Day, now.Hour, now.Minute, now.Second, now.Millisecond);
var diff = now - BsonInfo.Epoch;
return Convert.ToInt32(Math.Floor(diff.TotalSeconds));
}

/// <summary>
/// Generates the inc.
/// </summary>
/// <returns></returns>
private int GenerateInc()
{
lock(inclock)
{
return ++inc;
}
}

/// <summary>
/// Generates the constants.
/// </summary>
private void GenerateConstants()
{
machineHash = GenerateHostHash();
procID = BitConverter.GetBytes(GenerateProcId());
Array.Reverse(procID);
}

/// <summary>
/// Generates the host hash.
/// </summary>
/// <returns></returns>
private byte[] GenerateHostHash()
{
var md5 = MD5.Create();
var host = Dns.GetHostName();
return md5.ComputeHash(Encoding.Default.GetBytes(host));
}

/// <summary>
/// Generates the proc id.
/// </summary>
/// <returns></returns>
private int GenerateProcId()
{
var proc = Process.GetCurrentProcess();
return proc.Id;
}
}
}

0 comments on commit d5675b6

Please sign in to comment.