1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-20 05:15:37 +08:00

Revert "Return back DatabaseBackedStore's query and populate functions"

This reverts commit 7cf5d63cd3.
This commit is contained in:
Dean Herbert 2017-10-16 12:56:58 +09:00
parent 04e5f764a3
commit 8a0b184dd6
10 changed files with 80 additions and 120 deletions

View File

@ -6,13 +6,12 @@ using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using Newtonsoft.Json;
using osu.Game.Database;
using osu.Game.IO.Serialization;
using osu.Game.Rulesets;
namespace osu.Game.Beatmaps
{
public class BeatmapInfo : IEquatable<BeatmapInfo>, IJsonSerializable, IPopulate
public class BeatmapInfo : IEquatable<BeatmapInfo>, IJsonSerializable
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
@ -125,11 +124,5 @@ namespace osu.Game.Beatmaps
public bool BackgroundEquals(BeatmapInfo other) => other != null && BeatmapSet != null && other.BeatmapSet != null &&
BeatmapSet.Hash == other.BeatmapSet.Hash &&
(Metadata ?? BeatmapSet.Metadata).BackgroundFile == (other.Metadata ?? other.BeatmapSet.Metadata).BackgroundFile;
public void Populate(OsuDbContext connection)
{
var entry = connection.Entry(this);
entry.Reference<BeatmapDifficulty>(nameof(Difficulty));
}
}
}

View File

