2021-11-29 09:07:32 +00:00
|
|
|
// 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;
|
|
|
|
using System.Collections.Generic;
|
2022-06-27 06:52:45 +00:00
|
|
|
using System.Diagnostics;
|
2021-11-29 09:07:32 +00:00
|
|
|
using System.IO;
|
|
|
|
using System.Linq;
|
|
|
|
using osu.Framework.Platform;
|
2022-08-10 06:48:38 +00:00
|
|
|
using osu.Game.Beatmaps;
|
2022-01-11 07:30:35 +00:00
|
|
|
using osu.Game.Extensions;
|
2021-11-29 09:07:32 +00:00
|
|
|
using osu.Game.Models;
|
|
|
|
using osu.Game.Overlays.Notifications;
|
|
|
|
using Realms;
|
|
|
|
|
2022-06-15 08:13:32 +00:00
|
|
|
namespace osu.Game.Database
|
2021-11-29 09:07:32 +00:00
|
|
|
{
|
2022-06-16 09:53:13 +00:00
|
|
|
public class ModelManager<TModel> : IModelManager<TModel>, IModelFileManager<TModel, RealmNamedFileUsage>
|
2021-11-29 09:07:32 +00:00
|
|
|
where TModel : RealmObject, IHasRealmFiles, IHasGuidPrimaryKey, ISoftDelete
|
|
|
|
{
|
2022-06-16 09:56:53 +00:00
|
|
|
protected RealmAccess Realm { get; }
|
2022-06-16 09:53:13 +00:00
|
|
|
|
2021-11-29 09:07:32 +00:00
|
|
|
private readonly RealmFileStore realmFileStore;
|
|
|
|
|
2022-06-16 09:53:13 +00:00
|
|
|
public ModelManager(Storage storage, RealmAccess realm)
|
2021-11-29 09:07:32 +00:00
|
|
|
{
|
2022-01-24 10:59:58 +00:00
|
|
|
realmFileStore = new RealmFileStore(realm, storage);
|
2022-06-16 09:53:13 +00:00
|
|
|
Realm = realm;
|
2021-11-29 09:07:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void DeleteFile(TModel item, RealmNamedFileUsage file) =>
|
2022-01-11 10:25:50 +00:00
|
|
|
performFileOperation(item, managed => DeleteFile(managed, managed.Files.First(f => f.Filename == file.Filename), managed.Realm));
|
2021-11-29 09:07:32 +00:00
|
|
|
|
2022-01-11 07:30:35 +00:00
|
|
|
public void ReplaceFile(TModel item, RealmNamedFileUsage file, Stream contents) =>
|
|
|
|
performFileOperation(item, managed => ReplaceFile(file, contents, managed.Realm));
|
2021-11-29 09:07:32 +00:00
|
|
|
|
2022-01-11 07:30:35 +00:00
|
|
|
public void AddFile(TModel item, Stream contents, string filename) =>
|
|
|
|
performFileOperation(item, managed => AddFile(managed, contents, filename, managed.Realm));
|
|
|
|
|
|
|
|
private void performFileOperation(TModel item, Action<TModel> operation)
|
|
|
|
{
|
|
|
|
// While we are detaching so often, this seems like the easiest way to keep things in sync.
|
|
|
|
// This method should be removed as soon as all the surrounding pieces support non-detached operations.
|
|
|
|
if (!item.IsManaged)
|
|
|
|
{
|
2022-05-10 11:47:26 +00:00
|
|
|
// Importantly, begin the realm write *before* re-fetching, else the update realm may not be in a consistent state
|
|
|
|
// (ie. if an async import finished very recently).
|
|
|
|
Realm.Realm.Write(realm =>
|
|
|
|
{
|
2022-05-10 15:45:17 +00:00
|
|
|
var managed = realm.Find<TModel>(item.ID);
|
2022-06-27 06:52:45 +00:00
|
|
|
Debug.Assert(managed != null);
|
2022-05-10 11:47:26 +00:00
|
|
|
operation(managed);
|
|
|
|
|
|
|
|
item.Files.Clear();
|
|
|
|
item.Files.AddRange(managed.Files.Detach());
|
|
|
|
});
|
2022-01-11 07:30:35 +00:00
|
|
|
}
|
|
|
|
else
|
|
|
|
operation(item);
|
|
|
|
}
|
2021-11-29 09:07:32 +00:00
|
|
|
|
2021-12-01 03:55:19 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Delete a file from within an ongoing realm transaction.
|
|
|
|
/// </summary>
|
2022-06-16 09:53:13 +00:00
|
|
|
public void DeleteFile(TModel item, RealmNamedFileUsage file, Realm realm)
|
2021-11-29 09:07:32 +00:00
|
|
|
{
|
|
|
|
item.Files.Remove(file);
|
|
|
|
}
|
|
|
|
|
2021-12-01 03:55:19 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Replace a file from within an ongoing realm transaction.
|
|
|
|
/// </summary>
|
2022-06-16 09:53:13 +00:00
|
|
|
public void ReplaceFile(RealmNamedFileUsage file, Stream contents, Realm realm)
|
2021-11-29 09:07:32 +00:00
|
|
|
{
|
|
|
|
file.File = realmFileStore.Add(contents, realm);
|
|
|
|
}
|
|
|
|
|
2021-12-01 03:55:19 +00:00
|
|
|
/// <summary>
|
2021-12-02 08:17:12 +00:00
|
|
|
/// Add a file from within an ongoing realm transaction. If the file already exists, it is overwritten.
|
2021-12-01 03:55:19 +00:00
|
|
|
/// </summary>
|
2022-06-16 09:53:13 +00:00
|
|
|
public void AddFile(TModel item, Stream contents, string filename, Realm realm)
|
2021-11-29 09:07:32 +00:00
|
|
|
{
|
2022-08-10 06:48:38 +00:00
|
|
|
var existing = item.GetFile(filename);
|
2021-12-02 08:17:12 +00:00
|
|
|
|
|
|
|
if (existing != null)
|
|
|
|
{
|
2021-12-02 08:19:53 +00:00
|
|
|
ReplaceFile(existing, contents, realm);
|
2021-12-02 08:17:12 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2021-12-02 08:19:53 +00:00
|
|
|
var file = realmFileStore.Add(contents, realm);
|
2021-11-29 09:07:32 +00:00
|
|
|
var namedUsage = new RealmNamedFileUsage(file, filename);
|
|
|
|
|
|
|
|
item.Files.Add(namedUsage);
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Delete multiple items.
|
|
|
|
/// This will post notifications tracking progress.
|
|
|
|
/// </summary>
|
|
|
|
public void Delete(List<TModel> items, bool silent = false)
|
|
|
|
{
|
|
|
|
if (items.Count == 0) return;
|
|
|
|
|
|
|
|
var notification = new ProgressNotification
|
|
|
|
{
|
|
|
|
Progress = 0,
|
|
|
|
Text = $"Preparing to delete all {HumanisedModelName}s...",
|
|
|
|
CompletionText = $"Deleted all {HumanisedModelName}s!",
|
|
|
|
State = ProgressNotificationState.Active,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!silent)
|
|
|
|
PostNotification?.Invoke(notification);
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
foreach (var b in items)
|
|
|
|
{
|
|
|
|
if (notification.State == ProgressNotificationState.Cancelled)
|
|
|
|
// user requested abort
|
|
|
|
return;
|
|
|
|
|
|
|
|
notification.Text = $"Deleting {HumanisedModelName}s ({++i} of {items.Count})";
|
|
|
|
|
|
|
|
Delete(b);
|
|
|
|
|
|
|
|
notification.Progress = (float)i / items.Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
notification.State = ProgressNotificationState.Completed;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Restore multiple items that were previously deleted.
|
|
|
|
/// This will post notifications tracking progress.
|
|
|
|
/// </summary>
|
|
|
|
public void Undelete(List<TModel> items, bool silent = false)
|
|
|
|
{
|
|
|
|
if (!items.Any()) return;
|
|
|
|
|
|
|
|
var notification = new ProgressNotification
|
|
|
|
{
|
|
|
|
CompletionText = "Restored all deleted items!",
|
|
|
|
Progress = 0,
|
|
|
|
State = ProgressNotificationState.Active,
|
|
|
|
};
|
|
|
|
|
|
|
|
if (!silent)
|
|
|
|
PostNotification?.Invoke(notification);
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
|
|
foreach (var item in items)
|
|
|
|
{
|
|
|
|
if (notification.State == ProgressNotificationState.Cancelled)
|
|
|
|
// user requested abort
|
|
|
|
return;
|
|
|
|
|
|
|
|
notification.Text = $"Restoring ({++i} of {items.Count})";
|
|
|
|
|
|
|
|
Undelete(item);
|
|
|
|
|
|
|
|
notification.Progress = (float)i / items.Count;
|
|
|
|
}
|
|
|
|
|
|
|
|
notification.State = ProgressNotificationState.Completed;
|
|
|
|
}
|
|
|
|
|
|
|
|
public bool Delete(TModel item)
|
|
|
|
{
|
2022-05-10 11:47:26 +00:00
|
|
|
// Importantly, begin the realm write *before* re-fetching, else the update realm may not be in a consistent state
|
|
|
|
// (ie. if an async import finished very recently).
|
|
|
|
return Realm.Write(realm =>
|
2022-01-08 05:03:04 +00:00
|
|
|
{
|
|
|
|
if (!item.IsManaged)
|
|
|
|
item = realm.Find<TModel>(item.ID);
|
|
|
|
|
|
|
|
if (item?.DeletePending != false)
|
|
|
|
return false;
|
2021-11-29 09:07:32 +00:00
|
|
|
|
2022-05-10 11:47:26 +00:00
|
|
|
item.DeletePending = true;
|
2022-01-08 05:03:04 +00:00
|
|
|
return true;
|
2022-01-20 16:34:20 +00:00
|
|
|
});
|
2021-11-29 09:07:32 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public void Undelete(TModel item)
|
|
|
|
{
|
2022-05-10 11:47:26 +00:00
|
|
|
// Importantly, begin the realm write *before* re-fetching, else the update realm may not be in a consistent state
|
|
|
|
// (ie. if an async import finished very recently).
|
|
|
|
Realm.Write(realm =>
|
2022-01-08 05:03:04 +00:00
|
|
|
{
|
|
|
|
if (!item.IsManaged)
|
|
|
|
item = realm.Find<TModel>(item.ID);
|
|
|
|
|
|
|
|
if (item?.DeletePending != true)
|
|
|
|
return;
|
2021-11-29 09:07:32 +00:00
|
|
|
|
2022-05-10 11:47:26 +00:00
|
|
|
item.DeletePending = false;
|
2022-01-20 16:34:20 +00:00
|
|
|
});
|
2021-11-29 09:07:32 +00:00
|
|
|
}
|
|
|
|
|
2022-06-16 09:53:13 +00:00
|
|
|
public virtual bool IsAvailableLocally(TModel model) => true;
|
2022-06-16 10:05:25 +00:00
|
|
|
|
2022-06-16 09:53:13 +00:00
|
|
|
public Action<Notification>? PostNotification { get; set; }
|
2022-06-16 10:05:25 +00:00
|
|
|
|
2022-06-20 12:39:47 +00:00
|
|
|
public virtual string HumanisedModelName => $"{typeof(TModel).Name.Replace(@"Info", "").ToLowerInvariant()}";
|
2021-11-29 09:07:32 +00:00
|
|
|
}
|
|
|
|
}
|