Path: redb/redb.Core/Query/QueryPlannerScope.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/QueryPlannerScope.cs
----
using System;
using System.Threading;
using redb.Core.Models.Configuration;
namespace redb.Core.Query;
///
/// The query planner () switched for a block of code:
/// using (redb.WithQueryPlanner(false)) { ... }. The configuration is one object per process, and changing it
/// changes every concurrent request; a block changes only the calling flow (an ), the awaits
/// and tasks inside it included, and disposing it restores what was in effect before. Blocks nest.
///
/// Pro reads the planner through ; Free has no planner and ignores it.
///
///
public static class QueryPlannerScope
{
private sealed class Restore : IDisposable
{
private readonly bool? _previous;
private bool _disposed;
public Restore(bool? previous) => _previous = previous;
public void Dispose()
{
if (_disposed) return;
_disposed = true;
Current.Value = _previous;
}
}
private static readonly AsyncLocal Current = new();
/// The value set by the innermost block of the calling flow; null outside any block.
public static bool? BlockValue => Current.Value;
/// Whether the planner is on for the calling flow: the innermost block, else the configuration.
public static bool IsEnabled(RedbServiceConfiguration configuration) => Current.Value ?? configuration.EnablePvtPrefilter;
private sealed class NoChange : IDisposable
{
public void Dispose() { }
}
/// The block of a tier without a planner: it changes nothing.
internal static IDisposable Unchanged { get; } = new NoChange();
/// Sets the planner for a block of the calling flow; disposing restores the previous value.
internal static IDisposable Push(bool enabled)
{
var previous = Current.Value;
Current.Value = enabled;
return new Restore(previous);
}
}