1
0
mirror of https://github.com/ppy/osu.git synced 2025-01-12 22:33:05 +08:00

Share one context per file store for performance reasons

There is now a CreateContext method for retrieving a stand-alone context for threaded use.

We may want to add safety against this context being disposed (or just return a fresh one if it is).
This commit is contained in:
Dean Herbert 2017-10-23 16:35:35 +09:00
parent 4a68dd88cb
commit df20845fbb
5 changed files with 26 additions and 13 deletions

View File

@ -11,12 +11,24 @@ namespace osu.Game.Database
{
protected readonly Storage Storage;
protected readonly Func<OsuDbContext> GetContext;
/// <summary>
/// Create a new <see cref="OsuDbContext"/> instance (separate from the shared context via <see cref="GetContext"/> for performing isolated operations.
/// </summary>
protected readonly Func<OsuDbContext> CreateContext;
protected DatabaseBackedStore(Func<OsuDbContext> getContext, Storage storage = null)
private readonly Lazy<OsuDbContext> queryContext;
/// <summary>
/// Retrieve a shared context for performing lookups (or write operations on the update thread, for now).
/// </summary>
protected OsuDbContext GetContext() => queryContext.Value;
protected DatabaseBackedStore(Func<OsuDbContext> createContext, Storage storage = null)
{
CreateContext = createContext;
queryContext = new Lazy<OsuDbContext>(CreateContext);
Storage = storage;
GetContext = getContext;
try
{

View File

@ -24,6 +24,7 @@ namespace osu.Game.Database
public DbSet<DatabasedKeyBinding> DatabasedKeyBinding { get; set; }
public DbSet<FileInfo> FileInfo { get; set; }
public DbSet<RulesetInfo> RulesetInfo { get; set; }
private readonly string connectionString;
private static readonly Lazy<OsuDbLoggerFactory> logger = new Lazy<OsuDbLoggerFactory>(() => new OsuDbLoggerFactory());

View File

@ -22,7 +22,7 @@ namespace osu.Game.IO
public Storage Storage => base.Storage;
public FileStore(Func<OsuDbContext> getContext, Storage storage) : base(getContext, storage.GetStorageForDirectory(@"files"))
public FileStore(Func<OsuDbContext> createContext, Storage storage) : base(createContext, storage.GetStorageForDirectory(@"files"))
{
Store = new StorageBackedResourceStore(Storage);
}

View File

@ -17,8 +17,8 @@ namespace osu.Game.Input
{
public event Action KeyBindingChanged;
public KeyBindingStore(Func<OsuDbContext> getContext, RulesetStore rulesets, Storage storage = null)
: base(getContext, storage)
public KeyBindingStore(Func<OsuDbContext> createContext, RulesetStore rulesets, Storage storage = null)
: base(createContext, storage)
{
foreach (var info in rulesets.AvailableRulesets)
{
@ -38,13 +38,14 @@ namespace osu.Game.Input
private void insertDefaults(IEnumerable<KeyBinding> defaults, int? rulesetId = null, int? variant = null)
{
using (var context = GetContext())
var context = GetContext();
using (var transaction = context.BeginTransaction())
{
// compare counts in database vs defaults
foreach (var group in defaults.GroupBy(k => k.Action))
{
int count = query(context, rulesetId, variant).Count(k => (int)k.Action == (int)group.Key);
int count = Query(rulesetId, variant).Count(k => (int)k.Action == (int)group.Key);
int aimCount = group.Count();
if (aimCount <= count)
@ -71,10 +72,8 @@ namespace osu.Game.Input
/// <param name="rulesetId">The ruleset's internal ID.</param>
/// <param name="variant">An optional variant.</param>
/// <returns></returns>
public IEnumerable<KeyBinding> Query(int? rulesetId = null, int? variant = null) => query(GetContext(), rulesetId, variant);
private IEnumerable<KeyBinding> query(OsuDbContext context, int? rulesetId = null, int? variant = null) =>
context.DatabasedKeyBinding.Where(b => b.RulesetID == rulesetId && b.Variant == variant);
public IEnumerable<KeyBinding> Query(int? rulesetId = null, int? variant = null) =>
GetContext().DatabasedKeyBinding.Where(b => b.RulesetID == rulesetId && b.Variant == variant);
public void Update(KeyBinding keyBinding)
{

View File

@ -1,6 +1,7 @@
// Copyright (c) 2007-2017 ppy Pty Ltd <contact@ppy.sh>.
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
using System.Linq;
using System.Threading.Tasks;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
@ -51,7 +52,7 @@ namespace osu.Game.Overlays.Settings.Sections.Maintenance
restoreButton.Enabled.Value = false;
Task.Run(() =>
{
foreach (var b in beatmaps.QueryBeatmaps(b => b.Hidden))
foreach (var b in beatmaps.QueryBeatmaps(b => b.Hidden).ToList())
beatmaps.Restore(b);
}).ContinueWith(t => Schedule(() => restoreButton.Enabled.Value = true));
}