Skip to content

Commit

Permalink
Compartment indexing and TVP generator codegen (#457)
Browse files Browse the repository at this point in the history
  • Loading branch information
johnstairs authored May 12, 2019
1 parent 5a0b402 commit 8583c70
Show file tree
Hide file tree
Showing 19 changed files with 797 additions and 169 deletions.
1 change: 1 addition & 0 deletions src/Microsoft.Health.Fhir.SqlServer.Api/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -7,4 +7,5 @@
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Microsoft.Health.Fhir.SqlServer.Api.UnitTests")]
[assembly: InternalsVisibleTo("Microsoft.Health.Fhir.Tests.Integration")]
[assembly: NeutralResourcesLanguage("en-us")]
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,15 @@
// -------------------------------------------------------------------------------------------------

using System;
using System.Linq;
using EnsureThat;
using Microsoft.Extensions.Configuration;
using Microsoft.Health.Extensions.DependencyInjection;
using Microsoft.Health.Fhir.SqlServer.Api.Controllers;
using Microsoft.Health.Fhir.SqlServer.Configs;
using Microsoft.Health.Fhir.SqlServer.Features.Health;
using Microsoft.Health.Fhir.SqlServer.Features.Schema;
using Microsoft.Health.Fhir.SqlServer.Features.Schema.Model;
using Microsoft.Health.Fhir.SqlServer.Features.Storage;

namespace Microsoft.Extensions.DependencyInjection
Expand Down Expand Up @@ -71,7 +73,28 @@ public static IServiceCollection AddExperimentalSqlServer(this IServiceCollectio
// During normal usage, the controller should be automatically discovered.
serviceCollection.AddMvc().AddApplicationPart(typeof(SchemaController).Assembly);

AddSqlServerTableRowParameterGenerators(serviceCollection);

return serviceCollection;
}

internal static void AddSqlServerTableRowParameterGenerators(this IServiceCollection serviceCollection)
{
foreach (var type in typeof(SqlServerFhirDataStore).Assembly.GetTypes().Where(t => t.IsClass && !t.IsAbstract))
{
foreach (var interfaceType in type.GetInterfaces())
{
if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(IStoredProcedureTableValuedParametersGenerator<,>))
{
serviceCollection.AddSingleton(type);
}

if (interfaceType.IsGenericType && interfaceType.GetGenericTypeDefinition() == typeof(ITableValuedParameterRowGenerator<,>))
{
serviceCollection.AddSingleton(interfaceType, type);
}
}
}
}
}
}
2 changes: 2 additions & 0 deletions src/Microsoft.Health.Fhir.SqlServer/AssemblyInfo.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,7 @@
using System.Resources;
using System.Runtime.CompilerServices;

[assembly: InternalsVisibleTo("Microsoft.Health.Fhir.SqlServer.Api")]
[assembly: InternalsVisibleTo("Microsoft.Health.Fhir.SqlServer.UnitTests")]
[assembly: InternalsVisibleTo("Microsoft.Health.Fhir.Tests.Integration")]
[assembly: NeutralResourcesLanguage("en-us")]
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,6 @@
using Microsoft.Extensions.Diagnostics.HealthChecks;
using Microsoft.Extensions.Logging;
using Microsoft.Health.Fhir.SqlServer.Configs;
using Microsoft.Health.Fhir.SqlServer.Features.Storage;

