using ZB.MOM.WW.OtOpcUa.Core.Abstractions.DataConnections; namespace ZB.MOM.WW.OtOpcUa.Runtime.DataConnections; /// Epoch-local non-reusable item identifiers and revision fences. internal sealed class SubscriptionItemHandleTable { private readonly object _gate = new(); private readonly string _subscription; private readonly long _epoch; private readonly int _capacity; private readonly Dictionary _items = new(); private readonly HashSet _clientIds = new(StringComparer.Ordinal); private readonly Dictionary _reserved = new(); private readonly Dictionary _replacementByOld = new(); private ulong _next; internal SubscriptionItemHandleTable(string subscriptionId, long epoch, int capacity) { ArgumentException.ThrowIfNullOrWhiteSpace(subscriptionId); if (epoch <= 0 || capacity <= 0) throw new ArgumentOutOfRangeException(nameof(epoch)); _subscription = subscriptionId; _epoch = epoch; _capacity = capacity; } internal DataItemHandle Add(string clientItemId) { ArgumentException.ThrowIfNullOrWhiteSpace(clientItemId); lock (_gate) { if (_items.Count >= _capacity || _clientIds.Contains(clientItemId)) throw new InvalidOperationException("Item capacity or ID conflict."); var id = checked(++_next); _clientIds.Add(clientItemId); _items.Add(id, (clientItemId, 1)); return new(_subscription, _epoch, id, 1); } } internal bool Remove(DataItemHandle handle) { lock (_gate) { if (!Current(handle) || !_items.Remove(handle.Handle, out var item)) return false; if (_replacementByOld.Remove(handle.Handle, out var reserved)) _reserved.Remove(reserved); _clientIds.Remove(item.ClientId); return true; } } internal bool IsCurrent(DataItemHandle handle) { lock (_gate) return Current(handle); } internal DataItemHandle ReserveReplacement(DataItemHandle previous) { lock (_gate) { if (!Current(previous) || _replacementByOld.ContainsKey(previous.Handle) || _reserved.Count >= _capacity) throw new InvalidOperationException("Item cannot be revised."); var id = checked(++_next); var item = _items[previous.Handle]; var revision = checked(item.Revision + 1); _reserved.Add(id, (previous, item.ClientId, revision)); _replacementByOld.Add(previous.Handle, id); return new(_subscription, _epoch, id, revision); } } internal bool CommitReplacement(DataItemHandle previous, DataItemHandle replacement) { lock (_gate) { if (!Current(previous) || replacement.SubscriptionId != _subscription || replacement.StreamEpoch != _epoch || !_reserved.TryGetValue(replacement.Handle, out var reserved) || reserved.Previous != previous || reserved.Revision != replacement.ItemRevision) return false; _reserved.Remove(replacement.Handle); _replacementByOld.Remove(previous.Handle); _items.Remove(previous.Handle); _items.Add(replacement.Handle, (reserved.ClientId, reserved.Revision)); return true; } } internal void CancelReplacement(DataItemHandle replacement) { lock (_gate) if (_reserved.Remove(replacement.Handle, out var reserved)) _replacementByOld.Remove(reserved.Previous.Handle); } private bool Current(DataItemHandle handle) => handle is not null && handle.SubscriptionId == _subscription && handle.StreamEpoch == _epoch && _items.TryGetValue(handle.Handle, out var item) && item.Revision == handle.ItemRevision; }