. Net ULID 实现
A .Net ULID implementation
A GUID/UUID can be suboptimal for many use-cases because:
A ULID however:
PM> Install-Package NUlidOr simply use the Nuget package manager GUI in Visual Studio.
Creating a ULID:
// Create a ULID
var myulid = Ulid.NewUlid();
// Print ULID
Console.WriteLine(myulid);Output:
01ASB2XFCZJY7WHZ2FNRTMQJCT
Parsing a ULID:
// Parse ULID:
var myulid = Ulid.Parse("01ASB2XFCZJY7WHZ2FNRTMQJCT");
// Print time-part of ULID:
Console.WriteLine(myulid.Time);Output:
4-8-2016 15:31:59 +00:00
You can also convert from/to GUID/UUID's, get the byte-representation of a ULID, create a ULID with specific timestamp and you can even specify an IUlidRng to use for generating the randomness (by default NUlid uses the CSUlidRng but a SimpleUlidRng is also provided, as well as a MonotonicUlidRng). The ULID is implemented as a struct with (operator) overloads for (in)equality, comparison etc. built-in and is, generally, very much like .Net's native Guid struct. An extensive helpfile is provided in the Nuget package and the testsuite also serves as a (simple) demonstration of NUlid's features.
Below is the current specification of ULID as implemented in this repository.
01AN4Z07BY 79KA1307SR9X4MV3
|----------| |----------------|
Timestamp Randomness
10 chars 16 chars
48bits 80bits
base32 base32Timestamp
Randomness
As of v1.4.0 monotonic ULID's are supported (see below).
When generating a ULID within the same millisecond, it is possible to provide some guarantees regarding sort order (with some caveats). When you use the MonotonicUlidRng and a newly generated ULID in the same millisecond is detected, the random component is incremented by 1 bit in the least significant bit position (with carrying). For example:
// Create monotonic rng
var rng = new MonotonicRng();
// Create ULIDs, assume that these calls occur within the same millisecond:
Console.WriteLine(Ulid.NewUlid(rng)); // 01DBN5W2SG000DCBVYHX4T6MCX
Console.WriteLine(Ulid.NewUlid(rng)); // 01DBN5W2SG000DCBVYHX4T6MCY
Console.WriteLine(Ulid.NewUlid(rng)); // 01DBN5W2SG000DCBVYHX4T6MCZ
Console.WriteLine(Ulid.NewUlid(rng)); // 01DBN5W2SG000DCBVYHX4T6MD0
Console.WriteLine(Ulid.NewUlid(rng)); // 01DBN5W2SG000DCBVYHX4T6MD1
Console.WriteLine(Ulid.NewUlid(rng)); // 01DBN5W2SG000DCBVYHX4T6MD2By default the most significant bit of the random part is set to zero; this ensures you can generate enough ULID's after the initial one before causing an overflow. Some implementations simply pick a random value for the random part and increment this value, however, there's a (very small) chance that this random part is close to the overflow value. If you then happen to generate a lot of ULID's within the same millisecond there is a risk the you hit the overflow. By our method we ensure there's enough 'room' for new values before 'running out of values' (overflowing). It is, with some effort, even possible to 'resume counting' from any given ULID.
Crockford's Base32 is used as shown. This alphabet excludes the letters I, L, O, and U to avoid confusion and abuse.
0123456789ABCDEFGHJKMNPQRSTVWXYZThe components are encoded as 16 octets. Each component is encoded with the Most Significant Byte first (network byte order).
…ttttttttttrrrrrrrrrrrrrrrrWhere:
t is Timestamp
r is Randomness
Based on / inspired by alizain/ulid.
Below measurements are based on an Intel(R) Core(TM) i9-10900X CPU @ 3.70Ghz:
…暂无开放 Issues,或尚未同步最近议题。