TidGenerator class final
Generates TIDs (https://atproto.com/specs/tid) for use as record keys.
A TID packs a 64-bit integer -- one always-zero high bit, 53 bits of microseconds since the UNIX epoch, and a 10-bit clock identifier -- into 13 base32-sortable characters, so lexical order matches chronological order.
The generator is stateful because monotonicity is its whole point. A caller assigning record keys to several records at once will call next repeatedly inside the same microsecond, and a repeated or regressing TID silently reorders those records. next therefore never returns a value less than or equal to its previous result: when the clock reads the same or earlier -- because the resolution is too coarse, or because the wall clock stepped backwards after an NTP correction -- it advances by one microsecond instead.
The clock identifier is chosen randomly per instance and fixed for its lifetime; it is what keeps two concurrent writers from colliding on the same microsecond. There is deliberately no global instance: a process-wide generator would make monotonicity a hidden global invariant and leave the clock identifier untestable. Construct one where it is needed and keep it for as long as the keys it issues must stay ordered.
final generator = TidGenerator();
final rkey = generator.next();
Constructors
- TidGenerator()
- Returns a generator with a random clock identifier and the system clock.
- TidGenerator.withClockId(int clockId, {int now()?})
-
Returns a generator with an explicit
clockIdand an optionalnow, both injectable so tests are deterministic.
Properties
- hashCode → int
-
The hash code for this object.
no setterinherited
- runtimeType → Type
-
A representation of the runtime type of the object.
no setterinherited
Methods
-
next(
) → String - Returns the next TID, strictly greater than every TID this generator has returned before.
-
noSuchMethod(
Invocation invocation) → dynamic -
Invoked when a nonexistent method or property is accessed.
inherited
-
toString(
) → String -
A string representation of this object.
inherited
Operators
-
operator ==(
Object other) → bool -
The equality operator.
inherited