Path: redb/redb.Core/Query/TreeProjectedQueryable.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/Query/TreeProjectedQueryable.cs
----
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using System.Threading;
using System.Threading.Tasks;
using redb.Core.Models.Entities;
using redb.Core.Query;
namespace redb.Core.Query;
///
/// Implementation of projections for tree LINQ queries in REDB
/// Specialized version for TreeRedbObject<TProps>
///
public class TreeProjectedQueryable : IRedbProjectedQueryable
where TProps : class, new()
{
private readonly IRedbQueryable _sourceQuery;
private readonly Expression, TResult>> _projection;
// Chain of operations to execute after projection
private readonly List>> _wherePredicates = new();
private readonly List<(Expression KeySelector, bool IsDescending)> _orderByExpressions = new();
// TPQ-1 (review 2026-09-24), the tree twin of S-4 in RedbProjectedQueryable: Where/OrderBy run in memory after
// the projection, so a Take/Skip called after them must too - sent to the source query it cut the page from
// the unfiltered, unsorted rows. Distinct is over the projected rows, not over the source objects (every node
// is distinct there). Pipeline: Where* -> OrderBy* -> Distinct -> Skip/Take in call order. A Take/Skip before
// the first in-memory operation still goes to SQL.
private readonly bool _isDistinct;
private readonly List<(bool IsSkip, int Count)> _pagingOps = new();
public TreeProjectedQueryable(
IRedbQueryable sourceQuery,
Expression, TResult>> projection)
{
_sourceQuery = sourceQuery;
_projection = projection;
}
// Private constructor for creating copies with additional operations
private TreeProjectedQueryable(
IRedbQueryable sourceQuery,
Expression, TResult>> projection,
List>> wherePredicates,
List<(Expression KeySelector, bool IsDescending)> orderByExpressions,
bool isDistinct,
List<(bool IsSkip, int Count)> pagingOps)
{
_sourceQuery = sourceQuery;
_projection = projection;
_wherePredicates = new List>>(wherePredicates);
_orderByExpressions = new List<(Expression, bool)>(orderByExpressions);
_isDistinct = isDistinct;
_pagingOps = new List<(bool, int)>(pagingOps);
}
private TreeProjectedQueryable With(
IRedbQueryable? sourceQuery = null,
List>>? wherePredicates = null,
List<(Expression KeySelector, bool IsDescending)>? orderByExpressions = null,
bool? isDistinct = null)
=> new(sourceQuery ?? _sourceQuery, _projection, wherePredicates ?? _wherePredicates,
orderByExpressions ?? _orderByExpressions, isDistinct ?? _isDistinct, _pagingOps);
private bool HasInMemoryOps =>
_wherePredicates.Count > 0 || _orderByExpressions.Count > 0 || _isDistinct || _pagingOps.Count > 0;
public IRedbProjectedQueryable Where(Expression> predicate)
{
if (predicate == null)
throw new ArgumentNullException(nameof(predicate));
// Add filter to operations chain
return With(wherePredicates: new List>>(_wherePredicates) { predicate });
}
public IRedbProjectedQueryable OrderBy(Expression> keySelector)
{
if (keySelector == null)
throw new ArgumentNullException(nameof(keySelector));
// Replace existing sorting
return With(orderByExpressions: new List<(Expression, bool)> { (keySelector, false) });
}
public IRedbProjectedQueryable OrderByDescending(Expression> keySelector)
{
if (keySelector == null)
throw new ArgumentNullException(nameof(keySelector));
// Replace existing sorting
return With(orderByExpressions: new List<(Expression, bool)> { (keySelector, true) });
}
public IRedbProjectedQueryable ThenBy(Expression> keySelector)
=> AddThenBy(keySelector, descending: false);
public IRedbProjectedQueryable ThenByDescending(Expression> keySelector)
=> AddThenBy(keySelector, descending: true);
// A tree is loaded whole and sorted in memory: ThenBy adds a key to that ordering.
private IRedbProjectedQueryable AddThenBy(LambdaExpression keySelector, bool descending)
{
if (keySelector == null)
throw new ArgumentNullException(nameof(keySelector));
if (_orderByExpressions.Count == 0)
throw new InvalidOperationException("ThenBy continues an ordering: call OrderBy or OrderByDescending first.");
return With(orderByExpressions: new List<(Expression, bool)>(_orderByExpressions) { (keySelector, descending) });
}
public IRedbProjectedQueryable Take(int count)
{
if (HasInMemoryOps)
return WithPagingOp(isSkip: false, count);
return With(sourceQuery: _sourceQuery.Take(count));
}
public IRedbProjectedQueryable Skip(int count)
{
if (HasInMemoryOps)
return WithPagingOp(isSkip: true, count);
return With(sourceQuery: _sourceQuery.Skip(count));
}
private TreeProjectedQueryable WithPagingOp(bool isSkip, int count)
{
var copy = With();
copy._pagingOps.Add((isSkip, count));
return copy;
}
public IRedbProjectedQueryable Distinct() => With(isDistinct: true);
public async Task> ToListAsync(CancellationToken cancellationToken = default)
{
// Everything after the projection runs in memory over the loaded nodes.
var allObjects = await _sourceQuery.ToListAsync(cancellationToken: cancellationToken);
var projection = _projection.Compile();
IEnumerable projectedResults = allObjects.Select(redbObj => projection((TreeRedbObject)redbObj));
foreach (var wherePredicate in _wherePredicates)
projectedResults = projectedResults.Where(wherePredicate.Compile());
IOrderedEnumerable? orderedResults = null;
foreach (var (keySelector, isDescending) in _orderByExpressions)
{
var compiledKeySelector = ((LambdaExpression)keySelector).Compile();
if (orderedResults == null)
orderedResults = isDescending
? projectedResults.OrderByDescending(item => compiledKeySelector.DynamicInvoke(item))
: projectedResults.OrderBy(item => compiledKeySelector.DynamicInvoke(item));
else
orderedResults = isDescending
? orderedResults.ThenByDescending(item => compiledKeySelector.DynamicInvoke(item))
: orderedResults.ThenBy(item => compiledKeySelector.DynamicInvoke(item));
}
if (orderedResults != null)
projectedResults = orderedResults;
if (_isDistinct)
projectedResults = projectedResults.Distinct();
foreach (var (isSkip, n) in _pagingOps)
projectedResults = isSkip ? projectedResults.Skip(n) : projectedResults.Take(n);
return projectedResults.ToList();
}
public async Task CountAsync(CancellationToken cancellationToken = default)
{
var results = await ToListAsync(cancellationToken: cancellationToken);
return results.Count;
}
public async Task FirstOrDefaultAsync(CancellationToken cancellationToken = default)
{
var results = await ToListAsync(cancellationToken: cancellationToken);
return results.FirstOrDefault();
}
///
/// Get projection info for tree queries (not optimized yet)
///
public Task GetProjectionInfoAsync(CancellationToken cancellationToken = default)
{
var info = @"=== TREE PROJECTION INFO ===
SQL Function: search_objects_with_facets (full load)
Note: Tree projections are not yet optimized for SQL projection
All data is loaded and projected in memory";
return Task.FromResult(info);
}
}