2021-09-30 14:46: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.
|
|
|
|
|
|
|
|
using System;
|
2022-01-17 09:32:57 +00:00
|
|
|
using System.Diagnostics;
|
2021-11-30 02:47:29 +00:00
|
|
|
using osu.Framework.Development;
|
2023-07-07 01:09:24 +00:00
|
|
|
using osu.Framework.Extensions.ObjectExtensions;
|
2022-01-26 03:42:24 +00:00
|
|
|
using osu.Framework.Statistics;
|
2021-09-30 14:46:16 +00:00
|
|
|
using Realms;
|
|
|
|
|
|
|
|
namespace osu.Game.Database
|
|
|
|
{
|
|
|
|
/// <summary>
|
|
|
|
/// Provides a method of working with realm objects over longer application lifetimes.
|
|
|
|
/// </summary>
|
|
|
|
/// <typeparam name="T">The underlying object type.</typeparam>
|
2022-01-26 04:37:33 +00:00
|
|
|
public class RealmLive<T> : Live<T> where T : RealmObject, IHasGuidPrimaryKey
|
2021-09-30 14:46:16 +00:00
|
|
|
{
|
2022-01-26 04:37:33 +00:00
|
|
|
public override bool IsManaged => data.IsManaged;
|
2021-11-26 05:39:35 +00:00
|
|
|
|
2021-09-30 14:46:16 +00:00
|
|
|
/// <summary>
|
|
|
|
/// The original live data used to create this instance.
|
|
|
|
/// </summary>
|
2022-01-17 09:32:57 +00:00
|
|
|
private T data;
|
|
|
|
|
|
|
|
private bool dataIsFromUpdateThread;
|
2021-09-30 14:46:16 +00:00
|
|
|
|
2022-01-24 10:59:58 +00:00
|
|
|
private readonly RealmAccess realm;
|
2021-12-14 05:21:23 +00:00
|
|
|
|
2021-09-30 14:46:16 +00:00
|
|
|
/// <summary>
|
|
|
|
/// Construct a new instance of live realm data.
|
|
|
|
/// </summary>
|
2023-08-16 05:38:31 +00:00
|
|
|
/// <param name="data">The realm data. Must be managed (see <see cref="IRealmObjectBase.IsManaged"/>).</param>
|
2022-01-24 10:59:58 +00:00
|
|
|
/// <param name="realm">The realm factory the data was sourced from. May be null for an unmanaged object.</param>
|
|
|
|
public RealmLive(T data, RealmAccess realm)
|
2022-01-26 04:37:33 +00:00
|
|
|
: base(data.ID)
|
2021-09-30 14:46:16 +00:00
|
|
|
{
|
|
|
|
this.data = data;
|
2022-01-24 10:59:58 +00:00
|
|
|
this.realm = realm;
|
2021-12-14 05:21:23 +00:00
|
|
|
|
2022-01-17 09:32:57 +00:00
|
|
|
dataIsFromUpdateThread = ThreadSafety.IsUpdateThread;
|
2021-09-30 14:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Perform a read operation on this live object.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="perform">The action to perform.</param>
|
2022-01-26 04:37:33 +00:00
|
|
|
public override void PerformRead(Action<T> perform)
|
2021-09-30 14:46:16 +00:00
|
|
|
{
|
2021-11-30 02:47:29 +00:00
|
|
|
if (!IsManaged)
|
2021-09-30 14:46:16 +00:00
|
|
|
{
|
|
|
|
perform(data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-17 09:32:57 +00:00
|
|
|
realm.Run(r =>
|
|
|
|
{
|
|
|
|
if (ThreadSafety.IsUpdateThread)
|
|
|
|
{
|
|
|
|
ensureDataIsFromUpdateThread();
|
|
|
|
perform(data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2023-08-16 06:36:31 +00:00
|
|
|
perform(r.FindWithRefresh<T>(ID)!);
|
2022-01-26 04:29:12 +00:00
|
|
|
RealmLiveStatistics.USAGE_ASYNC.Value++;
|
2022-01-17 09:32:57 +00:00
|
|
|
});
|
2021-09-30 14:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Perform a read operation on this live object.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="perform">The action to perform.</param>
|
2022-01-26 04:37:33 +00:00
|
|
|
public override TReturn PerformRead<TReturn>(Func<T, TReturn> perform)
|
2021-09-30 14:46:16 +00:00
|
|
|
{
|
2021-11-30 02:47:29 +00:00
|
|
|
if (!IsManaged)
|
2021-09-30 14:46:16 +00:00
|
|
|
return perform(data);
|
|
|
|
|
2022-01-17 09:32:57 +00:00
|
|
|
if (ThreadSafety.IsUpdateThread)
|
|
|
|
{
|
|
|
|
ensureDataIsFromUpdateThread();
|
|
|
|
return perform(data);
|
|
|
|
}
|
|
|
|
|
2022-01-25 04:04:05 +00:00
|
|
|
return realm.Run(r =>
|
2022-01-13 04:14:44 +00:00
|
|
|
{
|
2023-08-16 06:36:31 +00:00
|
|
|
var returnData = perform(r.FindWithRefresh<T>(ID)!);
|
2022-01-26 04:29:12 +00:00
|
|
|
RealmLiveStatistics.USAGE_ASYNC.Value++;
|
2022-01-13 04:14:44 +00:00
|
|
|
|
|
|
|
if (returnData is RealmObjectBase realmObject && realmObject.IsManaged)
|
|
|
|
throw new InvalidOperationException(@$"Managed realm objects should not exit the scope of {nameof(PerformRead)}.");
|
|
|
|
|
|
|
|
return returnData;
|
2022-01-21 08:08:20 +00:00
|
|
|
});
|
2021-09-30 14:46:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Perform a write operation on this live object.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="perform">The action to perform.</param>
|
2022-01-26 04:37:33 +00:00
|
|
|
public override void PerformWrite(Action<T> perform)
|
2021-11-26 05:39:35 +00:00
|
|
|
{
|
|
|
|
if (!IsManaged)
|
2021-12-14 05:21:23 +00:00
|
|
|
throw new InvalidOperationException(@"Can't perform writes on a non-managed underlying value");
|
2021-11-26 05:39:35 +00:00
|
|
|
|
2021-09-30 14:46:16 +00:00
|
|
|
PerformRead(t =>
|
|
|
|
{
|
2023-07-06 04:37:42 +00:00
|
|
|
using (var transaction = t.Realm!.BeginWrite())
|
2022-06-30 06:11:22 +00:00
|
|
|
{
|
|
|
|
perform(t);
|
|
|
|
transaction.Commit();
|
|
|
|
}
|
|
|
|
|
2022-01-26 04:29:12 +00:00
|
|
|
RealmLiveStatistics.WRITES.Value++;
|
2021-09-30 14:46:16 +00:00
|
|
|
});
|
2021-11-26 05:39:35 +00:00
|
|
|
}
|
2021-09-30 14:46:16 +00:00
|
|
|
|
2022-01-26 04:37:33 +00:00
|
|
|
public override T Value
|
2021-09-30 14:46:16 +00:00
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2021-11-30 02:47:29 +00:00
|
|
|
if (!IsManaged)
|
2021-09-30 14:46:16 +00:00
|
|
|
return data;
|
|
|
|
|
2021-11-30 02:47:29 +00:00
|
|
|
if (!ThreadSafety.IsUpdateThread)
|
|
|
|
throw new InvalidOperationException($"Can't use {nameof(Value)} on managed objects from non-update threads");
|
2021-09-30 14:46:16 +00:00
|
|
|
|
2022-01-17 09:32:57 +00:00
|
|
|
ensureDataIsFromUpdateThread();
|
|
|
|
return data;
|
2021-09-30 14:46:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-17 09:32:57 +00:00
|
|
|
private void ensureDataIsFromUpdateThread()
|
|
|
|
{
|
|
|
|
Debug.Assert(ThreadSafety.IsUpdateThread);
|
|
|
|
|
2023-07-07 01:09:24 +00:00
|
|
|
if (dataIsFromUpdateThread && !data.Realm.AsNonNull().IsClosed)
|
2022-01-26 03:42:24 +00:00
|
|
|
{
|
2022-01-26 04:29:12 +00:00
|
|
|
RealmLiveStatistics.USAGE_UPDATE_IMMEDIATE.Value++;
|
2022-01-17 09:32:57 +00:00
|
|
|
return;
|
2022-01-26 03:42:24 +00:00
|
|
|
}
|
2022-01-17 09:32:57 +00:00
|
|
|
|
|
|
|
dataIsFromUpdateThread = true;
|
2023-08-16 06:36:31 +00:00
|
|
|
data = realm.Realm.FindWithRefresh<T>(ID)!;
|
2022-01-21 08:33:03 +00:00
|
|
|
|
2023-08-16 06:36:31 +00:00
|
|
|
RealmLiveStatistics.USAGE_UPDATE_REFETCH.Value++;
|
2022-01-21 08:33:03 +00:00
|
|
|
}
|
2021-09-30 14:46:16 +00:00
|
|
|
}
|
2022-01-26 03:42:24 +00:00
|
|
|
|
2022-01-26 04:29:12 +00:00
|
|
|
internal static class RealmLiveStatistics
|
2022-01-26 03:42:24 +00:00
|
|
|
{
|
2022-01-26 04:29:12 +00:00
|
|
|
public static readonly GlobalStatistic<int> WRITES = GlobalStatistics.Get<int>(@"Realm", @"Live writes");
|
|
|
|
public static readonly GlobalStatistic<int> USAGE_UPDATE_IMMEDIATE = GlobalStatistics.Get<int>(@"Realm", @"Live update read (fast)");
|
|
|
|
public static readonly GlobalStatistic<int> USAGE_UPDATE_REFETCH = GlobalStatistics.Get<int>(@"Realm", @"Live update read (slow)");
|
|
|
|
public static readonly GlobalStatistic<int> USAGE_ASYNC = GlobalStatistics.Get<int>(@"Realm", @"Live async read");
|
2022-01-26 03:42:24 +00:00
|
|
|
}
|
2021-09-30 14:46:16 +00:00
|
|
|
}
|