Class DistributedUIDGenerator
A Key generator that uses the current time in ms with the machine id to generate a unique value every time. This means it is safe for multiple users to add entries to the same table without suffering from conflicts due to the entries using the same key but having different values.
The implementation is based on this article https://www.callicoder.com/distributed-unique-id-sequence-number-generator/
The Key is made up of the following components:
Sequence Number12 Bits(0 - 11)A local counter per machine that starts at 0 and is incremented by 1 for each new id request that is made during the same millisecond. The value is limited to 12 bytes so can contain 4095 items before the ids for this millisecond are exhausted and the id generator must wait until the next millisecond before it can continue. | |
Machine Id10 Bits(12-21)The Id of the machine. By default, in the Editor, this value is generated from the machines network interface physical address however it can also be set to a user provided value. There is enough space for 1024 machines. | |
Epoch Timestamp.41 Bits(22-63)A timestamp using a custom epoch which is the time the class was created. The maximum timestamp that can be represented is 69 years from the custom epoch, at this point the Key generator will have exhausted its possible unique Ids. |
Namespace: UnityEngine.Localization.Tables
Syntax
public class DistributedUIDGenerator : object, IKeyGenerator
Constructors
DistributedUIDGenerator()
Create a default instance which uses the current time as the CustomEpoch the machines physical address as MachineId.
Declaration
public DistributedUIDGenerator()
DistributedUIDGenerator(Int64)
Creates an instance with a defined CustomEpoch.
Declaration
public DistributedUIDGenerator(long customEpoch)
Parameters
Type | Name | Description |
---|---|---|
Int64 | customEpoch | The custom epoch is used to calculate the timestamp by taking the current time and subtracting the CustomEpoch. The value is then stored in 41 bits giving it a maximum time of 69 years. |
Fields
MachineIdPrefKey
The name of the EditorPrefs that is used to store the machine id.
Declaration
public const string MachineIdPrefKey = null
Field Value
Type | Description |
---|---|
String |
Properties
CustomEpoch
The custom epoch used to generate the timestamp.
Declaration
public long CustomEpoch { get; }
Property Value
Type | Description |
---|---|
Int64 |
MachineId
The Id of the current machine. By default, in the Editor, this value is generated from the machines network interface physical address however it can also be set to a user provided value. There is enough space for 1024 unique machines. Set value will be clamped in the range 1-1023. The value is not serialized into the asset but stored into the EditorPrefs(Editor only).
Declaration
public int MachineId { get; set; }
Property Value
Type | Description |
---|---|
Int32 |
Methods
GetNextKey()
Returns the next Id using the current time, machine id and sequence number.
Declaration
public long GetNextKey()
Returns
Type | Description |
---|---|
Int64 |