Path: redb/redb.Core/Utils/SchemeHashCalculator.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/Utils/SchemeHashCalculator.cs
----
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using redb.Core.Models.Entities;
namespace redb.Core.Utils;
///
/// Utility for computing MD5 hash of scheme structure.
/// Used for change detection and cache invalidation.
///
public static class SchemeHashCalculator
{
///
/// Computes MD5 hash of scheme from all its structures.
/// Hash aggregates critical fields of all structures: Id, Name, IdType, IdParent, IsArray, AllowNotNull, StoreNull, Unique.
///
/// List of scheme structures.
/// MD5 hash as Guid.
public static Guid ComputeSchemeStructureHash(List structures)
{
if (structures == null || !structures.Any())
return Guid.Empty;
// Sort for stable hash (independent of load order)
var sorted = structures
.OrderBy(s => s.IdParent ?? 0)
.ThenBy(s => s.Order ?? 0)
.ThenBy(s => s.Id)
.ToList();
var sb = new StringBuilder();
foreach (var s in sorted)
{
// Include only critical fields (that affect data)
sb.Append($"{s.Id}|");
sb.Append($"{s.Name}|");
sb.Append($"{s.IdType}|");
sb.Append($"{s.IdParent}|");
sb.Append($"{s.CollectionType}|");
sb.Append($"{s.KeyType}|");
sb.Append($"{s.AllowNotNull}|");
sb.Append($"{s.StoreNull}|");
// V4 (Л2): virtual appearing/disappearing must change the hash (LAZY plan §6).
sb.Append($"{s.Lazy}|");
// V4: a key appearing or disappearing must change the hash, or the metadata cache and the
// structure tree keep answering from the old flag until restart.
sb.Append($"{s.Unique};");
}
// Compute MD5 and convert to Guid
var hash = RedbMd5.ComputeHash(Encoding.UTF8.GetBytes(sb.ToString()));
return new Guid(hash);
}
}