Path: redb/redb.Core/Providers/Base/QueryableProviderBase.cs Snapshot: redb, development snapshot 2026-12-06. This is the development state of the public mirrors; it may differ from the published packages. License: Apache-2.0. Repository: https://github.com/redbase-app/redb Page: https://redbase.app/src/dev-2026-12-06/redb/redb.Core/Providers/Base/QueryableProviderBase.cs ---- #pragma warning disable CS0618 // implements or uses the deprecated object permissions and roles (RedbDeprecation) using redb.Core.Data; using redb.Core.Models.Configuration; using redb.Core.Models.Contracts; using redb.Core.Models.Security; using redb.Core.Query; using redb.Core.Query.QueryExpressions; using redb.Core.Serialization; using Microsoft.Extensions.Logging; using System; using System.Collections.Generic; using System.Linq; using System.Threading; using System.Threading.Tasks; namespace redb.Core.Providers.Base { /// /// Base class for IQueryableProvider implementations. /// Contains all common logic for creating LINQ queries. /// DB-specific implementations provide concrete query providers via factory methods. /// public abstract class QueryableProviderBase : IQueryableProvider { protected readonly IRedbContext Context; protected readonly IRedbObjectSerializer Serializer; protected readonly ISchemeSyncProvider SchemeSync; protected readonly ILogger? Logger; protected readonly IRedbSecurityContext SecurityContext; protected readonly RedbServiceConfiguration Configuration; protected readonly ILazyPropsLoader? LazyPropsLoader; protected readonly string CacheDomain; /// The list provider of the service, set by RedbServiceBase: see WithListItems. internal IListProvider? ListItems { get; set; } // Hands the list provider to the query providers of a new query, for the groupings it may run: the key of a // grouping by a list item is read through it (Query.Grouping.ListItemGroupKeys). private IRedbQueryable WithListItems(IRedbQueryable query) where TProps : class, new() { if (query is redb.Core.Query.RedbQueryable flat && flat.QueryProvider is redb.Core.Query.Grouping.IListItemKeySource flatKeys) flatKeys.ListItems = ListItems; if (query is redb.Core.Query.Base.TreeQueryableBase tree && tree.TreeProvider is redb.Core.Query.Grouping.IListItemKeySource treeKeys) treeKeys.ListItems = ListItems; return query; } protected QueryableProviderBase( IRedbContext context, IRedbObjectSerializer serializer, ISchemeSyncProvider schemeSync, IRedbSecurityContext securityContext, ILazyPropsLoader? lazyPropsLoader = null, RedbServiceConfiguration? configuration = null, string? cacheDomain = null, ILogger? logger = null) { Context = context ?? throw new ArgumentNullException(nameof(context)); Serializer = serializer ?? throw new ArgumentNullException(nameof(serializer)); SchemeSync = schemeSync ?? throw new ArgumentNullException(nameof(schemeSync)); SecurityContext = securityContext ?? throw new ArgumentNullException(nameof(securityContext)); LazyPropsLoader = lazyPropsLoader; Configuration = configuration ?? new RedbServiceConfiguration(); CacheDomain = cacheDomain ?? Configuration.GetEffectiveCacheDomain(); Logger = logger; } // ===== ABSTRACT FACTORY METHODS ===== /// /// Create DB-specific query provider for flat queries. /// protected abstract IRedbQueryable CreateQuery(long schemeId, long? userId, bool checkPermissions) where TProps : class, new(); /// /// Create DB-specific query provider for tree queries. /// protected abstract IRedbQueryable CreateTreeQuery( long schemeId, long? userId, bool checkPermissions, long? rootObjectId, int? maxDepth) where TProps : class, new(); /// /// Create empty tree query (for null rootObject case). /// protected abstract IRedbQueryable CreateEmptyTreeQuery(long schemeId, long? userId, bool checkPermissions) where TProps : class, new(); /// /// Create tree query for multiple root objects. /// protected abstract IRedbQueryable CreateMultiRootTreeQuery( long schemeId, long? userId, bool checkPermissions, List rootObjects, int? maxDepth) where TProps : class, new(); /// /// Create tree query for multiple root object IDs. /// protected abstract IRedbQueryable CreateMultiRootTreeQueryByIds( long schemeId, long? userId, bool checkPermissions, IEnumerable rootObjectIds, int? maxDepth) where TProps : class, new(); // ===== FLAT QUERIES ===== public IRedbQueryable Query() where TProps : class, new() { var scheme = GetSchemeSync(); var effectiveUser = SecurityContext.GetEffectiveUser(); return WithListItems(CreateQuery(scheme.Id, effectiveUser.Id, Configuration.DefaultCheckPermissionsOnQuery)); } public IRedbQueryable Query(IRedbUser user) where TProps : class, new() { var scheme = GetSchemeSync(); return WithListItems(CreateQuery(scheme.Id, user.Id, Configuration.DefaultCheckPermissionsOnQuery)); } /// /// Get scheme synchronously - first from cache, then via Task.Run to avoid SynchronizationContext deadlock. /// Cache is warmed up by InitializeAsync() at startup, so Task.Run path is rarely used. /// private IRedbScheme GetSchemeSync() where TProps : class { // Try cache first (fast, no async, no DB call) var scheme = SchemeSync.GetSchemeFromCache(); if (scheme != null) return scheme; // Fallback: use Task.Run to avoid Blazor/ASP.NET SynchronizationContext deadlock. // This runs GetSchemeByTypeAsync on ThreadPool where there's no SynchronizationContext. // Note: GetSchemeByTypeAsync will cache the result, so subsequent calls will hit cache. scheme = Task.Run(() => SchemeSync.GetSchemeByTypeAsync()).GetAwaiter().GetResult(); if (scheme == null) throw new InvalidOperationException($"Scheme for type '{typeof(TProps).Name}' not found. Ensure InitializeAsync() was called at startup."); // Read once and cached for the process: a process that reads schemes another one owns (a Tsak worker // whose module does not sync them) meets this on every first query, and nothing is wrong with it. Logger?.LogDebug( "Scheme '{SchemeName}' was not in the cache and was read from the database on first use.", scheme.Name); return scheme; } // ===== TREE QUERIES ===== public IRedbQueryable TreeQuery() where TProps : class, new() { var scheme = GetSchemeSync(); var effectiveUser = SecurityContext.GetEffectiveUser(); return WithListItems(CreateTreeQuery(scheme.Id, effectiveUser.Id, Configuration.DefaultCheckPermissionsOnQuery, null, null)); } public IRedbQueryable TreeQuery(IRedbUser user) where TProps : class, new() { var scheme = GetSchemeSync(); return WithListItems(CreateTreeQuery(scheme.Id, user.Id, Configuration.DefaultCheckPermissionsOnQuery, null, null)); } // ===== SYNC TREE QUERIES WITH ROOT OBJECT ===== public IRedbQueryable TreeQuery(long rootObjectId, int? maxDepth = null) where TProps : class, new() { var scheme = GetSchemeSync(); var effectiveUser = SecurityContext.GetEffectiveUser(); return WithListItems(CreateTreeQuery(scheme.Id, effectiveUser.Id, Configuration.DefaultCheckPermissionsOnQuery, rootObjectId, maxDepth)); } public IRedbQueryable TreeQuery(IRedbObject? rootObject, int? maxDepth = null) where TProps : class, new() { var scheme = GetSchemeSync(); var effectiveUser = SecurityContext.GetEffectiveUser(); if (rootObject == null) return WithListItems(CreateEmptyTreeQuery(scheme.Id, effectiveUser.Id, Configuration.DefaultCheckPermissionsOnQuery)); return WithListItems(CreateTreeQuery(scheme.Id, effectiveUser.Id, Configuration.DefaultCheckPermissionsOnQuery, rootObject.Id, maxDepth)); } public IRedbQueryable TreeQuery(IEnumerable rootObjects, int? maxDepth = null) where TProps : class, new() { var rootList = rootObjects?.ToList() ?? []; var scheme = GetSchemeSync(); var effectiveUser = SecurityContext.GetEffectiveUser(); if (!rootList.Any()) return WithListItems(CreateEmptyTreeQuery(scheme.Id, effectiveUser.Id, Configuration.DefaultCheckPermissionsOnQuery)); if (rootList.Count == 1) return WithListItems(CreateTreeQuery(scheme.Id, effectiveUser.Id, Configuration.DefaultCheckPermissionsOnQuery, rootList[0].Id, maxDepth)); return WithListItems(CreateMultiRootTreeQuery(scheme.Id, effectiveUser.Id, Configuration.DefaultCheckPermissionsOnQuery, rootList, maxDepth)); } public IRedbQueryable TreeQuery(IEnumerable rootObjectIds, int? maxDepth = null) where TProps : class, new() { var idsList = rootObjectIds?.ToList() ?? []; var scheme = GetSchemeSync(); var effectiveUser = SecurityContext.GetEffectiveUser(); if (!idsList.Any()) return WithListItems(CreateEmptyTreeQuery(scheme.Id, effectiveUser.Id, Configuration.DefaultCheckPermissionsOnQuery)); if (idsList.Count == 1) return WithListItems(CreateTreeQuery(scheme.Id, effectiveUser.Id, Configuration.DefaultCheckPermissionsOnQuery, idsList[0], maxDepth)); return WithListItems(CreateMultiRootTreeQueryByIds(scheme.Id, effectiveUser.Id, Configuration.DefaultCheckPermissionsOnQuery, idsList, maxDepth)); } public IRedbQueryable TreeQuery(long rootObjectId, IRedbUser user, int? maxDepth = null) where TProps : class, new() { var scheme = GetSchemeSync(); return WithListItems(CreateTreeQuery(scheme.Id, user.Id, Configuration.DefaultCheckPermissionsOnQuery, rootObjectId, maxDepth)); } public IRedbQueryable TreeQuery(IRedbObject? rootObject, IRedbUser user, int? maxDepth = null) where TProps : class, new() { var scheme = GetSchemeSync(); if (rootObject == null) return WithListItems(CreateEmptyTreeQuery(scheme.Id, user.Id, Configuration.DefaultCheckPermissionsOnQuery)); return WithListItems(CreateTreeQuery(scheme.Id, user.Id, Configuration.DefaultCheckPermissionsOnQuery, rootObject.Id, maxDepth)); } public IRedbQueryable TreeQuery(IEnumerable rootObjects, IRedbUser user, int? maxDepth = null) where TProps : class, new() { var rootList = rootObjects?.ToList() ?? []; var scheme = GetSchemeSync(); if (!rootList.Any()) return WithListItems(CreateEmptyTreeQuery(scheme.Id, user.Id, Configuration.DefaultCheckPermissionsOnQuery)); if (rootList.Count == 1) return WithListItems(CreateTreeQuery(scheme.Id, user.Id, Configuration.DefaultCheckPermissionsOnQuery, rootList[0].Id, maxDepth)); return WithListItems(CreateMultiRootTreeQuery(scheme.Id, user.Id, Configuration.DefaultCheckPermissionsOnQuery, rootList, maxDepth)); } public IRedbQueryable TreeQuery(IEnumerable rootObjectIds, IRedbUser user, int? maxDepth = null) where TProps : class, new() { var idsList = rootObjectIds?.ToList() ?? []; var scheme = GetSchemeSync(); if (!idsList.Any()) return WithListItems(CreateEmptyTreeQuery(scheme.Id, user.Id, Configuration.DefaultCheckPermissionsOnQuery)); if (idsList.Count == 1) return WithListItems(CreateTreeQuery(scheme.Id, user.Id, Configuration.DefaultCheckPermissionsOnQuery, idsList[0], maxDepth)); return WithListItems(CreateMultiRootTreeQueryByIds(scheme.Id, user.Id, Configuration.DefaultCheckPermissionsOnQuery, idsList, maxDepth)); } } }