2026-04-18 06:04:39 +01:00
using System ;
using System.Collections.Concurrent ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using System.Reflection ;
2026-05-08 01:13:15 +01:00
using System.Runtime.CompilerServices ;
using System.Security.Cryptography ;
using System.Text ;
2026-08-19 01:36:23 +02:00
using System.Threading ;
2026-04-18 06:04:39 +01:00
using System.Threading.Tasks ;
using R3 ;
namespace GameCult.Caching
{
2026-05-08 01:13:15 +01:00
public sealed class CultSchemaCatalogEntry
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
public string SchemaId { get ; set ; } = string . Empty ;
public string SchemaName { get ; set ; } = string . Empty ;
public string SchemaVersion { get ; set ; } = string . Empty ;
public string ContentHash { get ; set ; } = string . Empty ;
public string CanonicalSchemaJson { get ; set ; } = string . Empty ;
public string [ ] CompatibleSchemaIds { get ; set ; } = Array . Empty < string > ( ) ;
2026-05-09 11:45:46 +01:00
public CultSchemaMemberCatalogEntry [ ] Members { get ; set ; } = Array . Empty < CultSchemaMemberCatalogEntry > ( ) ;
}
public sealed class CultSchemaMemberCatalogEntry
{
public int Slot { get ; set ; }
public string MemberName { get ; set ; } = string . Empty ;
public string TypeName { get ; set ; } = string . Empty ;
public bool IsReference { get ; set ; }
public bool IsMany { get ; set ; }
public string? TargetSchemaName { get ; set ; }
public bool IsName { get ; set ; }
public string? IndexAlias { get ; set ; }
2026-05-08 01:13:15 +01:00
}
2026-04-18 06:04:39 +01:00
2026-05-08 01:13:15 +01:00
public sealed class CultPersistedRecord
{
public string Key { get ; set ; } = string . Empty ;
public string SchemaId { get ; set ; } = string . Empty ;
public string StoredAt { get ; set ; } = string . Empty ;
public byte [ ] Payload { get ; set ; } = Array . Empty < byte > ( ) ;
}
public sealed class CultPersistedStoreSnapshot
{
public string FormatVersion { get ; set ; } = "cultcache.store.v1" ;
public CultSchemaCatalogEntry [ ] SchemaCatalog { get ; set ; } = Array . Empty < CultSchemaCatalogEntry > ( ) ;
public CultPersistedRecord [ ] Records { get ; set ; } = Array . Empty < CultPersistedRecord > ( ) ;
}
2026-05-09 11:45:46 +01:00
public enum CultSchemaMigrationKind
{
Exact ,
CompatibleDrift
}
public sealed class CultSchemaMigrationWarning
{
public string Code { get ; set ; } = string . Empty ;
public string Message { get ; set ; } = string . Empty ;
}
public sealed class CultSchemaMigrationReport
{
public string PersistedSchemaId { get ; set ; } = string . Empty ;
public string LocalSchemaId { get ; set ; } = string . Empty ;
public string PersistedSchemaName { get ; set ; } = string . Empty ;
public string LocalSchemaName { get ; set ; } = string . Empty ;
public CultSchemaMigrationKind Kind { get ; set ; }
public int [ ] IgnoredExtraSlots { get ; set ; } = Array . Empty < int > ( ) ;
public int [ ] DefaultedMissingSlots { get ; set ; } = Array . Empty < int > ( ) ;
public CultSchemaMigrationWarning [ ] Warnings { get ; set ; } = Array . Empty < CultSchemaMigrationWarning > ( ) ;
}
internal sealed class CultSchemaResolutionResult
{
public CultSchemaResolutionResult ( CultDocumentDescriptor descriptor , CultSchemaMigrationReport report )
{
Descriptor = descriptor ;
Report = report ;
}
public CultDocumentDescriptor Descriptor { get ; }
public CultSchemaMigrationReport Report { get ; }
}
2026-05-08 01:13:15 +01:00
public sealed class CultDocumentDescriptor
{
internal CultDocumentDescriptor (
Type documentType ,
string schemaName ,
string schemaVersion ,
string schemaId ,
string contentHash ,
string canonicalSchemaJson ,
bool isGlobal ,
string? nameMember ,
Func < object , string? > ? nameAccessor ,
2026-05-08 02:19:27 +01:00
Func < object , byte [ ] > ? generatedPayloadSerializer ,
Func < byte [ ] , object > ? generatedPayloadDeserializer ,
2026-05-08 01:13:15 +01:00
IReadOnlyDictionary < string , Func < object , string > > indexAccessors ,
IReadOnlyList < CultDocumentMemberDescriptor > members )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
DocumentType = documentType ;
SchemaName = schemaName ;
SchemaVersion = schemaVersion ;
SchemaId = schemaId ;
ContentHash = contentHash ;
CanonicalSchemaJson = canonicalSchemaJson ;
IsGlobal = isGlobal ;
NameMember = nameMember ;
NameAccessor = nameAccessor ;
2026-05-08 02:19:27 +01:00
GeneratedPayloadSerializer = generatedPayloadSerializer ;
GeneratedPayloadDeserializer = generatedPayloadDeserializer ;
2026-05-08 01:13:15 +01:00
IndexAccessors = indexAccessors ;
Members = members ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
public Type DocumentType { get ; }
public string SchemaName { get ; }
public string SchemaVersion { get ; }
public string SchemaId { get ; }
public string ContentHash { get ; }
public string CanonicalSchemaJson { get ; }
public bool IsGlobal { get ; }
public string? NameMember { get ; }
internal Func < object , string? > ? NameAccessor { get ; }
2026-05-08 02:19:27 +01:00
public Func < object , byte [ ] > ? GeneratedPayloadSerializer { get ; }
public Func < byte [ ] , object > ? GeneratedPayloadDeserializer { get ; }
2026-05-08 01:13:15 +01:00
internal IReadOnlyDictionary < string , Func < object , string > > IndexAccessors { get ; }
internal IReadOnlyList < CultDocumentMemberDescriptor > Members { get ; }
public CultSchemaCatalogEntry ToCatalogEntry ( )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
return new CultSchemaCatalogEntry
{
SchemaId = SchemaId ,
SchemaName = SchemaName ,
SchemaVersion = SchemaVersion ,
ContentHash = ContentHash ,
CanonicalSchemaJson = CanonicalSchemaJson ,
2026-05-09 11:45:46 +01:00
CompatibleSchemaIds = [ SchemaId ] ,
Members = Members
. OrderBy ( member = > member . Slot )
. Select ( member = > new CultSchemaMemberCatalogEntry
{
Slot = member . Slot ,
MemberName = member . MemberName ,
TypeName = member . TypeName ,
IsReference = member . IsReference ,
IsMany = member . IsMany ,
TargetSchemaName = member . TargetSchemaName ,
IsName = member . IsName ,
IndexAlias = member . IndexAlias
} )
. ToArray ( )
2026-05-08 01:13:15 +01:00
} ;
}
2026-04-18 06:04:39 +01:00
2026-05-08 01:13:15 +01:00
internal string GetPreferredFileStem ( object document )
{
var preferred = NameAccessor ? . Invoke ( document ) ;
return string . IsNullOrWhiteSpace ( preferred )
? SchemaName
: preferred ! ;
}
}
internal sealed class CultDocumentMemberDescriptor
{
public string MemberName { get ; set ; } = string . Empty ;
public int Slot { get ; set ; }
public string TypeName { get ; set ; } = string . Empty ;
public bool IsReference { get ; set ; }
public bool IsMany { get ; set ; }
public string? TargetSchemaName { get ; set ; }
2026-05-08 01:50:15 +01:00
public bool IsName { get ; set ; }
public string? IndexAlias { get ; set ; }
2026-05-08 01:13:15 +01:00
}
public sealed class CultStoredDocument
{
public CultStoredDocument (
CultRecordKey key ,
string storedAt ,
CultDocumentDescriptor descriptor ,
object document )
{
Key = key ;
StoredAt = storedAt ;
Descriptor = descriptor ;
Document = document ;
}
public CultRecordKey Key { get ; }
public string StoredAt { get ; }
public CultDocumentDescriptor Descriptor { get ; }
public object Document { get ; }
}
public sealed class CultDocumentRegistry
{
private static readonly Lazy < CultDocumentRegistry > SharedRegistry =
new ( ( ) = > new CultDocumentRegistry ( ) ) ;
2026-07-13 17:22:58 +01:00
private volatile RegistryIndexes _indexes = new ( ) ;
2026-07-13 17:16:41 +01:00
private readonly object _registrationGate = new ( ) ;
2026-05-08 01:13:15 +01:00
public static CultDocumentRegistry Shared = > SharedRegistry . Value ;
public CultDocumentRegistry ( )
{
Refresh ( ) ;
}
2026-07-20 01:48:56 +01:00
private CultDocumentRegistry ( bool discoverLoadedDocuments )
{
if ( discoverLoadedDocuments )
Refresh ( ) ;
}
public static CultDocumentRegistry ForTypes ( IEnumerable < Type > documentTypes )
{
if ( documentTypes = = null ) throw new ArgumentNullException ( nameof ( documentTypes ) ) ;
var registry = new CultDocumentRegistry ( discoverLoadedDocuments : false ) ;
foreach ( var type in documentTypes . Distinct ( ) )
{
registry . GetRequired ( type ? ? throw new ArgumentException (
"Document type collections cannot contain null entries." ,
nameof ( documentTypes ) ) ) ;
}
return registry ;
}
2026-07-13 17:22:58 +01:00
public IEnumerable < CultDocumentDescriptor > AllDescriptors = >
_indexes . ByType . Values . OrderBy ( d = > d . SchemaName , StringComparer . Ordinal ) ;
2026-05-08 01:13:15 +01:00
public void Refresh ( )
{
2026-07-13 17:22:58 +01:00
lock ( _registrationGate )
2026-04-18 06:04:39 +01:00
{
2026-07-13 17:22:58 +01:00
var rebuilt = new RegistryIndexes ( ) ;
var generatedTypes = new HashSet < Type > ( ) ;
foreach ( var definition in CultGeneratedDocumentMetadataLoader . LoadDefinitions ( ) )
{
var descriptor = BuildDescriptor ( definition ) ;
RegisterDescriptor ( rebuilt , descriptor ) ;
generatedTypes . Add ( descriptor . DocumentType ) ;
}
2026-05-08 01:50:15 +01:00
2026-07-13 17:22:58 +01:00
foreach ( var type in ReflectionExtensions . GetAttributedDocumentTypes ( ) . Where ( type = > ! generatedTypes . Contains ( type ) ) )
{
RegisterDescriptor ( rebuilt , BuildDescriptor ( type ) ) ;
}
_indexes = rebuilt ;
2026-05-08 01:13:15 +01:00
}
}
public CultDocumentDescriptor GetRequired ( Type type )
{
2026-07-13 17:22:58 +01:00
if ( _indexes . ByType . TryGetValue ( type , out var descriptor ) )
2026-05-08 01:13:15 +01:00
{
return descriptor ;
}
2026-05-08 01:50:15 +01:00
descriptor = TryBuildGeneratedDescriptor ( type ) ;
if ( descriptor ! = null )
{
2026-07-13 17:22:58 +01:00
return RegisterDescriptor ( descriptor ) ;
2026-05-08 01:50:15 +01:00
}
2026-05-08 01:13:15 +01:00
var attribute = type . GetCustomAttribute < CultDocumentAttribute > ( ) ;
if ( attribute = = null )
{
throw new InvalidOperationException (
$"Type {type.FullName} is not marked with {nameof(CultDocumentAttribute)}." ) ;
}
descriptor = BuildDescriptor ( type ) ;
2026-07-13 17:22:58 +01:00
return RegisterDescriptor ( descriptor ) ;
2026-05-08 01:13:15 +01:00
}
public CultDocumentDescriptor GetRequired < T > ( ) where T : class
{
return GetRequired ( typeof ( T ) ) ;
}
public CultDocumentDescriptor GetRequiredBySchemaId ( string schemaId )
{
2026-07-13 17:22:58 +01:00
if ( _indexes . BySchemaId . TryGetValue ( schemaId , out var descriptor ) )
2026-05-08 01:13:15 +01:00
{
return descriptor ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
throw new InvalidOperationException ( $"Unknown CultCache schema id '{schemaId}'." ) ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
public CultDocumentDescriptor ResolvePersistedSchema ( string schemaId , IReadOnlyCollection < CultSchemaCatalogEntry > catalog )
2026-05-09 11:45:46 +01:00
{
return ResolvePersistedSchemaDetailed ( schemaId , catalog ) . Descriptor ;
}
public CultSchemaMigrationReport ResolvePersistedSchemaReport ( string schemaId , IReadOnlyCollection < CultSchemaCatalogEntry > catalog )
{
return ResolvePersistedSchemaDetailed ( schemaId , catalog ) . Report ;
}
internal CultSchemaResolutionResult ResolvePersistedSchemaDetailed ( string schemaId , IReadOnlyCollection < CultSchemaCatalogEntry > catalog )
2026-04-18 06:04:39 +01:00
{
2026-07-13 17:22:58 +01:00
var indexes = _indexes ;
if ( indexes . BySchemaId . TryGetValue ( schemaId , out var exact ) )
2026-04-18 06:04:39 +01:00
{
2026-05-09 11:45:46 +01:00
return new CultSchemaResolutionResult (
exact ,
new CultSchemaMigrationReport
{
PersistedSchemaId = schemaId ,
LocalSchemaId = exact . SchemaId ,
PersistedSchemaName = exact . SchemaName ,
LocalSchemaName = exact . SchemaName ,
Kind = CultSchemaMigrationKind . Exact
} ) ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
var persisted = catalog . FirstOrDefault ( entry = > string . Equals ( entry . SchemaId , schemaId , StringComparison . Ordinal ) ) ;
if ( persisted = = null )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
throw new InvalidOperationException ( $"Persisted schema '{schemaId}' is not present in the embedded catalog." ) ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
2026-05-09 11:45:46 +01:00
foreach ( var compatibleSchemaId in persisted . CompatibleSchemaIds . Where ( candidate = > ! string . IsNullOrWhiteSpace ( candidate ) ) )
{
2026-07-13 17:22:58 +01:00
if ( indexes . BySchemaId . TryGetValue ( compatibleSchemaId , out var compatibleLocal ) )
2026-05-09 11:45:46 +01:00
{
return BuildCompatibleResolutionResult ( persisted , compatibleLocal ) ;
}
}
2026-07-13 17:22:58 +01:00
if ( indexes . BySchemaName . TryGetValue ( persisted . SchemaName , out var localVersions ) )
2026-04-18 06:04:39 +01:00
{
2026-07-13 17:16:41 +01:00
if ( localVersions . Length = = 1 )
{
return BuildCompatibleResolutionResult ( persisted , localVersions [ 0 ] ) ;
}
throw new InvalidOperationException (
$"CultCache schema name '{persisted.SchemaName}' is ambiguous across local versions " +
$"[{string.Join(" , ", localVersions.Select(candidate => candidate.SchemaVersion).OrderBy(version => version, StringComparer.Ordinal))}]. " +
"Persisted schema compatibility must identify an explicit schema id." ) ;
2026-05-08 01:13:15 +01:00
}
throw new InvalidOperationException (
$"No local CultCache schema matches persisted schema '{persisted.SchemaName}' ({schemaId})." ) ;
}
2026-07-13 17:22:58 +01:00
private CultDocumentDescriptor RegisterDescriptor ( CultDocumentDescriptor descriptor )
2026-05-08 01:50:15 +01:00
{
2026-07-13 17:16:41 +01:00
lock ( _registrationGate )
{
2026-07-13 17:22:58 +01:00
var current = _indexes ;
if ( current . ByType . TryGetValue ( descriptor . DocumentType , out var registered ) )
return registered ;
var updated = new RegistryIndexes ( current ) ;
registered = RegisterDescriptor ( updated , descriptor ) ;
_indexes = updated ;
return registered ;
}
}
2026-07-13 17:16:41 +01:00
2026-07-13 17:22:58 +01:00
private static CultDocumentDescriptor RegisterDescriptor (
RegistryIndexes indexes ,
CultDocumentDescriptor descriptor )
{
if ( indexes . ByType . TryGetValue ( descriptor . DocumentType , out var registered ) )
return registered ;
2026-07-13 17:16:41 +01:00
2026-07-13 17:22:58 +01:00
indexes . BySchemaName . TryGetValue ( descriptor . SchemaName , out var schemaNameVersions ) ;
var schemaVersionOwner = schemaNameVersions ? . FirstOrDefault ( candidate = >
string . Equals ( candidate . SchemaVersion , descriptor . SchemaVersion , StringComparison . Ordinal ) ) ;
if ( schemaVersionOwner ! = null )
{
if ( IsExactWireAlias ( schemaVersionOwner , descriptor ) )
2026-07-13 17:16:41 +01:00
{
2026-07-13 17:22:58 +01:00
indexes . ByType [ descriptor . DocumentType ] = descriptor ;
return descriptor ;
2026-07-13 17:16:41 +01:00
}
2026-07-13 17:22:58 +01:00
throw DuplicateSchemaRegistration (
$"schema name '{descriptor.SchemaName}' version '{descriptor.SchemaVersion}'" ,
schemaVersionOwner . DocumentType ,
descriptor . DocumentType ) ;
}
if ( indexes . BySchemaId . TryGetValue ( descriptor . SchemaId , out var schemaIdOwner ) )
{
throw DuplicateSchemaRegistration (
$"schema id '{descriptor.SchemaId}'" ,
schemaIdOwner . DocumentType ,
descriptor . DocumentType ) ;
2026-07-13 17:16:41 +01:00
}
2026-07-13 17:22:58 +01:00
indexes . ByType [ descriptor . DocumentType ] = descriptor ;
indexes . BySchemaId [ descriptor . SchemaId ] = descriptor ;
indexes . BySchemaName [ descriptor . SchemaName ] = schemaNameVersions = = null
? [ descriptor ]
: [ . . schemaNameVersions , descriptor ] ;
return descriptor ;
2026-05-08 01:50:15 +01:00
}
2026-07-13 17:16:41 +01:00
private static bool IsExactWireAlias (
CultDocumentDescriptor canonical ,
CultDocumentDescriptor candidate ) = >
string . Equals ( canonical . SchemaName , candidate . SchemaName , StringComparison . Ordinal ) & &
string . Equals ( canonical . SchemaVersion , candidate . SchemaVersion , StringComparison . Ordinal ) & &
string . Equals ( canonical . SchemaId , candidate . SchemaId , StringComparison . Ordinal ) & &
string . Equals ( canonical . ContentHash , candidate . ContentHash , StringComparison . Ordinal ) & &
string . Equals ( canonical . CanonicalSchemaJson , candidate . CanonicalSchemaJson , StringComparison . Ordinal ) ;
private static InvalidOperationException DuplicateSchemaRegistration (
string identity ,
Type existingType ,
Type claimedType ) = >
new (
2026-07-13 17:22:58 +01:00
$"CultCache {identity} is already registered to CLR type " +
2026-07-13 17:16:41 +01:00
$"'{existingType.FullName}' and cannot also be claimed by '{claimedType.FullName}'. " +
"Schema compatibility must be declared through explicit persisted-schema alias metadata." ) ;
2026-07-13 17:22:58 +01:00
private sealed class RegistryIndexes
{
public RegistryIndexes ( )
{
ByType = new Dictionary < Type , CultDocumentDescriptor > ( ) ;
BySchemaId = new Dictionary < string , CultDocumentDescriptor > ( StringComparer . Ordinal ) ;
BySchemaName = new Dictionary < string , CultDocumentDescriptor [ ] > ( StringComparer . Ordinal ) ;
}
public RegistryIndexes ( RegistryIndexes source )
{
ByType = new Dictionary < Type , CultDocumentDescriptor > ( source . ByType ) ;
BySchemaId = new Dictionary < string , CultDocumentDescriptor > ( source . BySchemaId , StringComparer . Ordinal ) ;
BySchemaName = new Dictionary < string , CultDocumentDescriptor [ ] > ( source . BySchemaName , StringComparer . Ordinal ) ;
}
public Dictionary < Type , CultDocumentDescriptor > ByType { get ; }
public Dictionary < string , CultDocumentDescriptor > BySchemaId { get ; }
public Dictionary < string , CultDocumentDescriptor [ ] > BySchemaName { get ; }
}
2026-05-08 01:50:15 +01:00
private static CultDocumentDescriptor ? TryBuildGeneratedDescriptor ( Type type )
{
var definition = CultGeneratedDocumentMetadataLoader . LoadDefinitions ( type . Assembly )
. FirstOrDefault ( candidate = > candidate . DocumentType = = type ) ;
return definition = = null
? null
: BuildDescriptor ( definition ) ;
}
2026-05-08 01:13:15 +01:00
private static CultDocumentDescriptor BuildDescriptor ( Type type )
{
var attribute = type . GetCustomAttribute < CultDocumentAttribute > ( )
? ? throw new InvalidOperationException (
$"Type {type.FullName} is not marked with {nameof(CultDocumentAttribute)}." ) ;
var members = DiscoverMembers ( type ) ;
var nameMember = members . FirstOrDefault ( member = > member . IsName ) ;
var indexAccessors = members
. Where ( member = > member . IndexAlias ! = null )
. ToDictionary (
member = > member . IndexAlias ! ,
member = > member . Getter ,
StringComparer . Ordinal ) ;
2026-05-08 01:50:15 +01:00
var descriptorMembers = members
. Select ( member = > new CultDocumentMemberDescriptor
{
MemberName = member . Member . Name ,
Slot = member . Slot ,
TypeName = CultSchemaTypeNames . FromType ( member . MemberType ) ,
IsReference = member . IsReference ,
IsMany = member . IsMany ,
TargetSchemaName = member . TargetSchemaName ,
IsName = member . IsName ,
IndexAlias = member . IndexAlias
} )
. ToArray ( ) ;
var schemaJson = BuildCanonicalSchemaJson ( attribute . SchemaName , attribute . SchemaVersion , descriptorMembers ) ;
2026-05-08 01:13:15 +01:00
var contentHash = Sha256 ( schemaJson ) ;
2026-05-08 01:50:15 +01:00
var semanticFingerprint = BuildSemanticFingerprint ( attribute . SchemaName , attribute . SchemaVersion , descriptorMembers ) ;
2026-05-08 01:13:15 +01:00
var schemaId = Sha256 ( semanticFingerprint ) ;
return new CultDocumentDescriptor (
type ,
attribute . SchemaName ,
attribute . SchemaVersion ,
schemaId ,
contentHash ,
schemaJson ,
type . GetCustomAttribute < CultGlobalAttribute > ( ) ! = null ,
nameMember ? . Member . Name ,
nameMember ? . GetterNullable ,
2026-05-08 02:19:27 +01:00
null ,
null ,
2026-05-08 01:13:15 +01:00
indexAccessors ,
2026-05-08 01:50:15 +01:00
descriptorMembers ) ;
}
private static CultDocumentDescriptor BuildDescriptor ( CultGeneratedDocumentDefinition definition )
{
var descriptorMembers = definition . Members
. OrderBy ( member = > member . Slot )
. Select ( member = > new CultDocumentMemberDescriptor
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:50:15 +01:00
MemberName = member . MemberName ,
2026-05-08 01:13:15 +01:00
Slot = member . Slot ,
2026-05-08 01:50:15 +01:00
TypeName = member . TypeName ,
2026-05-08 01:13:15 +01:00
IsReference = member . IsReference ,
IsMany = member . IsMany ,
2026-05-08 01:50:15 +01:00
TargetSchemaName = member . TargetSchemaName ,
IsName = member . IsName ,
IndexAlias = member . IndexAlias
} )
. ToArray ( ) ;
var schemaJson = BuildCanonicalSchemaJson ( definition . SchemaName , definition . SchemaVersion , descriptorMembers ) ;
var contentHash = Sha256 ( schemaJson ) ;
var semanticFingerprint = BuildSemanticFingerprint ( definition . SchemaName , definition . SchemaVersion , descriptorMembers ) ;
var schemaId = Sha256 ( semanticFingerprint ) ;
return new CultDocumentDescriptor (
definition . DocumentType ,
definition . SchemaName ,
definition . SchemaVersion ,
schemaId ,
contentHash ,
schemaJson ,
definition . IsGlobal ,
definition . NameMember ,
definition . NameAccessor ,
2026-05-08 02:19:27 +01:00
definition . SerializePayload ,
definition . DeserializePayload ,
2026-05-08 01:50:15 +01:00
definition . IndexAccessors . ToDictionary ( accessor = > accessor . Alias , accessor = > accessor . Accessor , StringComparer . Ordinal ) ,
descriptorMembers ) ;
2026-05-08 01:13:15 +01:00
}
private static string BuildSemanticFingerprint (
2026-05-08 01:50:15 +01:00
string schemaName ,
string schemaVersion ,
IReadOnlyList < CultDocumentMemberDescriptor > members )
2026-05-08 01:13:15 +01:00
{
var builder = new StringBuilder ( ) ;
2026-05-08 01:50:15 +01:00
builder . Append ( schemaName )
2026-05-08 01:13:15 +01:00
. Append ( '|' )
2026-05-08 01:50:15 +01:00
. Append ( schemaVersion ) ;
2026-05-08 01:13:15 +01:00
foreach ( var member in members . OrderBy ( member = > member . Slot ) )
{
builder . Append ( '|' )
. Append ( member . Slot )
. Append ( ':' )
2026-05-08 01:50:15 +01:00
. Append ( member . MemberName )
2026-05-08 01:13:15 +01:00
. Append ( ':' )
2026-05-08 01:50:15 +01:00
. Append ( member . TypeName )
2026-05-08 01:13:15 +01:00
. Append ( ':' )
. Append ( member . IsReference ? "ref" : "value" )
. Append ( ':' )
. Append ( member . TargetSchemaName ? ? string . Empty )
. Append ( ':' )
. Append ( member . IsMany ? "many" : "one" ) ;
}
return builder . ToString ( ) ;
}
private static string BuildCanonicalSchemaJson (
2026-05-08 01:50:15 +01:00
string schemaName ,
string schemaVersion ,
IReadOnlyList < CultDocumentMemberDescriptor > members )
2026-05-08 01:13:15 +01:00
{
var builder = new StringBuilder ( ) ;
builder . Append ( "{\"schemaName\":\"" )
2026-05-08 01:50:15 +01:00
. Append ( CultSchemaTypeNames . EscapeForLiteral ( schemaName ) )
2026-05-08 01:13:15 +01:00
. Append ( "\",\"schemaVersion\":\"" )
2026-05-08 01:50:15 +01:00
. Append ( CultSchemaTypeNames . EscapeForLiteral ( schemaVersion ) )
2026-05-08 01:13:15 +01:00
. Append ( "\",\"members\":[" ) ;
for ( var index = 0 ; index < members . Count ; index + + )
{
var member = members [ index ] ;
if ( index > 0 )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
builder . Append ( ',' ) ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
builder . Append ( "{\"slot\":" )
. Append ( member . Slot )
. Append ( ",\"name\":\"" )
2026-05-08 01:50:15 +01:00
. Append ( CultSchemaTypeNames . EscapeForLiteral ( member . MemberName ) )
2026-05-08 01:13:15 +01:00
. Append ( "\",\"type\":\"" )
2026-05-08 01:50:15 +01:00
. Append ( CultSchemaTypeNames . EscapeForLiteral ( member . TypeName ) )
2026-05-08 01:13:15 +01:00
. Append ( "\",\"isReference\":" )
. Append ( member . IsReference ? "true" : "false" )
. Append ( ",\"many\":" )
. Append ( member . IsMany ? "true" : "false" )
. Append ( ",\"targetSchemaName\":" ) ;
if ( member . TargetSchemaName = = null )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
builder . Append ( "null" ) ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
else
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:50:15 +01:00
builder . Append ( '"' ) . Append ( CultSchemaTypeNames . EscapeForLiteral ( member . TargetSchemaName ) ) . Append ( '"' ) ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
builder . Append ( ",\"indexAlias\":" ) ;
if ( member . IndexAlias = = null )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
builder . Append ( "null" ) ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
else
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:50:15 +01:00
builder . Append ( '"' ) . Append ( CultSchemaTypeNames . EscapeForLiteral ( member . IndexAlias ) ) . Append ( '"' ) ;
2026-04-18 06:04:39 +01:00
}
2026-04-18 13:01:24 +01:00
2026-05-08 01:13:15 +01:00
builder . Append ( ",\"isName\":" )
. Append ( member . IsName ? "true" : "false" )
. Append ( '}' ) ;
}
builder . Append ( "]}" ) ;
return builder . ToString ( ) ;
2026-04-18 06:04:39 +01:00
}
2026-05-09 11:45:46 +01:00
private static CultSchemaResolutionResult BuildCompatibleResolutionResult (
CultSchemaCatalogEntry persisted ,
CultDocumentDescriptor local )
{
if ( string . Equals ( persisted . ContentHash , local . ContentHash , StringComparison . Ordinal ) )
{
return new CultSchemaResolutionResult (
local ,
new CultSchemaMigrationReport
{
PersistedSchemaId = persisted . SchemaId ,
LocalSchemaId = local . SchemaId ,
PersistedSchemaName = persisted . SchemaName ,
LocalSchemaName = local . SchemaName ,
Kind = CultSchemaMigrationKind . Exact
} ) ;
}
var persistedSchema = new CanonicalSchemaShape (
persisted . SchemaName ,
persisted . SchemaVersion ,
persisted . Members . Select ( member = > new CanonicalSchemaMember (
member . Slot ,
member . MemberName ,
member . TypeName ,
member . IsReference ,
member . IsMany ,
member . TargetSchemaName ,
member . IsName ,
member . IndexAlias ) ) . ToArray ( ) ) ;
var localSchema = new CanonicalSchemaShape (
local . SchemaName ,
local . SchemaVersion ,
local . Members . Select ( member = > new CanonicalSchemaMember (
member . Slot ,
member . MemberName ,
member . TypeName ,
member . IsReference ,
member . IsMany ,
member . TargetSchemaName ,
member . IsName ,
member . IndexAlias ) ) . ToArray ( ) ) ;
var comparison = CompareSchemaShapes ( persistedSchema , localSchema ) ;
if ( ! comparison . IsCompatible )
{
throw new InvalidOperationException (
$"Persisted schema '{persisted.SchemaName}' ({persisted.SchemaId}) is incompatible with local schema '{local.SchemaName}' ({local.SchemaId}): {string.Join(" ; ", comparison.Errors)}" ) ;
}
var warnings = new List < CultSchemaMigrationWarning > ( ) ;
if ( ! string . Equals ( persisted . SchemaId , local . SchemaId , StringComparison . Ordinal ) )
{
warnings . Add ( new CultSchemaMigrationWarning
{
Code = "compatible_schema_id_drift" ,
Message = $"Persisted schema '{persisted.SchemaId}' resolved to local schema '{local.SchemaId}'."
} ) ;
}
if ( ! string . Equals ( persisted . ContentHash , local . ContentHash , StringComparison . Ordinal ) )
{
warnings . Add ( new CultSchemaMigrationWarning
{
Code = "content_hash_drift" ,
Message = $"Persisted schema content hash '{persisted.ContentHash}' differs from local hash '{local.ContentHash}'."
} ) ;
}
warnings . AddRange ( comparison . Warnings ) ;
return new CultSchemaResolutionResult (
local ,
new CultSchemaMigrationReport
{
PersistedSchemaId = persisted . SchemaId ,
LocalSchemaId = local . SchemaId ,
PersistedSchemaName = persisted . SchemaName ,
LocalSchemaName = local . SchemaName ,
Kind = CultSchemaMigrationKind . CompatibleDrift ,
IgnoredExtraSlots = comparison . IgnoredExtraSlots . OrderBy ( slot = > slot ) . ToArray ( ) ,
DefaultedMissingSlots = comparison . DefaultedMissingSlots . OrderBy ( slot = > slot ) . ToArray ( ) ,
Warnings = warnings . ToArray ( )
} ) ;
}
private static SchemaShapeComparison CompareSchemaShapes ( CanonicalSchemaShape persisted , CanonicalSchemaShape local )
{
var errors = new List < string > ( ) ;
var warnings = new List < CultSchemaMigrationWarning > ( ) ;
var ignoredExtraSlots = new List < int > ( ) ;
var defaultedMissingSlots = new List < int > ( ) ;
if ( ! string . Equals ( persisted . SchemaName , local . SchemaName , StringComparison . Ordinal ) )
{
errors . Add ( $"schema name drift '{persisted.SchemaName}' -> '{local.SchemaName}'" ) ;
}
var persistedBySlot = persisted . Members . ToDictionary ( member = > member . Slot ) ;
var localBySlot = local . Members . ToDictionary ( member = > member . Slot ) ;
foreach ( var localMember in local . Members . OrderBy ( member = > member . Slot ) )
{
if ( ! persistedBySlot . TryGetValue ( localMember . Slot , out var persistedMember ) )
{
defaultedMissingSlots . Add ( localMember . Slot ) ;
warnings . Add ( new CultSchemaMigrationWarning
{
Code = "defaulted_missing_slot" ,
Message = $"Local slot {localMember.Slot} ('{localMember.Name}') is missing from the persisted schema and will use the local default value."
} ) ;
continue ;
}
if ( ! string . Equals ( localMember . TypeName , persistedMember . TypeName , StringComparison . Ordinal ) )
{
errors . Add ( $"slot {localMember.Slot} changed type '{persistedMember.TypeName}' -> '{localMember.TypeName}'" ) ;
}
if ( localMember . IsReference ! = persistedMember . IsReference )
{
errors . Add ( $"slot {localMember.Slot} changed reference semantics" ) ;
}
if ( localMember . IsMany ! = persistedMember . IsMany )
{
errors . Add ( $"slot {localMember.Slot} changed cardinality" ) ;
}
if ( ! string . Equals ( localMember . TargetSchemaName , persistedMember . TargetSchemaName , StringComparison . Ordinal ) )
{
errors . Add ( $"slot {localMember.Slot} changed target schema '{persistedMember.TargetSchemaName ?? " < none > "}' -> '{localMember.TargetSchemaName ?? " < none > "}'" ) ;
}
if ( localMember . IsName ! = persistedMember . IsName )
{
errors . Add ( $"slot {localMember.Slot} changed name-lookup semantics" ) ;
}
if ( ! string . Equals ( localMember . IndexAlias , persistedMember . IndexAlias , StringComparison . Ordinal ) )
{
errors . Add ( $"slot {localMember.Slot} changed index alias '{persistedMember.IndexAlias ?? " < none > "}' -> '{localMember.IndexAlias ?? " < none > "}'" ) ;
}
}
foreach ( var persistedMember in persisted . Members . OrderBy ( member = > member . Slot ) )
{
if ( localBySlot . ContainsKey ( persistedMember . Slot ) )
{
continue ;
}
ignoredExtraSlots . Add ( persistedMember . Slot ) ;
warnings . Add ( new CultSchemaMigrationWarning
{
Code = "ignored_extra_slot" ,
Message = $"Persisted slot {persistedMember.Slot} ('{persistedMember.Name}') is not present locally and will be ignored."
} ) ;
}
return new SchemaShapeComparison (
errors . Count = = 0 ,
errors . ToArray ( ) ,
warnings . ToArray ( ) ,
ignoredExtraSlots . ToArray ( ) ,
defaultedMissingSlots . ToArray ( ) ) ;
}
private sealed class CanonicalSchemaShape
{
public CanonicalSchemaShape ( string schemaName , string schemaVersion , CanonicalSchemaMember [ ] members )
{
SchemaName = schemaName ;
SchemaVersion = schemaVersion ;
Members = members ;
}
public string SchemaName { get ; }
public string SchemaVersion { get ; }
public CanonicalSchemaMember [ ] Members { get ; }
}
private sealed class CanonicalSchemaMember
{
public CanonicalSchemaMember (
int slot ,
string name ,
string typeName ,
bool isReference ,
bool isMany ,
string? targetSchemaName ,
bool isName ,
string? indexAlias )
{
Slot = slot ;
Name = name ;
TypeName = typeName ;
IsReference = isReference ;
IsMany = isMany ;
TargetSchemaName = targetSchemaName ;
IsName = isName ;
IndexAlias = indexAlias ;
}
public int Slot { get ; }
public string Name { get ; }
public string TypeName { get ; }
public bool IsReference { get ; }
public bool IsMany { get ; }
public string? TargetSchemaName { get ; }
public bool IsName { get ; }
public string? IndexAlias { get ; }
}
private sealed class SchemaShapeComparison
{
public SchemaShapeComparison (
bool isCompatible ,
string [ ] errors ,
CultSchemaMigrationWarning [ ] warnings ,
int [ ] ignoredExtraSlots ,
int [ ] defaultedMissingSlots )
{
IsCompatible = isCompatible ;
Errors = errors ;
Warnings = warnings ;
IgnoredExtraSlots = ignoredExtraSlots ;
DefaultedMissingSlots = defaultedMissingSlots ;
}
public bool IsCompatible { get ; }
public string [ ] Errors { get ; }
public CultSchemaMigrationWarning [ ] Warnings { get ; }
public int [ ] IgnoredExtraSlots { get ; }
public int [ ] DefaultedMissingSlots { get ; }
}
2026-05-08 01:13:15 +01:00
private static IReadOnlyList < PersistedMember > DiscoverMembers ( Type type )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
var members = new List < PersistedMember > ( ) ;
2026-04-18 06:04:39 +01:00
2026-05-08 01:13:15 +01:00
foreach ( var field in type . GetFields ( BindingFlags . Instance | BindingFlags . Public ) )
2026-04-18 13:01:24 +01:00
{
2026-05-08 01:13:15 +01:00
if ( IsIgnored ( field ) )
2026-04-18 13:01:24 +01:00
{
2026-05-08 01:13:15 +01:00
continue ;
2026-04-18 13:01:24 +01:00
}
2026-05-08 01:13:15 +01:00
members . Add ( PersistedMember . FromMember ( field , field . FieldType , value = > field . GetValue ( value ) , GetKeyValue ( field ) ) ) ;
}
foreach ( var property in type . GetProperties ( BindingFlags . Instance | BindingFlags . Public ) )
{
if ( property . GetMethod = = null | | property . SetMethod = = null | | IsIgnored ( property ) )
2026-04-18 13:01:24 +01:00
{
2026-05-08 01:13:15 +01:00
continue ;
2026-04-18 13:01:24 +01:00
}
2026-05-08 01:13:15 +01:00
members . Add ( PersistedMember . FromMember ( property , property . PropertyType , value = > property . GetValue ( value ) , GetKeyValue ( property ) ) ) ;
}
2026-05-09 11:14:23 +01:00
var explicitMembers = members . Where ( member = > member . ExplicitSlot . HasValue ) . OrderBy ( member = > member . ExplicitSlot . GetValueOrDefault ( ) ) . ToArray ( ) ;
2026-05-08 01:13:15 +01:00
var implicitMembers = members . Where ( member = > ! member . ExplicitSlot . HasValue ) . OrderBy ( member = > member . MetadataToken ) . ToArray ( ) ;
var assigned = new List < PersistedMember > ( members . Count ) ;
2026-05-09 11:14:23 +01:00
var nextSlot = explicitMembers . Length = = 0 ? 0 : explicitMembers . Max ( member = > member . ExplicitSlot . GetValueOrDefault ( ) ) + 1 ;
2026-05-08 01:13:15 +01:00
foreach ( var member in explicitMembers )
{
2026-05-09 11:14:23 +01:00
member . Slot = member . ExplicitSlot . GetValueOrDefault ( ) ;
2026-05-08 01:13:15 +01:00
assigned . Add ( member ) ;
2026-04-18 13:01:24 +01:00
}
2026-05-08 01:13:15 +01:00
foreach ( var member in implicitMembers )
2026-04-18 13:01:24 +01:00
{
2026-05-08 01:13:15 +01:00
member . Slot = nextSlot + + ;
assigned . Add ( member ) ;
2026-04-18 13:01:24 +01:00
}
2026-05-08 01:13:15 +01:00
return assigned . OrderBy ( member = > member . Slot ) . ToArray ( ) ;
}
2026-04-18 06:04:39 +01:00
2026-05-08 01:13:15 +01:00
private static bool IsIgnored ( MemberInfo member )
{
return member . GetCustomAttributes ( ) . Any ( attribute = >
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
var name = attribute . GetType ( ) . FullName ;
return name = = "MessagePack.IgnoreMemberAttribute" ;
} ) ;
}
2026-04-18 06:04:39 +01:00
2026-05-08 01:13:15 +01:00
private static int? GetKeyValue ( MemberInfo member )
{
foreach ( var attribute in member . GetCustomAttributes ( ) )
{
var name = attribute . GetType ( ) . FullName ;
if ( name = = "MessagePack.KeyAttribute" )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
var property = attribute . GetType ( ) . GetProperty ( "IntKey" ) ;
if ( property ? . GetValue ( attribute ) is int intKey )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
return intKey ;
}
var ctorArg = attribute . GetType ( ) . GetProperty ( "StringKey" ) ;
if ( ctorArg ? . GetValue ( attribute ) is string )
{
return null ;
2026-04-18 06:04:39 +01:00
}
}
}
2026-05-08 01:13:15 +01:00
return null ;
}
private static string Sha256 ( string input )
{
using var sha = SHA256 . Create ( ) ;
var bytes = sha . ComputeHash ( Encoding . UTF8 . GetBytes ( input ) ) ;
return "sha256:" + BitConverter . ToString ( bytes ) . Replace ( "-" , string . Empty ) . ToLowerInvariant ( ) ;
}
2026-04-18 06:04:39 +01:00
2026-05-08 01:13:15 +01:00
private sealed class PersistedMember
{
public MemberInfo Member { get ; set ; } = default ! ;
public Type MemberType { get ; set ; } = default ! ;
public int MetadataToken { get ; set ; }
public int? ExplicitSlot { get ; set ; }
public int Slot { get ; set ; }
public bool IsName { get ; set ; }
public string? IndexAlias { get ; set ; }
public bool IsReference { get ; set ; }
public bool IsMany { get ; set ; }
public string? TargetSchemaName { get ; set ; }
public Func < object , string > Getter { get ; set ; } = default ! ;
public Func < object , string? > GetterNullable { get ; set ; } = default ! ;
public static PersistedMember FromMember (
MemberInfo member ,
Type memberType ,
Func < object , object? > getValue ,
int? explicitSlot )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
var referenceAttribute = member . GetCustomAttribute < CultReferenceAttribute > ( ) ;
var targetType = ResolveReferenceTarget ( memberType , referenceAttribute ? . TargetType ) ;
var targetSchemaName = targetType ? . GetCustomAttribute < CultDocumentAttribute > ( ) ? . SchemaName ;
return new PersistedMember
{
Member = member ,
MemberType = memberType ,
MetadataToken = member . MetadataToken ,
ExplicitSlot = explicitSlot ,
Slot = explicitSlot ? ? - 1 ,
IsName = member . GetCustomAttribute < CultNameAttribute > ( ) ! = null ,
IndexAlias = ResolveIndexAlias ( member ) ,
2026-05-08 01:50:15 +01:00
IsReference = targetType ! = null | | referenceAttribute ! = null ,
2026-05-08 01:13:15 +01:00
IsMany = referenceAttribute ? . Many ? ? false ,
TargetSchemaName = targetSchemaName ,
Getter = document = > getValue ( document ) ? . ToString ( ) ? ? string . Empty ,
GetterNullable = document = > getValue ( document ) ? . ToString ( )
} ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
private static string? ResolveIndexAlias ( MemberInfo member )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
var indexAttribute = member . GetCustomAttribute < CultIndexAttribute > ( ) ;
if ( indexAttribute = = null )
{
return null ;
}
return string . IsNullOrWhiteSpace ( indexAttribute . Alias )
? member . Name
: indexAttribute . Alias ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
private static Type ? ResolveReferenceTarget ( Type memberType , Type ? explicitTarget )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
if ( explicitTarget ! = null )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
return explicitTarget ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
if ( memberType . IsGenericType & &
memberType . GetGenericTypeDefinition ( ) = = typeof ( CultRecordRef < > ) )
{
return memberType . GetGenericArguments ( ) [ 0 ] ;
}
return null ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
}
}
public sealed class CultCache : IDisposable
{
private readonly CultDocumentRegistry _registry ;
private readonly List < CacheBackingStore > _backingStores = new ( ) ;
private readonly ConcurrentDictionary < string , CultStoredDocument > _entries = new ( StringComparer . Ordinal ) ;
2026-08-18 01:07:25 +02:00
private readonly ConcurrentDictionary < Type , ConcurrentDictionary < string , CultStoredDocument > > _typeMaps = new ( ) ;
2026-05-08 01:13:15 +01:00
private readonly ConcurrentDictionary < Type , ConcurrentDictionary < string , string > > _nameMaps = new ( ) ;
private readonly ConcurrentDictionary < ( Type Type , string Alias ) , ConcurrentDictionary < string , string > > _indexMaps = new ( ) ;
private readonly ConcurrentDictionary < Type , string > _globalKeys = new ( ) ;
private readonly ConditionalWeakTable < object , DocumentHandleBox > _documentHandles = new ( ) ;
2026-06-13 01:44:05 +01:00
private readonly Subject < object > _changes = new ( ) ;
2026-08-19 01:36:23 +02:00
private readonly SemaphoreSlim _transactionGate = new ( 1 , 1 ) ;
private readonly AsyncLocal < CultCacheTransaction ? > _ambientTransaction = new ( ) ;
private readonly object _stateGate = new ( ) ;
2026-05-09 11:45:46 +01:00
private bool _hasUnflushedMutations ;
2026-04-18 06:04:39 +01:00
2026-05-08 01:13:15 +01:00
public CultCache ( CultDocumentRegistry ? registry = null )
2026-07-20 02:23:42 +01:00
: this ( registry , initializeGlobals : true )
{
}
public CultCache ( CultDocumentRegistry ? registry , bool initializeGlobals )
2026-05-08 01:13:15 +01:00
{
_registry = registry ? ? CultDocumentRegistry . Shared ;
2026-07-20 02:23:42 +01:00
if ( initializeGlobals )
{
MaterializeMissingGlobals ( ) ;
}
}
2026-09-13 16:54:43 +02:00
// Durable open paths call this after hydration so persisted globals remain authoritative.
2026-07-20 02:23:42 +01:00
public void MaterializeMissingGlobals ( )
{
2026-05-08 01:13:15 +01:00
InitializeGlobals ( ) ;
2026-04-18 06:04:39 +01:00
}
2026-05-09 11:45:46 +01:00
public bool IsDirty = > _hasUnflushedMutations | | _backingStores . Any ( store = > store . IsDirty ) ;
public bool FlushAttachedStoresOnDispose { get ; set ; }
2026-05-08 01:13:15 +01:00
public event Action < object? , object? > ? OnUpdate ;
2026-05-09 11:45:46 +01:00
public IReadOnlyList < CacheBackingStore > BackingStores = > _backingStores ;
2026-08-19 01:36:23 +02:00
public IEnumerable < object > AllEntries
{
get
{
lock ( _stateGate )
return VisibleStoredDocuments ( ) . Select ( entry = > entry . Document ) . ToArray ( ) ;
}
}
2026-05-08 01:13:15 +01:00
2026-08-19 01:36:23 +02:00
public IEnumerable < CultStoredDocument > AllStoredDocuments
{
get
{
lock ( _stateGate )
return VisibleStoredDocuments ( )
. OrderBy ( entry = > entry . Descriptor . SchemaName , StringComparer . Ordinal )
. ThenBy ( entry = > entry . Key . Value , StringComparer . Ordinal )
. ToArray ( ) ;
}
}
2026-05-24 19:30:46 +01:00
2026-05-08 01:13:15 +01:00
public CultDocumentRegistry Registry = > _registry ;
2026-06-13 01:44:05 +01:00
public Observable < CultCacheDocumentChange < T > > Watch < T > ( ) where T : class
{
return _changes
. Where ( change = > change is CultCacheDocumentChange < T > )
. Select ( change = > ( CultCacheDocumentChange < T > ) change ) ;
}
public Observable < CultCacheDocumentChange < T > > WatchRecord < T > ( CultRecordKey key ) where T : class
{
return Watch < T > ( ) . Where ( change = > change . Key . Equals ( key ) ) ;
}
2026-05-08 01:13:15 +01:00
public void AddBackingStore ( CacheBackingStore store )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
if ( store = = null ) throw new ArgumentNullException ( nameof ( store ) ) ;
store . AttachRegistry ( _registry ) ;
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
store . Loaded = entry = > AddStoredDocumentInternal ( entry , store , raiseUpdate : true ) ;
store . Unloaded = entry = > RemoveStoredDocumentInternal ( entry , store , raiseUpdate : true ) ;
2026-05-09 11:45:46 +01:00
foreach ( var entry in _entries . Values . OrderBy ( entry = > entry . Key . Value , StringComparer . Ordinal ) )
{
store . Push ( entry ) ;
}
2026-05-08 01:13:15 +01:00
_backingStores . Add ( store ) ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
public async Task PullAllBackingStoresAsync ( )
2026-04-18 06:04:39 +01:00
{
2026-08-19 02:36:49 +02:00
if ( _ambientTransaction . Value ! = null )
throw new InvalidOperationException (
"CultCache hydration cannot run inside a mutation transaction; hydrate before opening the commit scope." ) ;
await _transactionGate . WaitAsync ( ) . ConfigureAwait ( false ) ;
try
2026-05-08 01:13:15 +01:00
{
2026-08-19 02:36:49 +02:00
foreach ( var store in _backingStores )
store . PullAll ( ) ;
2026-05-08 01:13:15 +01:00
2026-08-19 02:36:49 +02:00
_hasUnflushedMutations = _backingStores . Any ( store = > store . IsDirty ) ;
}
finally
{
_transactionGate . Release ( ) ;
}
2026-04-18 06:04:39 +01:00
}
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
public void FlushAllBackingStores ( )
2026-05-09 11:45:46 +01:00
{
2026-08-19 01:36:23 +02:00
if ( _ambientTransaction . Value ! = null )
throw new InvalidOperationException ( "A CultCache transaction owns durable commit; do not flush inside its stage callback." ) ;
_transactionGate . Wait ( ) ;
try
2026-05-09 11:45:46 +01:00
{
2026-08-19 01:36:23 +02:00
foreach ( var store in _backingStores )
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
{
store . PushAll ( ) ;
RecomputeDirtyState ( ) ;
}
2026-08-19 01:36:23 +02:00
}
finally
2026-05-09 11:45:46 +01:00
{
2026-08-19 01:36:23 +02:00
_transactionGate . Release ( ) ;
2026-05-09 11:45:46 +01:00
}
}
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
// soft is dead. CultCacheStudioWindow reflects on FlushAsync(bool) until Cut 6 rewrites it.
2026-05-09 13:32:30 +01:00
public Task FlushAsync ( bool soft = false )
{
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
FlushAllBackingStores ( ) ;
2026-05-09 13:32:30 +01:00
return Task . CompletedTask ;
}
2026-09-13 16:54:43 +02:00
// Staged records are visible only to the executing async flow. The durable backing store
// commits before the live cache and its observers advance; an exception discards the whole batch.
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
public async Task ExecuteTransactionAsync ( Func < Task > stageAsync )
2026-08-19 01:36:23 +02:00
{
if ( stageAsync = = null ) throw new ArgumentNullException ( nameof ( stageAsync ) ) ;
if ( _ambientTransaction . Value ! = null )
{
await stageAsync ( ) . ConfigureAwait ( false ) ;
return ;
}
await _transactionGate . WaitAsync ( ) . ConfigureAwait ( false ) ;
var transaction = new CultCacheTransaction ( ) ;
IReadOnlyList < ( CultStoredDocument Stored , object? Previous , bool Removed ) > changes ;
_ambientTransaction . Value = transaction ;
try
{
await stageAsync ( ) . ConfigureAwait ( false ) ;
2026-08-19 02:17:38 +02:00
transaction . Seal ( ) ;
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
changes = CommitTransaction ( transaction ) ;
2026-08-19 01:36:23 +02:00
}
finally
{
2026-08-19 02:17:38 +02:00
transaction . Seal ( ) ;
2026-08-19 01:36:23 +02:00
_ambientTransaction . Value = null ;
_transactionGate . Release ( ) ;
}
foreach ( var change in changes )
PublishChange ( change . Stored , change . Previous , change . Removed ) ;
}
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
public async Task < T > ExecuteTransactionAsync < T > ( Func < Task < T > > stageAsync )
2026-08-19 01:36:23 +02:00
{
if ( stageAsync = = null ) throw new ArgumentNullException ( nameof ( stageAsync ) ) ;
T result = default ! ;
await ExecuteTransactionAsync ( async ( ) = >
{
result = await stageAsync ( ) . ConfigureAwait ( false ) ;
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
} ) . ConfigureAwait ( false ) ;
2026-08-19 01:36:23 +02:00
return result ;
}
2026-05-08 01:13:15 +01:00
public async Task < CultRecordHandle < T > > AddAsync < T > ( T document , CultRecordHandle < T > ? handle = null )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
if ( document = = null ) throw new ArgumentNullException ( nameof ( document ) ) ;
2026-08-19 01:36:23 +02:00
if ( _ambientTransaction . Value is { } transaction )
{
var staged = CreateStoredDocument ( typeof ( T ) , document , handle ? . Key ) ;
transaction . Stage ( staged ) ;
return new CultRecordHandle < T > ( staged . Key ) ;
}
await _transactionGate . WaitAsync ( ) . ConfigureAwait ( false ) ;
try
{
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
var stored = CreateStoredDocument ( typeof ( T ) , document , handle ? . Key ) ;
AddStoredDocumentInternal ( stored , source : null , raiseUpdate : false ) ;
2026-08-19 01:36:23 +02:00
return new CultRecordHandle < T > ( stored . Key ) ;
}
finally
{
_transactionGate . Release ( ) ;
}
2026-04-18 06:04:39 +01:00
}
2026-05-09 13:32:30 +01:00
public Task < CultRecordHandle < T > > UpsertAsync < T > ( T document , CultRecordHandle < T > ? handle = null )
{
return AddAsync ( document , handle ) ;
}
2026-05-24 19:30:46 +01:00
public async Task < CultRecordKey > UpsertAsync ( Type documentType , object document , CultRecordKey ? key = null )
{
if ( documentType = = null ) throw new ArgumentNullException ( nameof ( documentType ) ) ;
if ( document = = null ) throw new ArgumentNullException ( nameof ( document ) ) ;
if ( ! documentType . IsInstanceOfType ( document ) )
{
throw new ArgumentException (
$"Document instance must be assignable to {documentType.FullName}." ,
nameof ( document ) ) ;
}
2026-08-19 01:36:23 +02:00
if ( _ambientTransaction . Value is { } transaction )
{
var staged = CreateStoredDocument ( documentType , document , key ) ;
transaction . Stage ( staged ) ;
return staged . Key ;
}
await _transactionGate . WaitAsync ( ) . ConfigureAwait ( false ) ;
try
{
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
var stored = CreateStoredDocument ( documentType , document , key ) ;
AddStoredDocumentInternal ( stored , source : null , raiseUpdate : false ) ;
2026-08-19 01:36:23 +02:00
return stored . Key ;
}
finally
{
_transactionGate . Release ( ) ;
}
2026-05-24 19:30:46 +01:00
}
2026-05-08 01:13:15 +01:00
public CultRecordHandle < T > ? TryGetHandle < T > ( T document )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
if ( document = = null ) throw new ArgumentNullException ( nameof ( document ) ) ;
return _documentHandles . TryGetValue ( document , out var box )
? new CultRecordHandle < T > ( box . Key )
: null ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
public object? Get ( CultRecordKey key )
2026-04-18 06:04:39 +01:00
{
2026-08-19 01:36:23 +02:00
lock ( _stateGate )
{
if ( _ambientTransaction . Value is { } transaction & &
transaction . TryGet ( key , out var staged ) )
return staged ? . Document ;
return _entries . TryGetValue ( key . Value , out var stored )
? stored . Document
: null ;
}
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
public T ? Get < T > ( CultRecordKey key ) where T : class
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
return Get ( key ) as T ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
public IEnumerable < T > GetAll < T > ( ) where T : class
2026-04-18 06:04:39 +01:00
{
2026-08-19 01:36:23 +02:00
lock ( _stateGate )
2026-08-18 01:07:25 +02:00
{
2026-08-19 01:36:23 +02:00
var type = typeof ( T ) ;
return VisibleStoredDocuments ( )
. Where ( entry = > type . IsAssignableFrom ( entry . Descriptor . DocumentType ) )
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
. Select ( entry = > ( T ) entry . Document )
2026-08-19 01:36:23 +02:00
. ToArray ( ) ;
2026-08-18 01:07:25 +02:00
}
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
public T ? GetGlobal < T > ( ) where T : class
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
return _globalKeys . TryGetValue ( typeof ( T ) , out var key )
? Get < T > ( new CultRecordKey ( key ) )
: null ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
public T ? GetByName < T > ( string name ) where T : class
2026-04-18 06:04:39 +01:00
{
var type = typeof ( T ) ;
2026-05-08 01:13:15 +01:00
if ( _nameMaps . TryGetValue ( type , out var map ) & &
map . TryGetValue ( name , out var key ) )
{
return Get < T > ( new CultRecordKey ( key ) ) ;
}
2026-04-18 06:04:39 +01:00
2026-05-08 01:13:15 +01:00
return null ;
}
2026-04-18 06:04:39 +01:00
2026-05-08 01:13:15 +01:00
public T ? GetByIndex < T > ( string alias , string value ) where T : class
{
if ( _indexMaps . TryGetValue ( ( typeof ( T ) , alias ) , out var map ) & &
map . TryGetValue ( value , out var key ) )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
return Get < T > ( new CultRecordKey ( key ) ) ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
return null ;
}
public void Remove < T > ( CultRecordHandle < T > handle )
{
2026-08-19 01:36:23 +02:00
if ( _ambientTransaction . Value is { } transaction )
{
transaction . Delete ( handle . Key ) ;
return ;
}
_transactionGate . Wait ( ) ;
try
2026-04-18 06:04:39 +01:00
{
2026-08-19 01:36:23 +02:00
if ( _entries . TryGetValue ( handle . Key . Value , out var stored ) )
RemoveStoredDocumentInternal ( stored , source : null , raiseUpdate : false ) ;
}
finally
{
_transactionGate . Release ( ) ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
}
2026-05-24 19:30:46 +01:00
public bool Remove ( CultRecordKey key )
{
2026-08-19 01:36:23 +02:00
if ( Get ( key ) = = null )
2026-05-24 19:30:46 +01:00
{
return false ;
}
2026-08-19 01:36:23 +02:00
if ( _ambientTransaction . Value is { } transaction )
{
transaction . Delete ( key ) ;
return true ;
}
_transactionGate . Wait ( ) ;
try
{
if ( _entries . TryGetValue ( key . Value , out var stored ) )
RemoveStoredDocumentInternal ( stored , source : null , raiseUpdate : false ) ;
}
finally
{
_transactionGate . Release ( ) ;
}
2026-05-24 19:30:46 +01:00
return true ;
}
2026-05-09 13:32:30 +01:00
public Task DeleteAsync < T > ( CultRecordHandle < T > handle )
{
Remove ( handle ) ;
return Task . CompletedTask ;
}
2026-05-08 01:13:15 +01:00
public void Dispose ( )
{
2026-05-09 11:45:46 +01:00
if ( FlushAttachedStoresOnDispose & & IsDirty )
{
FlushAllBackingStores ( ) ;
}
2026-05-08 01:13:15 +01:00
foreach ( var store in _backingStores . OfType < IDisposable > ( ) )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
store . Dispose ( ) ;
2026-04-18 06:04:39 +01:00
}
2026-06-13 01:44:05 +01:00
2026-08-19 01:36:23 +02:00
_transactionGate . Dispose ( ) ;
2026-06-13 01:44:05 +01:00
_changes . Dispose ( ) ;
2026-04-18 06:04:39 +01:00
}
2026-08-19 01:36:23 +02:00
private IEnumerable < CultStoredDocument > VisibleStoredDocuments ( )
{
if ( _ambientTransaction . Value = = null )
return _entries . Values . ToArray ( ) ;
var visible = _entries . ToDictionary ( pair = > pair . Key , pair = > pair . Value , StringComparer . Ordinal ) ;
foreach ( var pair in _ambientTransaction . Value . Mutations )
{
if ( pair . Value = = null )
visible . Remove ( pair . Key ) ;
else
visible [ pair . Key ] = pair . Value ;
}
return visible . Values . ToArray ( ) ;
}
private IReadOnlyList < ( CultStoredDocument Stored , object? Previous , bool Removed ) > CommitTransaction (
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
CultCacheTransaction transaction )
2026-08-19 01:36:23 +02:00
{
2026-08-19 02:17:38 +02:00
var mutations = transaction . SnapshotForCommit ( )
2026-08-19 01:36:23 +02:00
. OrderBy ( pair = > pair . Key , StringComparer . Ordinal )
. ToArray ( ) ;
if ( mutations . Length = = 0 )
return Array . Empty < ( CultStoredDocument , object? , bool ) > ( ) ;
if ( _backingStores . Count > 1 )
{
throw new InvalidOperationException (
"A CultCache transaction requires zero or one durable backing store; multiple stores cannot share one atomic commit boundary." ) ;
}
CultStoredDocument [ ] previous ;
lock ( _stateGate )
{
previous = mutations
. Select ( pair = > _entries . TryGetValue ( pair . Key , out var stored ) ? stored : null )
. Where ( stored = > stored ! = null )
. Cast < CultStoredDocument > ( )
. ToArray ( ) ;
}
var upserts = mutations . Where ( pair = > pair . Value ! = null ) . Select ( pair = > pair . Value ! ) . ToArray ( ) ;
var deleted = mutations
. Where ( pair = > pair . Value = = null )
. Select ( pair = > previous . FirstOrDefault ( stored = > string . Equals ( stored . Key . Value , pair . Key , StringComparison . Ordinal ) ) )
. Where ( stored = > stored ! = null )
. Cast < CultStoredDocument > ( )
. ToArray ( ) ;
foreach ( var store in _backingStores )
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
store . CommitBatch ( upserts , deleted ) ;
2026-08-19 01:36:23 +02:00
var changes = new List < ( CultStoredDocument Stored , object? Previous , bool Removed ) > ( ) ;
lock ( _stateGate )
{
foreach ( var pair in mutations )
{
_entries . TryGetValue ( pair . Key , out var existing ) ;
if ( existing ! = null )
{
RemoveIndexes ( existing ) ;
_documentHandles . Remove ( existing . Document ) ;
}
if ( pair . Value = = null )
{
if ( existing ! = null )
{
_entries . TryRemove ( pair . Key , out _ ) ;
changes . Add ( ( existing , existing . Document , true ) ) ;
}
continue ;
}
var stored = pair . Value ;
_entries [ pair . Key ] = stored ;
_documentHandles . Remove ( stored . Document ) ;
_documentHandles . Add ( stored . Document , new DocumentHandleBox ( stored . Key ) ) ;
AddIndexes ( stored ) ;
changes . Add ( ( stored , existing ? . Document , false ) ) ;
}
_hasUnflushedMutations = _backingStores . Any ( store = > store . IsDirty ) ;
}
return changes ;
}
private sealed class CultCacheTransaction
{
2026-08-19 02:17:38 +02:00
private readonly object _gate = new ( ) ;
2026-08-19 01:36:23 +02:00
private readonly Dictionary < string , CultStoredDocument ? > _mutations = new ( StringComparer . Ordinal ) ;
2026-08-19 02:17:38 +02:00
private bool _sealed ;
2026-08-19 01:36:23 +02:00
2026-08-19 02:17:38 +02:00
public IReadOnlyDictionary < string , CultStoredDocument ? > Mutations
{
get
{
lock ( _gate )
{
ThrowIfSealedForAmbientAccess ( ) ;
return new Dictionary < string , CultStoredDocument ? > ( _mutations , StringComparer . Ordinal ) ;
}
}
}
2026-08-19 01:36:23 +02:00
2026-08-19 02:17:38 +02:00
public void Stage ( CultStoredDocument stored )
{
lock ( _gate )
{
ThrowIfSealedForAmbientAccess ( ) ;
_mutations [ stored . Key . Value ] = stored ;
}
}
2026-08-19 01:36:23 +02:00
2026-08-19 02:17:38 +02:00
public void Delete ( CultRecordKey key )
{
lock ( _gate )
{
ThrowIfSealedForAmbientAccess ( ) ;
_mutations [ key . Value ] = null ;
}
}
2026-08-19 01:36:23 +02:00
2026-08-19 02:17:38 +02:00
public bool TryGet ( CultRecordKey key , out CultStoredDocument ? stored )
{
lock ( _gate )
{
ThrowIfSealedForAmbientAccess ( ) ;
return _mutations . TryGetValue ( key . Value , out stored ) ;
}
}
public void Seal ( )
{
lock ( _gate )
_sealed = true ;
}
public IReadOnlyDictionary < string , CultStoredDocument ? > SnapshotForCommit ( )
{
lock ( _gate )
{
if ( ! _sealed )
throw new InvalidOperationException ( "A CultCache transaction must be sealed before commit." ) ;
return new Dictionary < string , CultStoredDocument ? > ( _mutations , StringComparer . Ordinal ) ;
}
}
private void ThrowIfSealedForAmbientAccess ( )
{
if ( _sealed )
throw new InvalidOperationException (
"This CultCache transaction has already completed; escaped async work cannot read or mutate it." ) ;
}
2026-08-19 01:36:23 +02:00
}
2026-05-08 01:13:15 +01:00
internal CultStoredDocument CreateStoredDocument ( Type documentType , object document , CultRecordKey ? key = null , string? storedAt = null )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
var descriptor = _registry . GetRequired ( documentType ) ;
var resolvedKey = key ? ? ResolveKey ( document , descriptor ) ;
return new CultStoredDocument (
resolvedKey ,
storedAt ? ? DateTimeOffset . UtcNow . ToString ( "O" ) ,
descriptor ,
document ) ;
2026-04-18 06:04:39 +01:00
}
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
private void AddStoredDocumentInternal (
2026-05-08 01:13:15 +01:00
CultStoredDocument stored ,
CacheBackingStore ? source ,
bool raiseUpdate )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
CultStoredDocument ? existing = null ;
2026-08-19 01:36:23 +02:00
lock ( _stateGate )
2026-05-08 01:13:15 +01:00
{
2026-08-19 01:36:23 +02:00
_entries . TryGetValue ( stored . Key . Value , out existing ) ;
if ( existing ! = null )
RemoveIndexes ( existing ) ;
2026-05-08 01:13:15 +01:00
2026-08-19 01:36:23 +02:00
_entries [ stored . Key . Value ] = stored ;
_documentHandles . Remove ( stored . Document ) ;
_documentHandles . Add ( stored . Document , new DocumentHandleBox ( stored . Key ) ) ;
AddIndexes ( stored ) ;
2026-05-08 01:13:15 +01:00
2026-08-19 01:36:23 +02:00
foreach ( var store in _backingStores )
2026-05-08 01:13:15 +01:00
{
2026-08-19 01:36:23 +02:00
if ( store ! = source )
store . Push ( stored ) ;
2026-05-08 01:13:15 +01:00
}
2026-08-19 01:36:23 +02:00
if ( source = = null )
_hasUnflushedMutations = true ;
else
RecomputeDirtyState ( ) ;
2026-05-09 11:45:46 +01:00
}
2026-06-13 01:44:05 +01:00
PublishChange ( stored , existing ? . Document ) ;
2026-05-08 01:13:15 +01:00
if ( raiseUpdate )
{
OnUpdate ? . Invoke ( existing ? . Document , stored . Document ) ;
}
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
private void RemoveStoredDocumentInternal (
CultStoredDocument stored ,
CacheBackingStore ? source ,
bool raiseUpdate )
{
2026-08-19 01:36:23 +02:00
CultStoredDocument ? existing ;
lock ( _stateGate )
2026-04-18 06:04:39 +01:00
{
2026-08-19 01:36:23 +02:00
if ( ! _entries . TryRemove ( stored . Key . Value , out existing ) )
return ;
2026-04-18 06:04:39 +01:00
2026-08-19 01:36:23 +02:00
RemoveIndexes ( existing ) ;
_documentHandles . Remove ( existing . Document ) ;
2026-04-18 06:04:39 +01:00
2026-08-19 01:36:23 +02:00
foreach ( var store in _backingStores )
2026-04-18 06:04:39 +01:00
{
2026-08-19 01:36:23 +02:00
if ( store ! = source )
store . Delete ( existing ) ;
2026-04-18 06:04:39 +01:00
}
2026-08-19 01:36:23 +02:00
if ( source = = null )
_hasUnflushedMutations = true ;
else
RecomputeDirtyState ( ) ;
2026-05-09 11:45:46 +01:00
}
2026-06-13 01:44:05 +01:00
PublishChange ( existing , existing . Document , removed : true ) ;
2026-05-08 01:13:15 +01:00
if ( raiseUpdate )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
OnUpdate ? . Invoke ( existing . Document , null ) ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
}
2026-04-18 06:04:39 +01:00
2026-05-08 01:13:15 +01:00
private void InitializeGlobals ( )
{
foreach ( var descriptor in _registry . AllDescriptors . Where ( candidate = > candidate . IsGlobal ) )
2026-04-18 06:04:39 +01:00
{
2026-07-20 02:23:42 +01:00
var key = new CultRecordKey ( $"global:{descriptor.SchemaId}" ) ;
if ( _entries . ContainsKey ( key . Value ) | |
_backingStores . Any ( store = > store . ContainsDurableRecord ( key ) ) )
{
continue ;
}
2026-05-08 01:13:15 +01:00
if ( descriptor . DocumentType . GetConstructor ( Type . EmptyTypes ) = = null )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
continue ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
var instance = Activator . CreateInstance ( descriptor . DocumentType ) ;
if ( instance = = null )
{
continue ;
}
AddStoredDocumentInternal (
new CultStoredDocument (
2026-07-20 02:23:42 +01:00
key ,
2026-05-08 01:13:15 +01:00
DateTimeOffset . UtcNow . ToString ( "O" ) ,
descriptor ,
instance ) ,
source : null ,
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
raiseUpdate : false ) ;
2026-04-18 06:04:39 +01:00
}
}
2026-05-08 01:13:15 +01:00
private CultRecordKey ResolveKey ( object document , CultDocumentDescriptor descriptor )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
if ( _documentHandles . TryGetValue ( document , out var existing ) )
{
return existing . Key ;
}
if ( descriptor . IsGlobal )
{
return new CultRecordKey ( $"global:{descriptor.SchemaId}" ) ;
}
return new CultRecordKey ( Guid . NewGuid ( ) . ToString ( "N" ) ) ;
2026-04-18 06:04:39 +01:00
}
2026-04-18 13:01:24 +01:00
2026-05-08 01:13:15 +01:00
private void AddIndexes ( CultStoredDocument stored )
2026-04-18 13:01:24 +01:00
{
2026-08-18 01:07:25 +02:00
var typeMap = _typeMaps . GetOrAdd (
stored . Descriptor . DocumentType ,
_ = > new ConcurrentDictionary < string , CultStoredDocument > ( StringComparer . Ordinal ) ) ;
typeMap [ stored . Key . Value ] = stored ;
2026-05-08 01:13:15 +01:00
if ( stored . Descriptor . IsGlobal )
{
_globalKeys [ stored . Descriptor . DocumentType ] = stored . Key . Value ;
}
if ( stored . Descriptor . NameAccessor ? . Invoke ( stored . Document ) is { Length : > 0 } name )
{
var map = _nameMaps . GetOrAdd (
stored . Descriptor . DocumentType ,
_ = > new ConcurrentDictionary < string , string > ( StringComparer . Ordinal ) ) ;
map [ name ] = stored . Key . Value ;
}
foreach ( var pair in stored . Descriptor . IndexAccessors )
{
var value = pair . Value ( stored . Document ) ;
if ( string . IsNullOrWhiteSpace ( value ) )
{
continue ;
}
var map = _indexMaps . GetOrAdd (
( stored . Descriptor . DocumentType , pair . Key ) ,
_ = > new ConcurrentDictionary < string , string > ( StringComparer . Ordinal ) ) ;
map [ value ] = stored . Key . Value ;
}
2026-04-18 13:01:24 +01:00
}
2026-05-08 01:13:15 +01:00
private void RemoveIndexes ( CultStoredDocument stored )
2026-04-18 13:01:24 +01:00
{
2026-08-18 01:07:25 +02:00
if ( _typeMaps . TryGetValue ( stored . Descriptor . DocumentType , out var typeMap ) )
typeMap . TryRemove ( stored . Key . Value , out _ ) ;
2026-05-08 01:13:15 +01:00
if ( stored . Descriptor . IsGlobal )
{
_globalKeys . TryRemove ( stored . Descriptor . DocumentType , out _ ) ;
}
if ( stored . Descriptor . NameAccessor ? . Invoke ( stored . Document ) is { Length : > 0 } name & &
_nameMaps . TryGetValue ( stored . Descriptor . DocumentType , out var nameMap ) )
2026-04-18 13:01:24 +01:00
{
2026-05-08 01:13:15 +01:00
nameMap . TryRemove ( name , out _ ) ;
}
foreach ( var pair in stored . Descriptor . IndexAccessors )
{
var value = pair . Value ( stored . Document ) ;
if ( string . IsNullOrWhiteSpace ( value ) )
{
continue ;
}
if ( _indexMaps . TryGetValue ( ( stored . Descriptor . DocumentType , pair . Key ) , out var map ) )
2026-04-18 13:01:24 +01:00
{
2026-05-08 01:13:15 +01:00
map . TryRemove ( value , out _ ) ;
2026-04-18 13:01:24 +01:00
}
2026-05-08 01:13:15 +01:00
}
}
2026-04-18 13:01:24 +01:00
2026-05-08 01:13:15 +01:00
private sealed class DocumentHandleBox
{
public DocumentHandleBox ( CultRecordKey key )
{
Key = key ;
2026-04-18 13:01:24 +01:00
}
2026-05-08 01:13:15 +01:00
public CultRecordKey Key { get ; }
2026-04-18 13:01:24 +01:00
}
2026-05-09 11:45:46 +01:00
private void RecomputeDirtyState ( )
{
_hasUnflushedMutations = _backingStores . Count = = 0
? _hasUnflushedMutations
: _backingStores . Any ( store = > store . IsDirty ) ;
}
2026-06-13 01:44:05 +01:00
private void PublishChange ( CultStoredDocument stored , object? previousDocument , bool removed = false )
{
var changeType = typeof ( CultCacheDocumentChange < > ) . MakeGenericType ( stored . Descriptor . DocumentType ) ;
var change = Activator . CreateInstance (
changeType ,
removed ? CultCacheDocumentChangeKind . Removed :
previousDocument = = null ? CultCacheDocumentChangeKind . Added :
CultCacheDocumentChangeKind . Updated ,
stored . Key ,
removed ? null : stored . Document ,
previousDocument ) ;
if ( change ! = null )
{
_changes . OnNext ( change ) ;
}
}
2026-04-18 06:04:39 +01:00
}
public abstract class CacheBackingStore : IDisposable
{
2026-05-08 01:13:15 +01:00
private CultDocumentRegistry ? _registry ;
2026-05-09 11:45:46 +01:00
private bool _isDirty ;
private CultSchemaMigrationReport [ ] _lastSchemaMigrationReports = Array . Empty < CultSchemaMigrationReport > ( ) ;
2026-05-08 01:13:15 +01:00
protected CultDocumentRegistry Registry = >
_registry ? ? throw new InvalidOperationException ( "Backing store is not attached to a CultDocumentRegistry." ) ;
protected ConcurrentDictionary < string , CultStoredDocument > Entries { get ; } =
new ( StringComparer . Ordinal ) ;
2026-05-09 11:45:46 +01:00
public bool IsDirty
{
get = > _isDirty ;
protected set = > _isDirty = value ;
}
public bool FlushOnDispose { get ; set ; }
public IReadOnlyList < CultSchemaMigrationReport > LastSchemaMigrationReports = > _lastSchemaMigrationReports ;
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
// Set by the cache at attach; a store calls them for records it loads or drops.
protected internal Action < CultStoredDocument > ? Loaded ;
protected internal Action < CultStoredDocument > ? Unloaded ;
2026-05-08 01:13:15 +01:00
internal void AttachRegistry ( CultDocumentRegistry registry )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
_registry = registry ? ? throw new ArgumentNullException ( nameof ( registry ) ) ;
2026-04-18 06:04:39 +01:00
}
public abstract void PullAll ( ) ;
2026-07-20 02:23:42 +01:00
public virtual bool ContainsDurableRecord ( CultRecordKey key )
{
return Entries . ContainsKey ( key . Value ) ;
}
2026-05-08 01:13:15 +01:00
public abstract void Push ( CultStoredDocument entry ) ;
public abstract void Delete ( CultStoredDocument entry ) ;
2026-09-13 16:54:43 +02:00
// Stages and durably commits a batch as one step. Implementations must restore their prior
// staged view when finality fails.
2026-08-19 01:36:23 +02:00
public virtual void CommitBatch (
IReadOnlyCollection < CultStoredDocument > upserts ,
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
IReadOnlyCollection < CultStoredDocument > deletes )
2026-08-19 01:36:23 +02:00
{
var previousEntries = Entries . ToDictionary ( pair = > pair . Key , pair = > pair . Value , StringComparer . Ordinal ) ;
var wasDirty = IsDirty ;
try
{
foreach ( var entry in deletes )
Delete ( entry ) ;
foreach ( var entry in upserts )
Push ( entry ) ;
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
PushAll ( ) ;
2026-08-19 01:36:23 +02:00
}
catch
{
Entries . Clear ( ) ;
foreach ( var pair in previousEntries )
Entries [ pair . Key ] = pair . Value ;
IsDirty = wasDirty ;
throw ;
}
}
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
public abstract void PushAll ( ) ;
2026-05-08 01:13:15 +01:00
public virtual void Dispose ( )
2026-04-18 06:04:39 +01:00
{
2026-05-09 11:45:46 +01:00
if ( FlushOnDispose & & IsDirty )
{
PushAll ( ) ;
}
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
protected CultPersistedRecord ToPersistedRecord ( CultStoredDocument entry , Func < object , byte [ ] > serializePayload )
{
return new CultPersistedRecord
{
Key = entry . Key . Value ,
SchemaId = entry . Descriptor . SchemaId ,
StoredAt = entry . StoredAt ,
Payload = serializePayload ( entry . Document )
} ;
}
protected CultStoredDocument ToStoredDocument (
CultPersistedRecord record ,
IReadOnlyCollection < CultSchemaCatalogEntry > catalog ,
Func < Type , byte [ ] , object > deserializePayload )
2026-04-18 06:04:39 +01:00
{
2026-05-09 11:45:46 +01:00
var resolution = Registry . ResolvePersistedSchemaDetailed ( record . SchemaId , catalog ) ;
var document = deserializePayload ( resolution . Descriptor . DocumentType , record . Payload ) ;
2026-05-08 01:13:15 +01:00
return new CultStoredDocument (
new CultRecordKey ( record . Key ) ,
record . StoredAt ,
2026-05-09 11:45:46 +01:00
resolution . Descriptor ,
2026-05-08 01:13:15 +01:00
document ) ;
2026-04-18 06:04:39 +01:00
}
2026-05-09 11:45:46 +01:00
protected void SetLastSchemaMigrationReports ( IEnumerable < CultSchemaMigrationReport > reports )
{
_lastSchemaMigrationReports = reports ? . ToArray ( ) ? ? Array . Empty < CultSchemaMigrationReport > ( ) ;
}
protected void MarkFlushSucceeded ( )
{
IsDirty = false ;
}
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
2026-04-18 06:04:39 +01:00
public abstract class SingleFileBackingStore : CacheBackingStore
{
2026-05-08 01:13:15 +01:00
protected SingleFileBackingStore ( string filePath )
2026-04-18 06:04:39 +01:00
{
FileInfo = new FileInfo ( filePath ) ;
}
2026-05-08 01:13:15 +01:00
protected FileInfo FileInfo { get ; }
protected abstract byte [ ] SerializeSnapshot ( CultPersistedStoreSnapshot snapshot ) ;
protected abstract CultPersistedStoreSnapshot DeserializeSnapshot ( byte [ ] data ) ;
protected abstract byte [ ] SerializePayload ( object document ) ;
protected abstract object DeserializePayload ( Type documentType , byte [ ] payload ) ;
2026-04-18 06:04:39 +01:00
public override void PullAll ( )
{
2026-07-18 18:35:39 +01:00
FileInfo . Refresh ( ) ;
2026-05-08 01:13:15 +01:00
if ( ! FileInfo . Exists )
{
2026-05-09 11:45:46 +01:00
SetLastSchemaMigrationReports ( Array . Empty < CultSchemaMigrationReport > ( ) ) ;
IsDirty = false ;
2026-05-08 01:13:15 +01:00
return ;
}
2026-04-18 06:04:39 +01:00
2026-07-18 23:19:45 +01:00
// A single-file snapshot cannot distinguish local dirty keys from clean keys.
// Pulling while local mutations are staged would let an older disk snapshot
// erase those mutations before the next flush. Flush first; clean readers can
// still poll external snapshots incrementally.
if ( IsDirty )
return ;
2026-07-18 18:35:39 +01:00
CultPersistedStoreSnapshot snapshot ;
try
{
snapshot = DeserializeSnapshot ( ReadAllBytesShared ( FileInfo . FullName ) ) ;
}
catch ( FileNotFoundException )
{
FileInfo . Refresh ( ) ;
if ( ! FileInfo . Exists )
return ;
snapshot = DeserializeSnapshot ( ReadAllBytesShared ( FileInfo . FullName ) ) ;
}
2026-05-09 11:45:46 +01:00
var reports = new List < CultSchemaMigrationReport > ( snapshot . Records . Length ) ;
2026-07-18 18:35:39 +01:00
var persistedKeys = new HashSet < string > ( StringComparer . Ordinal ) ;
2026-05-08 01:13:15 +01:00
foreach ( var record in snapshot . Records )
2026-04-18 06:04:39 +01:00
{
2026-07-18 18:35:39 +01:00
persistedKeys . Add ( record . Key ) ;
2026-05-09 11:45:46 +01:00
reports . Add ( Registry . ResolvePersistedSchemaReport ( record . SchemaId , snapshot . SchemaCatalog ) ) ;
2026-05-08 01:13:15 +01:00
var stored = ToStoredDocument ( record , snapshot . SchemaCatalog , DeserializePayload ) ;
2026-07-18 18:35:39 +01:00
if ( Entries . TryGetValue ( stored . Key . Value , out var existing ) & &
string . Equals ( existing . StoredAt , stored . StoredAt , StringComparison . Ordinal ) & &
string . Equals ( existing . Descriptor . SchemaId , stored . Descriptor . SchemaId , StringComparison . Ordinal ) )
{
continue ;
}
2026-05-08 01:13:15 +01:00
Entries [ stored . Key . Value ] = stored ;
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
Loaded ? . Invoke ( stored ) ;
2026-07-18 18:35:39 +01:00
}
foreach ( var removedKey in Entries . Keys . Where ( key = > ! persistedKeys . Contains ( key ) ) . ToArray ( ) )
{
if ( Entries . TryRemove ( removedKey , out var removed ) )
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
Unloaded ? . Invoke ( removed ) ;
2026-04-18 06:04:39 +01:00
}
2026-05-09 11:45:46 +01:00
SetLastSchemaMigrationReports ( reports ) ;
IsDirty = false ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
public override void Push ( CultStoredDocument entry )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
Entries [ entry . Key . Value ] = entry ;
2026-05-09 11:45:46 +01:00
IsDirty = true ;
2026-04-18 06:04:39 +01:00
}
2026-05-08 01:13:15 +01:00
public override void Delete ( CultStoredDocument entry )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
Entries . TryRemove ( entry . Key . Value , out _ ) ;
2026-05-09 11:45:46 +01:00
IsDirty = true ;
2026-04-18 06:04:39 +01:00
}
Delete CultCache wrappers and unused members
Removes FlushBackingStore, both PrepareForReloadOrShutdown overloads,
PullBackingStoreRecordsAsync, TryGet<T>, TryGetByName, TryGetByIndex,
Resolve<T>, the cache and store Logger, and LastSuccessfulFlushAtUtc on the
cache and stores. GetStoredDocuments<T> folds into GetAll<T>.
The soft flag goes from FlushAllBackingStores, ExecuteTransactionAsync,
CommitBatch and PushAll. It stays on FlushAsync(bool) because
CultCacheStudioWindow reflects on that signature until Cut 6.
CultMesh.FlushAsync and CultNetLocal.FlushAsync keep their parameter and
stop forwarding it.
The store's EntryAdded/EntryUpdated/EntryDeleted subjects become
protected internal Loaded/Unloaded callbacks set by the cache at attach.
AddStoredDocumentInternal is synchronous, so the GetAwaiter().GetResult()
bridges on the store subscriptions and the global materializer are gone.
Tests: drop the LastSuccessfulFlushAtUtc asserts; OpenAsync and GetAll tests
read through Get/GetAll; the persisted-global test proves the durable global
loads without staging a default instead of going through selective hydration;
the lease-before-observers test observes through CultCache.Watch; the
PullSelected selective-hydration test is deleted.
Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
2026-09-13 16:44:11 +02:00
public override void PushAll ( )
2026-04-18 06:04:39 +01:00
{
2026-05-08 01:13:15 +01:00
var snapshot = new CultPersistedStoreSnapshot
2026-04-18 13:01:24 +01:00
{
2026-05-08 01:13:15 +01:00
SchemaCatalog = Entries . Values
. Select ( entry = > entry . Descriptor . ToCatalogEntry ( ) )
. GroupBy ( entry = > entry . SchemaId , StringComparer . Ordinal )
. Select ( group = > group . First ( ) )
. OrderBy ( entry = > entry . SchemaName , StringComparer . Ordinal )
. ToArray ( ) ,
Records = Entries . Values
. OrderBy ( entry = > entry . Key . Value , StringComparer . Ordinal )
. Select ( entry = > ToPersistedRecord ( entry , SerializePayload ) )
. ToArray ( )
} ;
2026-04-18 13:01:24 +01:00
2026-05-08 01:13:15 +01:00
Directory . CreateDirectory ( FileInfo . DirectoryName ! ) ;
2026-05-09 01:32:43 +01:00
WriteSnapshotAtomically ( FileInfo . FullName , SerializeSnapshot ( snapshot ) ) ;
2026-05-09 11:45:46 +01:00
MarkFlushSucceeded ( ) ;
2026-05-09 01:32:43 +01:00
}
private static void WriteSnapshotAtomically ( string path , byte [ ] payload )
{
var directory = Path . GetDirectoryName ( path ) ! ;
var tempPath = Path . Combine ( directory , $".{Path.GetFileName(path)}.{Guid.NewGuid():N}.tmp" ) ;
try
{
using ( var stream = new FileStream (
tempPath ,
FileMode . CreateNew ,
FileAccess . Write ,
FileShare . None ,
bufferSize : 4096 ,
FileOptions . WriteThrough ) )
{
stream . Write ( payload , 0 , payload . Length ) ;
stream . Flush ( true ) ;
}
if ( File . Exists ( path ) )
{
File . Replace ( tempPath , path , null ) ;
}
else
{
File . Move ( tempPath , path ) ;
}
}
finally
{
if ( File . Exists ( tempPath ) )
{
File . Delete ( tempPath ) ;
}
}
2026-04-18 06:04:39 +01:00
}
2026-07-18 18:35:39 +01:00
private static byte [ ] ReadAllBytesShared ( string path )
{
using var stream = new FileStream ( path , FileMode . Open , FileAccess . Read , FileShare . ReadWrite | FileShare . Delete ) ;
using var buffer = new MemoryStream ( ) ;
stream . CopyTo ( buffer ) ;
return buffer . ToArray ( ) ;
}
2026-04-18 06:04:39 +01:00
}
2026-04-18 13:01:24 +01:00
}