Persistent Sequences

A persistent sequence allows you to retrieve a unique number even after the SV Server (or Designer) restarts. To do so, you need to create the sequence using a name and optionally a scope.

Example

The example below, instantiates a sequence and requests the next value.

JavaScript

var sequence = hpsv.sequenceBuilder.getOrCreate("nameOfSequence");

var value = sequence.getNextValue();

C#

var sequence = hpsv.SequenceBuilder.GetOrCreate("nameOfSequence");

var value = sequence.GetNextValue();

By default, each service gets its own instance of the sequence, even if you use the same sequence name. To share a sequence between multiple services, you need to explicitly define a scope. The scopes types are service scopes, server scopes, or custom scopes.

JavaScript

var sequence = hpsv.sequenceBuilder.getOrCreate("nameOfSequence", { scope: "server" });

C#

var sequence = hpsv.SequenceBuilder.ServerScope().GetOrCreate("nameOfSequence");

If you need a shared sequence, but do not want to add the sequence globally, (to prevent its accidental re-use), you can use a custom scope. You may want to nest the custom scopes in order to match your organizational structure.

JavaScript

var sequence = hpsv.sequenceBuilder.getOrCreate("nameOfSequence", { scope: "nameOfScope" });

C#

var sequence = hpsv.SequenceBuilder.CustomScope("nameOfScope").GetOrCreate("nameOfSequence");

By default, the sequence begins with the value 0, and increments by 1. When it reaches long.MaxValue it throws an exception when you try to get next the value. You can customize the configuration by using additional parameters during creation.

The example below sets the initial/minimal/maximal values, and instructs SV to start from the minimal value after it reaches the maximal value. The cache parameter is for performance optimization. This allows you to generate up to N values from memory at the cost of non-continuous values, in case there is server crash.

JavaScript

var sequence = hpsv.sequenceBuilder.getOrCreate("name", { minValue: 0, initialValue: 100, maxValue: 200, recycle: true, cache:20 });

C#

var sequence = hpsv.SequenceBuilder.GetOrCreate("name", cfg=>cfg.WithMinValue(10).WithInitialValue(100).WithMaxValue(200).Recycle().Cache(20));

You can use the sequence instance to get the current value or next value. You can also reset the sequence to the initial value or reconfigure it with different parameters.

Back to top

Sequence parameters

The table below describes the valid sequence parameters:

Description JavaScript C#

Minimal value.

Valid values: long.MinValue..long.MaxValue, not equal to MaxValue

Default value: 0

minValue WithMinValue()

Maximal value.

Valid values: long.MinValue..long.MaxValue, not equal to MinValue

Default value: log.MaxValue

maxValue WithMaxValue()

Specifies initial value.

Valid values: MinValue..MaxValue

Default value: MinValue

initialValue WithInitialValue()

Specifies the increment used by each GetNextValue() call.

Valid values:

For increasing sequence (for example, MinValue < MaxValue) a positive value.

For decreasing sequence (for example, MinValue > MaxValue) a negative value.

Default value: 1

increment WithIncrement()
Allows the sequence to revert back to MinValue once MaxValue is reached. recycle Recycle()/NoRecycle()

Specifies the number of values which are kept in memory, but not written to the database, for sequence persistency (This is an internal performance optimization.) However, when SV fails, this does not guarantee a continuous sequence of values (that is, some values may be lost), although uniqueness of values is always preserved.

Valid values: 0..count of values between MinValue and MaxCalue

Default value: 0

cache Cache()

Specifies (in days) how long SV will keep the sequence. Once this duration has elapsed, the sequence is deleted.

Valid values: 0..int.MaxValue

Default value: 30

keepDays IfUnusedKeepFor()
Instructs SV to keep the sequence forever even if not used. keepForever IfUnusedKeepForever()

Back to top

Sequence Builder methods

The table below describes the valid sequence builder methods:

Description JavaScript C#
Gets existing sequence by name, or creates a new one (using configure) from current scope. getOrCreate() GetOrCreate()
Reconfigures existing sequence by name, or creates a new one (using configure) from current scope. reconfigure() Reconfigure()
Gets all existing sequences within current scope. getAll() GetAll()
Deletes the sequence by name within current scope. It silently ignores non existing sequence names. delete() Delete()

Back to top

Sequence methods

The table below describes the valid sequence methods:

Description JavaScript C#
Gets current value of the sequence. getCurrentValue() GetCurrentValue()
Increments the value of the sequence and returns the new value. getNextValue() GetNextValue()
Resets the value to InitialValue. reset() Reset()

Back to top

See also: