// Copyright (c) 2007-2017 ppy Pty Ltd . // Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE using System; using osu.Framework.Logging; using osu.Framework.Platform; namespace osu.Game.Database { public abstract class DatabaseBackedStore { protected readonly Storage Storage; /// /// Create a new instance (separate from the shared context via for performing isolated operations. /// protected readonly Func CreateContext; private readonly Lazy queryContext; /// /// Retrieve a shared context for performing lookups (or write operations on the update thread, for now). /// protected OsuDbContext GetContext() => queryContext.Value; protected DatabaseBackedStore(Func createContext, Storage storage = null) { CreateContext = createContext; queryContext = new Lazy(CreateContext); Storage = storage; try { Prepare(); } catch (Exception e) { Logger.Error(e, $@"Failed to initialise the {GetType()}! Trying again with a clean database..."); Prepare(true); } } /// /// Perform any common clean-up tasks. Should be run when idle, or whenever necessary. /// public virtual void Cleanup() { } /// /// Prepare this database for use. Tables should be created here. /// protected abstract void Prepare(bool reset = false); /// /// Reset this database to a default state. Undo all changes to database and storage backings. /// public void Reset() => Prepare(true); } }