| | | 1 | | using MemoryPack; |
| | | 2 | | using System; |
| | | 3 | | using System.Collections.Generic; |
| | | 4 | | |
| | | 5 | | namespace Chronicler; |
| | | 6 | | |
| | | 7 | | /// <summary> |
| | | 8 | | /// Stores named field payloads for the MemoryPack chronicler transport. |
| | | 9 | | /// </summary> |
| | | 10 | | [MemoryPackable] |
| | | 11 | | internal sealed partial class MemoryPackRecordEnvelope |
| | | 12 | | { |
| | | 13 | | /// <summary> |
| | | 14 | | /// Serialized payloads keyed by record name. |
| | | 15 | | /// </summary> |
| | | 16 | | public MemoryPackRecordEntryTable? Entries { get; set; } = new(); |
| | | 17 | | |
| | | 18 | | internal static MemoryPackRecordEnvelope FromEntries(OrderedStringMap<byte[]?> entries) |
| | | 19 | | { |
| | | 20 | | return new MemoryPackRecordEnvelope |
| | | 21 | | { |
| | | 22 | | Entries = new MemoryPackRecordEntryTable(entries) |
| | | 23 | | }; |
| | | 24 | | } |
| | | 25 | | |
| | | 26 | | internal OrderedStringMap<byte[]?> ToEntryMap() |
| | | 27 | | { |
| | | 28 | | return Entries?.ToEntryMap() ?? new OrderedStringMap<byte[]?>(8, StringComparer.Ordinal); |
| | | 29 | | } |
| | | 30 | | |
| | | 31 | | internal bool RemoveEntry(string name) |
| | | 32 | | { |
| | | 33 | | return Entries?.Remove(name) ?? false; |
| | | 34 | | } |
| | | 35 | | |
| | | 36 | | internal void SetEntry(string name, byte[]? payload) |
| | | 37 | | { |
| | | 38 | | Entries ??= new MemoryPackRecordEntryTable(); |
| | | 39 | | Entries[name] = payload; |
| | | 40 | | } |
| | | 41 | | |
| | | 42 | | internal bool TryGetEntry(string name, out byte[]? payload) |
| | | 43 | | { |
| | | 44 | | if (Entries == null) |
| | | 45 | | { |
| | | 46 | | payload = null; |
| | | 47 | | return false; |
| | | 48 | | } |
| | | 49 | | |
| | | 50 | | return Entries.TryGetValue(name, out payload); |
| | | 51 | | } |
| | | 52 | | } |
| | | 53 | | |
| | | 54 | | /// <summary> |
| | | 55 | | /// Stores ordered MemoryPack entry payloads while preserving the previous SwiftDictionary wire shape. |
| | | 56 | | /// </summary> |
| | | 57 | | [MemoryPackable] |
| | | 58 | | internal sealed partial class MemoryPackRecordEntryTable |
| | | 59 | | { |
| | | 60 | | private OrderedStringMap<byte[]?> _entries; |
| | | 61 | | |
| | | 62 | | public MemoryPackRecordEntryTable() |
| | | 63 | | { |
| | | 64 | | _entries = new OrderedStringMap<byte[]?>(8, StringComparer.Ordinal); |
| | | 65 | | } |
| | | 66 | | |
| | | 67 | | internal MemoryPackRecordEntryTable(OrderedStringMap<byte[]?> entries) |
| | | 68 | | { |
| | | 69 | | _entries = entries ?? throw new ArgumentNullException(nameof(entries)); |
| | | 70 | | } |
| | | 71 | | |
| | | 72 | | [MemoryPackConstructor] |
| | | 73 | | public MemoryPackRecordEntryTable(MemoryPackRecordEntryTableState state) |
| | | 74 | | : this() |
| | | 75 | | { |
| | | 76 | | LoadState(state); |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | /// <summary> |
| | | 80 | | /// Gets or sets the serializable table state. |
| | | 81 | | /// </summary> |
| | | 82 | | [MemoryPackInclude] |
| | | 83 | | public MemoryPackRecordEntryTableState State |
| | | 84 | | { |
| | | 85 | | get |
| | | 86 | | { |
| | | 87 | | var items = new KeyValuePair<string, byte[]?>[_entries.Count]; |
| | | 88 | | int index = 0; |
| | | 89 | | |
| | | 90 | | foreach (KeyValuePair<string, byte[]?> entry in _entries) |
| | | 91 | | items[index++] = entry; |
| | | 92 | | |
| | | 93 | | return new MemoryPackRecordEntryTableState(items); |
| | | 94 | | } |
| | | 95 | | internal set => LoadState(value); |
| | | 96 | | } |
| | | 97 | | |
| | | 98 | | internal byte[]? this[string name] |
| | | 99 | | { |
| | | 100 | | set => _entries[name] = value; |
| | | 101 | | } |
| | | 102 | | |
| | | 103 | | internal bool Remove(string name) |
| | | 104 | | { |
| | | 105 | | return _entries.Remove(name); |
| | | 106 | | } |
| | | 107 | | |
| | | 108 | | internal OrderedStringMap<byte[]?> ToEntryMap() |
| | | 109 | | { |
| | | 110 | | var entries = new OrderedStringMap<byte[]?>(_entries.Count, StringComparer.Ordinal); |
| | | 111 | | |
| | | 112 | | foreach (KeyValuePair<string, byte[]?> entry in _entries) |
| | | 113 | | entries[entry.Key] = entry.Value; |
| | | 114 | | |
| | | 115 | | return entries; |
| | | 116 | | } |
| | | 117 | | |
| | | 118 | | internal bool TryGetValue(string name, out byte[]? payload) |
| | | 119 | | { |
| | | 120 | | return _entries.TryGetValue(name, out payload); |
| | | 121 | | } |
| | | 122 | | |
| | | 123 | | private void LoadState(MemoryPackRecordEntryTableState state) |
| | | 124 | | { |
| | | 125 | | KeyValuePair<string, byte[]?>[]? items = state.Items; |
| | | 126 | | int capacity = items?.Length ?? 8; |
| | | 127 | | var entries = new OrderedStringMap<byte[]?>(capacity, StringComparer.Ordinal); |
| | | 128 | | |
| | | 129 | | if (items != null) |
| | | 130 | | { |
| | | 131 | | foreach (KeyValuePair<string, byte[]?> item in items) |
| | | 132 | | entries[item.Key] = item.Value; |
| | | 133 | | } |
| | | 134 | | |
| | | 135 | | _entries = entries; |
| | | 136 | | } |
| | | 137 | | } |
| | | 138 | | |
| | | 139 | | /// <summary> |
| | | 140 | | /// Represents the serializable state of a MemoryPack record entry table. |
| | | 141 | | /// </summary> |
| | | 142 | | [MemoryPackable] |
| | | 143 | | internal readonly partial struct MemoryPackRecordEntryTableState |
| | | 144 | | { |
| | | 145 | | /// <summary> |
| | | 146 | | /// Gets the ordered entry items. |
| | | 147 | | /// </summary> |
| | | 148 | | [MemoryPackInclude] |
| | | 149 | | public readonly KeyValuePair<string, byte[]?>[]? Items; |
| | | 150 | | |
| | | 151 | | [MemoryPackConstructor] |
| | | 152 | | public MemoryPackRecordEntryTableState(KeyValuePair<string, byte[]?>[]? items) |
| | | 153 | | { |
| | 112 | 154 | | Items = items; |
| | 112 | 155 | | } |
| | | 156 | | } |