2018-01-05 19:21:19 +08:00
|
|
|
|
// Copyright (c) 2007-2018 ppy Pty Ltd <contact@ppy.sh>.
|
2017-10-17 15:02:13 +08:00
|
|
|
|
// Licensed under the MIT Licence - https://raw.githubusercontent.com/ppy/osu/master/LICENCE
|
2017-10-17 14:00:27 +08:00
|
|
|
|
|
2018-02-12 16:55:11 +08:00
|
|
|
|
using System.Threading;
|
2017-10-17 14:00:27 +08:00
|
|
|
|
using osu.Framework.Platform;
|
|
|
|
|
|
|
|
|
|
namespace osu.Game.Database
|
|
|
|
|
{
|
|
|
|
|
public class DatabaseContextFactory
|
|
|
|
|
{
|
|
|
|
|
private readonly GameHost host;
|
|
|
|
|
|
2017-10-20 23:15:02 +08:00
|
|
|
|
private const string database_name = @"client";
|
|
|
|
|
|
2018-02-12 16:55:11 +08:00
|
|
|
|
private ThreadLocal<OsuDbContext> threadContexts;
|
|
|
|
|
|
|
|
|
|
private readonly object writeLock = new object();
|
|
|
|
|
|
|
|
|
|
private OsuDbContext writeContext;
|
|
|
|
|
|
|
|
|
|
private volatile int currentWriteUsages;
|
|
|
|
|
|
2017-10-17 14:00:27 +08:00
|
|
|
|
public DatabaseContextFactory(GameHost host)
|
|
|
|
|
{
|
|
|
|
|
this.host = host;
|
2018-02-12 16:55:11 +08: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()
|
|
|
|
|
{
|
|
|
|
|
lock (writeLock)
|
|
|
|
|
{
|
|
|
|
|
var usage = new DatabaseWriteUsage(writeContext ?? (writeContext = threadContexts.Value), usageCompleted);
|
|
|
|
|
Interlocked.Increment(ref currentWriteUsages);
|
|
|
|
|
return usage;
|
|
|
|
|
}
|
2017-10-17 14:00:27 +08:00
|
|
|
|
}
|
|
|
|
|
|
2018-02-12 16:55:11 +08:00
|
|
|
|
private void usageCompleted(DatabaseWriteUsage usage)
|
|
|
|
|
{
|
|
|
|
|
int usages = Interlocked.Decrement(ref currentWriteUsages);
|
|
|
|
|
if (usages == 0)
|
|
|
|
|
{
|
|
|
|
|
writeContext.Dispose();
|
|
|
|
|
writeContext = null;
|
|
|
|
|
|
|
|
|
|
// once all writes are complete, we want to refresh thread-specific contexts to make sure they don't have stale local caches.
|
|
|
|
|
recycleThreadContexts();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void recycleThreadContexts() => threadContexts = new ThreadLocal<OsuDbContext>(CreateContext);
|
|
|
|
|
|
|
|
|
|
protected virtual OsuDbContext CreateContext()
|
|
|
|
|
{
|
|
|
|
|
var ctx = new OsuDbContext(host.Storage.GetDatabaseConnectionString(database_name));
|
|
|
|
|
ctx.Database.AutoTransactionsEnabled = false;
|
|
|
|
|
|
|
|
|
|
return ctx;
|
|
|
|
|
}
|
2017-10-20 23:15:02 +08:00
|
|
|
|
|
|
|
|
|
public void ResetDatabase()
|
|
|
|
|
{
|
2018-02-12 16:55:11 +08:00
|
|
|
|
lock (writeLock)
|
|
|
|
|
{
|
|
|
|
|
recycleThreadContexts();
|
|
|
|
|
host.Storage.DeleteDatabase(database_name);
|
|
|
|
|
}
|
2017-10-20 23:15:02 +08:00
|
|
|
|
}
|
2017-10-17 14:00:27 +08:00
|
|
|
|
}
|
|
|
|
|
}
|