Merge pull request #15789 from peppy/realm-integration/file-store

Add automatic clean-up call for `RealmFileStore`
This commit is contained in:
Dan Balasescu 2021-11-25 15:10:41 +09:00 committed by GitHub
commit 546785b905
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 17 additions and 13 deletions

View File

@ -1050,7 +1050,7 @@ namespace osu.Game.Tests.Beatmaps.IO
private static void checkSingleReferencedFileCount(OsuGameBase osu, int expected) private static void checkSingleReferencedFileCount(OsuGameBase osu, int expected)
{ {
Assert.AreEqual(expected, osu.Dependencies.Get<FileStore>().QueryFiles(f => f.ReferenceCount == 1).Count()); Assert.AreEqual(expected, osu.Dependencies.Get<DatabaseContextFactory>().Get().FileInfo.Count(f => f.ReferenceCount == 1));
} }
private static void ensureLoaded(OsuGameBase osu, int timeout = 60000) private static void ensureLoaded(OsuGameBase osu, int timeout = 60000)

View File

@ -19,7 +19,7 @@ namespace osu.Game.Database
/// <param name="obj">The object to use as a reference when negotiating a local instance.</param> /// <param name="obj">The object to use as a reference when negotiating a local instance.</param>
/// <param name="lookupSource">An optional lookup source which will be used to query and populate a freshly retrieved replacement. If not provided, the refreshed object will still be returned but will not have any includes.</param> /// <param name="lookupSource">An optional lookup source which will be used to query and populate a freshly retrieved replacement. If not provided, the refreshed object will still be returned but will not have any includes.</param>
/// <typeparam name="T">A valid EF-stored type.</typeparam> /// <typeparam name="T">A valid EF-stored type.</typeparam>
protected virtual void Refresh<T>(ref T obj, IQueryable<T> lookupSource = null) where T : class, IHasPrimaryKey protected void Refresh<T>(ref T obj, IQueryable<T> lookupSource = null) where T : class, IHasPrimaryKey
{ {
using (var usage = ContextFactory.GetForWrite()) using (var usage = ContextFactory.GetForWrite())
{ {

View File

@ -14,6 +14,7 @@ using osu.Framework.Statistics;
using osu.Game.Configuration; using osu.Game.Configuration;
using osu.Game.Input.Bindings; using osu.Game.Input.Bindings;
using osu.Game.Models; using osu.Game.Models;
using osu.Game.Stores;
using Realms; using Realms;
#nullable enable #nullable enable
@ -121,6 +122,10 @@ namespace osu.Game.Database
transaction.Commit(); transaction.Commit();
} }
// clean up files after dropping any pending deletions.
// in the future we may want to only do this when the game is idle, rather than on every startup.
new RealmFileStore(this, storage).Cleanup();
} }
/// <summary> /// <summary>

View File

@ -2,11 +2,8 @@
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System; using System;
using System.Collections.Generic;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
using osu.Framework.Extensions; using osu.Framework.Extensions;
using osu.Framework.IO.Stores; using osu.Framework.IO.Stores;
using osu.Framework.Logging; using osu.Framework.Logging;
@ -31,13 +28,6 @@ namespace osu.Game.IO
Store = new StorageBackedResourceStore(Storage); Store = new StorageBackedResourceStore(Storage);
} }
/// <summary>
/// Perform a lookup query on available <see cref="FileInfo"/>s.
/// </summary>
/// <param name="query">The query.</param>
/// <returns>Results from the provided query.</returns>
public IEnumerable<FileInfo> QueryFiles(Expression<Func<FileInfo, bool>> query) => ContextFactory.Get().Set<FileInfo>().AsNoTracking().Where(f => f.ReferenceCount > 0).Where(query);
public FileInfo Add(Stream data, bool reference = true) public FileInfo Add(Stream data, bool reference = true)
{ {
using (var usage = ContextFactory.GetForWrite()) using (var usage = ContextFactory.GetForWrite())

View File

@ -86,9 +86,13 @@ namespace osu.Game.Stores
public void Cleanup() public void Cleanup()
{ {
var realm = realmFactory.Context; Logger.Log(@"Beginning realm file store cleanup");
int totalFiles = 0;
int removedFiles = 0;
// can potentially be run asynchronously, although we will need to consider operation order for disk deletion vs realm removal. // can potentially be run asynchronously, although we will need to consider operation order for disk deletion vs realm removal.
using (var realm = realmFactory.CreateContext())
using (var transaction = realm.BeginWrite()) using (var transaction = realm.BeginWrite())
{ {
// TODO: consider using a realm native query to avoid iterating all files (https://github.com/realm/realm-dotnet/issues/2659#issuecomment-927823707) // TODO: consider using a realm native query to avoid iterating all files (https://github.com/realm/realm-dotnet/issues/2659#issuecomment-927823707)
@ -96,11 +100,14 @@ namespace osu.Game.Stores
foreach (var file in files) foreach (var file in files)
{ {
totalFiles++;
if (file.BacklinksCount > 0) if (file.BacklinksCount > 0)
continue; continue;
try try
{ {
removedFiles++;
Storage.Delete(file.GetStoragePath()); Storage.Delete(file.GetStoragePath());
realm.Remove(file); realm.Remove(file);
} }
@ -112,6 +119,8 @@ namespace osu.Game.Stores
transaction.Commit(); transaction.Commit();
} }
Logger.Log($@"Finished realm file store cleanup ({removedFiles} of {totalFiles} deleted)");
} }
} }
} }