mirror of
https://github.com/osukey/osukey.git
synced 2025-08-05 07:33:55 +09:00
Make deletion and purging logic even more global
This commit is contained in:
@ -62,6 +62,8 @@ namespace osu.Game.Database
|
||||
|
||||
if (importHost != null)
|
||||
ipc = new ArchiveImportIPCChannel(importHost, this);
|
||||
|
||||
ModelStore.PurgeDeletable();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
@ -154,6 +156,13 @@ namespace osu.Game.Database
|
||||
/// <param name="item">The model to be imported.</param>
|
||||
public void Import(TModel item) => ModelStore.Add(item);
|
||||
|
||||
/// <summary>
|
||||
/// Perform an update of the specified item.
|
||||
/// TODO: Support file changes.
|
||||
/// </summary>
|
||||
/// <param name="item">The item to update.</param>
|
||||
public void Update(TModel item) => ModelStore.Update(item);
|
||||
|
||||
/// <summary>
|
||||
/// Delete an item from the manager.
|
||||
/// Is a no-op for already deleted items.
|
||||
@ -180,14 +189,48 @@ namespace osu.Game.Database
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Restore all items that were previously deleted.
|
||||
/// Delete multiple items.
|
||||
/// This will post notifications tracking progress.
|
||||
/// </summary>
|
||||
public void UndeleteAll()
|
||||
public void Delete(List<TModel> items)
|
||||
{
|
||||
var deletedItems = queryModel().Where(m => m.DeletePending).ToList();
|
||||
if (items.Count == 0) return;
|
||||
|
||||
if (!deletedItems.Any()) return;
|
||||
var notification = new ProgressNotification
|
||||
{
|
||||
Progress = 0,
|
||||
CompletionText = "Deleted all beatmaps!",
|
||||
State = ProgressNotificationState.Active,
|
||||
};
|
||||
|
||||
PostNotification?.Invoke(notification);
|
||||
|
||||
int i = 0;
|
||||
|
||||
using (ContextFactory.GetForWrite())
|
||||
{
|
||||
foreach (var b in items)
|
||||
{
|
||||
if (notification.State == ProgressNotificationState.Cancelled)
|
||||
// user requested abort
|
||||
return;
|
||||
|
||||
notification.Text = $"Deleting ({i} of {items.Count})";
|
||||
notification.Progress = (float)++i / items.Count;
|
||||
Delete(b);
|
||||
}
|
||||
}
|
||||
|
||||
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)
|
||||
{
|
||||
if (!items.Any()) return;
|
||||
|
||||
var notification = new ProgressNotification
|
||||
{
|
||||
@ -200,15 +243,18 @@ namespace osu.Game.Database
|
||||
|
||||
int i = 0;
|
||||
|
||||
foreach (var item in deletedItems)
|
||||
using (ContextFactory.GetForWrite())
|
||||
{
|
||||
if (notification.State == ProgressNotificationState.Cancelled)
|
||||
// user requested abort
|
||||
return;
|
||||
foreach (var item in items)
|
||||
{
|
||||
if (notification.State == ProgressNotificationState.Cancelled)
|
||||
// user requested abort
|
||||
return;
|
||||
|
||||
notification.Text = $"Restoring ({i} of {deletedItems.Count})";
|
||||
notification.Progress = (float)++i / deletedItems.Count;
|
||||
Undelete(item);
|
||||
notification.Text = $"Restoring ({i} of {items.Count})";
|
||||
notification.Progress = (float)++i / items.Count;
|
||||
Undelete(item);
|
||||
}
|
||||
}
|
||||
|
||||
notification.State = ProgressNotificationState.Completed;
|
||||
|
@ -49,7 +49,7 @@ namespace osu.Game.Database
|
||||
/// <summary>
|
||||
/// Perform any common clean-up tasks. Should be run when idle, or whenever necessary.
|
||||
/// </summary>
|
||||
public virtual void Cleanup()
|
||||
public virtual void PurgeDeletable()
|
||||
{
|
||||
}
|
||||
}
|
||||
|
@ -1,4 +1,7 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Linq.Expressions;
|
||||
using osu.Framework.Platform;
|
||||
|
||||
namespace osu.Game.Database
|
||||
@ -72,5 +75,37 @@ namespace osu.Game.Database
|
||||
ItemAdded?.Invoke(item);
|
||||
return true;
|
||||
}
|
||||
|
||||
protected virtual IQueryable<T> AddIncludesForDeletion(IQueryable<T> query) => query;
|
||||
|
||||
protected virtual void Purge(List<T> items, OsuDbContext context)
|
||||
{
|
||||
// cascades down to beatmaps.
|
||||
context.RemoveRange(items);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Purge items in a pending delete state.
|
||||
/// </summary>
|
||||
/// <param name="query">An optional query limiting the scope of the purge.</param>
|
||||
public void PurgeDeletable(Expression<Func<T, bool>> query = null)
|
||||
{
|
||||
using (var usage = ContextFactory.GetForWrite())
|
||||
{
|
||||
var context = usage.Context;
|
||||
|
||||
var lookup = context.Set<T>().Where(s => s.DeletePending);
|
||||
|
||||
if (query != null) lookup = lookup.Where(query);
|
||||
|
||||
AddIncludesForDeletion(lookup);
|
||||
|
||||
var purgeable = lookup.ToList();
|
||||
|
||||
if (!purgeable.Any()) return;
|
||||
|
||||
Purge(purgeable, context);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user