Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -499,6 +499,13 @@
searchResult = simpleReadResult;
return;
}
else if (await TryHandleMultipleResourceReadsAsync(expression, clonedSearchOptions, connection, sqlCommand, cancellationToken) is SearchResult multipleReadResult)
{
// Multiple resource IDs - handled using GetResources stored procedure
_logger.LogInformation("Multiple resource read using GetResources stored procedure.");
searchResult = multipleReadResult;
return;
}
else
{
var stringBuilder = new IndentedStringBuilder(new StringBuilder());
Expand Down Expand Up @@ -929,6 +936,214 @@
return false;
}

/// <summary>
/// Attempts to handle multiple resource read requests using the GetResources stored procedure.
/// This provides better performance than generated SQL for multiple resource lookups.
/// </summary>
/// <param name="expression">The SQL root expression to analyze</param>
/// <param name="searchOptions">Search options for the request</param>
/// <param name="connection">The SQL connection</param>
/// <param name="command">The SQL command to populate and execute</param>
/// <param name="cancellationToken">Cancellation token</param>
/// <returns>SearchResult if this was a multiple resource read, null otherwise</returns>
private async Task<SearchResult> TryHandleMultipleResourceReadsAsync(SqlRootExpression expression, SqlSearchOptions searchOptions, SqlConnection connection, SqlCommand command, CancellationToken cancellationToken)
{
// Only optimize for non-count queries without sorting or includes operations
if (searchOptions.CountOnly ||
searchOptions.IncludeTotal != TotalType.None ||
searchOptions.IsIncludesOperation)
{
return null;
}

// Only optimize for Latest searches (no history)
if (searchOptions.ResourceVersionTypes != ResourceVersionType.Latest)
{
return null;
}

// Ensure we have exactly two resource table expressions (one for resource type, one for resource ID)
if (expression.ResourceTableExpressions.Count != 2 ||
expression.SearchParamTableExpressions.Count > 0)
{
return null;
}

// Extract resource type and IDs from the expressions
if (!TryExtractResourceTypeAndIds(expression, out string resourceType, out List<string> resourceIds))
{
return null;
}

// Must have multiple IDs for this optimization
if (resourceIds == null || resourceIds.Count == 0)
{
return null;
}

// Get resource type ID
if (!_model.TryGetResourceTypeId(resourceType, out short resourceTypeId))
{
return null;
}

try
{
// Create a DataTable for the table-valued parameter
var resourceKeysTable = new DataTable();

Check warning

Code scanning / CodeQL

Missing Dispose call on local IDisposable Warning

Disposable 'DataTable' is created but not disposed.

Copilot Autofix

AI 3 days ago

In general, to fix a “missing Dispose call” for a local IDisposable, wrap the creation and use of the object in a using statement (or using declaration in newer C#), so that Dispose is called automatically when execution leaves the using scope, even if an exception is thrown.

For this specific case in SqlServerSearchService.cs, the best fix is to wrap the DataTable’s lifetime in a using block. That means replacing the standalone var resourceKeysTable = new DataTable(); and subsequent code that uses resourceKeysTable with a using (var resourceKeysTable = new DataTable()) { ... } block, moving all uses of resourceKeysTable (column definitions, row population, and parameter creation) inside that block. This keeps behavior the same: the DataTable exists for the duration of building the TVP parameter and calling AddWithValue, and then is disposed when the block exits. No additional imports or helper methods are needed, since DataTable and IDisposable are already available via existing using directives.

Concretely, within src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs, in the shown try block around line 990, introduce a using block starting at line 993, move lines 994–1002 and 1005–1009 inside that block, and adjust indentation accordingly. No other code outside that local section needs to change.

Suggested changeset 1
src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs

Autofix patch

Autofix patch
Run the following command in your local git repository to apply this patch
cat << 'EOF' | git apply
diff --git a/src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs b/src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
--- a/src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
+++ b/src/Microsoft.Health.Fhir.SqlServer/Features/Search/SqlServerSearchService.cs
@@ -990,23 +990,25 @@
             try
             {
                 // Create a DataTable for the table-valued parameter
-                var resourceKeysTable = new DataTable();
-                resourceKeysTable.Columns.Add("ResourceTypeId", typeof(short));
-                resourceKeysTable.Columns.Add("ResourceId", typeof(string));
-                resourceKeysTable.Columns.Add("Version", typeof(int));
-
-                // Populate the table with resource keys
-                foreach (var resourceId in resourceIds)
+                using (var resourceKeysTable = new DataTable())
                 {
-                    resourceKeysTable.Rows.Add(resourceTypeId, resourceId, DBNull.Value);
-                }
+                    resourceKeysTable.Columns.Add("ResourceTypeId", typeof(short));
+                    resourceKeysTable.Columns.Add("ResourceId", typeof(string));
+                    resourceKeysTable.Columns.Add("Version", typeof(int));
 
-                // Populate command to use GetResources stored procedure
-                command.CommandType = CommandType.StoredProcedure;
-                command.CommandText = "dbo.GetResources";
-                command.Connection = connection;
+                    // Populate the table with resource keys
+                    foreach (var resourceId in resourceIds)
+                    {
+                        resourceKeysTable.Rows.Add(resourceTypeId, resourceId, DBNull.Value);
+                    }
 
-                var parameter = command.Parameters.AddWithValue("@ResourceKeys", resourceKeysTable);
+                    // Populate command to use GetResources stored procedure
+                    command.CommandType = CommandType.StoredProcedure;
+                    command.CommandText = "dbo.GetResources";
+                    command.Connection = connection;
+
+                    var parameter = command.Parameters.AddWithValue("@ResourceKeys", resourceKeysTable);
+                }
                 parameter.SqlDbType = SqlDbType.Structured;
                 parameter.TypeName = "dbo.ResourceKeyList";
 
EOF
@@ -990,23 +990,25 @@
try
{
// Create a DataTable for the table-valued parameter
var resourceKeysTable = new DataTable();
resourceKeysTable.Columns.Add("ResourceTypeId", typeof(short));
resourceKeysTable.Columns.Add("ResourceId", typeof(string));
resourceKeysTable.Columns.Add("Version", typeof(int));

// Populate the table with resource keys
foreach (var resourceId in resourceIds)
using (var resourceKeysTable = new DataTable())
{
resourceKeysTable.Rows.Add(resourceTypeId, resourceId, DBNull.Value);
}
resourceKeysTable.Columns.Add("ResourceTypeId", typeof(short));
resourceKeysTable.Columns.Add("ResourceId", typeof(string));
resourceKeysTable.Columns.Add("Version", typeof(int));

// Populate command to use GetResources stored procedure
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "dbo.GetResources";
command.Connection = connection;
// Populate the table with resource keys
foreach (var resourceId in resourceIds)
{
resourceKeysTable.Rows.Add(resourceTypeId, resourceId, DBNull.Value);
}

var parameter = command.Parameters.AddWithValue("@ResourceKeys", resourceKeysTable);
// Populate command to use GetResources stored procedure
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "dbo.GetResources";
command.Connection = connection;

var parameter = command.Parameters.AddWithValue("@ResourceKeys", resourceKeysTable);
}
parameter.SqlDbType = SqlDbType.Structured;
parameter.TypeName = "dbo.ResourceKeyList";

Copilot is powered by AI and may make mistakes. Always verify output.
resourceKeysTable.Columns.Add("ResourceTypeId", typeof(short));
resourceKeysTable.Columns.Add("ResourceId", typeof(string));
resourceKeysTable.Columns.Add("Version", typeof(int));

// Populate the table with resource keys
foreach (var resourceId in resourceIds)
{
resourceKeysTable.Rows.Add(resourceTypeId, resourceId, DBNull.Value);
}

// Populate command to use GetResources stored procedure
command.CommandType = CommandType.StoredProcedure;
command.CommandText = "dbo.GetResources";
command.Connection = connection;

var parameter = command.Parameters.AddWithValue("@ResourceKeys", resourceKeysTable);
parameter.SqlDbType = SqlDbType.Structured;
parameter.TypeName = "dbo.ResourceKeyList";

LogSqlCommand(command);

// Execute and read results
var results = new List<SearchResultEntry>();

using (var reader = await command.ExecuteReaderAsync(CommandBehavior.SequentialAccess, cancellationToken))
{
while (await reader.ReadAsync(cancellationToken))
{
// GetResources returns: ResourceTypeId, ResourceId, ResourceSurrogateId, Version, IsDeleted, IsHistory, RawResource, IsRawResourceMetaSet, SearchParamHash
short resTypeId = reader.GetInt16(0); // ResourceTypeId
string resId = reader.GetString(1); // ResourceId
long resourceSurrogateId = reader.GetInt64(2); // ResourceSurrogateId
int version = reader.GetInt32(3); // Version
bool isDeleted = reader.GetBoolean(4); // IsDeleted
bool isHistory = reader.GetBoolean(5); // IsHistory
byte[] rawResourceBytes = reader.GetSqlBytes(6).Value; // RawResource
bool isRawResourceMetaSet = reader.GetBoolean(7); // IsRawResourceMetaSet
string searchParameterHash = await reader.IsDBNullAsync(8, cancellationToken) ? null : reader.GetString(8); // SearchParamHash

// Skip deleted or history records
if (isDeleted || isHistory)
{
continue;
}

Lazy<string> rawResource = new Lazy<string>(() =>
{
using var rawResourceStream = new MemoryStream(rawResourceBytes);
var decompressedResource = _compressedRawResourceConverter.ReadCompressedRawResource(rawResourceStream);

if (string.IsNullOrEmpty(decompressedResource))
{
decompressedResource = MissingResourceFactory.CreateJson(resId, _model.GetResourceTypeName(resTypeId), "warning", "incomplete");
_requestContextAccessor.SetMissingResourceCode(System.Net.HttpStatusCode.PartialContent);
}

return decompressedResource;
});

var searchResultEntry = new SearchResultEntry(
new ResourceWrapper(
resId,
version.ToString(CultureInfo.InvariantCulture),
_model.GetResourceTypeName(resTypeId),
searchOptions.OnlyIds ? null : new RawResource(rawResource, FhirResourceFormat.Json, isMetaSet: isRawResourceMetaSet),
new ResourceRequest("GET"),
resourceSurrogateId.ToLastUpdated(),
isDeleted,
null,
null,
null,
searchParameterHash,
resourceSurrogateId),
SearchEntryMode.Match);

results.Add(searchResultEntry);
}
}

return new SearchResult(results, null, null, searchOptions.UnsupportedSearchParams);
}
catch
{
// If anything goes wrong, fall back to SQL generation
return null;
}
}

/// <summary>
/// Attempts to extract resource type and multiple IDs from an expression with IN clause.
/// </summary>
/// <param name="expression">The SQL root expression to analyze</param>
/// <param name="resourceType">The extracted resource type</param>
/// <param name="resourceIds">The extracted list of resource IDs</param>
/// <returns>True if extraction was successful, false otherwise</returns>
private static bool TryExtractResourceTypeAndIds(SqlRootExpression expression, out string resourceType, out List<string> resourceIds)
{
resourceType = null;
resourceIds = null;

// Find resource type and ID expressions in ResourceTableExpressions
SearchParameterExpression resourceTypeExpression = null;
SearchParameterExpression resourceIdExpression = null;

foreach (var searchParamExpression in expression.ResourceTableExpressions.OfType<SearchParameterExpression>())
{
if (searchParamExpression.Parameter.Name == SearchParameterNames.ResourceType)
{
resourceTypeExpression = searchParamExpression;
}
else if (searchParamExpression.Parameter.Name == SearchParameterNames.Id)
{
resourceIdExpression = searchParamExpression;
}
}

if (resourceTypeExpression == null || resourceIdExpression == null)
{
return false;
}

// Extract resource type
if (!TryExtractSimpleStringValue(resourceTypeExpression.Expression, out resourceType))
{
return false;
}

// Check if the ID expression is an OR expression with multiple values
if (resourceIdExpression.Expression is MultiaryExpression multiaryExpression)
{
// Extract all IDs from the OR expression
resourceIds = new List<string>();
foreach (var subExpression in multiaryExpression.Expressions)
{
if (TryExtractSimpleStringValue(subExpression, out string idValue))
{
resourceIds.Add(idValue);
}
else
{
// If any sub-expression is not a simple string, fail the extraction
resourceIds.Clear();
return false;
}
}

return resourceIds.Count > 0;
}

// Not a multi-ID expression
return false;
}

/// <summary>
/// Searches for resources by their type and surrogate id and optionally a searchParamHash and will return resources
/// </summary>
Expand Down Expand Up @@ -1688,11 +1903,11 @@

_logger.LogWarning("Invalid Search Operation (SearchCountResultsExceedLimit)");
throw new InvalidSearchOperationException(string.Format(Core.Resources.SearchCountResultsExceedLimit, count, int.MaxValue));
}

searchResult = new SearchResult((int)count, clonedSearchOptions.UnsupportedSearchParams);

// call NextResultAsync to get the info messages

Check notice

Code scanning / CodeQL

Generic catch clause Note

Generic catch clause.
await reader.NextResultAsync(cancellationToken);

return;
Expand Down Expand Up @@ -2108,7 +2323,7 @@
break;

default:
// Non-search-parameter leaf (e.g. compartment) – ignore for stats
// Non-search-parameter leaf (e.g. compartment) � ignore for stats
break;
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@
<Compile Include="$(MSBuildThisFileDirectory)Persistence\SqlRetryServiceTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Persistence\SqlServerCreateStatsForSmartTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Persistence\SqlServerMemberMatchTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Persistence\SqlServerSearchServiceResourceReadOptimizationTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Persistence\SqlServerSetMergeTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Persistence\SqlServerCreateStatsTests.cs" />
<Compile Include="$(MSBuildThisFileDirectory)Persistence\SqlServerColumnTypeChangeTests.cs" />
Expand Down
Loading
Loading