2021-09-30 22:46: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.
|
|
|
|
|
|
|
|
using System;
|
2021-11-30 10:47:29 +08:00
|
|
|
using osu.Framework.Development;
|
2021-09-30 22:46:16 +08:00
|
|
|
using Realms;
|
|
|
|
|
|
|
|
#nullable enable
|
|
|
|
|
|
|
|
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>
|
|
|
|
public class RealmLive<T> : ILive<T> where T : RealmObject, IHasGuidPrimaryKey
|
|
|
|
{
|
|
|
|
public Guid ID { get; }
|
|
|
|
|
2021-11-29 16:21:51 +08:00
|
|
|
public bool IsManaged => data.IsManaged;
|
2021-11-26 13:39:35 +08:00
|
|
|
|
2021-09-30 22:46:16 +08:00
|
|
|
/// <summary>
|
|
|
|
/// The original live data used to create this instance.
|
|
|
|
/// </summary>
|
|
|
|
private readonly T data;
|
|
|
|
|
2021-12-16 14:11:48 +08:00
|
|
|
private readonly RealmContextFactory realmFactory;
|
2021-12-14 13:21:23 +08:00
|
|
|
|
2021-09-30 22:46:16 +08:00
|
|
|
/// <summary>
|
|
|
|
/// Construct a new instance of live realm data.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="data">The realm data.</param>
|
2021-12-14 13:21:23 +08:00
|
|
|
/// <param name="realmFactory">The realm factory the data was sourced from. May be null for an unmanaged object.</param>
|
2021-12-16 14:11:48 +08:00
|
|
|
public RealmLive(T data, RealmContextFactory realmFactory)
|
2021-09-30 22:46:16 +08:00
|
|
|
{
|
|
|
|
this.data = data;
|
2021-12-14 13:21:23 +08:00
|
|
|
this.realmFactory = realmFactory;
|
|
|
|
|
2021-09-30 22:46:16 +08:00
|
|
|
ID = data.ID;
|
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Perform a read operation on this live object.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="perform">The action to perform.</param>
|
|
|
|
public void PerformRead(Action<T> perform)
|
|
|
|
{
|
2021-11-30 10:47:29 +08:00
|
|
|
if (!IsManaged)
|
2021-09-30 22:46:16 +08:00
|
|
|
{
|
|
|
|
perform(data);
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2022-01-21 16:27:30 +08:00
|
|
|
realmFactory.Run(realm =>
|
|
|
|
{
|
2022-01-21 16:33:03 +08:00
|
|
|
perform(retrieveFromID(realm, ID));
|
2022-01-21 16:27:30 +08:00
|
|
|
});
|
2021-09-30 22:46:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Perform a read operation on this live object.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="perform">The action to perform.</param>
|
|
|
|
public TReturn PerformRead<TReturn>(Func<T, TReturn> perform)
|
|
|
|
{
|
2021-11-30 10:47:29 +08:00
|
|
|
if (!IsManaged)
|
2021-09-30 22:46:16 +08:00
|
|
|
return perform(data);
|
|
|
|
|
2022-01-21 16:08:20 +08:00
|
|
|
return realmFactory.Run(realm =>
|
2022-01-13 12:14:44 +08:00
|
|
|
{
|
2022-01-21 16:33:03 +08:00
|
|
|
var returnData = perform(retrieveFromID(realm, ID));
|
2022-01-13 12:14:44 +08: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 16:08:20 +08:00
|
|
|
});
|
2021-09-30 22:46:16 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
/// Perform a write operation on this live object.
|
|
|
|
/// </summary>
|
|
|
|
/// <param name="perform">The action to perform.</param>
|
2021-11-26 13:39:35 +08:00
|
|
|
public void PerformWrite(Action<T> perform)
|
|
|
|
{
|
|
|
|
if (!IsManaged)
|
2021-12-14 13:21:23 +08:00
|
|
|
throw new InvalidOperationException(@"Can't perform writes on a non-managed underlying value");
|
2021-11-26 13:39:35 +08:00
|
|
|
|
2021-09-30 22:46:16 +08:00
|
|
|
PerformRead(t =>
|
|
|
|
{
|
|
|
|
var transaction = t.Realm.BeginWrite();
|
|
|
|
perform(t);
|
|
|
|
transaction.Commit();
|
|
|
|
});
|
2021-11-26 13:39:35 +08:00
|
|
|
}
|
2021-09-30 22:46:16 +08:00
|
|
|
|
|
|
|
public T Value
|
|
|
|
{
|
|
|
|
get
|
|
|
|
{
|
2021-11-30 10:47:29 +08:00
|
|
|
if (!IsManaged)
|
2021-09-30 22:46:16 +08:00
|
|
|
return data;
|
|
|
|
|
2021-11-30 10:47:29 +08:00
|
|
|
if (!ThreadSafety.IsUpdateThread)
|
|
|
|
throw new InvalidOperationException($"Can't use {nameof(Value)} on managed objects from non-update threads");
|
2021-09-30 22:46:16 +08:00
|
|
|
|
2021-12-16 14:11:48 +08:00
|
|
|
return realmFactory.Context.Find<T>(ID);
|
2021-09-30 22:46:16 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-21 16:33:03 +08:00
|
|
|
private T retrieveFromID(Realm realm, Guid id)
|
|
|
|
{
|
|
|
|
var found = realm.Find<T>(ID);
|
|
|
|
|
|
|
|
if (found == null)
|
|
|
|
{
|
|
|
|
// It may be that we access this from the update thread before a refresh has taken place.
|
|
|
|
// To ensure that behaviour matches what we'd expect (the object *is* available), force
|
|
|
|
// a refresh to bring in any off-thread changes immediately.
|
|
|
|
realm.Refresh();
|
|
|
|
found = realm.Find<T>(ID);
|
|
|
|
}
|
|
|
|
|
|
|
|
return found;
|
|
|
|
}
|
|
|
|
|
2021-11-26 13:38:39 +08:00
|
|
|
public bool Equals(ILive<T>? other) => ID == other?.ID;
|
2021-11-29 17:00:03 +08:00
|
|
|
|
|
|
|
public override string ToString() => PerformRead(i => i.ToString());
|
2021-09-30 22:46:16 +08:00
|
|
|
}
|
|
|
|
}
|