@ -5,6 +5,7 @@ using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Linq.Expressions;
using System.Threading.Tasks;
using Ionic.Zip;
using osu.Framework.Audio.Track;
@ -259,13 +260,10 @@ namespace osu.Game.Beatmaps
/// <param name="beatmapSet">The beatmap set to delete.</param>
public void Delete(BeatmapSetInfo beatmapSet)
{
lock (beatmaps)
{
if (!beatmaps.Delete(beatmapSet)) return;
if (!beatmaps.Delete(beatmapSet)) return;
if (!beatmapSet.Protected)
files.Dereference(beatmapSet.Files.Select(f => f.FileInfo).ToArray());
}
if (!beatmapSet.Protected)
files.Dereference(beatmapSet.Files.Select(f => f.FileInfo).ToArray());
}
/// <summary>
@ -304,9 +302,6 @@ namespace osu.Game.Beatmaps
if (beatmapInfo == null || beatmapInfo == DefaultBeatmap?.BeatmapInfo)
return DefaultBeatmap;
lock (beatmaps)
beatmaps.Populate(beatmapInfo);
if (beatmapInfo.BeatmapSet == null)
throw new InvalidOperationException($@"Beatmap set {beatmapInfo.BeatmapSetInfoID} is not in the local database.");
@ -338,10 +333,7 @@ namespace osu.Game.Beatmaps
{
lock (beatmaps)
{
BeatmapSetInfo set = beatmaps.Query(query).FirstOrDefault();
if (set != null)
beatmaps.Populate(set);
BeatmapSetInfo set = beatmaps.QueryBeatmapSet(query);
return set;
}
@ -359,9 +351,9 @@ namespace osu.Game.Beatmaps
/// </summary>
/// <param name="query">The query.</param>
/// <returns>Results from the provided query.</returns>
public List<BeatmapSetInfo> QueryBeatmapSets(Func<BeatmapSetInfo, bool> query)
public List<BeatmapSetInfo> QueryBeatmapSets(Expression<Func<BeatmapSetInfo, bool>> query)
{
return beatmaps.QueryAndPopulate(query);
return beatmaps.QueryBeatmapSets(query);
}
/// <summary>
@ -371,15 +363,9 @@ namespace osu.Game.Beatmaps
/// <returns>The first result for the provided query, or null if no results were found.</returns>
public BeatmapInfo QueryBeatmap(Func<BeatmapInfo, bool> query)
{
lock (beatmaps)
{
BeatmapInfo set = beatmaps.Query(query).FirstOrDefault();
BeatmapInfo set = beatmaps.QueryBeatmap(query);
if (set != null)
beatmaps.Populate(set);
return set;
}
return set;
}
/// <summary>
@ -387,9 +373,9 @@ namespace osu.Game.Beatmaps
/// </summary>
/// <param name="query">The query.</param>
/// <returns>Results from the provided query.</returns>
public List<BeatmapInfo> QueryBeatmaps(Func<BeatmapInfo, bool> query)
public List<BeatmapInfo> QueryBeatmaps(Expression<Func<BeatmapInfo, bool>> query)
{
lock (beatmaps) return beatmaps.Query(query);
lock (beatmaps) return beatmaps.QueryBeatmaps(query);
}
/// <summary>
@ -428,7 +414,7 @@ namespace osu.Game.Beatmaps
// check if this beatmap has already been imported and exit early if so.
BeatmapSetInfo beatmapSet;
lock (beatmaps)
beatmapSet = beatmaps.QueryAndPopulate<BeatmapSetInfo>(b => b.Hash == hash).FirstOrDefault();
beatmapSet = beatmaps.QueryBeatmapSet(b => b.Hash == hash);
if (beatmapSet != null)
{
@ -492,8 +478,8 @@ namespace osu.Game.Beatmaps
beatmap.BeatmapInfo.Metadata = null;
// TODO: this should be done in a better place once we actually need to dynamically update it.
beatmap.BeatmapInfo.Ruleset = rulesets.Query<RulesetInfo>(r => r.ID == beatmap.BeatmapInfo.RulesetID).FirstOrDefault();
beatmap.BeatmapInfo.StarDifficulty = rulesets.Query<RulesetInfo>(r => r.ID == beatmap.BeatmapInfo.RulesetID).FirstOrDefault()?.CreateInstance()?.CreateDifficultyCalculator(beatmap)
beatmap.BeatmapInfo.Ruleset = rulesets.QueryRulesetInfo(r => r.ID == beatmap.BeatmapInfo.RulesetID);
beatmap.BeatmapInfo.StarDifficulty = rulesets.QueryRulesetInfo(r => r.ID == beatmap.BeatmapInfo.RulesetID)?.CreateInstance()?.CreateDifficultyCalculator(beatmap)
.Calculate() ?? 0;
beatmapSet.Beatmaps.Add(beatmap.BeatmapInfo);
@ -507,11 +493,11 @@ namespace osu.Game.Beatmaps
/// Returns a list of all usable <see cref="BeatmapSetInfo"/>s.
/// </summary>
/// <returns>A list of available <see cref="BeatmapSetInfo"/>.</returns>
public List<BeatmapSetInfo> GetAllUsableBeatmapSets(bool populate = true)
public List<BeatmapSetInfo> GetAllUsableBeatmapSets()
{
lock (beatmaps)
{
return populate ? beatmaps.QueryAndPopulate<BeatmapSetInfo>(b => !b.DeletePending) : beatmaps.Query<BeatmapSetInfo>(b => !b.DeletePending);
return beatmaps.QueryBeatmapSets(b => !b.DeletePending);
}
}

View File

@ -4,12 +4,10 @@
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations.Schema;
using System.Linq;
using osu.Game.Database;
using osu.Game.IO;
namespace osu.Game.Beatmaps
{
public class BeatmapSetInfo : IPopulate
public class BeatmapSetInfo
{
[DatabaseGenerated(DatabaseGeneratedOption.Identity)]
public int ID { get; set; }
@ -36,25 +34,5 @@ namespace osu.Game.Beatmaps
public List<BeatmapSetFileInfo> Files { get; set; }
public bool Protected { get; set; }
public void Populate(OsuDbContext connection)
{
var entry = connection.Entry(this);
entry.Collection<BeatmapInfo>(nameof(Beatmaps)).Load();
entry.Reference<BeatmapMetadata>(nameof(Metadata)).Load();
entry.Collection<BeatmapSetFileInfo>(nameof(Files)).Load();
foreach (var beatmap in Beatmaps)
{
var beatmapEntry = connection.Entry(beatmap);
beatmapEntry.Reference<BeatmapDifficulty>(nameof(beatmap.Difficulty)).Load();
}
foreach (var file in Files)
{
var fileEntry = connection.Entry(file);
fileEntry.Reference<FileInfo>(nameof(file.FileInfo)).Load();
}
}
}
}

View File

@ -2,7 +2,9 @@
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using System.Linq;
using System.Linq.Expressions;
using Microsoft.EntityFrameworkCore;
using osu.Game.Database;
@ -132,5 +134,45 @@ namespace osu.Game.Beatmaps
{
Connection.BeatmapSetInfo.RemoveRange(Connection.BeatmapSetInfo.Where(b => b.DeletePending && !b.Protected));
}
public BeatmapSetInfo QueryBeatmapSet(Func<BeatmapSetInfo, bool> query)
{
return Connection.BeatmapSetInfo
.Include(b => b.Metadata)
.Include(b => b.Beatmaps).ThenInclude(b => b.Ruleset)
.Include(b => b.Beatmaps).ThenInclude(b => b.Difficulty)
.Include(b => b.Files).ThenInclude(f => f.FileInfo)
.FirstOrDefault(query);
}
public List<BeatmapSetInfo> QueryBeatmapSets(Expression<Func<BeatmapSetInfo, bool>> query)
{
return Connection.BeatmapSetInfo
.Include(b => b.Metadata)
.Include(b => b.Beatmaps).ThenInclude(b => b.Ruleset)
.Include(b => b.Beatmaps).ThenInclude(b => b.Difficulty)
.Include(b => b.Files).ThenInclude(f => f.FileInfo)
.Where(query).ToList();
}
public BeatmapInfo QueryBeatmap(Func<BeatmapInfo, bool> query)
{
return Connection.BeatmapInfo
.Include(b => b.BeatmapSet)
.Include(b => b.Metadata)
.Include(b => b.Ruleset)
.Include(b => b.Difficulty)
.FirstOrDefault(query);
}
public List<BeatmapInfo> QueryBeatmaps(Expression<Func<BeatmapInfo, bool>> query)
{
return Connection.BeatmapInfo
.Include(b => b.BeatmapSet)
.Include(b => b.Metadata)
.Include(b => b.Ruleset)
.Include(b => b.Difficulty)
.Where(query).ToList();
}
}
}

