Path: redb/redb.Export/Flat/FlatNaming.cs
Snapshot: redb, 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
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Export/Flat/FlatNaming.cs
----
using System.Security.Cryptography;
using System.Text;
namespace redb.Export.Flat;
///
/// Names of the flat tables and columns, and the rule that keeps every identifier within the
/// strictest provider limit.
///
/// Table names come from the code as they are: the Props class name for a scheme table and the
/// property path (EmployeeProps.HomeAddress.Building) for every table below it. Service
/// tables and service columns keep the redb spelling so they cannot be mistaken for user members.
///
///
public static class FlatNaming
{
///
/// Identifier limit applied on every provider: the 63 bytes of PostgreSQL, the strictest of the
/// three, so that a table carries the same name in any target database.
///
public const int MaxIdentifierBytes = 63;
/// Shared header table: one row per object, the flat copy of _objects.
public const string ObjectsTable = "objects";
/// Flat copy of _lists.
public const string ListsTable = "lists";
/// Flat copy of _list_items.
public const string ListItemsTable = "list_items";
/// Primary key column of every flat table.
public const string IdColumn = "_id";
///
/// The one sequence of the flat database, the counterpart of redb's global_identity. Every key
/// a writer leaves out is taken from it, so new rows of any table never collide.
///
public const string IdentitySequence = "flat_identity";
/// Reference from a nested or collection table to the row that owns it.
public const string OwnerColumn = "_owner_id";
/// Position of an array element.
public const string IndexColumn = "_idx";
/// Dictionary key; the prefix of the per-component columns of a tuple key.
public const string KeyColumn = "_key";
/// Payload of a collection whose elements are not classes.
public const string ValueColumn = "_value";
// "~" plus eight hex digits.
private const int HashSuffixBytes = 9;
///
/// Returns unchanged when it fits ;
/// otherwise cuts it on a character boundary and appends ~ with eight hex digits of the
/// SHA-256 of the full name. The result is stable across runs and providers.
///
public static string Shorten(string fullName)
{
ArgumentException.ThrowIfNullOrEmpty(fullName);
var bytes = Encoding.UTF8.GetBytes(fullName);
if (bytes.Length <= MaxIdentifierBytes)
return fullName;
var budget = MaxIdentifierBytes - HashSuffixBytes;
var kept = new StringBuilder();
var used = 0;
foreach (var rune in fullName.EnumerateRunes())
{
if (used + rune.Utf8SequenceLength > budget)
break;
kept.Append(rune.ToString());
used += rune.Utf8SequenceLength;
}
var hash = Convert.ToHexString(SHA256.HashData(bytes), 0, 4).ToLowerInvariant();
return $"{kept}~{hash}";
}
/// Full (unshortened) name of the table that holds of .
public static string ChildTable(string ownerFullName, string propertyName) => $"{ownerFullName}.{propertyName}";
}