|
| 1 | +using System.Buffers; |
| 2 | + |
| 3 | +namespace RocksDb.Extensions; |
| 4 | + |
| 5 | +/// <summary> |
| 6 | +/// Base class for serializing collections containing variable-size elements like strings or complex objects |
| 7 | +/// where each element may occupy a different number of bytes when serialized. |
| 8 | +/// </summary> |
| 9 | +/// <remarks> |
| 10 | +/// The serialized format consists of: |
| 11 | +/// - 4 bytes: Collection length (number of elements) |
| 12 | +/// - For each element: |
| 13 | +/// - 4 bytes: Size of the serialized element |
| 14 | +/// - N bytes: Serialized element data |
| 15 | +/// </remarks> |
| 16 | +/// <typeparam name="TCollection">The collection type (e.g., IList{T}, ISet{T})</typeparam> |
| 17 | +/// <typeparam name="TElement">The element type</typeparam> |
| 18 | +internal abstract class VariableSizeCollectionSerializer<TCollection, TElement> : ISerializer<TCollection> |
| 19 | + where TCollection : ICollection<TElement> |
| 20 | +{ |
| 21 | + private readonly ISerializer<TElement> _scalarSerializer; |
| 22 | + |
| 23 | + protected VariableSizeCollectionSerializer(ISerializer<TElement> scalarSerializer) |
| 24 | + { |
| 25 | + _scalarSerializer = scalarSerializer; |
| 26 | + } |
| 27 | + |
| 28 | + /// <summary> |
| 29 | + /// Creates a new collection instance with the specified capacity. |
| 30 | + /// </summary> |
| 31 | + protected abstract TCollection CreateCollection(int capacity); |
| 32 | + |
| 33 | + /// <summary> |
| 34 | + /// Adds an element to the collection. |
| 35 | + /// </summary> |
| 36 | + protected abstract void AddElement(TCollection collection, TElement element); |
| 37 | + |
| 38 | + public bool TryCalculateSize(ref TCollection value, out int size) |
| 39 | + { |
| 40 | + size = sizeof(int); // size of the collection |
| 41 | + if (value.Count == 0) |
| 42 | + { |
| 43 | + return true; |
| 44 | + } |
| 45 | + |
| 46 | + foreach (var item in value) |
| 47 | + { |
| 48 | + var element = item; |
| 49 | + if (_scalarSerializer.TryCalculateSize(ref element, out var elementSize)) |
| 50 | + { |
| 51 | + size += sizeof(int); |
| 52 | + size += elementSize; |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + // Element serializer can't calculate size, so we can't either |
| 57 | + size = 0; |
| 58 | + return false; |
| 59 | + } |
| 60 | + } |
| 61 | + |
| 62 | + return true; |
| 63 | + } |
| 64 | + |
| 65 | + public void WriteTo(ref TCollection value, ref Span<byte> span) |
| 66 | + { |
| 67 | + // Write the size of the collection |
| 68 | + var slice = span.Slice(0, sizeof(int)); |
| 69 | + BitConverter.TryWriteBytes(slice, value.Count); |
| 70 | + |
| 71 | + // Write the elements of the collection |
| 72 | + int offset = sizeof(int); |
| 73 | + foreach (var item in value) |
| 74 | + { |
| 75 | + var element = item; |
| 76 | + if (_scalarSerializer.TryCalculateSize(ref element, out var elementSize)) |
| 77 | + { |
| 78 | + slice = span.Slice(offset, sizeof(int)); |
| 79 | + BitConverter.TryWriteBytes(slice, elementSize); |
| 80 | + offset += sizeof(int); |
| 81 | + |
| 82 | + slice = span.Slice(offset, elementSize); |
| 83 | + _scalarSerializer.WriteTo(ref element, ref slice); |
| 84 | + offset += elementSize; |
| 85 | + } |
| 86 | + } |
| 87 | + } |
| 88 | + |
| 89 | + public void WriteTo(ref TCollection value, IBufferWriter<byte> buffer) |
| 90 | + { |
| 91 | + throw new NotImplementedException(); |
| 92 | + } |
| 93 | + |
| 94 | + public TCollection Deserialize(ReadOnlySpan<byte> buffer) |
| 95 | + { |
| 96 | + // Read the size of the collection |
| 97 | + var slice = buffer.Slice(0, sizeof(int)); |
| 98 | + var size = BitConverter.ToInt32(slice); |
| 99 | + |
| 100 | + var collection = CreateCollection(size); |
| 101 | + |
| 102 | + // Read the elements of the collection |
| 103 | + int offset = sizeof(int); |
| 104 | + for (int i = 0; i < size; i++) |
| 105 | + { |
| 106 | + slice = buffer.Slice(offset, sizeof(int)); |
| 107 | + var elementSize = BitConverter.ToInt32(slice); |
| 108 | + offset += sizeof(int); |
| 109 | + |
| 110 | + slice = buffer.Slice(offset, elementSize); |
| 111 | + var element = _scalarSerializer.Deserialize(slice); |
| 112 | + AddElement(collection, element); |
| 113 | + offset += elementSize; |
| 114 | + } |
| 115 | + |
| 116 | + return collection; |
| 117 | + } |
| 118 | +} |
| 119 | + |
0 commit comments