| | | 1 | | //======================================================================= |
| | | 2 | | // ChronicleLinkRegistry.cs |
| | | 3 | | //======================================================================= |
| | | 4 | | // MIT License, Copyright (c) 2024-present David Oravsky (mrdav30) |
| | | 5 | | // See LICENSE file in the project root for full license information. |
| | | 6 | | //======================================================================= |
| | | 7 | | |
| | | 8 | | using System; |
| | | 9 | | using System.Collections.Generic; |
| | | 10 | | using System.Diagnostics.CodeAnalysis; |
| | | 11 | | using Chronicler.Serialization; |
| | | 12 | | |
| | | 13 | | namespace Chronicler; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Maps stable IDs to host-owned values within a recording session. |
| | | 17 | | /// </summary> |
| | | 18 | | /// <remarks> |
| | | 19 | | /// Each lookup uses the declared link type and slot. Slot names are case-sensitive. |
| | | 20 | | /// Null and empty slots select the same default slot. |
| | | 21 | | /// </remarks> |
| | | 22 | | public sealed class ChronicleLinkRegistry |
| | | 23 | | { |
| | 622 | 24 | | private readonly Dictionary<ChronicleLinkKey, object> _resolvers = new(); |
| | 622 | 25 | | private readonly Dictionary<ChronicleLinkKey, object> _registeredInstances = new(); |
| | | 26 | | |
| | | 27 | | /// <summary> |
| | | 28 | | /// Sets the custom resolver for a link type and slot. |
| | | 29 | | /// </summary> |
| | | 30 | | /// <typeparam name="T">The declared type used by the recording schema.</typeparam> |
| | | 31 | | /// <param name="resolver">The resolver to retain and consult before instance registrations.</param> |
| | | 32 | | /// <param name="slot">An optional identity domain. Null or empty selects the default slot.</param> |
| | | 33 | | /// <remarks>Replaces any previous resolver in that type and slot.</remarks> |
| | | 34 | | /// <exception cref="ArgumentNullException"><paramref name="resolver"/> is null.</exception> |
| | | 35 | | public void RegisterResolver<T>(IRecordLinkResolver<T> resolver, string? slot = null) |
| | | 36 | | { |
| | 13 | 37 | | _resolvers[new ChronicleLinkKey(typeof(T), slot)] = resolver ?? throw new ArgumentNullException(nameof(resolver) |
| | 12 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Associates a value with a stable ID. |
| | | 42 | | /// </summary> |
| | | 43 | | /// <typeparam name="T">The declared type used by the recording schema.</typeparam> |
| | | 44 | | /// <param name="id">A case-sensitive ID containing at least one non-whitespace character.</param> |
| | | 45 | | /// <param name="value">The value to retain. For reference types, the registry keeps the instance.</param> |
| | | 46 | | /// <param name="slot">An optional identity domain. Null or empty selects the default slot.</param> |
| | | 47 | | /// <remarks>Registering the same ID for the same type and slot replaces its value in place.</remarks> |
| | | 48 | | /// <exception cref="ArgumentException"><paramref name="id"/> is null, empty, or whitespace.</exception> |
| | | 49 | | public void RegisterInstance<T>(string id, T value, string? slot = null) |
| | | 50 | | { |
| | 34 | 51 | | if (string.IsNullOrWhiteSpace(id)) |
| | 3 | 52 | | throw new ArgumentException("A registered link id must not be null or empty.", nameof(id)); |
| | | 53 | | |
| | 31 | 54 | | RegisteredLinkTable<T> table = GetOrCreateInstanceTable<T>(slot); |
| | 31 | 55 | | table.Register(id, value); |
| | 31 | 56 | | } |
| | | 57 | | |
| | | 58 | | /// <summary> |
| | | 59 | | /// Removes an instance registration. |
| | | 60 | | /// </summary> |
| | | 61 | | /// <typeparam name="T">The declared type used to register the value.</typeparam> |
| | | 62 | | /// <param name="id">The case-sensitive ID of the registration.</param> |
| | | 63 | | /// <param name="slot">The registration's identity domain. Null or empty selects the default slot.</param> |
| | | 64 | | /// <returns> |
| | | 65 | | /// <see langword="true"/> if the registration was removed. Returns <see langword="false"/> for a blank ID |
| | | 66 | | /// or when the type and slot contain no registration for it. |
| | | 67 | | /// </returns> |
| | | 68 | | /// <remarks>The custom resolver for the same type and slot remains available.</remarks> |
| | | 69 | | public bool UnregisterInstance<T>(string id, string? slot = null) |
| | | 70 | | { |
| | 6 | 71 | | if (string.IsNullOrWhiteSpace(id)) |
| | 3 | 72 | | return false; |
| | | 73 | | |
| | 3 | 74 | | ChronicleLinkKey key = new(typeof(T), slot); |
| | 3 | 75 | | if (!_registeredInstances.TryGetValue(key, out object? tableObject)) |
| | 1 | 76 | | return false; |
| | | 77 | | |
| | 2 | 78 | | return ((RegisteredLinkTable<T>)tableObject!).Unregister(id); |
| | | 79 | | } |
| | | 80 | | |
| | | 81 | | /// <summary> |
| | | 82 | | /// Looks up the value for a recorded ID. |
| | | 83 | | /// </summary> |
| | | 84 | | /// <typeparam name="T">The declared link type to look up.</typeparam> |
| | | 85 | | /// <param name="id">The non-null recorded ID. Instance registrations use case-sensitive matching.</param> |
| | | 86 | | /// <param name="value">The resolved value on success. Otherwise, the default value of <typeparamref name="T"/>.</pa |
| | | 87 | | /// <param name="slot">The recorded link's identity domain. Null or empty selects the default slot.</param> |
| | | 88 | | /// <returns><see langword="true"/> if the resolver or an instance registration supplied a value.</returns> |
| | | 89 | | /// <remarks>Consults the custom resolver first and falls back to instance registrations on a miss.</remarks> |
| | | 90 | | /// <exception cref="ArgumentNullException"><paramref name="id"/> is null when an instance table is consulted.</exce |
| | | 91 | | public bool TryResolve<T>(string id, [MaybeNullWhen(false)] out T value, string? slot = null) |
| | | 92 | | { |
| | 24 | 93 | | ChronicleLinkKey key = new(typeof(T), slot); |
| | 24 | 94 | | if (_resolvers.TryGetValue(key, out object? resolverObject) |
| | 24 | 95 | | && ((IRecordLinkResolver<T>)resolverObject!).TryResolveReference(id, out value)) |
| | | 96 | | { |
| | 4 | 97 | | return true; |
| | | 98 | | } |
| | | 99 | | |
| | 20 | 100 | | if (_registeredInstances.TryGetValue(key, out object? tableObject) |
| | 20 | 101 | | && ((RegisteredLinkTable<T>)tableObject!).TryResolve(id, out value)) |
| | | 102 | | { |
| | 7 | 103 | | return true; |
| | | 104 | | } |
| | | 105 | | |
| | 13 | 106 | | value = default!; |
| | 13 | 107 | | return false; |
| | | 108 | | } |
| | | 109 | | |
| | | 110 | | /// <summary> |
| | | 111 | | /// Looks up the stable ID for a value. |
| | | 112 | | /// </summary> |
| | | 113 | | /// <typeparam name="T">The declared link type to look up.</typeparam> |
| | | 114 | | /// <param name="value">The value to identify in this session.</param> |
| | | 115 | | /// <param name="id">The stable ID on success. Otherwise, null.</param> |
| | | 116 | | /// <param name="slot">The link's identity domain. Null or empty selects the default slot.</param> |
| | | 117 | | /// <returns><see langword="true"/> if the resolver or an instance registration supplied an ID.</returns> |
| | | 118 | | /// <remarks> |
| | | 119 | | /// Consults the custom resolver first. Instance lookup uses reference identity for reference types |
| | | 120 | | /// and default equality for value types. If several registrations match, returns the first in registration order. |
| | | 121 | | /// </remarks> |
| | | 122 | | public bool TryGetReferenceId<T>(T value, [NotNullWhen(true)] out string? id, string? slot = null) |
| | | 123 | | { |
| | 39 | 124 | | ChronicleLinkKey key = new(typeof(T), slot); |
| | 39 | 125 | | if (_resolvers.TryGetValue(key, out object? resolverObject) |
| | 39 | 126 | | && ((IRecordLinkResolver<T>)resolverObject!).TryGetReferenceId(value, out id)) |
| | | 127 | | { |
| | 8 | 128 | | return true; |
| | | 129 | | } |
| | | 130 | | |
| | 31 | 131 | | if (_registeredInstances.TryGetValue(key, out object? tableObject) |
| | 31 | 132 | | && ((RegisteredLinkTable<T>)tableObject!).TryGetReferenceId(value, out id)) |
| | | 133 | | { |
| | 24 | 134 | | return true; |
| | | 135 | | } |
| | | 136 | | |
| | 7 | 137 | | id = null; |
| | 7 | 138 | | return false; |
| | | 139 | | } |
| | | 140 | | |
| | | 141 | | private RegisteredLinkTable<T> GetOrCreateInstanceTable<T>(string? slot) |
| | | 142 | | { |
| | 31 | 143 | | ChronicleLinkKey key = new(typeof(T), slot); |
| | 31 | 144 | | if (_registeredInstances.TryGetValue(key, out object? tableObject)) |
| | 2 | 145 | | return (RegisteredLinkTable<T>)tableObject!; |
| | | 146 | | |
| | 29 | 147 | | var table = new RegisteredLinkTable<T>(); |
| | 29 | 148 | | _registeredInstances[key] = table; |
| | 29 | 149 | | return table; |
| | | 150 | | } |
| | | 151 | | |
| | | 152 | | private readonly struct ChronicleLinkKey : IEquatable<ChronicleLinkKey> |
| | | 153 | | { |
| | | 154 | | private readonly Type _type; |
| | | 155 | | private readonly string _slot; |
| | | 156 | | |
| | | 157 | | public ChronicleLinkKey(Type type, string? slot) |
| | | 158 | | { |
| | 116 | 159 | | _type = type ?? throw new ArgumentNullException(nameof(type)); |
| | 115 | 160 | | _slot = slot ?? string.Empty; |
| | 115 | 161 | | } |
| | | 162 | | |
| | | 163 | | public bool Equals(ChronicleLinkKey other) |
| | | 164 | | { |
| | 51 | 165 | | return _type == other._type |
| | 51 | 166 | | && string.Equals(_slot, other._slot, StringComparison.Ordinal); |
| | | 167 | | } |
| | | 168 | | |
| | 2 | 169 | | public override bool Equals(object? obj) => obj is ChronicleLinkKey other && Equals(other); |
| | | 170 | | |
| | | 171 | | public override int GetHashCode() |
| | | 172 | | { |
| | | 173 | | unchecked |
| | | 174 | | { |
| | 90 | 175 | | return (_type.GetHashCode() * 397) ^ _slot.GetHashCode(); |
| | | 176 | | } |
| | | 177 | | } |
| | | 178 | | } |
| | | 179 | | |
| | | 180 | | private sealed class RegisteredLinkTable<T> |
| | | 181 | | { |
| | 29 | 182 | | private readonly OrderedStringMap<T> _byId = new(8, StringComparer.Ordinal); |
| | | 183 | | |
| | | 184 | | public void Register(string id, T value) |
| | | 185 | | { |
| | 31 | 186 | | _byId[id] = value; |
| | 31 | 187 | | } |
| | | 188 | | |
| | | 189 | | public bool Unregister(string id) |
| | | 190 | | { |
| | 2 | 191 | | return _byId.Remove(id); |
| | | 192 | | } |
| | | 193 | | |
| | | 194 | | public bool TryResolve(string id, [MaybeNullWhen(false)] out T value) |
| | | 195 | | { |
| | 7 | 196 | | return _byId.TryGetValue(id, out value); |
| | | 197 | | } |
| | | 198 | | |
| | | 199 | | public bool TryGetReferenceId(T value, [NotNullWhen(true)] out string? id) |
| | | 200 | | { |
| | 78 | 201 | | foreach (KeyValuePair<string, T> pair in _byId) |
| | | 202 | | { |
| | 26 | 203 | | if (!ValuesMatch(pair.Value, value)) |
| | | 204 | | continue; |
| | | 205 | | |
| | 24 | 206 | | id = pair.Key; |
| | 24 | 207 | | return true; |
| | | 208 | | } |
| | | 209 | | |
| | 1 | 210 | | id = null; |
| | 1 | 211 | | return false; |
| | 24 | 212 | | } |
| | | 213 | | |
| | | 214 | | private static bool ValuesMatch(T left, T right) |
| | | 215 | | { |
| | 26 | 216 | | if (typeof(T).IsValueType) |
| | 2 | 217 | | return EqualityComparer<T>.Default.Equals(left, right); |
| | | 218 | | |
| | 24 | 219 | | return ReferenceEquals(left, right); |
| | | 220 | | } |
| | | 221 | | } |
| | | 222 | | } |