Path: redb/redb.Examples/Examples/E091_TreeCount.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.Examples/Examples/E091_TreeCount.cs
----
using System.Diagnostics;
using redb.Core;
using redb.Examples.Models;
using redb.Examples.Output;
namespace redb.Examples.Examples;
///
/// Count tree nodes with TreeQuery.
///
/// **Requires E089 to run first (creates tree data).**
///
/// CountAsync on TreeQuery - counts all nodes in tree.
///
[ExampleMeta("E091", "Tree Count - CountAsync", "Trees",
ExampleTier.Free, 1, "Tree", "TreeQuery", "CountAsync", "Pro", Order = 91)]
public class E091_TreeCount : ExampleBase
{
public override async Task RunAsync(IRedbService redb)
{
var sw = Stopwatch.StartNew();
var count = await redb.TreeQuery().CountAsync();
sw.Stop();
if (count == 0)
return Fail("E091", "Tree Count - CountAsync", ExampleTier.Free, sw.ElapsedMilliseconds, "No tree. Run E089 first.");
return Ok("E091", "Tree Count - CountAsync", ExampleTier.Free, sw.ElapsedMilliseconds, count,
[$"TreeQuery().CountAsync()", $"Total: {count} nodes"]);
}
}