diff --git a/crates/cli/src/subcommands/generate/csharp.rs b/crates/cli/src/subcommands/generate/csharp.rs index c53f4e7126b..6148a411d8f 100644 --- a/crates/cli/src/subcommands/generate/csharp.rs +++ b/crates/cli/src/subcommands/generate/csharp.rs @@ -407,20 +407,20 @@ fn autogen_csharp_access_funcs_for_struct( None => continue, Some(x) => x, }; - writeln!( - output, - "public readonly ref struct {csharp_field_name_pascal}UniqueIndex" - ); + writeln!(output, "public class {csharp_field_name_pascal}UniqueIndex"); indented_block(output, |output| { + write!( + output, + "internal readonly Dictionary<{csharp_field_type}, {struct_name_pascal_case}> Cache = new(16);" + ); + writeln!(output); + writeln!( output, "public {struct_name_pascal_case}? Find({csharp_field_type} value)" ); indented_block(output, |output| { - writeln!( - output, - "{csharp_field_name_pascal}_Index.TryGetValue(value, out var r);" - ); + writeln!(output, "Cache.TryGetValue(value, out var r);"); writeln!(output, "return r;"); }); writeln!(output); @@ -428,7 +428,7 @@ fn autogen_csharp_access_funcs_for_struct( writeln!(output); writeln!( output, - "public {csharp_field_name_pascal}UniqueIndex {csharp_field_name_pascal} => new();" + "public {csharp_field_name_pascal}UniqueIndex {csharp_field_name_pascal} = new();" ); writeln!(output); } @@ -589,11 +589,6 @@ pub fn autogen_csharp_globals(ctx: &GenCtx, items: &[GenItem], namespace: &str) if !constraints[&ColList::new(col.col_pos)].has_unique() { continue; } - let type_name = ty_fmt(ctx, &col.col_type, namespace); - writeln!( - output, - "private static Dictionary<{type_name}, {table_type}> {field_name}_Index = new(16);" - ); unique_indexes.push(field_name); } if !unique_indexes.is_empty() { @@ -610,7 +605,7 @@ pub fn autogen_csharp_globals(ctx: &GenCtx, items: &[GenItem], namespace: &str) if !constraints[&ColList::new(col.col_pos)].has_unique() { continue; } - writeln!(output, "{field_name}_Index[value.{field_name}] = value;"); + writeln!(output, "{field_name}.Cache[value.{field_name}] = value;"); } }); writeln!(output); @@ -625,7 +620,7 @@ pub fn autogen_csharp_globals(ctx: &GenCtx, items: &[GenItem], namespace: &str) if !constraints[&ColList::new(col.col_pos)].has_unique() { continue; } - writeln!(output, "{field_name}_Index.Remove((({table_type})row).{field_name});"); + writeln!(output, "{field_name}.Cache.Remove((({table_type})row).{field_name});"); } }); writeln!(output); diff --git a/crates/cli/tests/snapshots/codegen__codegen_csharp.snap b/crates/cli/tests/snapshots/codegen__codegen_csharp.snap index 163116ed327..2095776370a 100644 --- a/crates/cli/tests/snapshots/codegen__codegen_csharp.snap +++ b/crates/cli/tests/snapshots/codegen__codegen_csharp.snap @@ -672,45 +672,45 @@ namespace SpacetimeDB public class PkMultiIdentityHandle : RemoteTableHandle { - private static Dictionary Id_Index = new(16); - private static Dictionary Other_Index = new(16); public override void InternalInvokeValueInserted(IDatabaseRow row) { var value = (PkMultiIdentity)row; - Id_Index[value.Id] = value; - Other_Index[value.Other] = value; + Id.Cache[value.Id] = value; + Other.Cache[value.Other] = value; } public override void InternalInvokeValueDeleted(IDatabaseRow row) { - Id_Index.Remove(((PkMultiIdentity)row).Id); - Other_Index.Remove(((PkMultiIdentity)row).Other); + Id.Cache.Remove(((PkMultiIdentity)row).Id); + Other.Cache.Remove(((PkMultiIdentity)row).Other); } - public readonly ref struct IdUniqueIndex + public class IdUniqueIndex { + internal readonly Dictionary Cache = new(16); public PkMultiIdentity? Find(uint value) { - Id_Index.TryGetValue(value, out var r); + Cache.TryGetValue(value, out var r); return r; } } - public IdUniqueIndex Id => new(); + public IdUniqueIndex Id = new(); - public readonly ref struct OtherUniqueIndex + public class OtherUniqueIndex { + internal readonly Dictionary Cache = new(16); public PkMultiIdentity? Find(uint value) { - Other_Index.TryGetValue(value, out var r); + Cache.TryGetValue(value, out var r); return r; } } - public OtherUniqueIndex Other => new(); + public OtherUniqueIndex Other = new(); internal PkMultiIdentityHandle() { @@ -754,30 +754,30 @@ namespace SpacetimeDB public class RepeatingTestArgHandle : RemoteTableHandle { - private static Dictionary ScheduledId_Index = new(16); public override void InternalInvokeValueInserted(IDatabaseRow row) { var value = (RepeatingTestArg)row; - ScheduledId_Index[value.ScheduledId] = value; + ScheduledId.Cache[value.ScheduledId] = value; } public override void InternalInvokeValueDeleted(IDatabaseRow row) { - ScheduledId_Index.Remove(((RepeatingTestArg)row).ScheduledId); + ScheduledId.Cache.Remove(((RepeatingTestArg)row).ScheduledId); } - public readonly ref struct ScheduledIdUniqueIndex + public class ScheduledIdUniqueIndex { + internal readonly Dictionary Cache = new(16); public RepeatingTestArg? Find(ulong value) { - ScheduledId_Index.TryGetValue(value, out var r); + Cache.TryGetValue(value, out var r); return r; } } - public ScheduledIdUniqueIndex ScheduledId => new(); + public ScheduledIdUniqueIndex ScheduledId = new(); internal RepeatingTestArgHandle() { @@ -821,30 +821,30 @@ namespace SpacetimeDB public class TestEHandle : RemoteTableHandle { - private static Dictionary Id_Index = new(16); public override void InternalInvokeValueInserted(IDatabaseRow row) { var value = (TestE)row; - Id_Index[value.Id] = value; + Id.Cache[value.Id] = value; } public override void InternalInvokeValueDeleted(IDatabaseRow row) { - Id_Index.Remove(((TestE)row).Id); + Id.Cache.Remove(((TestE)row).Id); } - public readonly ref struct IdUniqueIndex + public class IdUniqueIndex { + internal readonly Dictionary Cache = new(16); public TestE? Find(ulong value) { - Id_Index.TryGetValue(value, out var r); + Cache.TryGetValue(value, out var r); return r; } } - public IdUniqueIndex Id => new(); + public IdUniqueIndex Id = new(); public class NameIndex {