Skip to content
This repository has been archived by the owner on May 25, 2021. It is now read-only.

Commit

Permalink
added a couple methods to all generation of a node for TimeUUID based…
Browse files Browse the repository at this point in the history
… on the RFC 4122 specs
  • Loading branch information
nberardi committed Sep 25, 2012
1 parent 3be9d21 commit b930106
Showing 1 changed file with 59 additions and 11 deletions.
70 changes: 59 additions & 11 deletions src/GuidGenerator.cs
@@ -1,5 +1,7 @@
using System; using System;
using System.Diagnostics; using System.Diagnostics;
using System.Net;
using System.Net.NetworkInformation;


namespace FluentCassandra namespace FluentCassandra
{ {
Expand All @@ -9,18 +11,20 @@ namespace FluentCassandra
/// <seealso href="http://www.ietf.org/rfc/rfc4122.txt">RFC 4122 - A Universally Unique IDentifier (UUID) URN Namespace</seealso> /// <seealso href="http://www.ietf.org/rfc/rfc4122.txt">RFC 4122 - A Universally Unique IDentifier (UUID) URN Namespace</seealso>
public static partial class GuidGenerator public static partial class GuidGenerator
{ {
private static readonly Random Random;

// number of bytes in guid // number of bytes in guid
public const int ByteArraySize = 16; private const int ByteArraySize = 16;


// multiplex variant info // multiplex variant info
public const int VariantByte = 8; private const int VariantByte = 8;
public const int VariantByteMask = 0x3f; private const int VariantByteMask = 0x3f;
public const int VariantByteShift = 0x80; private const int VariantByteShift = 0x80;


// multiplex version info // multiplex version info
public const int VersionByte = 7; private const int VersionByte = 7;
public const int VersionByteMask = 0x0f; private const int VersionByteMask = 0x0f;
public const int VersionByteShift = 4; private const int VersionByteShift = 4;


// indexes within the uuid array for certain boundaries // indexes within the uuid array for certain boundaries
private const byte TimestampByte = 0; private const byte TimestampByte = 0;
Expand All @@ -30,15 +34,59 @@ public static partial class GuidGenerator
// offset to move from 1/1/0001, which is 0-time for .NET, to gregorian 0-time of 10/15/1582 // offset to move from 1/1/0001, which is 0-time for .NET, to gregorian 0-time of 10/15/1582
private static readonly DateTimeOffset GregorianCalendarStart = new DateTimeOffset(1582, 10, 15, 0, 0, 0, TimeSpan.Zero); private static readonly DateTimeOffset GregorianCalendarStart = new DateTimeOffset(1582, 10, 15, 0, 0, 0, TimeSpan.Zero);


// random clock sequence and node
public static byte[] DefaultNode { get; set; } public static byte[] DefaultNode { get; set; }


static GuidGenerator() static GuidGenerator()
{ {
DefaultNode = new byte[6]; Random = new Random();
DefaultNode = GetNodeBytes();
}

/// <summary>
/// Generates a random value for the node.
/// </summary>
/// <returns></returns>
public static byte[] GetNodeBytes()
{
var node = new byte[6];

Random.NextBytes(node);
return node;
}

/// <summary>
/// Generates a node based on the first 6 bytes of an IP address.
/// </summary>
/// <param name="ip"></param>
public static byte[] GetNodeBytes(IPAddress ip)
{
if (ip == null)
throw new ArgumentNullException("ip");

var bytes = ip.GetAddressBytes();

if (bytes.Length < 6)
throw new ArgumentOutOfRangeException("ip", "The passed in IP address must contain at least 6 bytes.");

var node = new byte[6];
Array.Copy(bytes, node, 6);

return node;
}

/// <summary>
/// Generates a node based on the bytes of the MAC address.
/// </summary>
/// <param name="mac"></param>
/// <remarks>The machines MAC address can be retrieved from <see cref="NetworkInterface.GetPhysicalAddress"/>.</remarks>
public static byte[] GetNodeBytes(PhysicalAddress mac)
{
if (mac == null)
throw new ArgumentNullException("mac");


var random = new Random(); var node = mac.GetAddressBytes();
random.NextBytes(DefaultNode);
return node;
} }


public static GuidVersion GetVersion(this Guid guid) public static GuidVersion GetVersion(this Guid guid)
Expand Down

0 comments on commit b930106

Please sign in to comment.