Skip to content

Commit 19d1fff

Browse files
committed
Use native query to avoid huge overheads when cleaning up realm files
1 parent f376bb5 commit 19d1fff

File tree

2 files changed

+7
-6
lines changed

2 files changed

+7
-6
lines changed

osu.Game/Database/RealmFileStore.cs

+3-6
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// See the LICENCE file in the repository root for full licence text.
33

44
using System;
5+
using System.Diagnostics;
56
using System.IO;
67
using System.Linq;
78
using osu.Framework.Extensions;
@@ -98,15 +99,11 @@ public void Cleanup()
9899
// can potentially be run asynchronously, although we will need to consider operation order for disk deletion vs realm removal.
99100
realm.Write(r =>
100101
{
101-
// TODO: consider using a realm native query to avoid iterating all files (https://github.com/realm/realm-dotnet/issues/2659#issuecomment-927823707)
102-
var files = r.All<RealmFile>().ToList();
103-
104-
foreach (var file in files)
102+
foreach (var file in r.All<RealmFile>().Filter("Usages.@count = 0"))
105103
{
106104
totalFiles++;
107105

108-
if (file.BacklinksCount > 0)
109-
continue;
106+
Debug.Assert(file.BacklinksCount == 0);
110107

111108
try
112109
{

osu.Game/Models/RealmFile.cs

+4
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
22
// See the LICENCE file in the repository root for full licence text.
33

4+
using System.Linq;
45
using osu.Game.IO;
56
using Realms;
67

@@ -11,5 +12,8 @@ public class RealmFile : RealmObject, IFileInfo
1112
{
1213
[PrimaryKey]
1314
public string Hash { get; set; } = string.Empty;
15+
16+
[Backlink(nameof(RealmNamedFileUsage.File))]
17+
public IQueryable<RealmNamedFileUsage> Usages { get; } = null!;
1418
}
1519
}

0 commit comments

Comments
 (0)