Table of Contents

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 ITimestampProvider

ITimestampProvider to use in this generator.

params IdentifierGeneratorParams

Optional 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

Timestamp

HighValuesLeft

Specifies the maximum number of high values that this generator can use to create new identifiers.

public ulong HighValuesLeft { get; }

Property Value

ulong

Remarks

See High for more information.

IsOutOfValues

Specifies whether or not this generator can still create identifiers.

public bool IsOutOfValues { get; }

Property Value

bool

LastHighValue

Specifies the last High value of an Identifier created by this generator.

public ulong LastHighValue { get; }

Property Value

ulong

LastLowValue

Specifies the last Low value of an Identifier created by this generator.

public int LastLowValue { get; }

Property Value

int

LastTimestamp

Timestamp of the last Identifier created by this generator.

public Timestamp LastTimestamp { get; }

Property Value

Timestamp

LowValueBounds

Specifies the range of available Low values for identifiers created by this generator.

public Bounds<ushort> LowValueBounds { get; }

Property Value

Bounds<ushort>

LowValueOverflowStrategy

Specifies LowValueOverflowStrategy used by this generator.

public LowValueOverflowStrategy LowValueOverflowStrategy { get; }

Property Value

LowValueOverflowStrategy

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

int

Remarks

See Low for more information.

MaxTimestamp

Maximum possible Timestamp of an Identifier created by this generator.

public Timestamp MaxTimestamp { get; }

Property Value

Timestamp

StartTimestamp

Timestamp of the creation of this generator instance.

public Timestamp StartTimestamp { get; }

Property Value

Timestamp

TimeEpsilon

Specifies the time resolution of this generator.

public Duration TimeEpsilon { get; }

Property Value

Duration

Timestamps

ITimestampProvider instance used by this generator.

public ITimestampProvider Timestamps { get; }

Property Value

ITimestampProvider

ValuesLeft

Specifies the maximum number of identifiers that this generate can still create.

public ulong ValuesLeft { get; }

Property Value

ulong

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 Duration

Time 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 Identifier

Identifier to extract Timestamp from.

Returns

Timestamp

New Timestamp instance.

TryGenerate(out Identifier)

Attempts to generate a new Identifier.

public bool TryGenerate(out Identifier result)

Parameters

result Identifier

out 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.