View File

@ -2,7 +2,6 @@
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.EntityFrameworkCore;
using osu.Framework.Logging;
@ -50,46 +49,6 @@ namespace osu.Game.Database
/// </summary>
public void Reset() => Prepare(true);
public List<T> Query<T>(Func<T, bool> filter = null) where T : class
{
checkType(typeof(T));
var dbSet = Connection.GetType().GetProperties().Single(property => property.PropertyType == typeof(DbSet<T>)).GetValue(Connection) as DbSet<T>;
var query = dbSet.ToList();
if (filter != null)
query = query.Where(filter).ToList();
return query;
}
/// <summary>
/// Query and populate results.
/// </summary>
/// <param name="filter">An filter to refine results.</param>
/// <returns></returns>
public List<T> QueryAndPopulate<T>(Func<T, bool> filter)
where T : class, IPopulate
{
checkType(typeof(T));
var query = Query(filter);
foreach (var item in query)
Populate(item);
return query;
}
/// <summary>
/// Populate a database-backed item.
/// </summary>
/// <param name="item"></param>
public void Populate(IPopulate item)
{
checkType(item.GetType());
item.Populate(Connection);
}
private void checkType(Type type)
{
if (!ValidTypes.Contains(type))

View File

@ -1,10 +0,0 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
namespace osu.Game.Database
{
public interface IPopulate
{
void Populate(OsuDbContext connection);
}
}

View File

@ -42,14 +42,14 @@ namespace osu.Game.Rulesets
{
Connection.Database.ExecuteSqlCommand("DELETE FROM RulesetInfo");
}
var instances = loaded_assemblies.Values.Select(r => (Ruleset)Activator.CreateInstance(r, new RulesetInfo()));
//add all legacy modes in correct order
foreach (var r in instances.Where(r => r.LegacyID >= 0).OrderBy(r => r.LegacyID))
{
var rulesetInfo = createRulesetInfo(r);
if (Connection.RulesetInfo.SingleOrDefault(rsi => rsi.ID == rulesetInfo.ID) == null)
if (Connection.RulesetInfo.SingleOrDefault(rsi=>rsi.ID==rulesetInfo.ID)==null)
{
Connection.RulesetInfo.Add(rulesetInfo);
}
@ -108,6 +108,19 @@ namespace osu.Game.Rulesets
protected override Type[] ValidTypes => new[] { typeof(RulesetInfo) };
public RulesetInfo GetRuleset(int id) => Query<RulesetInfo>().First(r => r.ID == id);
public RulesetInfo GetRuleset(int id) => Connection.RulesetInfo.First(r => r.ID == id);
public RulesetInfo QueryRulesetInfo(Func<RulesetInfo, bool> query)
{
return Connection.RulesetInfo.FirstOrDefault(query);
}
public List<RulesetInfo> QueryRulesets(Func<RulesetInfo, bool> query = null)
{
var rulesets = Connection.RulesetInfo;
if (query != null)
return rulesets.Where(query).ToList();
return rulesets.ToList();
}
}
}

