1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-19 07:42:58 +08:00

Merge branch 'master' into realm-context-use-update-when-feasible

This commit is contained in:
Dean Herbert 2022-01-21 18:03:09 +09:00
commit 8f1dfa33a2
6 changed files with 137 additions and 73 deletions

View File

@ -26,8 +26,6 @@ namespace osu.Game.Database
private readonly OsuConfigManager config;
private readonly Storage storage;
private bool hasTakenBackup;
public EFToRealmMigrator(DatabaseContextFactory efContextFactory, RealmContextFactory realmContextFactory, OsuConfigManager config, Storage storage)
{
this.efContextFactory = efContextFactory;
@ -38,6 +36,8 @@ namespace osu.Game.Database
public void Run()
{
createBackup();
using (var ef = efContextFactory.Get())
{
migrateSettings(ef);
@ -77,8 +77,6 @@ namespace osu.Game.Database
{
Logger.Log($"Found {count} beatmaps in EF", LoggingTarget.Database);
ensureBackup();
// only migrate data if the realm database is empty.
// note that this cannot be written as: `realm.All<BeatmapSetInfo>().All(s => s.Protected)`, because realm does not support `.All()`.
if (realm.All<BeatmapSetInfo>().Any(s => !s.Protected))
@ -210,8 +208,6 @@ namespace osu.Game.Database
{
Logger.Log($"Found {count} scores in EF", LoggingTarget.Database);
ensureBackup();
// only migrate data if the realm database is empty.
if (realm.All<ScoreInfo>().Any())
{
@ -291,8 +287,6 @@ namespace osu.Game.Database
if (!existingSkins.Any())
return;
ensureBackup();
var userSkinChoice = config.GetBindable<string>(OsuSetting.Skin);
int.TryParse(userSkinChoice.Value, out int userSkinInt);
@ -365,7 +359,6 @@ namespace osu.Game.Database
return;
Logger.Log("Beginning settings migration to realm", LoggingTarget.Database);
ensureBackup();
realmContextFactory.Run(realm =>
{
@ -404,21 +397,16 @@ namespace osu.Game.Database
private string? getRulesetShortNameFromLegacyID(long rulesetId) =>
efContextFactory.Get().RulesetInfo.FirstOrDefault(r => r.ID == rulesetId)?.ShortName;
private void ensureBackup()
private void createBackup()
{
if (!hasTakenBackup)
{
string migration = $"before_final_migration_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}";
string migration = $"before_final_migration_{DateTimeOffset.UtcNow.ToUnixTimeSeconds()}";
efContextFactory.CreateBackup($"client.{migration}.db");
realmContextFactory.CreateBackup($"client.{migration}.realm");
efContextFactory.CreateBackup($"client.{migration}.db");
realmContextFactory.CreateBackup($"client.{migration}.realm");
using (var source = storage.GetStream("collection.db"))
using (var destination = storage.GetStream($"collection.{migration}.db", FileAccess.Write, FileMode.CreateNew))
source.CopyTo(destination);
hasTakenBackup = true;
}
using (var source = storage.GetStream("collection.db"))
using (var destination = storage.GetStream($"collection.{migration}.db", FileAccess.Write, FileMode.CreateNew))
source.CopyTo(destination);
}
}
}

View File

@ -420,9 +420,24 @@ namespace osu.Game.Database
using (BlockAllOperations())
{
Logger.Log($"Creating full realm database backup at {backupFilename}", LoggingTarget.Database);
using (var source = storage.GetStream(Filename))
using (var destination = storage.GetStream(backupFilename, FileAccess.Write, FileMode.CreateNew))
source.CopyTo(destination);
int attempts = 10;
while (attempts-- > 0)
{
try
{
using (var source = storage.GetStream(Filename))
using (var destination = storage.GetStream(backupFilename, FileAccess.Write, FileMode.CreateNew))
source.CopyTo(destination);
return;
}
catch (IOException)
{
// file may be locked during use.
Thread.Sleep(500);
}
}
}
}

View File

@ -114,9 +114,10 @@ namespace osu.Game.Screens.Select
{
CarouselRoot newRoot = new CarouselRoot(this);
newRoot.AddChildren(beatmapSets.Select(createCarouselSet).Where(g => g != null));
newRoot.AddChildren(beatmapSets.Select(s => createCarouselSet(s.Detach())).Where(g => g != null));
root = newRoot;
if (selectedBeatmapSet != null && !beatmapSets.Contains(selectedBeatmapSet.BeatmapSet))
selectedBeatmapSet = null;
@ -208,7 +209,7 @@ namespace osu.Game.Screens.Select
return;
foreach (int i in changes.InsertedIndices)
RemoveBeatmapSet(sender[i]);
removeBeatmapSet(sender[i].ID);
}
private void beatmapSetsChanged(IRealmCollection<BeatmapSetInfo> sender, ChangeSet changes, Exception error)
@ -222,24 +223,21 @@ namespace osu.Game.Screens.Select
// During initial population, we must manually account for the fact that our original query was done on an async thread.
// Since then, there may have been imports or deletions.
// Here we manually catch up on any changes.
var populatedSets = new HashSet<Guid>();
foreach (var s in beatmapSets)
populatedSets.Add(s.BeatmapSet.ID);
var realmSets = new HashSet<Guid>();
foreach (var s in sender)
realmSets.Add(s.ID);
foreach (var s in realmSets)
for (int i = 0; i < sender.Count; i++)
realmSets.Add(sender[i].ID);
foreach (var id in realmSets)
{
if (!populatedSets.Contains(s))
UpdateBeatmapSet(realmFactory.Context.Find<BeatmapSetInfo>(s));
if (!root.BeatmapSetsByID.ContainsKey(id))
UpdateBeatmapSet(realmFactory.Context.Find<BeatmapSetInfo>(id).Detach());
}
foreach (var s in populatedSets)
foreach (var id in root.BeatmapSetsByID.Keys)
{
if (!realmSets.Contains(s))
RemoveBeatmapSet(realmFactory.Context.Find<BeatmapSetInfo>(s));
if (!realmSets.Contains(id))
removeBeatmapSet(id);
}
signalBeatmapsLoaded();
@ -247,10 +245,10 @@ namespace osu.Game.Screens.Select
}
foreach (int i in changes.NewModifiedIndices)
UpdateBeatmapSet(sender[i]);
UpdateBeatmapSet(sender[i].Detach());
foreach (int i in changes.InsertedIndices)
UpdateBeatmapSet(sender[i]);
UpdateBeatmapSet(sender[i].Detach());
}
private void beatmapsChanged(IRealmCollection<BeatmapInfo> sender, ChangeSet changes, Exception error)
@ -260,16 +258,30 @@ namespace osu.Game.Screens.Select
return;
foreach (int i in changes.InsertedIndices)
UpdateBeatmapSet(sender[i].BeatmapSet);
{
var beatmapInfo = sender[i];
var beatmapSet = beatmapInfo.BeatmapSet;
Debug.Assert(beatmapSet != null);
// Only require to action here if the beatmap is missing.
// This avoids processing these events unnecessarily when new beatmaps are imported, for example.
if (root.BeatmapSetsByID.TryGetValue(beatmapSet.ID, out var existingSet)
&& existingSet.BeatmapSet.Beatmaps.All(b => b.ID != beatmapInfo.ID))
{
UpdateBeatmapSet(beatmapSet.Detach());
}
}
}
private IRealmCollection<BeatmapSetInfo> getBeatmapSets(Realm realm) => realm.All<BeatmapSetInfo>().Where(s => !s.DeletePending && !s.Protected).AsRealmCollection();
public void RemoveBeatmapSet(BeatmapSetInfo beatmapSet) => Schedule(() =>
{
var existingSet = beatmapSets.FirstOrDefault(b => b.BeatmapSet.Equals(beatmapSet));
public void RemoveBeatmapSet(BeatmapSetInfo beatmapSet) =>
removeBeatmapSet(beatmapSet.ID);
if (existingSet == null)
private void removeBeatmapSet(Guid beatmapSetID) => Schedule(() =>
{
if (!root.BeatmapSetsByID.TryGetValue(beatmapSetID, out var existingSet))
return;
root.RemoveChild(existingSet);
@ -280,35 +292,32 @@ namespace osu.Game.Screens.Select
{
Guid? previouslySelectedID = null;
CarouselBeatmapSet existingSet = beatmapSets.FirstOrDefault(b => b.BeatmapSet.Equals(beatmapSet));
// If the selected beatmap is about to be removed, store its ID so it can be re-selected if required
if (existingSet?.State?.Value == CarouselItemState.Selected)
if (selectedBeatmapSet?.BeatmapSet.ID == beatmapSet.ID)
previouslySelectedID = selectedBeatmap?.BeatmapInfo.ID;
var newSet = createCarouselSet(beatmapSet);
if (existingSet != null)
root.RemoveChild(existingSet);
root.RemoveChild(beatmapSet.ID);
if (newSet == null)
if (newSet != null)
{
itemsCache.Invalidate();
return;
root.AddChild(newSet);
// check if we can/need to maintain our current selection.
if (previouslySelectedID != null)
select((CarouselItem)newSet.Beatmaps.FirstOrDefault(b => b.BeatmapInfo.ID == previouslySelectedID) ?? newSet);
}
root.AddChild(newSet);
// only reset scroll position if already near the scroll target.
// without this, during a large beatmap import it is impossible to navigate the carousel.
applyActiveCriteria(false, alwaysResetScrollPosition: false);
// check if we can/need to maintain our current selection.
if (previouslySelectedID != null)
select((CarouselItem)newSet.Beatmaps.FirstOrDefault(b => b.BeatmapInfo.ID == previouslySelectedID) ?? newSet);
itemsCache.Invalidate();
Schedule(() => BeatmapSetsChanged?.Invoke());
Schedule(() =>
{
if (!Scroll.UserScrolling)
ScrollToSelected(true);
BeatmapSetsChanged?.Invoke();
});
});
/// <summary>
@ -710,8 +719,6 @@ namespace osu.Game.Screens.Select
private CarouselBeatmapSet createCarouselSet(BeatmapSetInfo beatmapSet)
{
beatmapSet = beatmapSet.Detach();
// This can be moved to the realm query if required using:
// .Filter("DeletePending == false && Protected == false && ANY Beatmaps.Hidden == false")
//
@ -912,6 +919,8 @@ namespace osu.Game.Screens.Select
{
private readonly BeatmapCarousel carousel;
public readonly Dictionary<Guid, CarouselBeatmapSet> BeatmapSetsByID = new Dictionary<Guid, CarouselBeatmapSet>();
public CarouselRoot(BeatmapCarousel carousel)
{
// root should always remain selected. if not, PerformSelection will not be called.
@ -921,6 +930,28 @@ namespace osu.Game.Screens.Select
this.carousel = carousel;
}
public override void AddChild(CarouselItem i)
{
CarouselBeatmapSet set = (CarouselBeatmapSet)i;
BeatmapSetsByID.Add(set.BeatmapSet.ID, set);
base.AddChild(i);
}
public void RemoveChild(Guid beatmapSetID)
{
if (BeatmapSetsByID.TryGetValue(beatmapSetID, out var carouselBeatmapSet))
RemoveChild(carouselBeatmapSet);
}
public override void RemoveChild(CarouselItem i)
{
CarouselBeatmapSet set = (CarouselBeatmapSet)i;
BeatmapSetsByID.Remove(set.BeatmapSet.ID);
base.RemoveChild(i);
}
protected override void PerformSelection()
{
if (LastSelected == null || LastSelected.Filtered.Value)

View File

@ -55,7 +55,7 @@ namespace osu.Game.Screens.Select.Carousel
match &= !criteria.UserStarDifficulty.HasFilter || criteria.UserStarDifficulty.IsInRange(BeatmapInfo.StarRating);
if (match)
if (match && criteria.SearchTerms.Length > 0)
{
string[] terms = BeatmapInfo.GetSearchableTerms();

View File

@ -4,6 +4,8 @@
using System.Collections.Generic;
using System.Linq;
#nullable enable
namespace osu.Game.Screens.Select.Carousel
{
/// <summary>
@ -11,7 +13,7 @@ namespace osu.Game.Screens.Select.Carousel
/// </summary>
public class CarouselGroup : CarouselItem
{
public override DrawableCarouselItem CreateDrawableRepresentation() => null;
public override DrawableCarouselItem? CreateDrawableRepresentation() => null;
public IReadOnlyList<CarouselItem> Children => InternalChildren;
@ -23,6 +25,10 @@ namespace osu.Game.Screens.Select.Carousel
/// </summary>
private ulong currentChildID;
private Comparer<CarouselItem>? criteriaComparer;
private FilterCriteria? lastCriteria;
public virtual void RemoveChild(CarouselItem i)
{
InternalChildren.Remove(i);
@ -36,10 +42,24 @@ namespace osu.Game.Screens.Select.Carousel
{
i.State.ValueChanged += state => ChildItemStateChanged(i, state.NewValue);
i.ChildID = ++currentChildID;
InternalChildren.Add(i);
if (lastCriteria != null)
{
i.Filter(lastCriteria);
int index = InternalChildren.BinarySearch(i, criteriaComparer);
if (index < 0) index = ~index; // BinarySearch hacks multiple return values with 2's complement.
InternalChildren.Insert(index, i);
}
else
{
// criteria may be null for initial population. the filtering will be applied post-add.
InternalChildren.Add(i);
}
}
public CarouselGroup(List<CarouselItem> items = null)
public CarouselGroup(List<CarouselItem>? items = null)
{
if (items != null) InternalChildren = items;
@ -67,9 +87,12 @@ namespace osu.Game.Screens.Select.Carousel
base.Filter(criteria);
InternalChildren.ForEach(c => c.Filter(criteria));
// IEnumerable<T>.OrderBy() is used instead of List<T>.Sort() to ensure sorting stability
var criteriaComparer = Comparer<CarouselItem>.Create((x, y) => x.CompareTo(criteria, y));
criteriaComparer = Comparer<CarouselItem>.Create((x, y) => x.CompareTo(criteria, y));
InternalChildren = InternalChildren.OrderBy(c => c, criteriaComparer).ToList();
lastCriteria = criteria;
}
protected virtual void ChildItemStateChanged(CarouselItem item, CarouselItemState value)

View File

@ -55,10 +55,16 @@ namespace osu.Game.Screens.Select.Carousel
updateSelectedIndex();
}
private bool addingChildren;
public void AddChildren(IEnumerable<CarouselItem> items)
{
addingChildren = true;
foreach (var i in items)
base.AddChild(i);
AddChild(i);
addingChildren = false;
attemptSelection();
}
@ -66,7 +72,8 @@ namespace osu.Game.Screens.Select.Carousel
public override void AddChild(CarouselItem i)
{
base.AddChild(i);
attemptSelection();
if (!addingChildren)
attemptSelection();
}
protected override void ChildItemStateChanged(CarouselItem item, CarouselItemState value)