Skip to content

Calculate closures correctly #309

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

Open
wants to merge 15 commits into
base: dev
Choose a base branch
from
Open
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
14 changes: 14 additions & 0 deletions .github/copilot-instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Coding standards, domain knowledge, and preferences that AI should follow

## C# Coding Standards

- Use the csharpier formatter for formatting C# code.
- Use the .editorconfig file for code style settings.
- Always use `var` when the type is obvious from the right side of the assignment.
- Always add braces for `if`, `else`, `for`, `foreach`, `while`, and `do` statements, even if they are single-line statements.

## Testing

- Use xUnit for unit testing.
- Use FluentAssertions for assertions in tests.
- Use Moq for mocking dependencies in tests.
22 changes: 22 additions & 0 deletions .github/git-commit-instructions.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Git Commit Instructions

To ensure high-quality and consistent commits, please follow these guidelines:

1. **Format your code**
- Run the `csharpier` formatter on all C# files before committing.
- Ensure your code adheres to the `.editorconfig` settings.

2. **Write clear commit messages**
- Use the present tense ("Add feature" not "Added feature").
- Start with a short summary (max 72 characters), followed by a blank line and a detailed description if necessary.

3. **Test your changes**
- Run all unit tests before committing.
- Add or update xUnit tests as needed.
- Use FluentAssertions for assertions and Moq for mocking in tests.

4. **Review your changes**
- Double-check for accidental debug code or commented-out code.
- Ensure only relevant files are staged.

Thank you for helping maintain code quality!
4 changes: 3 additions & 1 deletion Speckle.Sdk.slnx
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,8 @@
<File Path="GitVersion.yml" />
<File Path="global.json" />
<File Path="README.md" />
<File Path=".github\copilot-instructions.md" />
<File Path=".github\git-commit-instructions.md" />
</Folder>
<Folder Name="/config/workflows/">
<File Path=".github/workflows/pr.yml" />
Expand All @@ -35,4 +37,4 @@
<Project Path="tests/Speckle.Sdk.Tests.Integration/Speckle.Sdk.Tests.Integration.csproj" />
<Project Path="tests/Speckle.Sdk.Tests.Unit/Speckle.Sdk.Tests.Unit.csproj" />
</Folder>
</Solution>
</Solution>
18 changes: 18 additions & 0 deletions src/Speckle.Sdk/Common/NotNullExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,4 +95,22 @@ public static IEnumerable<T> Empty<T>(this IEnumerable<T>? obj)
}
return obj;
}

public static string NotNullOrWhiteSpace(
[NotNull] this string? value,
[CallerArgumentExpression(nameof(value))] string? paramName = null
)
{
if (value is null)
{
throw new ArgumentNullException(paramName ?? "Value is null");
}

if (string.IsNullOrWhiteSpace(value))
{
throw new ArgumentException("Value cannot be empty or whitespace.", paramName);
}

return value;
}
}
53 changes: 37 additions & 16 deletions src/Speckle.Sdk/SQLite/SQLiteJsonCacheManager.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
using System.Text;
using Microsoft.Data.Sqlite;
using Speckle.InterfaceGenerator;
using Speckle.Sdk.Common;
using Speckle.Sdk.Dependencies;

namespace Speckle.Sdk.SQLite;
Expand Down Expand Up @@ -120,7 +121,10 @@ public void DeleteObject(string id) =>
);

//This does an insert or ignores if already exists
public void SaveObject(string id, string json) =>
public void SaveObject(string id, string json)
{
id.NotNullOrWhiteSpace();
json.NotNullOrWhiteSpace();
_pool.Use(
CacheOperation.InsertOrIgnore,
command =>
Expand All @@ -130,6 +134,7 @@ public void SaveObject(string id, string json) =>
command.ExecuteNonQuery();
}
);
}

//This does an insert or replaces if already exists
public void UpdateObject(string id, string json) =>
Expand All @@ -148,29 +153,45 @@ public void SaveObjects(IEnumerable<(string id, string json)> items) =>
CacheOperation.BulkInsertOrIgnore,
cmd =>
{
CreateBulkInsert(cmd, items);
return cmd.ExecuteNonQuery();
if (CreateBulkInsert(cmd, items))
{
cmd.ExecuteNonQuery();
}
}
);

private void CreateBulkInsert(SqliteCommand cmd, IEnumerable<(string id, string json)> items)
private bool CreateBulkInsert(SqliteCommand cmd, IEnumerable<(string id, string json)> items)
{
StringBuilder sb = Pools.StringBuilders.Get();
sb.AppendLine(CacheDbCommands.Commands[(int)CacheOperation.BulkInsertOrIgnore]);
int i = 0;
foreach (var (id, json) in items)
try
{
sb.Append($"(@key{i}, @value{i}),");
cmd.Parameters.AddWithValue($"@key{i}", id);
cmd.Parameters.AddWithValue($"@value{i}", json);
i++;
}
sb.Remove(sb.Length - 1, 1);
sb.Append(';');
sb.AppendLine(CacheDbCommands.Commands[(int)CacheOperation.BulkInsertOrIgnore]);
int i = 0;
foreach (var (id, json) in items)
{
sb.Append($"(@key{i}, @value{i}),");
cmd.Parameters.AddWithValue($"@key{i}", id);
cmd.Parameters.AddWithValue($"@value{i}", json);
i++;
}

if (i == 0)
{
return false;
}

sb.Remove(sb.Length - 1, 1);
sb.Append(';');
#pragma warning disable CA2100
cmd.CommandText = sb.ToString();
cmd.CommandText = sb.ToString();
#pragma warning restore CA2100
Pools.StringBuilders.Return(sb);
}
finally
{
Pools.StringBuilders.Return(sb);
}

return true;
}

public bool HasObject(string objectId) =>
Expand Down
58 changes: 58 additions & 0 deletions src/Speckle.Sdk/Serialisation/V2/Send/ClosureMath.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
namespace Speckle.Sdk.Serialisation.V2.Send;

public static class ClosureMath
{
public static void IncrementClosures(this Dictionary<Id, int> current, IEnumerable<KeyValuePair<Id, int>> child)
{
foreach (var closure in child)
{
if (current.TryGetValue(closure.Key, out var count))
{
current[closure.Key] = Math.Max(closure.Value, count) + 1;
}
else
{
current[closure.Key] = closure.Value + 1;
}
}
}

public static void MergeClosures(this Dictionary<Id, int> current, IEnumerable<KeyValuePair<Id, int>> child)
{
foreach (var closure in child)
{
if (current.TryGetValue(closure.Key, out var count))
{
current[closure.Key] = Math.Max(closure.Value, count);
}
else
{
current[closure.Key] = closure.Value;
}
}
}

public static void IncrementClosure(this Dictionary<Id, int> current, Id id)
{
if (current.TryGetValue(id, out var count))
{
current[id] = count + 1;
}
else
{
current[id] = 1;
}
}

public static void MergeClosure(this Dictionary<Id, int> current, Id id)
{
if (current.TryGetValue(id, out var count))
{
current[id] = count;
}
else
{
current[id] = 1;
}
}
}
Loading