View File

@ -75,7 +75,7 @@ namespace osu.Game.Tests.Visual
new BeatmapInfo
{
OnlineBeatmapID = 1234 + i,
Ruleset = rulesets.Query<RulesetInfo>().First(),
Ruleset = rulesets.QueryRulesets().First(),
Path = "normal.osu",
Version = "Normal",
Difficulty = new BeatmapDifficulty
@ -86,7 +86,7 @@ namespace osu.Game.Tests.Visual
new BeatmapInfo
{
OnlineBeatmapID = 1235 + i,
Ruleset = rulesets.Query<RulesetInfo>().First(),
Ruleset = rulesets.QueryRulesets().First(),
Path = "hard.osu",
Version = "Hard",
Difficulty = new BeatmapDifficulty
@ -97,7 +97,7 @@ namespace osu.Game.Tests.Visual
new BeatmapInfo
{
OnlineBeatmapID = 1236 + i,
Ruleset = rulesets.Query<RulesetInfo>().First(),
Ruleset = rulesets.QueryRulesets().First(),
Path = "insane.osu",
Version = "Insane",
Difficulty = new BeatmapDifficulty

View File

@ -50,7 +50,7 @@ namespace osu.Game.Tests.Visual
string instantiation = ruleset?.AssemblyQualifiedName;
foreach (var r in rulesets.Query<RulesetInfo>(rs => rs.Available && (instantiation == null || rs.InstantiationInfo == instantiation)))
foreach (var r in rulesets.QueryRulesets(rs => rs.Available && (instantiation == null || rs.InstantiationInfo == instantiation)))
AddStep(r.Name, () => loadPlayerFor(r));
}

View File

@ -277,7 +277,6 @@
<Compile Include="Beatmaps\Drawables\BeatmapPanel.cs" />
<Compile Include="Beatmaps\Drawables\BeatmapSetCover.cs" />
<Compile Include="Beatmaps\Drawables\BeatmapSetHeader.cs" />
<Compile Include="Database\IPopulate.cs" />
<Compile Include="Migrations\20171014052545_Init.cs" />
<Compile Include="Migrations\20171014052545_Init.designer.cs">
<DependentUpon>20171014052545_Init.cs</DependentUpon>