mirror of
https://github.com/ppy/osu.git
synced 2025-01-14 19:22:56 +08:00
Simplify and document DatabaseStore API
This commit is contained in:
parent
a90eff69db
commit
96b08b8777
@ -115,9 +115,11 @@ namespace osu.Game.Tests.Beatmaps.IO
|
|||||||
{
|
{
|
||||||
IEnumerable<BeatmapSetInfo> resultSets = null;
|
IEnumerable<BeatmapSetInfo> resultSets = null;
|
||||||
|
|
||||||
|
var store = osu.Dependencies.Get<BeatmapStore>();
|
||||||
|
|
||||||
Action waitAction = () =>
|
Action waitAction = () =>
|
||||||
{
|
{
|
||||||
while (!(resultSets = osu.Dependencies.Get<BeatmapStore>().Database.
|
while (!(resultSets = store.Database.
|
||||||
Query<BeatmapSetInfo>().Where(s => s.OnlineBeatmapSetID == 241526)).Any())
|
Query<BeatmapSetInfo>().Where(s => s.OnlineBeatmapSetID == 241526)).Any())
|
||||||
Thread.Sleep(50);
|
Thread.Sleep(50);
|
||||||
};
|
};
|
||||||
@ -134,15 +136,16 @@ namespace osu.Game.Tests.Beatmaps.IO
|
|||||||
//if we don't re-check here, the set will be inserted but the beatmaps won't be present yet.
|
//if we don't re-check here, the set will be inserted but the beatmaps won't be present yet.
|
||||||
waitAction = () =>
|
waitAction = () =>
|
||||||
{
|
{
|
||||||
while ((resultBeatmaps = osu.Dependencies.Get<BeatmapStore>().Database.
|
while ((resultBeatmaps = store.Database.
|
||||||
GetAllWithChildren<BeatmapInfo>(s => s.OnlineBeatmapSetID == 241526 && s.BaseDifficultyID > 0)).Count() != 12)
|
QueryAndPopulate<BeatmapInfo>(s => s.OnlineBeatmapSetID == 241526 && s.BaseDifficultyID > 0)).Count() != 12)
|
||||||
Thread.Sleep(50);
|
Thread.Sleep(50);
|
||||||
};
|
};
|
||||||
|
|
||||||
Assert.IsTrue(waitAction.BeginInvoke(null, null).AsyncWaitHandle.WaitOne(timeout),
|
Assert.IsTrue(waitAction.BeginInvoke(null, null).AsyncWaitHandle.WaitOne(timeout),
|
||||||
@"Beatmaps did not import to the database in allocated time");
|
@"Beatmaps did not import to the database in allocated time");
|
||||||
|
|
||||||
var set = osu.Dependencies.Get<BeatmapStore>().Database.GetChildren(resultSets.First());
|
var set = resultSets.First();
|
||||||
|
store.Database.Populate(set);
|
||||||
|
|
||||||
Assert.IsTrue(set.Beatmaps.Count == resultBeatmaps.Count(),
|
Assert.IsTrue(set.Beatmaps.Count == resultBeatmaps.Count(),
|
||||||
$@"Incorrect database beatmap count post-import ({resultBeatmaps.Count()} but should be {set.Beatmaps.Count}).");
|
$@"Incorrect database beatmap count post-import ({resultBeatmaps.Count()} but should be {set.Beatmaps.Count}).");
|
||||||
@ -152,16 +155,16 @@ namespace osu.Game.Tests.Beatmaps.IO
|
|||||||
|
|
||||||
Assert.IsTrue(set.Beatmaps.Count > 0);
|
Assert.IsTrue(set.Beatmaps.Count > 0);
|
||||||
|
|
||||||
var beatmap = osu.Dependencies.Get<BeatmapStore>().GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 0))?.Beatmap;
|
var beatmap = store.GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 0))?.Beatmap;
|
||||||
Assert.IsTrue(beatmap?.HitObjects.Count > 0);
|
Assert.IsTrue(beatmap?.HitObjects.Count > 0);
|
||||||
|
|
||||||
beatmap = osu.Dependencies.Get<BeatmapStore>().GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 1))?.Beatmap;
|
beatmap = store.GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 1))?.Beatmap;
|
||||||
Assert.IsTrue(beatmap?.HitObjects.Count > 0);
|
Assert.IsTrue(beatmap?.HitObjects.Count > 0);
|
||||||
|
|
||||||
beatmap = osu.Dependencies.Get<BeatmapStore>().GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 2))?.Beatmap;
|
beatmap = store.GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 2))?.Beatmap;
|
||||||
Assert.IsTrue(beatmap?.HitObjects.Count > 0);
|
Assert.IsTrue(beatmap?.HitObjects.Count > 0);
|
||||||
|
|
||||||
beatmap = osu.Dependencies.Get<BeatmapStore>().GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 3))?.Beatmap;
|
beatmap = store.GetWorkingBeatmap(set.Beatmaps.First(b => b.RulesetID == 3))?.Beatmap;
|
||||||
Assert.IsTrue(beatmap?.HitObjects.Count > 0);
|
Assert.IsTrue(beatmap?.HitObjects.Count > 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -94,7 +94,7 @@ namespace osu.Game.Beatmaps
|
|||||||
|
|
||||||
private void cleanupPendingDeletions()
|
private void cleanupPendingDeletions()
|
||||||
{
|
{
|
||||||
foreach (var b in GetAllWithChildren<BeatmapSetInfo>(b => b.DeletePending && !b.Protected))
|
foreach (var b in QueryAndPopulate<BeatmapSetInfo>(b => b.DeletePending && !b.Protected))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -150,9 +150,7 @@ namespace osu.Game.Beatmaps
|
|||||||
if (beatmapInfo == null || beatmapInfo == DefaultBeatmap?.BeatmapInfo)
|
if (beatmapInfo == null || beatmapInfo == DefaultBeatmap?.BeatmapInfo)
|
||||||
return DefaultBeatmap;
|
return DefaultBeatmap;
|
||||||
|
|
||||||
beatmapInfo = Database.GetChildren(beatmapInfo, true);
|
Database.Populate(beatmapInfo);
|
||||||
|
|
||||||
Database.GetChildren(beatmapInfo.BeatmapSet, true);
|
|
||||||
|
|
||||||
if (beatmapInfo.BeatmapSet == null)
|
if (beatmapInfo.BeatmapSet == null)
|
||||||
throw new InvalidOperationException($@"Beatmap set {beatmapInfo.BeatmapSetInfoID} is not in the local database.");
|
throw new InvalidOperationException($@"Beatmap set {beatmapInfo.BeatmapSetInfoID} is not in the local database.");
|
||||||
@ -185,7 +183,7 @@ namespace osu.Game.Beatmaps
|
|||||||
BeatmapSetInfo set = Database.Query<BeatmapSetInfo>().FirstOrDefault(query);
|
BeatmapSetInfo set = Database.Query<BeatmapSetInfo>().FirstOrDefault(query);
|
||||||
|
|
||||||
if (set != null)
|
if (set != null)
|
||||||
Database.GetChildren(set, true);
|
Database.Populate(set);
|
||||||
|
|
||||||
return set;
|
return set;
|
||||||
}
|
}
|
||||||
@ -235,7 +233,7 @@ namespace osu.Game.Beatmaps
|
|||||||
|
|
||||||
if (existing != null)
|
if (existing != null)
|
||||||
{
|
{
|
||||||
Database.GetChildren(existing);
|
Database.Populate(existing);
|
||||||
Undelete(existing);
|
Undelete(existing);
|
||||||
return existing;
|
return existing;
|
||||||
}
|
}
|
||||||
|
@ -3,6 +3,7 @@
|
|||||||
|
|
||||||
using System;
|
using System;
|
||||||
using System.Collections.Generic;
|
using System.Collections.Generic;
|
||||||
|
using System.Linq;
|
||||||
using System.Linq.Expressions;
|
using System.Linq.Expressions;
|
||||||
using osu.Framework.Logging;
|
using osu.Framework.Logging;
|
||||||
using osu.Framework.Platform;
|
using osu.Framework.Platform;
|
||||||
@ -42,28 +43,50 @@ namespace osu.Game.Database
|
|||||||
/// </summary>
|
/// </summary>
|
||||||
public void Reset() => Prepare(true);
|
public void Reset() => Prepare(true);
|
||||||
|
|
||||||
public TableQuery<T> Query<T>() where T : class
|
|
||||||
|
public TableQuery<T> Query<T>(Expression<Func<T, bool>> filter = null) where T : class
|
||||||
{
|
{
|
||||||
return Connection.Table<T>();
|
checkType(typeof(T));
|
||||||
|
|
||||||
|
var query = Connection.Table<T>();
|
||||||
|
|
||||||
|
if (filter != null)
|
||||||
|
query = query.Where(filter);
|
||||||
|
|
||||||
|
return query;
|
||||||
}
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// This is expensive. Use with caution.
|
/// Query and populate results.
|
||||||
/// </summary>
|
/// </summary>
|
||||||
public List<T> GetAllWithChildren<T>(Expression<Func<T, bool>> filter = null, bool recursive = true)
|
/// <param name="filter">An optional filter to refine results.</param>
|
||||||
|
/// <returns></returns>
|
||||||
|
public List<T> QueryAndPopulate<T>(Expression<Func<T, bool>> filter = null)
|
||||||
where T : class
|
where T : class
|
||||||
{
|
{
|
||||||
return Connection.GetAllWithChildren(filter, recursive);
|
checkType(typeof(T));
|
||||||
|
|
||||||
|
return Connection.GetAllWithChildren(filter, true);
|
||||||
}
|
}
|
||||||
|
|
||||||
public T GetChildren<T>(T item, bool recursive = false)
|
/// <summary>
|
||||||
|
/// Populate a database-backed item.
|
||||||
|
/// </summary>
|
||||||
|
/// <param name="item"></param>
|
||||||
|
/// <param name="recursive">Whether population should recurse beyond a single level.</param>
|
||||||
|
public void Populate<T>(T item, bool recursive = true)
|
||||||
{
|
{
|
||||||
if (item == null) return default(T);
|
checkType(item.GetType());
|
||||||
|
|
||||||
Connection.GetChildren(item, recursive);
|
Connection.GetChildren(item, recursive);
|
||||||
return item;
|
}
|
||||||
|
|
||||||
|
private void checkType(Type type)
|
||||||
|
{
|
||||||
|
if (!ValidTypes.Contains(type))
|
||||||
|
throw new InvalidOperationException($"The requested operation specified a type of {type}, which is invalid for this {nameof(DatabaseStore)}.");
|
||||||
}
|
}
|
||||||
|
|
||||||
protected abstract Type[] ValidTypes { get; }
|
protected abstract Type[] ValidTypes { get; }
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -97,7 +97,7 @@ namespace osu.Game.IO
|
|||||||
|
|
||||||
private void deletePending()
|
private void deletePending()
|
||||||
{
|
{
|
||||||
foreach (var f in GetAllWithChildren<FileInfo>(f => f.ReferenceCount < 1))
|
foreach (var f in QueryAndPopulate<FileInfo>(f => f.ReferenceCount < 1))
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
|
@ -77,7 +77,7 @@ namespace osu.Game.Overlays.Music
|
|||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
list.BeatmapSets = BeatmapSets = beatmaps.Database.GetAllWithChildren<BeatmapSetInfo>(b => !b.DeletePending).ToList();
|
list.BeatmapSets = BeatmapSets = beatmaps.Database.QueryAndPopulate<BeatmapSetInfo>(b => !b.DeletePending).ToList();
|
||||||
|
|
||||||
beatmaps.BeatmapSetAdded += s => list.AddBeatmapSet(s);
|
beatmaps.BeatmapSetAdded += s => list.AddBeatmapSet(s);
|
||||||
beatmaps.BeatmapSetRemoved += s => list.RemoveBeatmapSet(s);
|
beatmaps.BeatmapSetRemoved += s => list.RemoveBeatmapSet(s);
|
||||||
|
@ -76,10 +76,13 @@ namespace osu.Game.Screens.Menu
|
|||||||
|
|
||||||
if (!menuMusic)
|
if (!menuMusic)
|
||||||
{
|
{
|
||||||
var query = beatmaps.Database.Query<BeatmapSetInfo>().Where(b => !b.DeletePending);
|
var query = beatmaps.Database.Query<BeatmapSetInfo>(b => !b.DeletePending);
|
||||||
int count = query.Count();
|
int count = query.Count();
|
||||||
if (count > 0)
|
if (count > 0)
|
||||||
|
{
|
||||||
setInfo = query.ElementAt(RNG.Next(0, count - 1));
|
setInfo = query.ElementAt(RNG.Next(0, count - 1));
|
||||||
|
beatmaps.Database.Populate(setInfo);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if (setInfo == null)
|
if (setInfo == null)
|
||||||
|
@ -180,7 +180,7 @@ namespace osu.Game.Screens.Select
|
|||||||
|
|
||||||
initialAddSetsTask = new CancellationTokenSource();
|
initialAddSetsTask = new CancellationTokenSource();
|
||||||
|
|
||||||
carousel.Beatmaps = store.Database.GetAllWithChildren<BeatmapSetInfo>(b => !b.DeletePending);
|
carousel.Beatmaps = store.Database.QueryAndPopulate<BeatmapSetInfo>(b => !b.DeletePending);
|
||||||
|
|
||||||
Beatmap.ValueChanged += beatmap_ValueChanged;
|
Beatmap.ValueChanged += beatmap_ValueChanged;
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user