Class UniqueKeyEncoder

Namespace: redb.Core.Utils
Assembly: redb.Core.dll

Turns the value of a [RedbUnique] field into the 128-bit key stored in
_values._unique: the RedbMd5 hash of the value's canonical form.

The requirement is strict and two-sided: one logical value must give exactly one form, and
different logical values must not give the same one. A hash of an uncanonicalised value has the
same hole, only quieter — 1.50m and 1.5m would be two keys, 12:00+03:00
and 09:00Z two keys, "é" as one code point and as two would be two keys.


The input is the as it is about to be written — the typed column, not
the CLR value. That is what makes a recomputation from the database
(RecomputeUniqueAsync) produce the same key as the original save: both start from the
same representation. Precision is therefore pinned to what every provider keeps:
milliseconds for timestamps (SQLite stores a Julian REAL), 18 decimal places for
decimal (the column scale on PostgreSQL and MSSQL).


Rules, one per typed column; the column tag makes the form injective across columns:
_String: Unicode NFC. No trimming, no case folding — the application owns the
notion of its key (see )._Long: invariant decimal digits, one form for every integer width._Guid: canonical D form, lower case._Double: round-trip R; -0 is 0; NaN and infinities are
refused — they are not values that can be equal to themselves._DateTimeOffset: UTC, truncated to milliseconds, yyyy-MM-ddTHH:mm:ss.fffZ._Boolean: 1 / 0._Numeric: rounded to 18 places, trailing zeros dropped (G29), one zero._ByteArray: the bytes themselves; there is nothing to canonicalise.
A value with no typed column set (a NULL) has no key: NULL never takes part in uniqueness.


is stamped into _structures._unique_version when a structure's
keys are computed. Any change to the rules above — a different scale, trimming, another
algorithm — must bump it; scheme synchronisation then recomputes every key of a structure whose
stamp is behind, so stored keys never silently disagree with the encoder that checks them.

public static class UniqueKeyEncoder

Inheritance

Object→UniqueKeyEncoder

Methods

CanonicalText(RedbValue, out byte)

The canonical text of a scalar column, with the column tag; null when no scalar

public static string? CanonicalText(RedbValue value, out byte tag)

Compute(RedbValue, long?)

The salted form for collection-scoped element keys (S3): the same canonical content,

public static Guid? Compute(RedbValue value, long? collectionSalt)

Compute(RedbValue)

The key for value's typed content, or null when every typed column

public static Guid? Compute(RedbValue value)