Class IdentifierGenerator
- Namespace
- LfrlAnvil.Identifiers
- Assembly
- LfrlAnvil.Identifiers.dll
Represents a generator of Identifier instances.
public sealed class IdentifierGenerator : IIdentifierGenerator, IGenerator<Identifier>, IGenerator
- Inheritance
-
IdentifierGenerator
- Implements
- Inherited Members
Remarks
Generators use the High value as a representation of the time slice during which an Identifier has been created and the Low value as a sequential number unique within a single High value (or a time slice).
Constructors
IdentifierGenerator(ITimestampProvider, IdentifierGeneratorParams)
Creates a new IdentifierGenerator instance.
public IdentifierGenerator(ITimestampProvider timestamps, IdentifierGeneratorParams @params = default)
Parameters
timestamps
ITimestampProviderITimestampProvider to use in this generator.
params
IdentifierGeneratorParamsOptional parameters. See IdentifierGeneratorParams for more information.
Exceptions
- ArgumentOutOfRangeException
When TimeEpsilon is less than or equal to Zero or the current Timestamp returned by the ITimestampProvider instance is less than Zero or BaseTimestamp is not between Zero and the current Timestamp.
Properties
BaseTimestamp
Timestamp of the first possible Identifier created by this generator.
public Timestamp BaseTimestamp { get; }
Property Value
HighValuesLeft
Specifies the maximum number of high values that this generator can use to create new identifiers.
public ulong HighValuesLeft { get; }
Property Value
Remarks
See High for more information.
IsOutOfValues
Specifies whether or not this generator can still create identifiers.
public bool IsOutOfValues { get; }
Property Value
LastHighValue
Specifies the last High value of an Identifier created by this generator.
public ulong LastHighValue { get; }
Property Value
LastLowValue
Specifies the last Low value of an Identifier created by this generator.
public int LastLowValue { get; }
Property Value
LastTimestamp
Timestamp of the last Identifier created by this generator.
public Timestamp LastTimestamp { get; }
Property Value
LowValueBounds
Specifies the range of available Low values for identifiers created by this generator.
public Bounds<ushort> LowValueBounds { get; }
Property Value
LowValueOverflowStrategy
Specifies LowValueOverflowStrategy used by this generator.
public LowValueOverflowStrategy LowValueOverflowStrategy { get; }
Property Value
LowValuesLeft
Specifies the maximum number of identifiers this generator can still create for the current time slice without having to resolve low value overflow.
public int LowValuesLeft { get; }
Property Value
Remarks
See Low for more information.
MaxTimestamp
Maximum possible Timestamp of an Identifier created by this generator.
public Timestamp MaxTimestamp { get; }
Property Value
StartTimestamp
Timestamp of the creation of this generator instance.
public Timestamp StartTimestamp { get; }
Property Value
TimeEpsilon
Specifies the time resolution of this generator.
public Duration TimeEpsilon { get; }
Property Value
Timestamps
ITimestampProvider instance used by this generator.
public ITimestampProvider Timestamps { get; }
Property Value
ValuesLeft
Specifies the maximum number of identifiers that this generate can still create.
public ulong ValuesLeft { get; }
Property Value
Methods
CalculateThroughput(Duration)
Calculates maximum possible number of identifiers that this generator can produce in the given time, without having to resort to LowValueOverflowStrategy resolution.
[Pure]
public ulong CalculateThroughput(Duration duration)
Parameters
duration
DurationTime to calculate this generator's throughput for.
Returns
- ulong
Maximum possible number of identifiers that this generator can produce in the given time.
Generate()
Generates a new Identifier.
public Identifier Generate()
Returns
- Identifier
New Identifier instance.
Remarks
Generators will fail to create an Identifier when they are completely out of values or when low value overflow has occurred and the current LowValueOverflowStrategy is equal to Forbidden.
Exceptions
- IdentifierGenerationException
When generator has failed to create a new Identifier.
GetTimestamp(Identifier)
Extracts a Timestamp used to create the provided id
.
[Pure]
public Timestamp GetTimestamp(Identifier id)
Parameters
id
IdentifierIdentifier to extract Timestamp from.
Returns
TryGenerate(out Identifier)
Attempts to generate a new Identifier.
public bool TryGenerate(out Identifier result)
Parameters
result
Identifierout parameter that returns generated Identifier if successful.
Returns
- bool
true if an Identifier has been generated successfully, otherwise false.
Remarks
Generators will fail to create an Identifier when they are completely out of values or when low value overflow has occurred and the current LowValueOverflowStrategy is equal to Forbidden.