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-07-04 16:59:39 +08:00
|
|
|
using osu.Framework.Development;
|
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;
|
|
|
|
using Realms;
|
|
|
|
|
2021-09-30 22:42:40 +08:00
|
|
|
#nullable enable
|
|
|
|
|
2021-01-07 13:07:36 +08:00
|
|
|
namespace osu.Game.Database
|
|
|
|
{
|
2021-09-30 22:42:40 +08:00
|
|
|
/// <summary>
|
|
|
|
/// A factory which provides both the main (update thread bound) realm context and creates contexts for async usage.
|
|
|
|
/// </summary>
|
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-12 13:36:35 +08:00
|
|
|
/// <summary>
|
2021-09-30 22:42:40 +08:00
|
|
|
/// The filename of this realm.
|
2021-01-12 13:36:35 +08:00
|
|
|
/// </summary>
|
2021-09-30 22:42:40 +08:00
|
|
|
public readonly string Filename;
|
|
|
|
|
|
|
|
private const int schema_version = 6;
|
2021-01-07 13:07:36 +08:00
|
|
|
|
2021-06-24 13:37:26 +08:00
|
|
|
/// <summary>
|
2021-09-30 22:42:40 +08:00
|
|
|
/// Lock object which is held during <see cref="BlockAllOperations"/> sections, blocking context creation during blocking periods.
|
2021-06-24 13:37:26 +08:00
|
|
|
/// </summary>
|
2021-09-30 22:42:40 +08:00
|
|
|
private readonly SemaphoreSlim contextCreationLock = new SemaphoreSlim(1);
|
2021-06-24 13:37:26 +08:00
|
|
|
|
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-22 16:28:47 +08:00
|
|
|
|
2021-10-01 02:45:00 +08:00
|
|
|
private readonly object contextLock = new object();
|
2021-09-30 22:42:40 +08:00
|
|
|
private Realm? context;
|
2021-01-13 16:34:44 +08:00
|
|
|
|
|
|
|
public Realm Context
|
2021-01-07 13:07:36 +08:00
|
|
|
{
|
2021-01-13 16:34:44 +08:00
|
|
|
get
|
|
|
|
{
|
2021-07-04 16:59:39 +08:00
|
|
|
if (!ThreadSafety.IsUpdateThread)
|
2021-09-30 22:42:40 +08:00
|
|
|
throw new InvalidOperationException($"Use {nameof(CreateContext)} when performing realm operations from a non-update thread");
|
2021-07-04 16:59:39 +08:00
|
|
|
|
2021-10-01 02:45:00 +08:00
|
|
|
lock (contextLock)
|
2021-01-13 16:34:44 +08:00
|
|
|
{
|
2021-10-01 02:45:00 +08:00
|
|
|
if (context == null)
|
|
|
|
{
|
2021-10-01 02:46:53 +08:00
|
|
|
context = CreateContext();
|
2021-10-01 02:45:00 +08:00
|
|
|
Logger.Log($"Opened realm \"{context.Config.DatabasePath}\" at version {context.Config.SchemaVersion}");
|
|
|
|
}
|
|
|
|
|
|
|
|
// creating a context will ensure our schema is up-to-date and migrated.
|
|
|
|
return context;
|
2021-07-04 16:59:39 +08:00
|
|
|
}
|
2021-01-13 16:34:44 +08:00
|
|
|
}
|
2021-01-07 14:41:29 +08:00
|
|
|
}
|
|
|
|
|
2021-09-30 22:42:40 +08:00
|
|
|
public RealmContextFactory(Storage storage, string filename)
|
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-09-30 22:42:40 +08:00
|
|
|
Filename = filename;
|
2021-01-07 13:07:36 +08:00
|
|
|
|
2021-09-30 22:42:40 +08:00
|
|
|
const string realm_extension = ".realm";
|
2021-01-14 14:51:19 +08:00
|
|
|
|
2021-09-30 22:42:40 +08:00
|
|
|
if (!Filename.EndsWith(realm_extension, StringComparison.Ordinal))
|
|
|
|
Filename += realm_extension;
|
2021-06-24 13:37:26 +08:00
|
|
|
}
|
2021-01-13 16:34:44 +08:00
|
|
|
|
2021-09-30 22:42:40 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Compact this realm.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
public bool Compact() => Realm.Compact(getConfiguration());
|
|
|
|
|
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-10-01 02:45:00 +08:00
|
|
|
lock (contextLock)
|
|
|
|
{
|
|
|
|
if (context?.Refresh() == true)
|
|
|
|
refreshes.Value++;
|
|
|
|
}
|
2021-01-07 13:07:36 +08:00
|
|
|
}
|
|
|
|
|
2021-10-01 02:46:53 +08:00
|
|
|
public Realm CreateContext()
|
2021-01-12 13:36:35 +08:00
|
|
|
{
|
2021-10-01 02:46:53 +08:00
|
|
|
if (IsDisposed)
|
|
|
|
throw new ObjectDisposedException(nameof(RealmContextFactory));
|
|
|
|
|
2021-06-24 13:37:26 +08:00
|
|
|
try
|
|
|
|
{
|
2021-09-30 22:42:40 +08:00
|
|
|
contextCreationLock.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-09-30 22:42:40 +08:00
|
|
|
return Realm.GetInstance(getConfiguration());
|
2021-06-24 13:37:26 +08:00
|
|
|
}
|
|
|
|
finally
|
2021-01-12 13:36:35 +08:00
|
|
|
{
|
2021-09-30 22:42:40 +08:00
|
|
|
contextCreationLock.Release();
|
2021-06-24 13:37:26 +08:00
|
|
|
}
|
2021-01-12 13:36:35 +08:00
|
|
|
}
|
|
|
|
|
2021-09-30 22:42:40 +08:00
|
|
|
private RealmConfiguration getConfiguration()
|
2021-06-28 15:14:14 +08:00
|
|
|
{
|
2021-09-30 22:42:40 +08:00
|
|
|
return new RealmConfiguration(storage.GetFullPath(Filename, true))
|
|
|
|
{
|
|
|
|
SchemaVersion = schema_version,
|
|
|
|
MigrationCallback = onMigration,
|
|
|
|
};
|
2021-06-28 15:14:14 +08:00
|
|
|
}
|
|
|
|
|
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-13 17:24:19 +08:00
|
|
|
}
|
2021-01-07 13:07:36 +08:00
|
|
|
|
2021-09-30 22:42:40 +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-01-22 16:28:47 +08:00
|
|
|
{
|
2021-09-30 22:42:40 +08:00
|
|
|
if (IsDisposed)
|
|
|
|
throw new ObjectDisposedException(nameof(RealmContextFactory));
|
2021-06-29 19:21:31 +08:00
|
|
|
|
2021-10-01 00:32:28 +08:00
|
|
|
// TODO: this can be added for safety once we figure how to bypass in test
|
|
|
|
// if (!ThreadSafety.IsUpdateThread)
|
|
|
|
// throw new InvalidOperationException($"{nameof(BlockAllOperations)} must be called from the update thread.");
|
|
|
|
|
2021-09-30 22:42:40 +08:00
|
|
|
Logger.Log(@"Blocking realm operations.", LoggingTarget.Database);
|
2021-07-04 15:24:39 +08:00
|
|
|
|
2021-10-01 00:32:28 +08:00
|
|
|
try
|
|
|
|
{
|
|
|
|
contextCreationLock.Wait();
|
|
|
|
|
2021-10-01 02:53:33 +08:00
|
|
|
lock (contextLock)
|
|
|
|
{
|
|
|
|
context?.Dispose();
|
|
|
|
context = null;
|
|
|
|
}
|
|
|
|
|
2021-10-01 00:32:28 +08:00
|
|
|
const int sleep_length = 200;
|
|
|
|
int timeout = 5000;
|
2021-01-22 16:28:47 +08:00
|
|
|
|
2021-10-01 00:32:28 +08:00
|
|
|
// see https://github.com/realm/realm-dotnet/discussions/2657
|
|
|
|
while (!Compact())
|
|
|
|
{
|
|
|
|
Thread.Sleep(sleep_length);
|
|
|
|
timeout -= sleep_length;
|
2021-06-29 19:21:31 +08:00
|
|
|
|
2021-10-01 00:32:28 +08:00
|
|
|
if (timeout < 0)
|
|
|
|
throw new TimeoutException("Took too long to acquire lock");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
catch
|
|
|
|
{
|
|
|
|
contextCreationLock.Release();
|
|
|
|
throw;
|
|
|
|
}
|
|
|
|
|
|
|
|
return new InvokeOnDisposal<RealmContextFactory>(this, factory =>
|
2021-09-30 22:42:40 +08:00
|
|
|
{
|
|
|
|
factory.contextCreationLock.Release();
|
|
|
|
Logger.Log(@"Restoring realm operations.", LoggingTarget.Database);
|
2021-10-01 00:32:28 +08:00
|
|
|
});
|
2021-01-15 13:26:06 +08:00
|
|
|
}
|
|
|
|
|
2021-06-28 15:14:14 +08:00
|
|
|
protected override void Dispose(bool isDisposing)
|
|
|
|
{
|
2021-10-01 02:46:53 +08:00
|
|
|
lock (contextLock)
|
|
|
|
{
|
|
|
|
context?.Dispose();
|
|
|
|
}
|
2021-09-30 22:42:40 +08:00
|
|
|
|
2021-06-28 15:14:14 +08:00
|
|
|
if (!IsDisposed)
|
|
|
|
{
|
2021-10-01 00:32:28 +08:00
|
|
|
// intentionally block context creation indefinitely. this ensures that nothing can start consuming a new context after disposal.
|
|
|
|
contextCreationLock.Wait();
|
2021-09-30 22:42:40 +08:00
|
|
|
contextCreationLock.Dispose();
|
2021-06-28 15:14:14 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
base.Dispose(isDisposing);
|
|
|
|
}
|
2021-01-07 13:07:36 +08:00
|
|
|
}
|
|
|
|
}
|