java.lang.Object
me.ahoo.cosid.runtime.CosIdRuntime
- All Implemented Interfaces:
AutoCloseable
The one registry and lifecycle owner of stores, the machine slot and generators (07-runtime-and-spring.md ยง1).
Its generators are fixed at start (D17); there is no global singleton, and runtimes in one JVM do not share a slot.
Code that needs ids gets the runtime, or its generators, from whoever started it: in Spring, inject the
IdGenerator beans; static code (4.x DefaultIdGeneratorProvider.INSTANCE) keeps them in a holder of its
own, set when the context has started and cleared when it closes (the migration guide shows one).
Lookups of a disabled generator (D24, GeneratorSpec.Disabled) throw
GeneratorDisabledException.
-
Method Summary
Modifier and TypeMethodDescriptionvoidclose()Closes in reverse dependency order: generators (terminal state, time-based ones first), the slot (released with the largest timestamp they issued), stores, the own executor.describe()What each generator is: its layout, slot and window, its ready values and batch size, and the like.filler()FillsCosIdfields from this runtime's generators.The ids the generator of that name issued through the views of this runtime: itsgenerator,stringGenerator,text(name),getby an interface type,periodic(name)excluded, and thefiller(); a uuidv7 generator counts every id it issues, by its concrete type too.Thelonggenerator of that name.<T> TThe generator of that name astype: anIdGenerator,PartsIdGenerator(time-based) orGroupedIdGenerator(periodic segment), the concrete generator (TimeBasedGenerator,SegmentGenerator,PeriodicSegmentGenerator,UuidV7Generator; never counted), or forlongids aStringIdGenerator, anIntIdGenerator(range checked) or itsIdCodec; for the 80-bit CosId itsPartsCodec; for a periodic segment itsGroupedCodec.machines()The machines that started: those an enabled time-based generator chose (D37).The namespace of the runtime: of the default machine and of the segment keys.The periodic segment generator of that name.slot()The slot lease of the default machine.The slot lease of a machine (D37).static CosIdRuntimestart(RuntimeSpec spec, RuntimeOptions options) Checks the whole spec, then opens the stores, acquires the slot and creates the generators, rolling back what was created on failure.stringGenerator(String name) Thelonggenerator of that name with its codec.The text ids of the generator of that name, whatever its strategy (R1-int-01): the codec of itslongids, its parts codec (cosid80), its grouped codec (periodic segment) or the canonical UUID form (uuidv7, D46).
-
Method Details
-
start
Checks the whole spec, then opens the stores, acquires the slot and creates the generators, rolling back what was created on failure. The machine slot is acquired only when a time-based generator is enabled, and only the stores that the enabled generators and the slot use are opened.- Parameters:
spec- the specoptions- the options- Returns:
- the started runtime
- Throws:
CosIdException- if the spec is invalid (a time-based generator without a machine, a layout narrower than the machine bits, an unknown store, a store timeout above the reserve timeout, ...) or a step fails
-
namespace
The namespace of the runtime: of the default machine and of the segment keys.- Returns:
- the namespace
-
generator
Thelonggenerator of that name.- Parameters:
name- the generator name- Returns:
- the generator
- Throws:
NoSuchElementException- if there is none, or it issues nolongidsGeneratorDisabledException- if it is disabled
-
stringGenerator
Thelonggenerator of that name with its codec. For the text ids of any strategy, cosid80, periodic segments and uuidv7 included, usetext(String).- Parameters:
name- the generator name- Returns:
- the generator
- Throws:
NoSuchElementException- if there is none, or it has nolongids (cosid80, periodic segment, uuidv7: usetext(String))GeneratorDisabledException- if it is disabled
-
periodic
The periodic segment generator of that name.- Parameters:
name- the generator name- Returns:
- the generator
- Throws:
NoSuchElementException- if there is noneGeneratorDisabledException- if it is disabled
-
generated
The ids the generator of that name issued through the views of this runtime: itsgenerator,stringGenerator,text(name),getby an interface type,periodic(name)excluded, and thefiller(); a uuidv7 generator counts every id it issues, by its concrete type too.- Parameters:
name- the generator name- Returns:
- the count; empty unless
RuntimeOptions.countGenerated()is on, or when it is disabled - Throws:
NoSuchElementException- if there is none
-
get
The generator of that name astype: anIdGenerator,PartsIdGenerator(time-based) orGroupedIdGenerator(periodic segment), the concrete generator (TimeBasedGenerator,SegmentGenerator,PeriodicSegmentGenerator,UuidV7Generator; never counted), or forlongids aStringIdGenerator, anIntIdGenerator(range checked) or itsIdCodec; for the 80-bit CosId itsPartsCodec; for a periodic segment itsGroupedCodec.Whatever it returns belongs to the runtime (R1-core-18): never close it, even where its type is
AutoCloseable; it closes with the runtime (close()), and closing it earlier would break every other user of the generator.- Type Parameters:
T- the wanted type- Parameters:
name- the generator nametype- the wanted type- Returns:
- the generator
- Throws:
NoSuchElementException- if there is none of that typeGeneratorDisabledException- if it is disabled
-
describe
What each generator is: its layout, slot and window, its ready values and batch size, and the like.- Returns:
- the generators by name, in name order; a disabled one tells
disabled = true
-
slot
The slot lease of the default machine.- Returns:
- the lease, or empty without a leased default machine
-
slot
The slot lease of a machine (D37).- Parameters:
machine- the machine name; "default" for the default one- Returns:
- the lease, or empty when that machine is static or did not start (no enabled generator chose it)
-
machines
The machines that started: those an enabled time-based generator chose (D37).- Returns:
- the machine names in name order, "default" included when it started
-
text
The text ids of the generator of that name, whatever its strategy (R1-int-01): the codec of itslongids, its parts codec (cosid80), its grouped codec (periodic segment) or the canonical UUID form (uuidv7, D46).- Parameters:
name- the generator name- Returns:
- the text ids
- Throws:
NoSuchElementException- if there is noneGeneratorDisabledException- if it is disabled
-
filler
FillsCosIdfields from this runtime's generators.- Returns:
- the filler
-
close
public void close()Closes in reverse dependency order: generators (terminal state, time-based ones first), the slot (released with the largest timestamp they issued), stores, the own executor. Closing again does nothing.- Specified by:
closein interfaceAutoCloseable- Throws:
CosIdException- if a step failed; every step still ran
-