namespace Microsoft.Health.Fhir.SqlServer.Features.Health
{
Expand All @@ -21,9 +20,9 @@ namespace Microsoft.Health.Fhir.SqlServer.Features.Health
public class SqlServerHealthCheck : IHealthCheck
{
private readonly SqlServerDataStoreConfiguration _configuration;
private readonly ILogger<SqlServerFhirDataStore> _logger;
private readonly ILogger<SqlServerHealthCheck> _logger;

public SqlServerHealthCheck(SqlServerDataStoreConfiguration configuration, ILogger<SqlServerFhirDataStore> logger)
public SqlServerHealthCheck(SqlServerDataStoreConfiguration configuration, ILogger<SqlServerHealthCheck> logger)
{
EnsureArg.IsNotNull(configuration, nameof(configuration));
EnsureArg.IsNotNull(logger, nameof(logger));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -208,7 +208,7 @@ CREATE UNIQUE CLUSTERED INDEX IXC_Claim on dbo.ClaimType
Name
)

CREATE TYPE dbo.ResourceWriteClaimTableType AS TABLE
CREATE TYPE dbo.ResourceWriteClaimTableType_1 AS TABLE
(
ClaimTypeId tinyint NOT NULL,
ClaimValue nvarchar(128) NOT NULL
Expand All @@ -227,6 +227,52 @@ CREATE CLUSTERED INDEX IXC_LastModifiedClaim on dbo.ResourceWriteClaim
ClaimTypeId
)

/*************************************************************
Compartments
**************************************************************/

CREATE TABLE dbo.CompartmentType
(
CompartmentTypeId tinyint IDENTITY(1,1) NOT NULL,
Name varchar(128) NOT NULL
)

CREATE UNIQUE CLUSTERED INDEX IXC_CompartmentType on dbo.CompartmentType
(
Name
)

CREATE TYPE dbo.CompartmentAssignmentTableType_1 AS TABLE
(
CompartmentTypeId tinyint NOT NULL,
ReferenceResourceId varchar(64) NOT NULL
)

CREATE TABLE dbo.CompartmentAssignment
(
ResourceSurrogateId bigint NOT NULL,
CompartmentTypeId tinyint NOT NULL,
ReferenceResourceId varchar(64) NOT NULL,
IsHistory bit NOT NULL,
) WITH (DATA_COMPRESSION = PAGE)

CREATE CLUSTERED INDEX IXC_CompartmentAssignment
ON dbo.CompartmentAssignment
(
ResourceSurrogateId,
CompartmentTypeId,
ReferenceResourceId
)

CREATE NONCLUSTERED INDEX IX_CompartmentAssignment_CompartmentTypeId_ReferenceResourceId
ON dbo.CompartmentAssignment
(
CompartmentTypeId,
ReferenceResourceId
)
WHERE IsHistory = 0
WITH (DATA_COMPRESSION = PAGE)


GO

Expand Down Expand Up @@ -286,7 +332,8 @@ CREATE PROCEDURE dbo.UpsertResource
@keepHistory bit,
@requestMethod varchar(10),
@rawResource varbinary(max),
@resourceWriteClaims dbo.ResourceWriteClaimTableType READONLY
@resourceWriteClaims dbo.ResourceWriteClaimTableType_1 READONLY,
@compartmentAssignments dbo.CompartmentAssignmentTableType_1 READONLY
AS
SET NOCOUNT ON

Expand Down Expand Up @@ -332,7 +379,30 @@ AS
END
ELSE BEGIN
-- There is a previous version
SET @version = (select (Version + 1) from @previousVersion)
DECLARE @previousResourceSurrogateId bigint

SELECT @version = (Version + 1), @previousResourceSurrogateId = ResourceSurrogateId
FROM @previousVersion

IF (@keepHistory = 1) BEGIN

-- note there is no IsHistory column on ResourceWriteClaim since we do not query it

UPDATE dbo.CompartmentAssignment
SET IsHistory = 1
WHERE ResourceSurrogateId = @previousResourceSurrogateId

END
ELSE BEGIN

DELETE FROM dbo.ResourceWriteClaim
WHERE ResourceSurrogateId = @previousResourceSurrogateId

DELETE FROM dbo.CompartmentAssignment
WHERE ResourceSurrogateId = @previousResourceSurrogateId


END
END


Expand All @@ -351,6 +421,10 @@ AS
(ResourceSurrogateId, ClaimTypeId, ClaimValue)
SELECT @resourceSurrogateId, ClaimTypeId, ClaimValue from @resourceWriteClaims

INSERT INTO dbo.CompartmentAssignment
(ResourceSurrogateId, CompartmentTypeId, ReferenceResourceId, IsHistory)
SELECT @resourceSurrogateId, CompartmentTypeId, ReferenceResourceId, 0
FROM @compartmentAssignments

select @version

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
// -------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// -------------------------------------------------------------------------------------------------

namespace Microsoft.Health.Fhir.SqlServer.Features.Schema.Model
{
/// <summary>
/// Generates the full set of table-valued parameters for a stored procedure.
/// </summary>
/// <typeparam name="TInput">The type of the input</typeparam>
/// <typeparam name="TOutput">The type of the output. Intended to be a struct with properties for each TVP</typeparam>
internal interface IStoredProcedureTableValuedParametersGenerator<in TInput, out TOutput>
{
TOutput Generate(TInput input);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
// -------------------------------------------------------------------------------------------------
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License (MIT). See LICENSE in the repo root for license information.
// -------------------------------------------------------------------------------------------------

using System.Collections.Generic;

namespace Microsoft.Health.Fhir.SqlServer.Features.Schema.Model
{
/// <summary>
/// Generates a sequence of row structs for a table-valued parameter.
/// </summary>
/// <typeparam name="TInput">The input type</typeparam>
/// <typeparam name="TRow">The row struct type</typeparam>
internal interface ITableValuedParameterRowGenerator<in TInput, out TRow>
where TRow : struct
{
IEnumerable<TRow> GenerateRows(TInput input);
}
}
Loading

0 comments on commit 8583c70

Please sign in to comment.