Path: redb/redb.Examples/Examples/E089_TreeCreate.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/E089_TreeCreate.cs ---- using System.Diagnostics; using redb.Core; using redb.Core.Models.Entities; using redb.Examples.Models; using redb.Examples.Output; namespace redb.Examples.Examples; /// /// Sequential create tree hierarchy - deliberate ANTIPATTERN demo. /// /// This example is SLOW BY DESIGN: it saves ~100 tree nodes one CreateChildAsync at a time, /// paying a full save pipeline (key allocation, hash, values insert, commit) per node, to give /// E088 (bulk AddNewObjectsAsync, one round-trip for the whole level) a baseline to beat. /// Do NOT copy this shape into application code - use the bulk API of E088. /// /// Structure: /// - 1 root (TechCorp) /// - 10 regional offices /// - 50 departments (5 per office) /// - 40 teams (4 per first 10 departments) /// [ExampleMeta("E089", "Tree Create Sequential - ANTIPATTERN (slow by design)", "Trees", ExampleTier.Free, 3, "Tree", "CreateChildAsync", "Sequential", "Antipattern", "Pro", Order = 89)] public class E089_TreeCreate : ExampleBase { private const int OfficeCount = 10; private const int DeptPerOffice = 5; private const int TeamsForFirstDepts = 10; private const int TeamsPerDept = 4; public override async Task RunAsync(IRedbService redb) { // Run E087 first to cleanup existing data // Measure sequential creation var sw = Stopwatch.StartNew(); var count = await CreateTreeSequentially(redb); sw.Stop(); var rate = count * 1000 / Math.Max(sw.ElapsedMilliseconds, 1); Console.WriteLine($"[E089] ANTIPATTERN demo: {count} nodes saved one round-trip each on purpose - compare with E088 (bulk)."); return Ok("E089", "Tree Create Sequential - ANTIPATTERN (slow by design)", ExampleTier.Free, sw.ElapsedMilliseconds, count, [ "ANTIPATTERN: one CreateChildAsync round-trip per node, slow on purpose.", $"Sequential created: {count} tree nodes in 4 levels", $"Rate: {rate} nodes/sec - compare with E088 (bulk AddNewObjectsAsync)" ]); } /// Creates tree using sequential CreateChildAsync calls. private static async Task CreateTreeSequentially(IRedbService redb) { var count = 0; // Level 0: Root var root = CreateDept("TechCorp", "CORP", "Headquarters", 50_000_000m); root.id = await redb.SaveAsync(root); count++; // Level 1: Regional offices var offices = new TreeRedbObject[OfficeCount]; for (int i = 0; i < OfficeCount; i++) { var city = Cities[i % Cities.Length]; offices[i] = CreateDept($"{city} Office", $"OFF-{i + 1:D2}", $"Regional office {city}", 5_000_000m - i * 200_000m); offices[i].id = await redb.CreateChildAsync(offices[i], root); count++; } // Level 2: Departments in each office var depts = new List>(); for (int o = 0; o < OfficeCount; o++) { for (int d = 0; d < DeptPerOffice; d++) { var deptName = Departments[d % Departments.Length]; var dept = CreateDept($"{deptName} {o + 1}-{d + 1}", $"DEPT-{o + 1:D2}-{d + 1:D2}", deptName, 1_000_000m - d * 100_000m); dept.id = await redb.CreateChildAsync(dept, offices[o]); depts.Add(dept); count++; } } // Level 3: Teams in first 10 departments for (int d = 0; d < TeamsForFirstDepts && d < depts.Count; d++) { for (int t = 0; t < TeamsPerDept; t++) { var teamName = Teams[t % Teams.Length]; var team = CreateDept($"{teamName} Team {d + 1}-{t + 1}", $"TEAM-{d + 1:D2}-{t + 1:D2}", teamName, 300_000m - t * 50_000m); await redb.CreateChildAsync(team, depts[d]); count++; } } return count; } private static readonly string[] Cities = ["Moscow", "SPB", "Kazan", "Novosibirsk", "Yekaterinburg", "Nizhny", "Samara", "Omsk", "Chelyabinsk", "Rostov"]; private static readonly string[] Departments = ["IT", "Sales", "HR", "Finance", "Marketing"]; private static readonly string[] Teams = ["Alpha", "Beta", "Gamma", "Delta"]; private static TreeRedbObject CreateDept(string name, string code, string desc, decimal budget) => new() { name = name, Props = new DepartmentProps { Name = name, Code = code, Description = desc, Budget = budget, IsActive = true } }; }