Skip to content

Use native query to avoid huge overheads when cleaning up realm files #26451

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

Merged
merged 4 commits into from
Jan 9, 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
10 changes: 3 additions & 7 deletions osu.Game/Database/RealmFileStore.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
// See the LICENCE file in the repository root for full licence text.

using System;
using System.Diagnostics;
using System.IO;
using System.Linq;
using osu.Framework.Extensions;
using osu.Framework.IO.Stores;
using osu.Framework.Logging;
Expand Down Expand Up @@ -98,15 +98,11 @@ public void Cleanup()
// can potentially be run asynchronously, although we will need to consider operation order for disk deletion vs realm removal.
realm.Write(r =>
{
// TODO: consider using a realm native query to avoid iterating all files (https://github.com/realm/realm-dotnet/issues/2659#issuecomment-927823707)
var files = r.All<RealmFile>().ToList();

foreach (var file in files)
foreach (var file in r.All<RealmFile>().Filter(@$"{nameof(RealmFile.Usages)}.@count = 0"))
{
totalFiles++;

if (file.BacklinksCount > 0)
continue;
Debug.Assert(file.BacklinksCount == 0);

try
{
Expand Down
4 changes: 4 additions & 0 deletions osu.Game/Models/RealmFile.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text.

using System.Linq;
using osu.Game.IO;
using Realms;

Expand All @@ -11,5 +12,8 @@ public class RealmFile : RealmObject, IFileInfo
{
[PrimaryKey]
public string Hash { get; set; } = string.Empty;

[Backlink(nameof(RealmNamedFileUsage.File))]
public IQueryable<RealmNamedFileUsage> Usages { get; } = null!;
}
}