2018-01-05 11:21:19 +00:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
2017-10-17 07:02:13 +00:00
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
2017-10-17 06:00:27 +00:00
|
|
|
|
|
2018-02-12 08:55:11 +00:00
|
|
|
|
using System.Threading;
|
2017-10-17 06:00:27 +00:00
|
|
|
|
using osu.Framework.Platform;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Database
|
|
|
|
|
{
|
2018-02-12 14:10:05 +00:00
|
|
|
|
public class DatabaseContextFactory : IDatabaseContextFactory
|
2017-10-17 06:00:27 +00:00
|
|
|
|
{
|
|
|
|
|
private readonly GameHost host;
|
|
|
|
|
|
2017-10-20 15:15:02 +00:00
|
|
|
|
private const string database_name = @"client";
|
|
|
|
|
|
2018-02-12 08:55:11 +00:00
|
|
|
|
private ThreadLocal<OsuDbContext> threadContexts;
|
|
|
|
|
|
|
|
|
|
private readonly object writeLock = new object();
|
|
|
|
|
|
2018-02-12 10:57:21 +00:00
|
|
|
|
private bool currentWriteDidWrite;
|
2018-02-12 08:55:11 +00:00
|
|
|
|
private volatile int currentWriteUsages;
|
|
|
|
|
|
2017-10-17 06:00:27 +00:00
|
|
|
|
public DatabaseContextFactory(GameHost host)
|
|
|
|
|
{
|
|
|
|
|
this.host = host;
|
2018-02-12 08:55:11 +00:00
|
|
|
|
recycleThreadContexts();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Get a context for read-only usage.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public OsuDbContext Get() => threadContexts.Value;
|
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Request a context for write usage. Can be consumed in a nested fashion (and will return the same underlying context).
|
|
|
|
|
/// This method may block if a write is already active on a different thread.
|
|
|
|
|
/// </summary>
|
|
|
|
|
/// <returns>A usage containing a usable context.</returns>
|
|
|
|
|
public DatabaseWriteUsage GetForWrite()
|
|
|
|
|
{
|
2018-02-12 10:57:21 +00:00
|
|
|
|
Monitor.Enter(writeLock);
|
|
|
|
|
|
|
|
|
|
Interlocked.Increment(ref currentWriteUsages);
|
|
|
|
|
|
2018-02-13 05:58:15 +00:00
|
|
|
|
return new DatabaseWriteUsage(threadContexts.Value, usageCompleted);
|
2017-10-17 06:00:27 +00:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-12 08:55:11 +00:00
|
|
|
|
private void usageCompleted(DatabaseWriteUsage usage)
|
|
|
|
|
{
|
|
|
|
|
int usages = Interlocked.Decrement(ref currentWriteUsages);
|
2018-02-12 10:57:21 +00:00
|
|
|
|
|
|
|
|
|
try
|
2018-02-12 08:55:11 +00:00
|
|
|
|
{
|
2018-02-12 10:57:21 +00:00
|
|
|
|
currentWriteDidWrite |= usage.PerformedWrite;
|
|
|
|
|
|
|
|
|
|
if (usages > 0) return;
|
|
|
|
|
|
|
|
|
|
if (currentWriteDidWrite)
|
|
|
|
|
{
|
2018-02-13 06:08:45 +00:00
|
|
|
|
// explicitly dispose to ensure any outstanding flushes happen as soon as possible (and underlying resources are purged).
|
|
|
|
|
usage.Context.Dispose();
|
|
|
|
|
|
2018-02-12 10:57:21 +00:00
|
|
|
|
currentWriteDidWrite = false;
|
2018-02-13 06:08:45 +00:00
|
|
|
|
|
2018-02-12 10:57:21 +00:00
|
|
|
|
// once all writes are complete, we want to refresh thread-specific contexts to make sure they don't have stale local caches.
|
|
|
|
|
recycleThreadContexts();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
finally
|
|
|
|
|
{
|
|
|
|
|
Monitor.Exit(writeLock);
|
2018-02-12 08:55:11 +00:00
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-02-13 06:08:45 +00:00
|
|
|
|
private void recycleThreadContexts() => threadContexts = new ThreadLocal<OsuDbContext>(CreateContext);
|
2018-02-12 08:55:11 +00:00
|
|
|
|
|
|
|
|
|
protected virtual OsuDbContext CreateContext()
|
|
|
|
|
{
|
|
|
|
|
var ctx = new OsuDbContext(host.Storage.GetDatabaseConnectionString(database_name));
|
|
|
|
|
ctx.Database.AutoTransactionsEnabled = false;
|
|
|
|
|
|
|
|
|
|
return ctx;
|
|
|
|
|
}
|
2017-10-20 15:15:02 +00:00
|
|
|
|
|
|
|
|
|
public void ResetDatabase()
|
|
|
|
|
{
|
2018-02-12 08:55:11 +00:00
|
|
|
|
lock (writeLock)
|
|
|
|
|
{
|
|
|
|
|
recycleThreadContexts();
|
|
|
|
|
host.Storage.DeleteDatabase(database_name);
|
|
|
|
|
}
|
2017-10-20 15:15:02 +00:00
|
|
|
|
}
|
2017-10-17 06:00:27 +00:00
|
|
|
|
}
|
|
|
|
|
}
|