|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
| 4 | +using System.Diagnostics.CodeAnalysis; |
4 | 5 | using Microsoft.EntityFrameworkCore.SqlServer.Metadata.Internal; |
5 | 6 |
|
6 | 7 | // ReSharper disable once CheckNamespace |
@@ -431,4 +432,155 @@ public static void SetDataCompression(this IMutableIndex index, DataCompressionT |
431 | 432 | /// <returns>The <see cref="ConfigurationSource" /> for the data compression the index uses.</returns> |
432 | 433 | public static ConfigurationSource? GetDataCompressionConfigurationSource(this IConventionIndex index) |
433 | 434 | => index.FindAnnotation(SqlServerAnnotationNames.DataCompression)?.GetConfigurationSource(); |
| 435 | + |
| 436 | + /// <summary> |
| 437 | + /// Returns whether the index is a vector index. |
| 438 | + /// </summary> |
| 439 | + /// <param name="index">The index.</param> |
| 440 | + /// <returns>Whether the index is a vector index.</returns> |
| 441 | + [Experimental(EFDiagnostics.SqlServerVectorSearch)] |
| 442 | + public static bool IsVectorIndex(this IReadOnlyIndex index) |
| 443 | + => index is RuntimeIndex |
| 444 | + ? throw new InvalidOperationException(CoreStrings.RuntimeModelMissingData) |
| 445 | + : index[SqlServerAnnotationNames.VectorIndexMetric] is not null; |
| 446 | + |
| 447 | + /// <summary> |
| 448 | + /// Returns the similarity metric for the vector index. |
| 449 | + /// </summary> |
| 450 | + /// <param name="index">The index.</param> |
| 451 | + /// <returns>The similarity metric for the vector index.</returns> |
| 452 | + [Experimental(EFDiagnostics.SqlServerVectorSearch)] |
| 453 | + public static string? GetVectorMetric(this IReadOnlyIndex index) |
| 454 | + => index is RuntimeIndex |
| 455 | + ? throw new InvalidOperationException(CoreStrings.RuntimeModelMissingData) |
| 456 | + : (string?)index[SqlServerAnnotationNames.VectorIndexMetric]; |
| 457 | + |
| 458 | + /// <summary> |
| 459 | + /// Returns the similarity metric for the vector index. |
| 460 | + /// </summary> |
| 461 | + /// <param name="index">The index.</param> |
| 462 | + /// <param name="storeObject">The identifier of the store object.</param> |
| 463 | + /// <returns>The similarity metric for the vector index.</returns> |
| 464 | + [Experimental(EFDiagnostics.SqlServerVectorSearch)] |
| 465 | + public static string? GetVectorMetric(this IReadOnlyIndex index, in StoreObjectIdentifier storeObject) |
| 466 | + { |
| 467 | + if (index is RuntimeIndex) |
| 468 | + { |
| 469 | + throw new InvalidOperationException(CoreStrings.RuntimeModelMissingData); |
| 470 | + } |
| 471 | + |
| 472 | + var annotation = index.FindAnnotation(SqlServerAnnotationNames.VectorIndexMetric); |
| 473 | + if (annotation != null) |
| 474 | + { |
| 475 | + return (string?)annotation.Value; |
| 476 | + } |
| 477 | + |
| 478 | + var sharedTableRootIndex = index.FindSharedObjectRootIndex(storeObject); |
| 479 | + return sharedTableRootIndex?.GetVectorMetric(storeObject); |
| 480 | + } |
| 481 | + |
| 482 | + /// <summary> |
| 483 | + /// Sets the similarity metric for the vector index. |
| 484 | + /// </summary> |
| 485 | + /// <param name="index">The index.</param> |
| 486 | + /// <param name="metric">The value to set.</param> |
| 487 | + [Experimental(EFDiagnostics.SqlServerVectorSearch)] |
| 488 | + public static void SetVectorMetric(this IMutableIndex index, string? metric) |
| 489 | + => index.SetAnnotation(SqlServerAnnotationNames.VectorIndexMetric, metric); |
| 490 | + |
| 491 | + /// <summary> |
| 492 | + /// Sets the similarity metric for the vector index. |
| 493 | + /// </summary> |
| 494 | + /// <param name="index">The index.</param> |
| 495 | + /// <param name="metric">The value to set.</param> |
| 496 | + /// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param> |
| 497 | + /// <returns>The configured value.</returns> |
| 498 | + [Experimental(EFDiagnostics.SqlServerVectorSearch)] |
| 499 | + public static string? SetVectorMetric( |
| 500 | + this IConventionIndex index, |
| 501 | + string? metric, |
| 502 | + bool fromDataAnnotation = false) |
| 503 | + => (string?)index.SetAnnotation( |
| 504 | + SqlServerAnnotationNames.VectorIndexMetric, |
| 505 | + metric, |
| 506 | + fromDataAnnotation)?.Value; |
| 507 | + |
| 508 | + /// <summary> |
| 509 | + /// Returns the <see cref="ConfigurationSource" /> for the similarity metric of the vector index. |
| 510 | + /// </summary> |
| 511 | + /// <param name="index">The index.</param> |
| 512 | + /// <returns>The <see cref="ConfigurationSource" /> for the similarity metric of the vector index.</returns> |
| 513 | + [Experimental(EFDiagnostics.SqlServerVectorSearch)] |
| 514 | + public static ConfigurationSource? GetVectorMetricConfigurationSource(this IConventionIndex index) |
| 515 | + => index.FindAnnotation(SqlServerAnnotationNames.VectorIndexMetric)?.GetConfigurationSource(); |
| 516 | + |
| 517 | + /// <summary> |
| 518 | + /// Returns the type of the vector index. |
| 519 | + /// </summary> |
| 520 | + /// <param name="index">The index.</param> |
| 521 | + /// <returns>The type of the vector index.</returns> |
| 522 | + [Experimental(EFDiagnostics.SqlServerVectorSearch)] |
| 523 | + public static string? GetVectorIndexType(this IReadOnlyIndex index) |
| 524 | + => (index is RuntimeIndex) |
| 525 | + ? throw new InvalidOperationException(CoreStrings.RuntimeModelMissingData) |
| 526 | + : (string?)index[SqlServerAnnotationNames.VectorIndexType]; |
| 527 | + |
| 528 | + /// <summary> |
| 529 | + /// Returns the type of the vector index. |
| 530 | + /// </summary> |
| 531 | + /// <param name="index">The index.</param> |
| 532 | + /// <param name="storeObject">The identifier of the store object.</param> |
| 533 | + /// <returns>The type of the vector index.</returns> |
| 534 | + [Experimental(EFDiagnostics.SqlServerVectorSearch)] |
| 535 | + public static string? GetVectorIndexType(this IReadOnlyIndex index, in StoreObjectIdentifier storeObject) |
| 536 | + { |
| 537 | + if (index is RuntimeIndex) |
| 538 | + { |
| 539 | + throw new InvalidOperationException(CoreStrings.RuntimeModelMissingData); |
| 540 | + } |
| 541 | + |
| 542 | + var annotation = index.FindAnnotation(SqlServerAnnotationNames.VectorIndexType); |
| 543 | + if (annotation != null) |
| 544 | + { |
| 545 | + return (string?)annotation.Value; |
| 546 | + } |
| 547 | + |
| 548 | + var sharedTableRootIndex = index.FindSharedObjectRootIndex(storeObject); |
| 549 | + return sharedTableRootIndex?.GetVectorIndexType(storeObject); |
| 550 | + } |
| 551 | + |
| 552 | + /// <summary> |
| 553 | + /// Sets the type of the vector index. |
| 554 | + /// </summary> |
| 555 | + /// <param name="index">The index.</param> |
| 556 | + /// <param name="type">The value to set.</param> |
| 557 | + [Experimental(EFDiagnostics.SqlServerVectorSearch)] |
| 558 | + public static void SetVectorIndexType(this IMutableIndex index, string? type) |
| 559 | + => index.SetAnnotation(SqlServerAnnotationNames.VectorIndexType, type); |
| 560 | + |
| 561 | + /// <summary> |
| 562 | + /// Sets the type of the vector index. |
| 563 | + /// </summary> |
| 564 | + /// <param name="index">The index.</param> |
| 565 | + /// <param name="type">The value to set.</param> |
| 566 | + /// <param name="fromDataAnnotation">Indicates whether the configuration was specified using a data annotation.</param> |
| 567 | + /// <returns>The configured value.</returns> |
| 568 | + [Experimental(EFDiagnostics.SqlServerVectorSearch)] |
| 569 | + public static string? SetVectorIndexType( |
| 570 | + this IConventionIndex index, |
| 571 | + string? type, |
| 572 | + bool fromDataAnnotation = false) |
| 573 | + => (string?)index.SetAnnotation( |
| 574 | + SqlServerAnnotationNames.VectorIndexType, |
| 575 | + type, |
| 576 | + fromDataAnnotation)?.Value; |
| 577 | + |
| 578 | + /// <summary> |
| 579 | + /// Returns the <see cref="ConfigurationSource" /> for the type of the vector index. |
| 580 | + /// </summary> |
| 581 | + /// <param name="index">The index.</param> |
| 582 | + /// <returns>The <see cref="ConfigurationSource" /> for the type of the vector index.</returns> |
| 583 | + [Experimental(EFDiagnostics.SqlServerVectorSearch)] |
| 584 | + public static ConfigurationSource? GetVectorIndexTypeConfigurationSource(this IConventionIndex index) |
| 585 | + => index.FindAnnotation(SqlServerAnnotationNames.VectorIndexType)?.GetConfigurationSource(); |
434 | 586 | } |
0 commit comments