Tidy up write usage class

This commit is contained in:
Dean Herbert
2021-01-13 18:24:19 +09:00
parent 5fa3a22f28
commit 8442b34e84
3 changed files with 22 additions and 116 deletions

View File

@ -2,6 +2,7 @@
// See the LICENCE file in the repository root for full licence text.
using System.Threading;
using osu.Framework.Allocation;
using osu.Framework.Graphics;
using osu.Framework.Logging;
using osu.Framework.Platform;
@ -13,6 +14,7 @@ namespace osu.Game.Database
public class RealmContextFactory : Component, IRealmFactory
{
private readonly Storage storage;
private const string database_name = @"client";
private const int schema_version = 5;
@ -22,20 +24,11 @@ namespace osu.Game.Database
/// </summary>
private readonly object writeLock = new object();
private ThreadLocal<bool> refreshCompleted = new ThreadLocal<bool>();
private bool rollbackRequired;
private int currentWriteUsages;
private Transaction currentWriteTransaction;
private static readonly GlobalStatistic<int> reads = GlobalStatistics.Get<int>("Realm", "Get (Read)");
private static readonly GlobalStatistic<int> writes = GlobalStatistics.Get<int>("Realm", "Get (Write)");
private static readonly GlobalStatistic<int> refreshes = GlobalStatistics.Get<int>("Realm", "Refreshes");
private static readonly GlobalStatistic<int> commits = GlobalStatistics.Get<int>("Realm", "Commits");
private static readonly GlobalStatistic<int> rollbacks = GlobalStatistics.Get<int>("Realm", "Rollbacks");
private static readonly GlobalStatistic<int> contexts_open = GlobalStatistics.Get<int>("Realm", "Contexts (Open)");
private static readonly GlobalStatistic<int> refreshes = GlobalStatistics.Get<int>("Realm", "Dirty Refreshes");
private static readonly GlobalStatistic<int> contexts_created = GlobalStatistics.Get<int>("Realm", "Contexts (Created)");
private Realm context;
@ -72,24 +65,8 @@ namespace osu.Game.Database
writes.Value++;
Monitor.Enter(writeLock);
Realm realm;
try
{
realm = createContext();
currentWriteTransaction ??= realm.BeginWrite();
}
catch
{
// retrieval of a context could trigger a fatal error.
Monitor.Exit(writeLock);
throw;
}
Interlocked.Increment(ref currentWriteUsages);
return new RealmWriteUsage(realm, usageCompleted) { IsTransactionLeader = currentWriteTransaction != null && currentWriteUsages == 1 };
return new RealmWriteUsage(this);
}
protected override void Update()
@ -111,41 +88,25 @@ namespace osu.Game.Database
});
}
private void usageCompleted(RealmWriteUsage usage)
{
int usages = Interlocked.Decrement(ref currentWriteUsages);
try
{
rollbackRequired |= usage.RollbackRequired;
if (usages == 0)
{
if (rollbackRequired)
{
rollbacks.Value++;
currentWriteTransaction?.Rollback();
}
else
{
commits.Value++;
currentWriteTransaction?.Commit();
}
currentWriteTransaction = null;
rollbackRequired = false;
refreshCompleted = new ThreadLocal<bool>();
}
}
finally
{
Monitor.Exit(writeLock);
}
}
private void onMigration(Migration migration, ulong lastSchemaVersion)
{
}
public class RealmWriteUsage : InvokeOnDisposal<RealmContextFactory>
{
public readonly Realm Context;
public RealmWriteUsage(RealmContextFactory factory)
: base(factory, usageCompleted)
{
Context = factory.createContext();
Context.BeginWrite();
}
private static void usageCompleted(RealmContextFactory factory)
{
Monitor.Exit(factory.writeLock);
}
}
}
}