public class Sequences extends Object
For each sequence name, a call to generateId(String)
will return a unique number. The initial or next
number being returned can be specified by setNextValue(String, long, boolean)
.
Note that these sequences are global and not tenant aware. Therefore care must be taken to generate unique names for
sequences. A viable option is to use BaseEntity.getUniqueName()
of the entity which utilizes
this generator.
Modifier and Type | Field and Description |
---|---|
static String |
FRAMEWORK_SEQUENCES
Names the framework which must be enabled to activate the sequences feature.
|
static sirius.kernel.health.Log |
LOG |
Constructor and Description |
---|
Sequences() |
Modifier and Type | Method and Description |
---|---|
long |
generateId(String sequence)
Returns the next value in the given sequence.
|
List<String> |
getKnownSequences()
Enumerates all sequences known to the framework.
|
long |
peekNextValue(String sequence)
Identifies the next value for the given sequence without using it.
|
void |
setNextValue(String sequence,
long nextValue,
boolean force)
Sets the initial or next value of the given sequence.
|
public static final String FRAMEWORK_SEQUENCES
public static final sirius.kernel.health.Log LOG
public long generateId(String sequence)
Note that this method doesn't use locks or transactions. It rather utilizes optimistic locking, which scales extremely well. However, the algorithm used is not intended for extreme parallel usage. In such scenarios, it will not content but rather give up after some tries and report an appropriate exception.
sequence
- the name of the sequence which is counted up.sirius.kernel.health.HandledException
- If the system was unable to generate a unique sequence number. This might happen in
extreme load conditions, as internally an optimistic locking algorithm is employed.public void setNextValue(String sequence, long nextValue, boolean force)
Unless force is set to true, the value has to be higher than the current counter value to prevent non unique numbers from being generated.
sequence
- the sequence to updatenextValue
- the next value that will be returned when calling generateId(String)
for this
sequence.force
- if true, no sanity checks are performed and the sequence can be reset to ANY
value. This is rather dangerous, as it might lead to the generation of duplicate ids. If
set to false, the given nextValue has to be higher than the current sequence
value.public long peekNextValue(String sequence)
Note that this method is only used for reporting and statistics and must never be called by production code, as there is no guarantee that there isn't a parallel thread which currently acquires the returned value while this method is running.
sequence
- the sequence to peek the next value forCopyright © 2018. All rights reserved.