Path: redb/redb.MSSql/Providers/LazyPropsLoader.cs
Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages.
License: Apache-2.0. Repository: https://github.com/redbase-app/redb
Page: https://redbase.app/src/dev-2026-12-06/redb/redb.MSSql/Providers/LazyPropsLoader.cs
----
using System.Collections.Concurrent;
using redb.Core.Caching;
using redb.Core.Data;
using redb.Core.Models.Configuration;
using redb.Core.Models.Entities;
using redb.Core.Exceptions;
using redb.Core.Models.Configuration;
using redb.Core.Providers;
using redb.Core.Serialization;
using Microsoft.Extensions.Logging;
using redb.MSSql.Sql;
namespace redb.MSSql.Providers;
///
/// OpenSource implementation of lazy Props loading for MS SQL Server.
/// Uses get_object_json SQL function for simple and efficient loading.
///
public class LazyPropsLoader : ILazyPropsLoader
{
// get_object_json depth the batch uses when the caller names none (query pages, tree loads).
private const int DefaultBatchDepth = 10;
private readonly ConcurrentDictionary _loadingInProgress = new();
private readonly IRedbContext _context;
private readonly IRedbObjectSerializer _serializer;
private readonly RedbServiceConfiguration _config;
private readonly MsSqlDialect _sql;
private readonly GlobalPropsCache _propsCache;
///
public string? CacheDomain => _propsCache.Domain;
///
public redb.Core.Data.IRedbContext? ScopeContext => _context;
private readonly ILogger? _logger;
public LazyPropsLoader(
IRedbContext context,
ISchemeSyncProvider schemeSync,
IRedbObjectSerializer serializer,
RedbServiceConfiguration config,
IListProvider? listProvider = null,
ILogger? logger = null)
{
_context = context ?? throw new ArgumentNullException(nameof(context));
_serializer = serializer ?? throw new ArgumentNullException(nameof(serializer));
_config = config ?? throw new ArgumentNullException(nameof(config));
_propsCache = schemeSync?.PropsCache ?? throw new ArgumentNullException(nameof(schemeSync));
_sql = new MsSqlDialect();
_logger = logger;
}
///
/// Synchronous Props loading (for getter).
///
public TProps? LoadProps(long objectId, long schemeId) where TProps : class, new()
{
if (_config.LazyReferenceAccess == LazyReferenceAccessMode.Throw)
throw new RedbSynchronousLazyLoadException(objectId, schemeId);
// Blocking by design (the getter is synchronous), but never on the caller's context: the load
// runs on the thread pool, so a host with a SynchronizationContext (Blazor Server, WPF, MAUI)
// waits instead of deadlocking on its own continuations (review). The caller blocks meanwhile,
// so the context stays single-threaded; ExecutionContext (the AsyncLocal scope) flows in.
return Task.Run(() => LoadPropsAsync(objectId, schemeId)).GetAwaiter().GetResult();
}
///
/// Async Props loading for single object via get_object_json.
/// Simple and efficient for OpenSource version.
///
public async Task LoadPropsAsync(long objectId, long schemeId, CancellationToken cancellationToken = default) where TProps : class, new()
{
if (_context.IsDisposed)
throw new RedbLazyLoadScopeEndedException(objectId, schemeId);
// 1. Check cache first
if (_config.EnablePropsCache && _propsCache.Instance != null)
{
var hashes = await _context.QueryScalarListAsync(
_sql.LazyLoader_SelectObjectHash(), new object[] { objectId }, cancellationToken);
if (hashes.Count > 0)
{
var hash = hashes[0];
var cached = _propsCache.Get(objectId, hash);
if (cached != null)
{
var propsFromCache = cached.GetPropsDirectly();
if (propsFromCache != null)
return propsFromCache;
}
}
}
// 2. Load via get_object_json — ONE query, simple!
var jsonResults = await _context.QueryScalarListAsync(
_sql.LazyLoader_GetObjectJson(), new object[] { objectId, 1 }, cancellationToken); // V4 (L.3): the reloaded object's own references are stubs again
var json = jsonResults.FirstOrDefault();
if (string.IsNullOrEmpty(json))
return null; // V4 (L.3): a reference whose target left for the trash reloads as null Props, not an exception
// 3. Deserialize
var obj = _serializer.Deserialize(json);
redb.Core.Utils.LazyReferenceInstaller.Install(obj, this); // V4 (L.3): nested stubs get their loader
// 4. Cache the result
if (_config.EnablePropsCache && obj.hash.HasValue)
{
redb.Core.Caching.CachePublication.AfterCommit(_context, () => _propsCache.Set(obj));
}
// Raw access instead of the getter: for an object with properties:null (values wiped by a
// race, or a bare object) Install has attached the loader to the root itself, so the Props
// getter would start a new load - endless recursion eating a pool thread per turn.
return obj.GetPropsDirectly();
}
///
/// BULK Props loading for multiple objects via get_object_json batch.
///
public async Task LoadPropsForManyAsync(List> objects, int? propsDepth, CancellationToken cancellationToken = default) where TProps : class, new()
{
var depth = propsDepth ?? DefaultBatchDepth;
if (objects.Count == 0) return;
var requested = objects;
// Protection from infinite recursion
var uniqueIds = objects.Select(o => o.id).Distinct().ToList();
var alreadyLoading = uniqueIds.Where(id => _loadingInProgress.ContainsKey(id)).ToList();
if (alreadyLoading.Any())
{
objects = objects.Where(o => !alreadyLoading.Contains(o.id)).ToList();
uniqueIds = uniqueIds.Except(alreadyLoading).ToList();
if (objects.Count == 0) return;
}
foreach (var id in uniqueIds)
_loadingInProgress.TryAdd(id, 0);
try
{
HashSet needToLoad;
Dictionary> fromCache;
// 1. Check cache
if (_config.EnablePropsCache && _propsCache.Instance != null)
{
var objectsData = objects
.Where(o => o.hash.HasValue)
.Select(o => (o.id, o.hash!.Value))
.ToList();
needToLoad = _propsCache.FilterNeedToLoad(objectsData, out fromCache);
// An object without a hash cannot be matched by the cache: it loads from the database, never dropped.
foreach (var obj in objects)
if (!obj.hash.HasValue)
needToLoad.Add(obj.id);
foreach (var obj in objects)
{
if (fromCache.TryGetValue(obj.id, out var cachedObj))
{
obj.Props = cachedObj.Props;
obj._propsLoaded = true;
obj._lazyLoader = null;
}
}
}
else
{
needToLoad = objects.Select(o => o.id).ToHashSet();
fromCache = new Dictionary>();
}
// 2. Load from DB via get_object_json batch
if (needToLoad.Count > 0)
{
// For MSSQL, convert array to comma-separated string
var idsString = string.Join(",", needToLoad);
// Batch query via STRING_SPLIT + get_object_json
var results = await _context.QueryAsync(
_sql.LazyLoader_GetObjectJsonBatch(), new object[] { idsString, depth }, cancellationToken);
var jsonById = results.ToDictionary(r => r.Id, r => r.JsonData);
foreach (var obj in objects.Where(o => needToLoad.Contains(o.id)))
{
if (jsonById.TryGetValue(obj.id, out var json) && !string.IsNullOrEmpty(json))
{
try
{
var loaded = _serializer.Deserialize(json);
// Copy Props from loaded object
obj.Props = loaded.Props;
obj._propsLoaded = true;
obj._lazyLoader = null;
redb.Core.Utils.LazyReferenceInstaller.Install(obj, this); // V4 (L.3)
// Cache
if (_config.EnablePropsCache && obj.hash.HasValue)
{
redb.Core.Caching.CachePublication.AfterCommit(_context, () => _propsCache.Set(obj));
}
}
catch
{
// Deserialization error - skip this object
}
}
}
// get_object_json is null only for an object that is gone: deleted after the query found it. It
// leaves the list instead of being handed out without Props; the page is shorter by it.
requested.RemoveAll(o => needToLoad.Contains(o.id) && !(jsonById.TryGetValue(o.id, out var j) && !string.IsNullOrEmpty(j)));
}
}
finally
{
foreach (var id in uniqueIds)
_loadingInProgress.TryRemove(id, out _);
}
}
///
/// BULK Props loading with projection filter (for Select projections).
/// In OpenSource version, projection is ignored — full objects loaded via get_object_json.
///
public Task LoadPropsForManyAsync(
List> objects,
HashSet? projectedStructureIds, CancellationToken cancellationToken = default) where TProps : class, new()
{
// OpenSource: ignore projection, load full objects
return LoadPropsForManyAsync(objects, propsDepth: null);
}
///
/// BULK Props loading at the default batch depth; the depth form above is the core.
/// A stub reload passes 1 (transitive laziness), query pages their PropsDepth.
///
public Task LoadPropsForManyAsync(List> objects, CancellationToken cancellationToken = default) where TProps : class, new()
=> LoadPropsForManyAsync(objects, propsDepth: null);
///
/// BULK Props loading with projection filter and custom depth.
/// In OpenSource version the projection is ignored (full objects); the depth is honoured.
///
public Task LoadPropsForManyAsync(
List> objects,
HashSet? projectedStructureIds,
int? propsDepth, CancellationToken cancellationToken = default) where TProps : class, new()
{
// OpenSource: ignore projection; the depth is honoured
return LoadPropsForManyAsync(objects, propsDepth);
}
///
/// BULK loading for polymorphic objects (different schemes).
/// Each object is deserialized to its own type based on scheme_id.
///
public async Task LoadPropsForManyPolymorphicAsync(List objects, CancellationToken cancellationToken = default)
{
if (objects.Count == 0) return;
var uniqueIds = objects.Select(o => o.Id).Distinct().ToList();
var alreadyLoading = uniqueIds.Where(id => _loadingInProgress.ContainsKey(id)).ToList();
if (alreadyLoading.Any())
{
objects = objects.Where(o => !alreadyLoading.Contains(o.Id)).ToList();
uniqueIds = uniqueIds.Except(alreadyLoading).ToList();
if (objects.Count == 0) return;
}
foreach (var id in uniqueIds)
_loadingInProgress.TryAdd(id, 0);
try
{
// For MSSQL, convert array to comma-separated string
var idsString = string.Join(",", uniqueIds);
var depth = DefaultBatchDepth;
// Batch query
var results = await _context.QueryAsync(
_sql.LazyLoader_GetObjectJsonBatch(), new object[] { idsString, depth }, cancellationToken);
var jsonById = results.ToDictionary(r => r.Id, r => r.JsonData);
foreach (var obj in objects)
{
if (jsonById.TryGetValue(obj.Id, out var json) && !string.IsNullOrEmpty(json))
{
try
{
// Get Props type from object's generic parameter (the type or its RedbObject base)
var objType = obj.GetType();
var genericObjType = redb.Core.Utils.RedbObjectTypes.GenericOf(objType);
if (genericObjType != null)
{
var propsType = genericObjType.GetGenericArguments()[0];
var loaded = _serializer.DeserializeDynamic(json, propsType);
// Copy Props via reflection
var propsProperty = objType.GetProperty("Props");
var loadedProps = loaded.GetType().GetProperty("Props")?.GetValue(loaded);
propsProperty?.SetValue(obj, loadedProps);
// Mark as loaded
var loadedField = objType.GetField("_propsLoaded");
loadedField?.SetValue(obj, true);
var loaderField = objType.GetField("_lazyLoader");
loaderField?.SetValue(obj, null);
}
}
catch (Exception)
{
// Skip failed deserialization
}
}
}
}
finally
{
foreach (var id in uniqueIds)
_loadingInProgress.TryRemove(id, out _);
}
}
}
///
/// DTO for batch get_object_json results.
///
internal class ObjectJsonResult
{
public long Id { get; set; }
public string JsonData { get; set; } = string.Empty;
}