2021-01-21 19:53:16 +08:00
|
|
|
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
|
|
|
|
// See the LICENCE file in the repository root for full licence text.
|
|
|
|
|
2021-01-14 15:13:10 +08:00
|
|
|
using System;
|
2021-01-07 13:07:36 +08:00
|
|
|
using System.Threading;
|
2021-01-22 16:28:47 +08:00
|
|
|
using osu.Framework.Allocation;
|
2021-01-13 16:34:44 +08:00
|
|
|
using osu.Framework.Graphics;
|
2021-01-12 13:36:35 +08:00
|
|
|
using osu.Framework.Logging;
|
2021-01-07 13:07:36 +08:00
|
|
|
using osu.Framework.Platform;
|
|
|
|
using osu.Framework.Statistics;
|
2021-01-19 17:22:23 +08:00
|
|
|
using osu.Game.Input.Bindings;
|
2021-01-07 13:07:36 +08:00
|
|
|
using Realms;
|
|
|
|
|
|
|
|
namespace osu.Game.Database
|
|
|
|
{
|
2021-01-13 16:34:44 +08:00
|
|
|
public class RealmContextFactory : Component, IRealmFactory
|
2021-01-07 13:07:36 +08:00
|
|
|
{
|
|
|
|
private readonly Storage storage;
|
2021-01-13 17:24:19 +08:00
|
|
|
|
2021-01-07 13:07:36 +08:00
|
|
|
private const string database_name = @"client";
|
|
|
|
|
2021-01-19 17:22:23 +08:00
|
|
|
private const int schema_version = 6;
|
2021-01-12 13:25:07 +08:00
|
|
|
|
2021-01-12 13:36:35 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Lock object which is held for the duration of a write operation (via <see cref="GetForWrite"/>).
|
|
|
|
/// </summary>
|
2021-01-07 13:07:36 +08:00
|
|
|
private readonly object writeLock = new object();
|
|
|
|
|
2021-06-24 13:37:26 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Lock object which is held during <see cref="BlockAllOperations"/> sections.
|
|
|
|
/// </summary>
|
|
|
|
private readonly SemaphoreSlim blockingLock = new SemaphoreSlim(1);
|
|
|
|
|
2021-01-13 16:34:44 +08:00
|
|
|
private static readonly GlobalStatistic<int> reads = GlobalStatistics.Get<int>("Realm", "Get (Read)");
|
|
|
|
private static readonly GlobalStatistic<int> writes = GlobalStatistics.Get<int>("Realm", "Get (Write)");
|
2021-01-13 17:24:19 +08:00
|
|
|
private static readonly GlobalStatistic<int> refreshes = GlobalStatistics.Get<int>("Realm", "Dirty Refreshes");
|
2021-01-13 16:34:44 +08:00
|
|
|
private static readonly GlobalStatistic<int> contexts_created = GlobalStatistics.Get<int>("Realm", "Contexts (Created)");
|
2021-01-14 14:51:19 +08:00
|
|
|
private static readonly GlobalStatistic<int> pending_writes = GlobalStatistics.Get<int>("Realm", "Pending writes");
|
2021-01-22 16:28:47 +08:00
|
|
|
private static readonly GlobalStatistic<int> active_usages = GlobalStatistics.Get<int>("Realm", "Active usages");
|
|
|
|
|
2021-01-13 16:34:44 +08:00
|
|
|
private Realm context;
|
|
|
|
|
|
|
|
public Realm Context
|
2021-01-07 13:07:36 +08:00
|
|
|
{
|
2021-01-13 16:34:44 +08:00
|
|
|
get
|
|
|
|
{
|
|
|
|
if (context == null)
|
|
|
|
{
|
|
|
|
context = createContext();
|
|
|
|
Logger.Log($"Opened realm \"{context.Config.DatabasePath}\" at version {context.Config.SchemaVersion}");
|
|
|
|
}
|
2021-01-11 17:57:56 +08:00
|
|
|
|
2021-01-13 16:34:44 +08:00
|
|
|
// creating a context will ensure our schema is up-to-date and migrated.
|
2021-01-07 14:41:29 +08:00
|
|
|
|
2021-01-13 16:34:44 +08:00
|
|
|
return context;
|
|
|
|
}
|
2021-01-07 14:41:29 +08:00
|
|
|
}
|
|
|
|
|
2021-01-13 16:34:44 +08:00
|
|
|
public RealmContextFactory(Storage storage)
|
2021-01-07 14:41:29 +08:00
|
|
|
{
|
2021-01-13 16:34:44 +08:00
|
|
|
this.storage = storage;
|
2021-01-07 13:07:36 +08:00
|
|
|
}
|
|
|
|
|
2021-01-22 16:28:47 +08:00
|
|
|
public RealmUsage GetForRead()
|
2021-01-07 13:07:36 +08:00
|
|
|
{
|
|
|
|
reads.Value++;
|
2021-06-24 13:37:26 +08:00
|
|
|
return new RealmUsage(createContext());
|
2021-01-07 13:07:36 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
public RealmWriteUsage GetForWrite()
|
|
|
|
{
|
|
|
|
writes.Value++;
|
2021-01-14 14:51:19 +08:00
|
|
|
pending_writes.Value++;
|
|
|
|
|
2021-01-07 13:07:36 +08:00
|
|
|
Monitor.Enter(writeLock);
|
2021-06-24 13:37:26 +08:00
|
|
|
return new RealmWriteUsage(createContext(), writeComplete);
|
|
|
|
}
|
2021-01-13 16:34:44 +08:00
|
|
|
|
2021-06-28 15:14:14 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Flush any active contexts and block any further writes.
|
|
|
|
/// </summary>
|
|
|
|
/// <remarks>
|
|
|
|
/// This should be used in places we need to ensure no ongoing reads/writes are occurring with realm.
|
|
|
|
/// ie. to move the realm backing file to a new location.
|
|
|
|
/// </remarks>
|
|
|
|
/// <returns>An <see cref="IDisposable"/> which should be disposed to end the blocking section.</returns>
|
|
|
|
public IDisposable BlockAllOperations()
|
2021-06-24 13:37:26 +08:00
|
|
|
{
|
2021-06-28 15:14:14 +08:00
|
|
|
if (IsDisposed)
|
2021-06-28 15:17:09 +08:00
|
|
|
throw new ObjectDisposedException(nameof(RealmContextFactory));
|
2021-06-28 15:14:14 +08:00
|
|
|
|
|
|
|
blockingLock.Wait();
|
|
|
|
flushContexts();
|
|
|
|
|
|
|
|
return new InvokeOnDisposal<RealmContextFactory>(this, endBlockingSection);
|
|
|
|
|
|
|
|
static void endBlockingSection(RealmContextFactory factory) => factory.blockingLock.Release();
|
2021-01-07 13:07:36 +08:00
|
|
|
}
|
|
|
|
|
2021-01-13 16:34:44 +08:00
|
|
|
protected override void Update()
|
2021-01-07 13:07:36 +08:00
|
|
|
{
|
2021-01-13 16:34:44 +08:00
|
|
|
base.Update();
|
2021-01-07 13:07:36 +08:00
|
|
|
|
2021-01-21 18:02:09 +08:00
|
|
|
if (context?.Refresh() == true)
|
2021-01-13 16:34:44 +08:00
|
|
|
refreshes.Value++;
|
2021-01-07 13:07:36 +08:00
|
|
|
}
|
|
|
|
|
2021-01-12 13:36:35 +08:00
|
|
|
private Realm createContext()
|
|
|
|
{
|
2021-06-24 13:37:26 +08:00
|
|
|
try
|
|
|
|
{
|
2021-06-28 14:04:14 +08:00
|
|
|
if (IsDisposed)
|
2021-06-28 15:17:09 +08:00
|
|
|
throw new ObjectDisposedException(nameof(RealmContextFactory));
|
2021-06-28 14:04:14 +08:00
|
|
|
|
2021-06-24 13:37:26 +08:00
|
|
|
blockingLock.Wait();
|
2021-01-22 16:28:47 +08:00
|
|
|
|
2021-06-24 13:37:26 +08:00
|
|
|
contexts_created.Value++;
|
2021-01-12 13:36:35 +08:00
|
|
|
|
2021-06-24 13:37:26 +08:00
|
|
|
return Realm.GetInstance(new RealmConfiguration(storage.GetFullPath($"{database_name}.realm", true))
|
|
|
|
{
|
|
|
|
SchemaVersion = schema_version,
|
|
|
|
MigrationCallback = onMigration,
|
|
|
|
});
|
|
|
|
}
|
|
|
|
finally
|
2021-01-12 13:36:35 +08:00
|
|
|
{
|
2021-06-24 13:37:26 +08:00
|
|
|
blockingLock.Release();
|
|
|
|
}
|
2021-01-12 13:36:35 +08:00
|
|
|
}
|
|
|
|
|
2021-06-28 15:14:14 +08:00
|
|
|
private void writeComplete()
|
|
|
|
{
|
|
|
|
Monitor.Exit(writeLock);
|
|
|
|
pending_writes.Value--;
|
|
|
|
}
|
|
|
|
|
2021-01-13 17:24:19 +08:00
|
|
|
private void onMigration(Migration migration, ulong lastSchemaVersion)
|
2021-01-07 13:07:36 +08:00
|
|
|
{
|
2021-01-19 17:22:23 +08:00
|
|
|
switch (lastSchemaVersion)
|
|
|
|
{
|
|
|
|
case 5:
|
|
|
|
// let's keep things simple. changing the type of the primary key is a bit involved.
|
|
|
|
migration.NewRealm.RemoveAll<RealmKeyBinding>();
|
|
|
|
break;
|
|
|
|
}
|
2021-01-13 17:24:19 +08:00
|
|
|
}
|
2021-01-07 13:07:36 +08:00
|
|
|
|
2021-01-22 16:28:47 +08:00
|
|
|
private void flushContexts()
|
|
|
|
{
|
|
|
|
var previousContext = context;
|
|
|
|
context = null;
|
|
|
|
|
|
|
|
// wait for all threaded usages to finish
|
|
|
|
while (active_usages.Value > 0)
|
|
|
|
Thread.Sleep(50);
|
|
|
|
|
|
|
|
previousContext?.Dispose();
|
2021-01-15 13:26:06 +08:00
|
|
|
}
|
|
|
|
|
2021-06-28 15:14:14 +08:00
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
|
|
|
if (!IsDisposed)
|
|
|
|
{
|
|
|
|
// intentionally block all operations indefinitely. this ensures that nothing can start consuming a new context after disposal.
|
|
|
|
BlockAllOperations();
|
|
|
|
blockingLock?.Dispose();
|
|
|
|
}
|
|
|
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
}
|
|
|
|
|
2021-01-14 15:13:10 +08:00
|
|
|
/// <summary>
|
2021-01-22 16:28:47 +08:00
|
|
|
/// A usage of realm from an arbitrary thread.
|
2021-01-14 15:13:10 +08:00
|
|
|
/// </summary>
|
2021-01-22 16:28:47 +08:00
|
|
|
public class RealmUsage : IDisposable
|
2021-01-13 17:24:19 +08:00
|
|
|
{
|
2021-01-14 15:13:10 +08:00
|
|
|
public readonly Realm Realm;
|
2021-01-07 13:07:36 +08:00
|
|
|
|
2021-06-24 13:37:26 +08:00
|
|
|
internal RealmUsage(Realm context)
|
2021-01-22 16:28:47 +08:00
|
|
|
{
|
2021-03-31 14:16:01 +08:00
|
|
|
active_usages.Value++;
|
2021-06-24 13:37:26 +08:00
|
|
|
Realm = context;
|
2021-01-22 16:28:47 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Disposes this instance, calling the initially captured action.
|
|
|
|
/// </summary>
|
|
|
|
public virtual void Dispose()
|
|
|
|
{
|
|
|
|
Realm?.Dispose();
|
|
|
|
active_usages.Value--;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// A transaction used for making changes to realm data.
|
|
|
|
/// </summary>
|
|
|
|
public class RealmWriteUsage : RealmUsage
|
|
|
|
{
|
2021-06-24 13:37:26 +08:00
|
|
|
private readonly Action onWriteComplete;
|
2021-01-14 15:13:10 +08:00
|
|
|
private readonly Transaction transaction;
|
|
|
|
|
2021-06-24 13:37:26 +08:00
|
|
|
internal RealmWriteUsage(Realm context, Action onWriteComplete)
|
|
|
|
: base(context)
|
2021-01-07 13:07:36 +08:00
|
|
|
{
|
2021-06-24 13:37:26 +08:00
|
|
|
this.onWriteComplete = onWriteComplete;
|
2021-01-14 15:13:10 +08:00
|
|
|
transaction = Realm.BeginWrite();
|
2021-01-07 13:07:36 +08:00
|
|
|
}
|
2021-01-13 16:34:44 +08:00
|
|
|
|
2021-01-14 15:13:10 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Commit all changes made in this transaction.
|
|
|
|
/// </summary>
|
|
|
|
public void Commit() => transaction.Commit();
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Revert all changes made in this transaction.
|
|
|
|
/// </summary>
|
|
|
|
public void Rollback() => transaction.Rollback();
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Disposes this instance, calling the initially captured action.
|
|
|
|
/// </summary>
|
2021-01-22 16:28:47 +08:00
|
|
|
public override void Dispose()
|
2021-01-13 17:24:19 +08:00
|
|
|
{
|
2021-01-14 15:13:10 +08:00
|
|
|
// rollback if not explicitly committed.
|
|
|
|
transaction?.Dispose();
|
|
|
|
|
2021-01-22 16:28:47 +08:00
|
|
|
base.Dispose();
|
|
|
|
|
2021-06-24 13:37:26 +08:00
|
|
|
onWriteComplete();
|
2021-01-13 17:24:19 +08:00
|
|
|
}
|
2021-01-13 16:34:44 +08:00
|
|
|
}
|
2021-01-07 13:07:36 +08:00
|
|
|
}
|
|
|
|
}
|