|
| 1 | +using System; |
| 2 | +using System.Buffers; |
| 3 | +using System.Buffers.Binary; |
| 4 | +using System.Runtime.InteropServices; |
| 5 | +using System.Text.Json; |
| 6 | +using System.Text.Json.Serialization; |
| 7 | + |
| 8 | +using Elastic.Clients.Elasticsearch.Serialization; |
| 9 | + |
| 10 | +namespace Elastic.Clients.Elasticsearch; |
| 11 | + |
| 12 | +/// <summary> |
| 13 | +/// The encoding to use when serializing vector data using the <see cref="FloatVectorDataConverter"/> converter. |
| 14 | +/// </summary> |
| 15 | +public enum FloatVectorDataEncoding |
| 16 | +{ |
| 17 | + /// <summary> |
| 18 | + /// Legacy (JSON array) vector encoding for backwards compatibility. |
| 19 | + /// </summary> |
| 20 | + Legacy, |
| 21 | + |
| 22 | + /// <summary> |
| 23 | + /// <c>Base64</c> vector encoding. |
| 24 | + /// </summary> |
| 25 | + /// <remarks> |
| 26 | + /// <c>Base64</c> encoding is available starting from Elasticsearch 9.3.0. |
| 27 | + /// </remarks> |
| 28 | + Base64 |
| 29 | +} |
| 30 | + |
| 31 | +public sealed class FloatVectorDataConverter : |
| 32 | + JsonConverter<ReadOnlyMemory<float>> |
| 33 | +{ |
| 34 | + private FloatVectorDataEncoding? _encoding; |
| 35 | + |
| 36 | + public override ReadOnlyMemory<float> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 37 | + { |
| 38 | + return reader.TokenType switch |
| 39 | + { |
| 40 | + JsonTokenType.StartArray => new(reader.ReadCollectionValue<float>(options, null)!.ToArray()), |
| 41 | + JsonTokenType.String => ReadBase64VectorData(ref reader), |
| 42 | + _ => throw reader.UnexpectedTokenException(JsonTokenType.StartArray, JsonTokenType.String) |
| 43 | + }; |
| 44 | + } |
| 45 | + |
| 46 | + public override void Write(Utf8JsonWriter writer, ReadOnlyMemory<float> value, JsonSerializerOptions options) |
| 47 | + { |
| 48 | + var encoding = _encoding; |
| 49 | + if (encoding is null) |
| 50 | + { |
| 51 | + var settings = ContextProvider<IElasticsearchClientSettings>.GetContext(options); |
| 52 | + _encoding = settings.FloatVectorDataEncoding; |
| 53 | + } |
| 54 | + |
| 55 | + switch (_encoding) |
| 56 | + { |
| 57 | + case FloatVectorDataEncoding.Legacy: |
| 58 | + writer.WriteSpanValue(options, value.Span, null); |
| 59 | + break; |
| 60 | + |
| 61 | + case FloatVectorDataEncoding.Base64: |
| 62 | + WriteBase64VectorData(writer, value); |
| 63 | + break; |
| 64 | + |
| 65 | + default: |
| 66 | + throw new NotSupportedException(); |
| 67 | + } |
| 68 | + } |
| 69 | + |
| 70 | + private static ReadOnlyMemory<float> ReadBase64VectorData(ref Utf8JsonReader reader) |
| 71 | + { |
| 72 | + var bytes = reader.GetBytesFromBase64(); |
| 73 | + |
| 74 | + if ((bytes.Length & 3) != 0) |
| 75 | + { |
| 76 | + throw new ArgumentException("Decoded vector data length is not a multiple of 4 (not valid 32-bit floats)."); |
| 77 | + } |
| 78 | + |
| 79 | + var span = bytes.AsSpan(); |
| 80 | + |
| 81 | + if (BitConverter.IsLittleEndian) |
| 82 | + { |
| 83 | + // Host is little-endian. We must swap the byte order. |
| 84 | + |
| 85 | + var intSourceDest = MemoryMarshal.Cast<byte, int>(span); |
| 86 | + |
| 87 | + for (var i = 0; i < intSourceDest.Length; i++) |
| 88 | + { |
| 89 | + intSourceDest[i] = BinaryPrimitives.ReverseEndianness(intSourceDest[i]); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + var result = new float[bytes.Length / 4]; |
| 94 | + Buffer.BlockCopy(bytes, 0, result, 0, bytes.Length); |
| 95 | + |
| 96 | + return new(result); |
| 97 | + } |
| 98 | + |
| 99 | + private static void WriteBase64VectorData(Utf8JsonWriter writer, ReadOnlyMemory<float> value) |
| 100 | + { |
| 101 | + if (value.IsEmpty) |
| 102 | + { |
| 103 | + writer.WriteStringValue(string.Empty); |
| 104 | + return; |
| 105 | + } |
| 106 | + |
| 107 | + // If the host is big-endian we can reinterpret the memory as bytes without copying. |
| 108 | + if (!BitConverter.IsLittleEndian) |
| 109 | + { |
| 110 | + writer.WriteBase64StringValue(MemoryMarshal.AsBytes(value.Span)); |
| 111 | + } |
| 112 | + |
| 113 | + // Host is little-endian. We must swap the byte order. |
| 114 | + |
| 115 | + var pool = MemoryPool<byte>.Shared; |
| 116 | + var required = checked(value.Length * sizeof(float)); |
| 117 | + var owner = pool.Rent(required); |
| 118 | + |
| 119 | + try |
| 120 | + { |
| 121 | + var dest = owner.Memory.Span[..required]; |
| 122 | + |
| 123 | + var intSource = MemoryMarshal.Cast<float, int>(value.Span); |
| 124 | + var intDest = MemoryMarshal.Cast<byte, int>(dest); |
| 125 | + |
| 126 | + for (var i = 0; i < intSource.Length; i++) |
| 127 | + { |
| 128 | + intDest[i] = BinaryPrimitives.ReverseEndianness(intSource[i]); |
| 129 | + } |
| 130 | + |
| 131 | + writer.WriteBase64StringValue(dest); |
| 132 | + } |
| 133 | + finally |
| 134 | + { |
| 135 | + owner.Dispose(); |
| 136 | + } |
| 137 | + } |
| 138 | +} |
| 139 | + |
| 140 | +/// <summary> |
| 141 | +/// The encoding to use when serializing vector data using the <see cref="FloatVectorDataConverter"/> converter. |
| 142 | +/// </summary> |
| 143 | +public enum ByteVectorDataEncoding |
| 144 | +{ |
| 145 | + /// <summary> |
| 146 | + /// Legacy (JSON array) vector encoding for backwards compatibility. |
| 147 | + /// </summary> |
| 148 | + Legacy, |
| 149 | + |
| 150 | + /// <summary> |
| 151 | + /// Hexadecimal string vector encoding. |
| 152 | + /// </summary> |
| 153 | + /// <remarks> |
| 154 | + /// Hexadecimal encoding is available starting from Elasticsearch 8.14.0. |
| 155 | + /// </remarks> |
| 156 | + Hex, |
| 157 | + |
| 158 | + /// <summary> |
| 159 | + /// <c>Base64</c> vector encoding. |
| 160 | + /// </summary> |
| 161 | + /// <remarks> |
| 162 | + /// <c>Base64</c> encoding is available starting from Elasticsearch 9.3.0. |
| 163 | + /// </remarks> |
| 164 | + Base64 |
| 165 | +} |
| 166 | + |
| 167 | +public sealed class ByteVectorDataConverter : |
| 168 | + JsonConverter<ReadOnlyMemory<byte>> |
| 169 | +{ |
| 170 | + private ByteVectorDataEncoding? _encoding; |
| 171 | + |
| 172 | + public override ReadOnlyMemory<byte> Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) |
| 173 | + { |
| 174 | + return reader.TokenType switch |
| 175 | + { |
| 176 | + JsonTokenType.StartArray => new(reader.ReadCollectionValue(options, (ref r, _) => unchecked((byte)r.GetSByte()))!.ToArray()), |
| 177 | + JsonTokenType.String => ReadStringVectorData(ref reader), |
| 178 | + _ => throw reader.UnexpectedTokenException(JsonTokenType.StartArray, JsonTokenType.String) |
| 179 | + }; |
| 180 | + } |
| 181 | + |
| 182 | + public override void Write(Utf8JsonWriter writer, ReadOnlyMemory<byte> value, JsonSerializerOptions options) |
| 183 | + { |
| 184 | + if (_encoding is null) |
| 185 | + { |
| 186 | + var settings = ContextProvider<IElasticsearchClientSettings>.GetContext(options); |
| 187 | + _encoding = settings.ByteVectorDataEncoding; |
| 188 | + } |
| 189 | + |
| 190 | + switch (_encoding) |
| 191 | + { |
| 192 | + case ByteVectorDataEncoding.Legacy: |
| 193 | + writer.WriteSpanValue(options, value.Span, (w, _, b) => w.WriteNumberValue(unchecked((sbyte)b))); |
| 194 | + break; |
| 195 | + |
| 196 | + case ByteVectorDataEncoding.Hex: |
| 197 | + WriteHexVectorData(writer, value); |
| 198 | + break; |
| 199 | + |
| 200 | + case ByteVectorDataEncoding.Base64: |
| 201 | + writer.WriteBase64StringValue(value.Span); |
| 202 | + break; |
| 203 | + |
| 204 | + default: |
| 205 | + throw new NotSupportedException(); |
| 206 | + } |
| 207 | + } |
| 208 | + |
| 209 | + private static ReadOnlyMemory<byte> ReadStringVectorData(ref Utf8JsonReader reader) |
| 210 | + { |
| 211 | + if (reader.TryGetBytesFromBase64(out var result)) |
| 212 | + { |
| 213 | + return result; |
| 214 | + } |
| 215 | + |
| 216 | + return ReadHexVectorData(ref reader); |
| 217 | + } |
| 218 | + |
| 219 | + private static ReadOnlyMemory<byte> ReadHexVectorData(ref Utf8JsonReader reader) |
| 220 | + { |
| 221 | +#if NET5_0_OR_GREATER |
| 222 | + var data = Convert.FromHexString(reader.GetString()!); |
| 223 | +#else |
| 224 | + var data = FromHex(reader.GetString()!); |
| 225 | +#endif |
| 226 | + |
| 227 | + return new(data); |
| 228 | + } |
| 229 | + |
| 230 | + private static void WriteHexVectorData(Utf8JsonWriter writer, ReadOnlyMemory<byte> value) |
| 231 | + { |
| 232 | + if (value.IsEmpty) |
| 233 | + { |
| 234 | + writer.WriteStringValue(string.Empty); |
| 235 | + return; |
| 236 | + } |
| 237 | + |
| 238 | + // We don't use Convert.ToHexString even for .NET 5.0+ to be able to use pooled memory. |
| 239 | + |
| 240 | + var pool = MemoryPool<char>.Shared; |
| 241 | + var required = checked(value.Length * 2); |
| 242 | + var owner = pool.Rent(required); |
| 243 | + |
| 244 | + try |
| 245 | + { |
| 246 | + var source = value.Span; |
| 247 | + var dest = owner.Memory.Span[..required]; |
| 248 | + |
| 249 | + byte b; |
| 250 | + |
| 251 | + for(int bx = 0, cx = 0; bx < source.Length; ++bx, ++cx) |
| 252 | + { |
| 253 | + b = ((byte)(source[bx] >> 4)); |
| 254 | + dest[cx] = (char)(b > 9 ? b + 0x37 : b + 0x30); |
| 255 | + b = ((byte)(source[bx] & 0x0F)); |
| 256 | + dest[++cx]=(char)(b > 9 ? b + 0x37 : b + 0x30); |
| 257 | + } |
| 258 | + |
| 259 | + writer.WriteStringValue(dest); |
| 260 | + } |
| 261 | + finally |
| 262 | + { |
| 263 | + owner.Dispose(); |
| 264 | + } |
| 265 | + } |
| 266 | + |
| 267 | +#if !NET5_0_OR_GREATER |
| 268 | + public static byte[] FromHex(string data) |
| 269 | + { |
| 270 | + if (data.Length is 0) |
| 271 | + { |
| 272 | + return []; |
| 273 | + } |
| 274 | + |
| 275 | + if (data.Length % 2 != 0) |
| 276 | + { |
| 277 | + throw new ArgumentException("Decoded vector data length is not a multiple of 2 (not valid 8-bit hex niblets)."); |
| 278 | + } |
| 279 | + |
| 280 | + var buffer = new byte[data.Length / 2]; |
| 281 | + char c; |
| 282 | + |
| 283 | + for (int bx = 0, sx = 0; bx < buffer.Length; ++bx, ++sx) |
| 284 | + { |
| 285 | + c = data[sx]; |
| 286 | + buffer[bx] = (byte)((c > '9' ? (c > 'Z' ? (c - 'a' + 10) : (c - 'A' + 10)) : (c - '0')) << 4); |
| 287 | + c = data[++sx]; |
| 288 | + buffer[bx] |= (byte)(c > '9' ? (c > 'Z' ? (c - 'a' + 10) : (c - 'A' + 10)) : (c - '0')); |
| 289 | + } |
| 290 | + |
| 291 | + return buffer; |
| 292 | + } |
| 293 | +#endif |
| 294 | +} |
0 commit comments