Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C# codegen making state relative to a DbConnection #1869

Merged
merged 7 commits into from
Oct 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 11 additions & 16 deletions crates/cli/src/subcommands/generate/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -407,28 +407,28 @@ 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)"
lcodes marked this conversation as resolved.
Show resolved Hide resolved
);
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);
});
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);
}
Expand Down Expand Up @@ -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() {
Expand All @@ -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);
Expand All @@ -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);
Expand Down
48 changes: 24 additions & 24 deletions crates/cli/tests/snapshots/codegen__codegen_csharp.snap
Original file line number Diff line number Diff line change
Expand Up @@ -672,45 +672,45 @@ namespace SpacetimeDB

public class PkMultiIdentityHandle : RemoteTableHandle<EventContext, PkMultiIdentity>
{
private static Dictionary<uint, PkMultiIdentity> Id_Index = new(16);
private static Dictionary<uint, PkMultiIdentity> 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<uint, PkMultiIdentity> 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<uint, PkMultiIdentity> 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()
{
Expand Down Expand Up @@ -754,30 +754,30 @@ namespace SpacetimeDB

public class RepeatingTestArgHandle : RemoteTableHandle<EventContext, RepeatingTestArg>
{
private static Dictionary<ulong, RepeatingTestArg> 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<ulong, RepeatingTestArg> 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()
{
Expand Down Expand Up @@ -821,30 +821,30 @@ namespace SpacetimeDB

public class TestEHandle : RemoteTableHandle<EventContext, TestE>
{
private static Dictionary<ulong, TestE> 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<ulong, TestE> 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
{
Expand Down
Loading