Path: redb-route/redb.Route.Core/RedbUniqueKey.cs
Snapshot: redb-route, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb-route
Page: https://redbase.app/src/dev-2026-12-06/redb-route/redb.Route.Core/RedbUniqueKey.cs
----
using System.Security.Cryptography;
using System.Text;
namespace redb.Route.RedbCore;
///
/// Normalizes an application-composed business key so it fits the redb object key column
/// _objects._value_unique (440 characters, enforced in C# by the core on every provider)
/// and MSSQL's _objects._name (nvarchar(450)). A short key passes through untouched and
/// stays readable; an over-long one keeps a readable prefix and gets a SHA-256 tail computed
/// over the FULL original, so distinct long keys stay distinct and the mapping is deterministic.
/// Shared by and the redb.Route.Llm stores
/// (client-supplied keys — conversation ids, message keys — have no length contract).
///
public static class RedbUniqueKey
{
/// The hard cap of _objects._value_unique.
public const int MaxLength = 440;
/// Deterministic 440-safe form of .
public static string Normalize(string raw)
{
ArgumentNullException.ThrowIfNull(raw);
if (raw.Length <= MaxLength) return raw;
var hash = Convert.ToHexString(SHA256.HashData(Encoding.UTF8.GetBytes(raw)));
var cut = MaxLength - hash.Length - 1;
// Never split a surrogate pair: a prefix ending in a lone high surrogate is not
// encodable to UTF-8 and the provider would reject the save of the whole row.
if (char.IsHighSurrogate(raw[cut - 1]))
cut--;
return $"{raw[..cut]}#{hash}";
}
}