2021-01-21 11:53:16 +00: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 07:13:10 +00:00
|
|
|
using System;
|
2021-10-18 06:35:51 +00:00
|
|
|
using System.Linq;
|
2021-11-05 08:01:00 +00:00
|
|
|
using System.Reflection;
|
2021-01-07 05:07:36 +00:00
|
|
|
using System.Threading;
|
2021-01-22 08:28:47 +00:00
|
|
|
using osu.Framework.Allocation;
|
2021-07-04 08:59:39 +00:00
|
|
|
using osu.Framework.Development;
|
2021-10-29 02:14:22 +00:00
|
|
|
using osu.Framework.Input.Bindings;
|
2021-01-12 05:36:35 +00:00
|
|
|
using osu.Framework.Logging;
|
2021-01-07 05:07:36 +00:00
|
|
|
using osu.Framework.Platform;
|
|
|
|
using osu.Framework.Statistics;
|
2021-10-29 02:14:22 +00:00
|
|
|
using osu.Game.Input.Bindings;
|
2021-10-18 06:35:51 +00:00
|
|
|
using osu.Game.Models;
|
2021-01-07 05:07:36 +00:00
|
|
|
using Realms;
|
|
|
|
|
2021-09-30 14:42:40 +00:00
|
|
|
#nullable enable
|
|
|
|
|
2021-01-07 05:07:36 +00:00
|
|
|
namespace osu.Game.Database
|
|
|
|
{
|
2021-09-30 14:42:40 +00:00
|
|
|
/// <summary>
|
|
|
|
/// A factory which provides both the main (update thread bound) realm context and creates contexts for async usage.
|
|
|
|
/// </summary>
|
2021-10-15 04:58:14 +00:00
|
|
|
public class RealmContextFactory : IDisposable, IRealmFactory
|
2021-01-07 05:07:36 +00:00
|
|
|
{
|
|
|
|
private readonly Storage storage;
|
2021-01-13 09:24:19 +00:00
|
|
|
|
2021-01-12 05:36:35 +00:00
|
|
|
/// <summary>
|
2021-09-30 14:42:40 +00:00
|
|
|
/// The filename of this realm.
|
2021-01-12 05:36:35 +00:00
|
|
|
/// </summary>
|
2021-09-30 14:42:40 +00:00
|
|
|
public readonly string Filename;
|
|
|
|
|
2021-10-18 06:35:51 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Version history:
|
|
|
|
/// 6 First tracked version (~20211018)
|
|
|
|
/// 7 Changed OnlineID fields to non-nullable to add indexing support (20211018)
|
2021-10-29 02:14:22 +00:00
|
|
|
/// 8 Rebind scroll adjust keys to not have control modifier (20211029)
|
2021-11-04 09:32:50 +00:00
|
|
|
/// 9 Converted BeatmapMetadata.Author from string to RealmUser (20211104)
|
2021-10-18 06:35:51 +00:00
|
|
|
/// </summary>
|
2021-11-04 09:32:50 +00:00
|
|
|
private const int schema_version = 9;
|
2021-01-07 05:07:36 +00:00
|
|
|
|
2021-06-24 05:37:26 +00:00
|
|
|
/// <summary>
|
2021-09-30 14:42:40 +00:00
|
|
|
/// Lock object which is held during <see cref="BlockAllOperations"/> sections, blocking context creation during blocking periods.
|
2021-06-24 05:37:26 +00:00
|
|
|
/// </summary>
|
2021-09-30 14:42:40 +00:00
|
|
|
private readonly SemaphoreSlim contextCreationLock = new SemaphoreSlim(1);
|
2021-06-24 05:37:26 +00:00
|
|
|
|
2021-01-13 09:24:19 +00:00
|
|
|
private static readonly GlobalStatistic<int> refreshes = GlobalStatistics.Get<int>("Realm", "Dirty Refreshes");
|
2021-01-13 08:34:44 +00:00
|
|
|
private static readonly GlobalStatistic<int> contexts_created = GlobalStatistics.Get<int>("Realm", "Contexts (Created)");
|
2021-01-22 08:28:47 +00:00
|
|
|
|
2021-09-30 18:45:00 +00:00
|
|
|
private readonly object contextLock = new object();
|
2021-09-30 14:42:40 +00:00
|
|
|
private Realm? context;
|
2021-01-13 08:34:44 +00:00
|
|
|
|
|
|
|
public Realm Context
|
2021-01-07 05:07:36 +00:00
|
|
|
{
|
2021-01-13 08:34:44 +00:00
|
|
|
get
|
|
|
|
{
|
2021-07-04 08:59:39 +00:00
|
|
|
if (!ThreadSafety.IsUpdateThread)
|
2021-09-30 14:42:40 +00:00
|
|
|
throw new InvalidOperationException($"Use {nameof(CreateContext)} when performing realm operations from a non-update thread");
|
2021-07-04 08:59:39 +00:00
|
|
|
|
2021-09-30 18:45:00 +00:00
|
|
|
lock (contextLock)
|
2021-01-13 08:34:44 +00:00
|
|
|
{
|
2021-09-30 18:45:00 +00:00
|
|
|
if (context == null)
|
|
|
|
{
|
2021-09-30 18:46:53 +00:00
|
|
|
context = CreateContext();
|
2021-09-30 18:45:00 +00: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 08:59:39 +00:00
|
|
|
}
|
2021-01-13 08:34:44 +00:00
|
|
|
}
|
2021-01-07 06:41:29 +00:00
|
|
|
}
|
|
|
|
|
2021-09-30 14:42:40 +00:00
|
|
|
public RealmContextFactory(Storage storage, string filename)
|
2021-01-07 06:41:29 +00:00
|
|
|
{
|
2021-01-13 08:34:44 +00:00
|
|
|
this.storage = storage;
|
2021-01-07 05:07:36 +00:00
|
|
|
|
2021-09-30 14:42:40 +00:00
|
|
|
Filename = filename;
|
2021-01-07 05:07:36 +00:00
|
|
|
|
2021-09-30 14:42:40 +00:00
|
|
|
const string realm_extension = ".realm";
|
2021-01-14 06:51:19 +00:00
|
|
|
|
2021-09-30 14:42:40 +00:00
|
|
|
if (!Filename.EndsWith(realm_extension, StringComparison.Ordinal))
|
|
|
|
Filename += realm_extension;
|
2021-09-30 14:45:09 +00:00
|
|
|
|
|
|
|
cleanupPendingDeletions();
|
|
|
|
}
|
|
|
|
|
|
|
|
private void cleanupPendingDeletions()
|
|
|
|
{
|
|
|
|
using (var realm = CreateContext())
|
|
|
|
using (var transaction = realm.BeginWrite())
|
|
|
|
{
|
|
|
|
var pendingDeleteSets = realm.All<RealmBeatmapSet>().Where(s => s.DeletePending);
|
|
|
|
|
|
|
|
foreach (var s in pendingDeleteSets)
|
|
|
|
{
|
|
|
|
foreach (var b in s.Beatmaps)
|
|
|
|
realm.Remove(b);
|
|
|
|
|
|
|
|
realm.Remove(s);
|
|
|
|
}
|
|
|
|
|
|
|
|
transaction.Commit();
|
|
|
|
}
|
2021-06-24 05:37:26 +00:00
|
|
|
}
|
2021-01-13 08:34:44 +00:00
|
|
|
|
2021-09-30 14:42:40 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Compact this realm.
|
|
|
|
/// </summary>
|
|
|
|
/// <returns></returns>
|
|
|
|
public bool Compact() => Realm.Compact(getConfiguration());
|
|
|
|
|
2021-10-15 04:58:14 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Perform a blocking refresh on the main realm context.
|
|
|
|
/// </summary>
|
|
|
|
public void Refresh()
|
2021-01-07 05:07:36 +00:00
|
|
|
{
|
2021-09-30 18:45:00 +00:00
|
|
|
lock (contextLock)
|
|
|
|
{
|
|
|
|
if (context?.Refresh() == true)
|
|
|
|
refreshes.Value++;
|
|
|
|
}
|
2021-01-07 05:07:36 +00:00
|
|
|
}
|
|
|
|
|
2021-09-30 18:46:53 +00:00
|
|
|
public Realm CreateContext()
|
2021-01-12 05:36:35 +00:00
|
|
|
{
|
2021-10-15 04:58:14 +00:00
|
|
|
if (isDisposed)
|
2021-09-30 18:46:53 +00:00
|
|
|
throw new ObjectDisposedException(nameof(RealmContextFactory));
|
|
|
|
|
2021-06-24 05:37:26 +00:00
|
|
|
try
|
|
|
|
{
|
2021-09-30 14:42:40 +00:00
|
|
|
contextCreationLock.Wait();
|
2021-01-22 08:28:47 +00:00
|
|
|
|
2021-06-24 05:37:26 +00:00
|
|
|
contexts_created.Value++;
|
2021-01-12 05:36:35 +00:00
|
|
|
|
2021-09-30 14:42:40 +00:00
|
|
|
return Realm.GetInstance(getConfiguration());
|
2021-06-24 05:37:26 +00:00
|
|
|
}
|
|
|
|
finally
|
2021-01-12 05:36:35 +00:00
|
|
|
{
|
2021-09-30 14:42:40 +00:00
|
|
|
contextCreationLock.Release();
|
2021-06-24 05:37:26 +00:00
|
|
|
}
|
2021-01-12 05:36:35 +00:00
|
|
|
}
|
|
|
|
|
2021-09-30 14:42:40 +00:00
|
|
|
private RealmConfiguration getConfiguration()
|
2021-06-28 07:14:14 +00:00
|
|
|
{
|
2021-09-30 14:42:40 +00:00
|
|
|
return new RealmConfiguration(storage.GetFullPath(Filename, true))
|
|
|
|
{
|
|
|
|
SchemaVersion = schema_version,
|
|
|
|
MigrationCallback = onMigration,
|
|
|
|
};
|
2021-06-28 07:14:14 +00:00
|
|
|
}
|
|
|
|
|
2021-01-13 09:24:19 +00:00
|
|
|
private void onMigration(Migration migration, ulong lastSchemaVersion)
|
2021-01-07 05:07:36 +00:00
|
|
|
{
|
2021-11-04 09:39:23 +00:00
|
|
|
for (ulong i = lastSchemaVersion; i <= schema_version; i++)
|
|
|
|
applyMigrationsForVersion(migration, i);
|
|
|
|
}
|
|
|
|
|
|
|
|
private void applyMigrationsForVersion(Migration migration, ulong version)
|
|
|
|
{
|
|
|
|
switch (version)
|
2021-11-04 09:32:50 +00:00
|
|
|
{
|
2021-11-04 09:39:23 +00:00
|
|
|
case 7:
|
|
|
|
convertOnlineIDs<RealmBeatmap>();
|
|
|
|
convertOnlineIDs<RealmBeatmapSet>();
|
|
|
|
convertOnlineIDs<RealmRuleset>();
|
2021-11-04 09:32:50 +00:00
|
|
|
|
2021-11-04 09:39:23 +00:00
|
|
|
void convertOnlineIDs<T>() where T : RealmObject
|
|
|
|
{
|
2021-11-05 08:01:00 +00:00
|
|
|
string className = getMappedOrOriginalName(typeof(T));
|
2021-11-04 09:32:50 +00:00
|
|
|
|
2021-11-04 09:39:23 +00:00
|
|
|
// version was not bumped when the beatmap/ruleset models were added
|
|
|
|
// therefore we must manually check for their presence to avoid throwing on the `DynamicApi` calls.
|
|
|
|
if (!migration.OldRealm.Schema.TryFindObjectSchema(className, out _))
|
|
|
|
return;
|
2021-11-04 09:32:50 +00:00
|
|
|
|
2021-11-04 09:39:23 +00:00
|
|
|
var oldItems = migration.OldRealm.DynamicApi.All(className);
|
|
|
|
var newItems = migration.NewRealm.DynamicApi.All(className);
|
2021-11-04 09:32:50 +00:00
|
|
|
|
2021-11-04 09:39:23 +00:00
|
|
|
int itemCount = newItems.Count();
|
2021-11-04 09:32:50 +00:00
|
|
|
|
2021-11-04 09:39:23 +00:00
|
|
|
for (int i = 0; i < itemCount; i++)
|
|
|
|
{
|
|
|
|
dynamic? oldItem = oldItems.ElementAt(i);
|
|
|
|
dynamic? newItem = newItems.ElementAt(i);
|
2021-10-29 02:14:22 +00:00
|
|
|
|
2021-11-04 09:39:23 +00:00
|
|
|
long? nullableOnlineID = oldItem?.OnlineID;
|
|
|
|
newItem.OnlineID = (int)(nullableOnlineID ?? -1);
|
|
|
|
}
|
|
|
|
}
|
2021-10-29 02:14:22 +00:00
|
|
|
|
2021-11-04 09:39:23 +00:00
|
|
|
break;
|
2021-10-29 02:14:22 +00:00
|
|
|
|
2021-11-04 09:39:23 +00:00
|
|
|
case 8:
|
|
|
|
// Ctrl -/+ now adjusts UI scale so let's clear any bindings which overlap these combinations.
|
|
|
|
// New defaults will be populated by the key store afterwards.
|
|
|
|
var keyBindings = migration.NewRealm.All<RealmKeyBinding>();
|
2021-10-18 06:35:51 +00:00
|
|
|
|
2021-11-04 09:39:23 +00:00
|
|
|
var increaseSpeedBinding = keyBindings.FirstOrDefault(k => k.ActionInt == (int)GlobalAction.IncreaseScrollSpeed);
|
|
|
|
if (increaseSpeedBinding != null && increaseSpeedBinding.KeyCombination.Keys.SequenceEqual(new[] { InputKey.Control, InputKey.Plus }))
|
|
|
|
migration.NewRealm.Remove(increaseSpeedBinding);
|
2021-10-18 06:35:51 +00:00
|
|
|
|
2021-11-04 09:39:23 +00:00
|
|
|
var decreaseSpeedBinding = keyBindings.FirstOrDefault(k => k.ActionInt == (int)GlobalAction.DecreaseScrollSpeed);
|
|
|
|
if (decreaseSpeedBinding != null && decreaseSpeedBinding.KeyCombination.Keys.SequenceEqual(new[] { InputKey.Control, InputKey.Minus }))
|
|
|
|
migration.NewRealm.Remove(decreaseSpeedBinding);
|
|
|
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 9:
|
|
|
|
// Pretty pointless to do this as beatmaps aren't really loaded via realm yet, but oh well.
|
2021-11-05 08:02:23 +00:00
|
|
|
var oldMetadata = migration.OldRealm.DynamicApi.All(getMappedOrOriginalName(typeof(RealmBeatmapMetadata)));
|
|
|
|
var newMetadata = migration.NewRealm.All<RealmBeatmapMetadata>();
|
2021-10-18 06:35:51 +00:00
|
|
|
|
2021-11-05 09:24:07 +00:00
|
|
|
int metadataCount = newMetadata.Count();
|
2021-10-18 06:35:51 +00:00
|
|
|
|
2021-11-05 09:24:07 +00:00
|
|
|
for (int i = 0; i < metadataCount; i++)
|
2021-10-18 06:35:51 +00:00
|
|
|
{
|
2021-11-05 08:02:23 +00:00
|
|
|
dynamic? oldItem = oldMetadata.ElementAt(i);
|
|
|
|
var newItem = newMetadata.ElementAt(i);
|
2021-10-18 06:35:51 +00:00
|
|
|
|
2021-11-04 09:39:23 +00:00
|
|
|
string username = oldItem.Author;
|
|
|
|
newItem.Author = new RealmUser
|
|
|
|
{
|
|
|
|
Username = username
|
|
|
|
};
|
2021-10-18 06:35:51 +00:00
|
|
|
}
|
2021-11-04 09:39:23 +00:00
|
|
|
|
|
|
|
break;
|
2021-10-18 06:35:51 +00:00
|
|
|
}
|
2021-01-13 09:24:19 +00:00
|
|
|
}
|
2021-01-07 05:07:36 +00:00
|
|
|
|
2021-09-30 14:42:40 +00: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 08:28:47 +00:00
|
|
|
{
|
2021-10-15 04:58:14 +00:00
|
|
|
if (isDisposed)
|
2021-09-30 14:42:40 +00:00
|
|
|
throw new ObjectDisposedException(nameof(RealmContextFactory));
|
2021-06-29 11:21:31 +00:00
|
|
|
|
2021-10-11 06:20:12 +00:00
|
|
|
if (!ThreadSafety.IsUpdateThread)
|
|
|
|
throw new InvalidOperationException($"{nameof(BlockAllOperations)} must be called from the update thread.");
|
2021-09-30 16:32:28 +00:00
|
|
|
|
2021-09-30 14:42:40 +00:00
|
|
|
Logger.Log(@"Blocking realm operations.", LoggingTarget.Database);
|
2021-07-04 07:24:39 +00:00
|
|
|
|
2021-09-30 16:32:28 +00:00
|
|
|
try
|
|
|
|
{
|
|
|
|
contextCreationLock.Wait();
|
|
|
|
|
2021-09-30 18:53:33 +00:00
|
|
|
lock (contextLock)
|
|
|
|
{
|
|
|
|
context?.Dispose();
|
|
|
|
context = null;
|
|
|
|
}
|
|
|
|
|
2021-09-30 16:32:28 +00:00
|
|
|
const int sleep_length = 200;
|
|
|
|
int timeout = 5000;
|
2021-01-22 08:28:47 +00:00
|
|
|
|
2021-09-30 16:32:28 +00:00
|
|
|
// see https://github.com/realm/realm-dotnet/discussions/2657
|
|
|
|
while (!Compact())
|
|
|
|
{
|
|
|
|
Thread.Sleep(sleep_length);
|
|
|
|
timeout -= sleep_length;
|
2021-06-29 11:21:31 +00:00
|
|
|
|
2021-09-30 16:32:28 +00: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 14:42:40 +00:00
|
|
|
{
|
|
|
|
factory.contextCreationLock.Release();
|
|
|
|
Logger.Log(@"Restoring realm operations.", LoggingTarget.Database);
|
2021-09-30 16:32:28 +00:00
|
|
|
});
|
2021-01-15 05:26:06 +00:00
|
|
|
}
|
|
|
|
|
2021-11-05 08:01:00 +00:00
|
|
|
// https://github.com/realm/realm-dotnet/blob/32f4ebcc88b3e80a3b254412665340cd9f3bd6b5/Realm/Realm/Extensions/ReflectionExtensions.cs#L46
|
|
|
|
private static string getMappedOrOriginalName(MemberInfo member) => member.GetCustomAttribute<MapToAttribute>()?.Mapping ?? member.Name;
|
|
|
|
|
2021-10-15 04:58:14 +00:00
|
|
|
private bool isDisposed;
|
|
|
|
|
|
|
|
public void Dispose()
|
2021-06-28 07:14:14 +00:00
|
|
|
{
|
2021-09-30 18:46:53 +00:00
|
|
|
lock (contextLock)
|
|
|
|
{
|
|
|
|
context?.Dispose();
|
|
|
|
}
|
2021-09-30 14:42:40 +00:00
|
|
|
|
2021-10-15 04:58:14 +00:00
|
|
|
if (!isDisposed)
|
2021-06-28 07:14:14 +00:00
|
|
|
{
|
2021-09-30 16:32:28 +00:00
|
|
|
// intentionally block context creation indefinitely. this ensures that nothing can start consuming a new context after disposal.
|
|
|
|
contextCreationLock.Wait();
|
2021-09-30 14:42:40 +00:00
|
|
|
contextCreationLock.Dispose();
|
2021-06-28 07:14:14 +00:00
|
|
|
|
2021-10-15 04:58:14 +00:00
|
|
|
isDisposed = true;
|
|
|
|
}
|
2021-06-28 07:14:14 +00:00
|
|
|
}
|
2021-01-07 05:07:36 +00:00
|
|
|
}
|
|
|
|
}
|