Path: redb/redb.Core/Data/RootStructureKey.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.Core/Data/RootStructureKey.cs ---- using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; using Microsoft.Extensions.Logging; using redb.Core.Models.Entities; using redb.Core.Query; using redb.Core.Utils; namespace redb.Core.Data { /// /// The unique key of the root structures of a scheme: one field name once per scheme. The key of /// _structures (_id_scheme, _name, _id_parent) lets a root field in twice on PostgreSQL and SQLite, where two /// NULL parents are distinct, and nodes syncing one scheme at once used to do exactly that (SQL Server counts NULLs /// as equal and refused it). The SQL is the dialect's (the "root structure key" group of ); /// this class decides. /// /// /// A database written before the schema lock may hold such duplicates already, and the key cannot be built over /// them. A duplicate nothing points to (no value, no child with a value, no structure overriding it, no migration /// record) is removed with its subtree; one with values is left alone, and the key waits until the duplicates are /// resolved by hand (UPGRADING.md, "Duplicate root structures"). Run under the schema lock, in one transaction. /// /// public static class RootStructureKey { /// Name of the unique key, as the dialects create it. public const string IndexName = "UX__structures__root"; /// /// Removes the empty root duplicates and builds the key when none with values remain. Called only for a dialect /// whose is not null. /// /// The context of the service; joins its transaction or opens one. /// The dialect. /// Whether _migrations exists; its rows point to structures. /// Where removals are reported. /// Cancellation. /// True when the key exists afterwards. public static async Task EnsureAsync( IRedbContext context, ISqlDialect sql, bool hasMigrationsTable, ILogger? logger, CancellationToken cancellationToken = default) { IRedbTransaction? own = context.IsInTransaction ? null : await context.BeginTransactionAsync(cancellationToken: cancellationToken); try { var created = await EnsureInTransactionAsync(context, sql, hasMigrationsTable, logger, cancellationToken); if (own != null) await own.CommitAsync(); return created; } catch (Exception failure) { if (own != null) await RollbackAfterFailure.RollbackAsync(own, failure); throw; } finally { if (own != null) await own.DisposeAsync(); } } private static async Task EnsureInTransactionAsync( IRedbContext context, ISqlDialect sql, bool hasMigrationsTable, ILogger? logger, CancellationToken cancellationToken) { var roots = await context.QueryAsync( sql.Structures_SelectRootDuplicates(), Array.Empty(), cancellationToken); // PostgreSQL: the foreign key of _values cascades on delete, so a node of the previous version writing into // a duplicate between the look and the delete would lose its value. Locked first, its insert waits and then // fails on the key. SQLite: no lock statement, the schema lock is the only writer. var lockSubtree = sql.Structures_LockSubtree(); var unresolved = new List(); var touchedSchemes = new HashSet(); foreach (var group in roots.GroupBy(r => (r.IdScheme, r.Name))) { var used = new List(); var empty = new List(); foreach (var root in group) { if (lockSubtree != null) await context.ExecuteAsync(lockSubtree, new object[] { root.Id }, cancellationToken); // Asked after the lock, in a statement of its own: it sees every value committed before the lock. var referenced = await context.ExecuteScalarAsync( sql.Structures_SubtreeIsReferenced(hasMigrationsTable), new object[] { root.Id }, cancellationToken); (referenced == 1 ? used : empty).Add(root); } // One copy stays: the one with values, or the first one when none has any. var keep = used.Count > 0 ? used : new List { empty[0] }; var remove = empty.Where(e => !keep.Contains(e)).ToList(); var schemeName = await SchemeNameAsync(context, sql, group.Key.IdScheme, cancellationToken); foreach (var duplicate in remove) { await context.ExecuteAsync(sql.Structures_DeleteSubtreeMetadataCache(), new object[] { duplicate.Id }, cancellationToken); // The children go with it (ON DELETE CASCADE of _id_parent); none of them holds a value. await context.ExecuteAsync(sql.Structures_DeleteById(), new object[] { duplicate.Id }, cancellationToken); touchedSchemes.Add(group.Key.IdScheme); } if (remove.Count > 0) logger?.LogInformation( "REDB schema upgrade: removed {Count} empty duplicate(s) of root field '{Field}' of scheme '{Scheme}' " + "(id {SchemeId}): structure id(s) {Ids}; kept {Kept}.", remove.Count, group.Key.Name, schemeName, group.Key.IdScheme, string.Join(", ", remove.Select(r => r.Id)), string.Join(", ", keep.Select(k => k.Id))); if (used.Count > 1) unresolved.Add($"'{schemeName}' (id {group.Key.IdScheme}) field '{group.Key.Name}': structures " + string.Join(", ", used.Select(u => u.Id))); } foreach (var schemeId in touchedSchemes) { // The nodes compare the hash with their cached structure tree: a new one makes them reload it. var structures = await context.QueryAsync( sql.Structures_SelectBySchemeShort(), new object[] { schemeId }, cancellationToken); await context.ExecuteAsync(sql.Schemes_UpdateHash(), new object[] { SchemeHashCalculator.ComputeSchemeStructureHash(structures), schemeId }, cancellationToken); await context.ExecuteAsync(sql.Schemes_SyncMetadataCache(), new object[] { schemeId }, cancellationToken); } if (unresolved.Count > 0) { // Each remaining duplicate is logged as an error by the schema check of the start that follows // (FindDuplicatesAsync), at every start until resolved. logger?.LogInformation( "REDB schema upgrade: the unique key of root structures ({Index}) is not built while duplicates holding " + "values remain: {Duplicates}.", IndexName, string.Join("; ", unresolved)); return false; } await context.ExecuteAsync(sql.Structures_CreateRootKey(), Array.Empty(), cancellationToken); logger?.LogInformation("REDB schema upgrade: the unique key of root structures ({Index}) is built.", IndexName); return true; } /// /// The root duplicates in the database, one problem per field (read only). Asked only while the key is missing: /// with it, there can be none. /// public static async Task> FindDuplicatesAsync( IRedbContext context, ISqlDialect sql, CancellationToken cancellationToken = default) { var roots = await context.QueryAsync( sql.Structures_SelectRootDuplicates(), Array.Empty(), cancellationToken); var problems = new List(); foreach (var group in roots.GroupBy(r => (r.IdScheme, r.Name))) { var schemeName = await SchemeNameAsync(context, sql, group.Key.IdScheme, cancellationToken); problems.Add(DuplicateProblem(group.Key.IdScheme, schemeName, group.Key.Name, group.Select(r => r.Id).ToList())); } return problems; } /// The problem, and its logged text, for one root field found more than once. public static Models.RedbSchemaProblem DuplicateProblem(long schemeId, string schemeName, string field, IReadOnlyList ids) => new(Models.RedbSchemaProblemKind.DuplicateRootStructure, schemeId, schemeName, field, ids, $"REDB schema problem: root field '{field}' of scheme '{schemeName}' (id {schemeId}) exists {ids.Count} times " + $"(structures {string.Join(", ", ids)}), and objects may hold values on more than one copy. The unique key " + $"of root structures ({IndexName}) is not built until it is resolved. Resolve it by hand as described in " + "UPGRADING.md, section \"Duplicate root structures\"; this error repeats at every start until then."); private static async Task SchemeNameAsync(IRedbContext context, ISqlDialect sql, long schemeId, CancellationToken cancellationToken) => (await context.QueryFirstOrDefaultAsync(sql.Schemes_SelectById(), new object[] { schemeId }, cancellationToken)) ?.Name ?? ""; } }