-
Notifications
You must be signed in to change notification settings - Fork 0
/
SequentialGuidGenerator.cs
62 lines (49 loc) · 2.28 KB
/
SequentialGuidGenerator.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
using System;
using System.Security.Cryptography;
namespace Peddler {
/// <summary>
/// A COMB (standing for "combined guid with timestamp") algorithm used to create
/// "sequential" <see cref="Guid" /> instances.
/// </summary>
/// <remarks>
/// <para>
/// Inspired by the examples Jeremy Todd's SequentialGuid GitHub project.
/// Check it out <a href="https://github.com/jhtodd/SequentialGuid">here</a>.
/// </para>
/// </remarks>
public class SequentialGuidGenerator : GuidGenerator {
private RandomNumberGenerator random { get; } = RandomNumberGenerator.Create();
/// <summary>
/// Generates a new, non-empty <see cref="Guid" /> instance.
/// </summary>
/// <remarks>
/// This utilizes then a custom implementation for <see cref="Guid" />
/// that takes in to account the current timestamp in order to generate
/// values that will be considered sequentially close together.
/// </remarks>
public override Guid Next() {
byte[] guidBytes = new byte[16];
// (1) Put in 10 random bytes starting at index 6 into the array.
var randomBytes = new byte[10];
this.random.GetBytes(randomBytes);
Buffer.BlockCopy(randomBytes, 0, guidBytes, 6, 10);
// (2) Put in 6 timestamp-baased bytes starting at index 0 into the array.
// If the system is little-endian, flip it so the most significant
// byte of the timestamp value is first.
var timestampBytes = BitConverter.GetBytes(DateTime.UtcNow.Ticks / 10000L);
if (BitConverter.IsLittleEndian) {
Array.Reverse(timestampBytes);
}
Buffer.BlockCopy(timestampBytes, 2, guidBytes, 0, 6);
// We have to compensate for the fact that .NET regards the Data1 and Data2
// block as an Int32 and an Int16, respectively.
// That means that it switches the order on little-endian systems.
// So again, we have to reverse.
if (BitConverter.IsLittleEndian) {
Array.Reverse(guidBytes, 0, 4);
Array.Reverse(guidBytes, 4, 2);
}
return new Guid(guidBytes);
}
}
}