Introduction
A globally unique ID generator library called Xid.Net is prepared for safe and direct usage in your code. It offers an interface akin to that of the System. Guid produces fewer ids (12 bytes compared to 16 in raw format, 20 compared to 36 when translated to a string). Xid generation is frequently much faster than Guid generation.
Xid generates globally unique IDs using the Mongo Object ID algorithm, using a different serialization (base32 vs. base64) to reduce length when sent as a string: Object-id can be found at https://docs.mongodb.org/manual/reference/.
Install the Xid.NET in .NET Core
Install-Package Xid.NET
Alternatively, using the .NET CLI
dotnet add package Xid.NET
Alternatively, use the NuGet Package Manager.
Usage
Creating a Xid
var xid = Xid.NewXid();
Console.WriteLine("{0} - {1}", nameof(xid), xid);
Convert Xid to string
var xid = Xid.NewXid();
Console.WriteLine("{0} - {1}", nameof(xid), xid);
string convertedXidToString = xid.ToString();
Console.WriteLine("\n{0} - {1}", nameof(convertedXidToString), convertedXidToString);
Covert string to Xid
var xid = "cpcklr6s0007qc1ba0qg";
Console.WriteLine("{0} - {1}", nameof(xid), xid);
var id = Xid.Parse(xid);
Console.WriteLine("\n{0} - {1}", nameof(id), id);
_ = Xid.TryParse(xid, out Xid xid1);
Console.WriteLine("\n{0} - {1}", nameof(xid1), xid1);
Convert Xid to bytes and bytes to Xid
var xid = Xid.NewXid();
Console.WriteLine("{0} - {1}", nameof(xid), xid);
var byteXid = xid.ToBytes();
Console.WriteLine("\n{0} - {1}", nameof(byteXid.Length), byteXid.Length);
var byteToXid = new Xid(byteXid);
Console.WriteLine("\n{0} - {1}", nameof(byteToXid), byteToXid);
Getting components of the Xid
var xid = Xid.NewXid();
Console.WriteLine("{0} - {1}", nameof(xid), xid);
var machineId = xid.GetMachineId();
Console.WriteLine("\nMachine {0} - {1}", nameof(machineId.Length), machineId.Length);
var processId = xid.GetProcessId();
Console.WriteLine("\n{0} - {1}", nameof(processId), processId);
var timeStamp = xid.GetTimestamp();
Console.WriteLine("\n{0} - {1}", nameof(timeStamp), timeStamp);
var counter = xid.GetCounter();
Console.WriteLine("\n{0} - {1}", nameof(counter), counter);
Xid implementation and format
Mongo 12 byte Object IDs are compatible with the binary representation of the ID. When stored in such form (20 bytes), the string representation employs base32 hex (without padding) for improved space economy. The sortable feature of the id is preserved by using the base32 hexadecimal version.
Because case sensitivity and the two non-alphanumeric characters could cause problems when transferred as a string between different computers, Xid does not use base64. Because Base36 is nonstandard, the resulting size is unpredictable (not bit aligned), and it would not be sortable if it was not used. Expect a 20-character, all lowercase sequence ([0-9a-v]{20}) with characters in the range of a to v and 0 to 9 numbers to validate a base32 xid.
The Xid.NewXid is thread-safe
Below is the format of a Xid is;
- 4-byte value representing the seconds since the Unix epoch,
- 3-byte machine identifier,
- 2-byte process id, and
- 3-byte counter, starting with a random value.
Features
- Size, greater than a snowflake, smaller than a UUID at 12 bytes (96 bits).
- By default, Base32 hex encoded (20 characters when sent as a readable string, but still sortable).
- Non-configured; no separate machine or data center ID needs to be created.
- K placed the order.
- The precision of 1 second in embedded time.
- 16,777,216 (24 bits) unique IDs each second and per host/process are guaranteed to be unique.
- Lock-free, as opposed to UUIDv1 and v2.
We learned the new technique and evolved together.
Happy coding!