Path: redb/redb.Core/Models/Entities/RedbScheme.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/Models/Entities/RedbScheme.cs
----
using redb.Core.Models.Contracts;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.Json.Serialization;
namespace redb.Core.Models.Entities
{
///
/// REDB scheme entity with direct data storage.
/// Maps to _schemes table in PostgreSQL.
/// Contains collection of structures (fields).
///
public class RedbScheme : IRedbScheme
{
///
/// Unique scheme identifier.
///
[JsonPropertyName("id")]
public long Id { get; set; }
///
/// Parent scheme identifier (for scheme hierarchy).
///
[JsonPropertyName("id_parent")]
public long? IdParent { get; set; }
///
/// Scheme name.
///
[JsonPropertyName("name")]
public string Name { get; set; } = string.Empty;
///
/// Scheme alias (short name).
///
[JsonPropertyName("alias")]
public string? Alias { get; set; }
///
/// Namespace for C# class generation.
///
[JsonPropertyName("name_space")]
public string? NameSpace { get; set; }
///
/// MD5 hash of all structures (aggregated).
/// Used for change detection and cache invalidation.
///
[JsonPropertyName("structure_hash")]
public Guid? StructureHash { get; set; }
///
/// Free-form marker for future / custom extensions (450 chars). Written by
/// [RedbTags] at synchronisation when present; direct writes survive sync.
///
public string? Tags { get; set; }
///
/// The previous names of the scheme, oldest first, as a JSON array of strings; NULL when it was never renamed.
/// Written by the sync when it applies 's previous names; read by
/// the sync to stop a class that predates the rename.
///
public string? RenamedFrom { get; set; }
///
/// The version of the Props class the scheme was last synchronised with (_schemes._version,
/// ); NULL or 0 for a class without one. Written by the sync
/// when a class of a higher version synchronises; never lowered.
///
public int? Version { get; set; }
///
/// Scheme type ID (Class, Array, Dictionary, JsonDocument, XDocument).
///
[JsonPropertyName("type")]
public long Type { get; set; } = -9223372036854775675; // Default: Class
// === Structures collection ===
private List _structures = new();
private Dictionary? _structuresByName;
///
/// Collection of structures (fields) for this scheme.
///
[JsonPropertyName("structures")]
public IReadOnlyCollection Structures => _structures.AsReadOnly();
///
/// Internal structures list (for provider).
///
internal List StructuresInternal => _structures;
///
/// Set structures collection (for mapping).
///
public void SetStructures(IEnumerable structures)
{
_structures = structures?.ToList() ?? new List();
_structuresByName = null; // Invalidate cache
}
///
/// Fast lookup of structure by name.
///
public IRedbStructure? GetStructureByName(string name)
{
if (_structuresByName == null)
{
_structuresByName = _structures.ToDictionary(s => s.Name, s => (IRedbStructure)s);
}
return _structuresByName.TryGetValue(name, out var structure) ? structure : null;
}
///
/// Default constructor for deserialization and mapping.
///
public RedbScheme()
{
}
///
/// Constructor with name.
///
public RedbScheme(string name)
{
Name = name ?? throw new ArgumentNullException(nameof(name));
}
public override string ToString()
{
var nameSpace = !string.IsNullOrEmpty(NameSpace) ? $"{NameSpace}." : "";
var alias = !string.IsNullOrEmpty(Alias) ? $" ({Alias})" : "";
return $"Scheme {Id}: {nameSpace}{Name}{alias}";
}
}
}