| | | 1 | | //======================================================================= |
| | | 2 | | // MemoryPackRecordEntryTable.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 | | #if !CHRONICLER_DISABLE_MEMORYPACK |
| | | 9 | | using System; |
| | | 10 | | using System.Collections.Generic; |
| | | 11 | | using MemoryPack; |
| | | 12 | | |
| | | 13 | | namespace Chronicler.Serialization; |
| | | 14 | | |
| | | 15 | | /// <summary> |
| | | 16 | | /// Stores ordered MemoryPack entry payloads while preserving the previous SwiftDictionary wire shape. |
| | | 17 | | /// </summary> |
| | | 18 | | [MemoryPackable] |
| | | 19 | | internal sealed partial class MemoryPackRecordEntryTable |
| | | 20 | | { |
| | | 21 | | private OrderedStringMap<byte[]?> _entries; |
| | | 22 | | |
| | 830 | 23 | | public MemoryPackRecordEntryTable() |
| | | 24 | | { |
| | 830 | 25 | | _entries = new OrderedStringMap<byte[]?>(8, StringComparer.Ordinal); |
| | 830 | 26 | | } |
| | | 27 | | |
| | 186 | 28 | | internal MemoryPackRecordEntryTable(OrderedStringMap<byte[]?> entries) |
| | | 29 | | { |
| | 186 | 30 | | _entries = entries; |
| | 186 | 31 | | } |
| | | 32 | | |
| | | 33 | | [MemoryPackConstructor] |
| | | 34 | | public MemoryPackRecordEntryTable(MemoryPackRecordEntryTableState state) |
| | 320 | 35 | | : this() |
| | | 36 | | { |
| | 320 | 37 | | LoadState(state); |
| | 320 | 38 | | } |
| | | 39 | | |
| | | 40 | | /// <summary> |
| | | 41 | | /// Gets or sets the serializable table state. |
| | | 42 | | /// </summary> |
| | | 43 | | [MemoryPackInclude] |
| | | 44 | | public MemoryPackRecordEntryTableState State |
| | | 45 | | { |
| | | 46 | | get |
| | | 47 | | { |
| | 294 | 48 | | var items = new KeyValuePair<string, byte[]?>[_entries.Count]; |
| | 294 | 49 | | int index = 0; |
| | | 50 | | |
| | 1676 | 51 | | foreach (KeyValuePair<string, byte[]?> entry in _entries) |
| | 544 | 52 | | items[index++] = entry; |
| | | 53 | | |
| | 294 | 54 | | return new MemoryPackRecordEntryTableState(items); |
| | | 55 | | } |
| | 3 | 56 | | internal set => LoadState(value); |
| | | 57 | | } |
| | | 58 | | |
| | | 59 | | internal byte[]? this[string name] |
| | | 60 | | { |
| | 66 | 61 | | set => _entries[name] = value; |
| | | 62 | | } |
| | | 63 | | |
| | | 64 | | internal bool Remove(string name) |
| | | 65 | | { |
| | 39 | 66 | | return _entries.Remove(name); |
| | | 67 | | } |
| | | 68 | | |
| | | 69 | | internal OrderedStringMap<byte[]?> ToEntryMap() |
| | | 70 | | { |
| | 215 | 71 | | var entries = new OrderedStringMap<byte[]?>(_entries.Count, StringComparer.Ordinal); |
| | | 72 | | |
| | 1152 | 73 | | foreach (KeyValuePair<string, byte[]?> entry in _entries) |
| | 361 | 74 | | entries[entry.Key] = entry.Value; |
| | | 75 | | |
| | 215 | 76 | | return entries; |
| | | 77 | | } |
| | | 78 | | |
| | | 79 | | internal bool TryGetValue(string name, out byte[]? payload) |
| | | 80 | | { |
| | 47 | 81 | | return _entries.TryGetValue(name, out payload); |
| | | 82 | | } |
| | | 83 | | |
| | | 84 | | private void LoadState(MemoryPackRecordEntryTableState state) |
| | | 85 | | { |
| | 323 | 86 | | KeyValuePair<string, byte[]?>[]? items = state.Items; |
| | 323 | 87 | | int capacity = items?.Length ?? 8; |
| | 323 | 88 | | var entries = new OrderedStringMap<byte[]?>(capacity, StringComparer.Ordinal); |
| | | 89 | | |
| | 323 | 90 | | if (items != null) |
| | | 91 | | { |
| | 1768 | 92 | | foreach (KeyValuePair<string, byte[]?> item in items) |
| | 563 | 93 | | entries[item.Key] = item.Value; |
| | | 94 | | } |
| | | 95 | | |
| | 323 | 96 | | _entries = entries; |
| | 323 | 97 | | } |
| | | 98 | | } |
| | | 99 | | #endif |