Skip to content

Commit

Permalink
Tryout: tracking versions in atomic:operations
Browse files Browse the repository at this point in the history
  • Loading branch information
Bart Koelman authored and bkoelman committed Apr 11, 2022
1 parent 405dbd2 commit 934f96a
Show file tree
Hide file tree
Showing 13 changed files with 511 additions and 12 deletions.
13 changes: 13 additions & 0 deletions src/JsonApiDotNetCore/AtomicOperations/IVersionTracker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Resources;

namespace JsonApiDotNetCore.AtomicOperations;

public interface IVersionTracker
{
bool RequiresVersionTracking();

void CaptureVersions(ResourceType resourceType, IIdentifiable resource);

string? GetVersion(ResourceType resourceType, string stringId);
}
41 changes: 40 additions & 1 deletion src/JsonApiDotNetCore/AtomicOperations/OperationsProcessor.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,19 +15,21 @@ public class OperationsProcessor : IOperationsProcessor
private readonly IOperationProcessorAccessor _operationProcessorAccessor;
private readonly IOperationsTransactionFactory _operationsTransactionFactory;
private readonly ILocalIdTracker _localIdTracker;
private readonly IVersionTracker _versionTracker;
private readonly IResourceGraph _resourceGraph;
private readonly IJsonApiRequest _request;
private readonly ITargetedFields _targetedFields;
private readonly ISparseFieldSetCache _sparseFieldSetCache;
private readonly LocalIdValidator _localIdValidator;

public OperationsProcessor(IOperationProcessorAccessor operationProcessorAccessor, IOperationsTransactionFactory operationsTransactionFactory,
ILocalIdTracker localIdTracker, IResourceGraph resourceGraph, IJsonApiRequest request, ITargetedFields targetedFields,
ILocalIdTracker localIdTracker, IVersionTracker versionTracker, IResourceGraph resourceGraph, IJsonApiRequest request, ITargetedFields targetedFields,
ISparseFieldSetCache sparseFieldSetCache)
{
ArgumentGuard.NotNull(operationProcessorAccessor, nameof(operationProcessorAccessor));
ArgumentGuard.NotNull(operationsTransactionFactory, nameof(operationsTransactionFactory));
ArgumentGuard.NotNull(localIdTracker, nameof(localIdTracker));
ArgumentGuard.NotNull(versionTracker, nameof(versionTracker));
ArgumentGuard.NotNull(resourceGraph, nameof(resourceGraph));
ArgumentGuard.NotNull(request, nameof(request));
ArgumentGuard.NotNull(targetedFields, nameof(targetedFields));
Expand All @@ -36,6 +38,7 @@ public OperationsProcessor(IOperationProcessorAccessor operationProcessorAccesso
_operationProcessorAccessor = operationProcessorAccessor;
_operationsTransactionFactory = operationsTransactionFactory;
_localIdTracker = localIdTracker;
_versionTracker = versionTracker;
_resourceGraph = resourceGraph;
_request = request;
_targetedFields = targetedFields;
Expand Down Expand Up @@ -104,11 +107,15 @@ public OperationsProcessor(IOperationProcessorAccessor operationProcessorAccesso
cancellationToken.ThrowIfCancellationRequested();

TrackLocalIdsForOperation(operation);
RefreshVersionsForOperation(operation);

_targetedFields.CopyFrom(operation.TargetedFields);
_request.CopyFrom(operation.Request);

return await _operationProcessorAccessor.ProcessAsync(operation, cancellationToken);

// Ideally we'd take the versions from response here and update the version cache, but currently
// not all resource service methods return data. Therefore this is handled elsewhere.
}

protected void TrackLocalIdsForOperation(OperationContainer operation)
Expand Down Expand Up @@ -144,4 +151,36 @@ private void AssignStringId(IIdentifiable resource)
resource.StringId = _localIdTracker.GetValue(resource.LocalId, resourceType);
}
}

private void RefreshVersionsForOperation(OperationContainer operation)
{
if (operation.Request.PrimaryResourceType!.IsVersioned)
{
string? requestVersion = operation.Resource.GetVersion();

if (requestVersion == null)
{
string? trackedVersion = _versionTracker.GetVersion(operation.Request.PrimaryResourceType, operation.Resource.StringId!);
operation.Resource.SetVersion(trackedVersion);

((JsonApiRequest)operation.Request).PrimaryVersion = trackedVersion;
}
}

foreach (IIdentifiable rightResource in operation.GetSecondaryResources())
{
ResourceType rightResourceType = _resourceGraph.GetResourceType(rightResource.GetClrType());

if (rightResourceType.IsVersioned)
{
string? requestVersion = rightResource.GetVersion();

if (requestVersion == null)
{
string? trackedVersion = _versionTracker.GetVersion(rightResourceType, rightResource.StringId!);
rightResource.SetVersion(trackedVersion);
}
}
}
}
}
91 changes: 91 additions & 0 deletions src/JsonApiDotNetCore/AtomicOperations/VersionTracker.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Middleware;
using JsonApiDotNetCore.Resources;
using JsonApiDotNetCore.Resources.Annotations;

namespace JsonApiDotNetCore.AtomicOperations;

public sealed class VersionTracker : IVersionTracker
{
private static readonly CollectionConverter CollectionConverter = new();

private readonly ITargetedFields _targetedFields;
private readonly IJsonApiRequest _request;
private readonly Dictionary<string, string> _versionPerResource = new();

public VersionTracker(ITargetedFields targetedFields, IJsonApiRequest request)
{
ArgumentGuard.NotNull(targetedFields, nameof(targetedFields));
ArgumentGuard.NotNull(request, nameof(request));

_targetedFields = targetedFields;
_request = request;
}

public bool RequiresVersionTracking()
{
if (_request.Kind != EndpointKind.AtomicOperations)
{
return false;
}

return _request.PrimaryResourceType!.IsVersioned || _targetedFields.Relationships.Any(relationship => relationship.RightType.IsVersioned);
}

public void CaptureVersions(ResourceType resourceType, IIdentifiable resource)
{
if (_request.Kind == EndpointKind.AtomicOperations)
{
if (resourceType.IsVersioned)
{
string? leftVersion = resource.GetVersion();
SetVersion(resourceType, resource.StringId!, leftVersion);
}

foreach (RelationshipAttribute relationship in _targetedFields.Relationships)
{
if (relationship.RightType.IsVersioned)
{
CaptureVersionsInRelationship(resource, relationship);
}
}
}
}

private void CaptureVersionsInRelationship(IIdentifiable resource, RelationshipAttribute relationship)
{
object? afterRightValue = relationship.GetValue(resource);
IReadOnlyCollection<IIdentifiable> afterRightResources = CollectionConverter.ExtractResources(afterRightValue);

foreach (IIdentifiable rightResource in afterRightResources)
{
string? rightVersion = rightResource.GetVersion();
SetVersion(relationship.RightType, rightResource.StringId!, rightVersion);
}
}

private void SetVersion(ResourceType resourceType, string stringId, string? version)
{
string key = GetKey(resourceType, stringId);

if (version == null)
{
_versionPerResource.Remove(key);
}
else
{
_versionPerResource[key] = version;
}
}

public string? GetVersion(ResourceType resourceType, string stringId)
{
string key = GetKey(resourceType, stringId);
return _versionPerResource.TryGetValue(key, out string? version) ? version : null;
}

private string GetKey(ResourceType resourceType, string stringId)
{
return $"{resourceType.PublicName}::{stringId}";
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -275,6 +275,7 @@ private void AddOperationsLayer()
_services.AddScoped<IOperationsProcessor, OperationsProcessor>();
_services.AddScoped<IOperationProcessorAccessor, OperationProcessorAccessor>();
_services.AddScoped<ILocalIdTracker, LocalIdTracker>();
_services.AddScoped<IVersionTracker, VersionTracker>();
}

public void Dispose()
Expand Down
5 changes: 5 additions & 0 deletions src/JsonApiDotNetCore/Queries/IQueryLayerComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,11 @@ QueryLayer WrapLayerForSecondaryEndpoint<TId>(QueryLayer secondaryLayer, Resourc
/// </summary>
QueryLayer ComposeForUpdate<TId>(TId id, ResourceType primaryResourceType);

/// <summary>
/// Builds a query that retrieves the primary resource, along with the subset of versioned targeted relationships, after a create/update/delete request.
/// </summary>
QueryLayer ComposeForGetVersionsAfterWrite<TId>(TId id, ResourceType primaryResourceType, TopFieldSelection fieldSelection);

/// <summary>
/// Builds a query for each targeted relationship with a filter to match on its right resource IDs.
/// </summary>
Expand Down
40 changes: 40 additions & 0 deletions src/JsonApiDotNetCore/Queries/Internal/QueryLayerComposer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,46 @@ public QueryLayer ComposeForUpdate<TId>(TId id, ResourceType primaryResourceType
return primaryLayer;
}

public QueryLayer ComposeForGetVersionsAfterWrite<TId>(TId id, ResourceType primaryResourceType, TopFieldSelection fieldSelection)
{
ArgumentGuard.NotNull(primaryResourceType, nameof(primaryResourceType));

// @formatter:wrap_chained_method_calls chop_always
// @formatter:keep_existing_linebreaks true

IImmutableSet<IncludeElementExpression> includeElements = _targetedFields.Relationships
.Where(relationship => relationship.RightType.IsVersioned)
.Select(relationship => new IncludeElementExpression(relationship))
.ToImmutableHashSet();

// @formatter:keep_existing_linebreaks restore
// @formatter:wrap_chained_method_calls restore

AttrAttribute primaryIdAttribute = GetIdAttribute(primaryResourceType);

QueryLayer primaryLayer = new(primaryResourceType)
{
Include = includeElements.Any() ? new IncludeExpression(includeElements) : IncludeExpression.Empty,
Filter = CreateFilterByIds(id.AsArray(), primaryIdAttribute, null)
};

if (fieldSelection == TopFieldSelection.OnlyIdAttribute)
{
var primarySelection = new FieldSelection();
FieldSelectors primarySelectors = primarySelection.GetOrCreateSelectors(primaryLayer.ResourceType);
primarySelectors.IncludeAttribute(primaryIdAttribute);

foreach (IncludeElementExpression include in includeElements)
{
primarySelectors.IncludeRelationship(include.Relationship, null);
}

primaryLayer.Selection = primarySelection;
}

return primaryLayer;
}

/// <inheritdoc />
public IEnumerable<(QueryLayer, RelationshipAttribute)> ComposeForGetTargetedSecondaryResourceIds(IIdentifiable primaryResource)
{
Expand Down
34 changes: 31 additions & 3 deletions src/JsonApiDotNetCore/Services/JsonApiResourceService.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
using System.Net;
using System.Runtime.CompilerServices;
using JetBrains.Annotations;
using JsonApiDotNetCore.AtomicOperations;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Diagnostics;
using JsonApiDotNetCore.Errors;
Expand Down Expand Up @@ -30,11 +31,12 @@ public class JsonApiResourceService<TResource, TId> : IResourceService<TResource
private readonly TraceLogWriter<JsonApiResourceService<TResource, TId>> _traceWriter;
private readonly IJsonApiRequest _request;
private readonly IResourceChangeTracker<TResource> _resourceChangeTracker;
private readonly IVersionTracker _versionTracker;
private readonly IResourceDefinitionAccessor _resourceDefinitionAccessor;

public JsonApiResourceService(IResourceRepositoryAccessor repositoryAccessor, IQueryLayerComposer queryLayerComposer, IPaginationContext paginationContext,
IJsonApiOptions options, ILoggerFactory loggerFactory, IJsonApiRequest request, IResourceChangeTracker<TResource> resourceChangeTracker,
IResourceDefinitionAccessor resourceDefinitionAccessor)
IVersionTracker versionTracker, IResourceDefinitionAccessor resourceDefinitionAccessor)
{
ArgumentGuard.NotNull(repositoryAccessor, nameof(repositoryAccessor));
ArgumentGuard.NotNull(queryLayerComposer, nameof(queryLayerComposer));
Expand All @@ -43,6 +45,7 @@ public JsonApiResourceService(IResourceRepositoryAccessor repositoryAccessor, IQ
ArgumentGuard.NotNull(loggerFactory, nameof(loggerFactory));
ArgumentGuard.NotNull(request, nameof(request));
ArgumentGuard.NotNull(resourceChangeTracker, nameof(resourceChangeTracker));
ArgumentGuard.NotNull(versionTracker, nameof(versionTracker));
ArgumentGuard.NotNull(resourceDefinitionAccessor, nameof(resourceDefinitionAccessor));

_repositoryAccessor = repositoryAccessor;
Expand All @@ -51,6 +54,7 @@ public JsonApiResourceService(IResourceRepositoryAccessor repositoryAccessor, IQ
_options = options;
_request = request;
_resourceChangeTracker = resourceChangeTracker;
_versionTracker = versionTracker;
_resourceDefinitionAccessor = resourceDefinitionAccessor;
_traceWriter = new TraceLogWriter<JsonApiResourceService<TResource, TId>>(loggerFactory);
}
Expand Down Expand Up @@ -226,7 +230,8 @@ private async Task RetrieveResourceCountForNonPrimaryEndpointAsync(TId id, HasMa
throw;
}

TResource resourceFromDatabase = await GetPrimaryResourceByIdAsync(resourceForDatabase.Id, TopFieldSelection.WithAllAttributes, cancellationToken);
TResource resourceFromDatabase =
await GetPrimaryResourceAfterWriteAsync(resourceForDatabase.Id, TopFieldSelection.WithAllAttributes, cancellationToken);

_resourceChangeTracker.SetFinallyStoredAttributeValues(resourceFromDatabase);

Expand Down Expand Up @@ -477,7 +482,7 @@ private async Task<TResource> GetForHasManyUpdateAsync(HasManyAttribute hasManyR
throw;
}

TResource afterResourceFromDatabase = await GetPrimaryResourceByIdAsync(id, TopFieldSelection.WithAllAttributes, cancellationToken);
TResource afterResourceFromDatabase = await GetPrimaryResourceAfterWriteAsync(id, TopFieldSelection.WithAllAttributes, cancellationToken);

_resourceChangeTracker.SetFinallyStoredAttributeValues(afterResourceFromDatabase);

Expand Down Expand Up @@ -522,6 +527,11 @@ public virtual async Task SetRelationshipAsync(TId leftId, string relationshipNa
AssertIsNotResourceVersionMismatch(exception);
throw;
}

if (_versionTracker.RequiresVersionTracking())
{
await GetPrimaryResourceAfterWriteAsync(leftId, TopFieldSelection.OnlyIdAttribute, cancellationToken);
}
}

/// <inheritdoc />
Expand Down Expand Up @@ -614,6 +624,24 @@ protected async Task<TResource> GetPrimaryResourceByIdAsync(TId id, TopFieldSele
return primaryResources.SingleOrDefault();
}

private async Task<TResource> GetPrimaryResourceAfterWriteAsync(TId id, TopFieldSelection fieldSelection, CancellationToken cancellationToken)
{
AssertPrimaryResourceTypeInJsonApiRequestIsNotNull(_request.PrimaryResourceType);

if (_versionTracker.RequiresVersionTracking())
{
QueryLayer queryLayer = _queryLayerComposer.ComposeForGetVersionsAfterWrite(id, _request.PrimaryResourceType, fieldSelection);
IReadOnlyCollection<TResource> primaryResources = await _repositoryAccessor.GetAsync<TResource>(queryLayer, cancellationToken);
TResource? primaryResource = primaryResources.SingleOrDefault();
AssertPrimaryResourceExists(primaryResource);

_versionTracker.CaptureVersions(_request.PrimaryResourceType, primaryResource);
return primaryResource;
}

return await GetPrimaryResourceByIdAsync(id, fieldSelection, cancellationToken);
}

protected async Task<TResource> GetPrimaryResourceForUpdateAsync(TId id, CancellationToken cancellationToken)
{
AssertPrimaryResourceTypeInJsonApiRequestIsNotNull(_request.PrimaryResourceType);
Expand Down
6 changes: 4 additions & 2 deletions test/DiscoveryTests/PrivateResourceService.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using JetBrains.Annotations;
using JsonApiDotNetCore.AtomicOperations;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Middleware;
using JsonApiDotNetCore.Queries;
Expand All @@ -14,8 +15,9 @@ public sealed class PrivateResourceService : JsonApiResourceService<PrivateResou
{
public PrivateResourceService(IResourceRepositoryAccessor repositoryAccessor, IQueryLayerComposer queryLayerComposer, IPaginationContext paginationContext,
IJsonApiOptions options, ILoggerFactory loggerFactory, IJsonApiRequest request, IResourceChangeTracker<PrivateResource> resourceChangeTracker,
IResourceDefinitionAccessor resourceDefinitionAccessor)
: base(repositoryAccessor, queryLayerComposer, paginationContext, options, loggerFactory, request, resourceChangeTracker, resourceDefinitionAccessor)
IVersionTracker versionTracker, IResourceDefinitionAccessor resourceDefinitionAccessor)
: base(repositoryAccessor, queryLayerComposer, paginationContext, options, loggerFactory, request, resourceChangeTracker, versionTracker,
resourceDefinitionAccessor)
{
}
}
2 changes: 2 additions & 0 deletions test/DiscoveryTests/ServiceDiscoveryFacadeTests.cs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
using FluentAssertions;
using JsonApiDotNetCore.AtomicOperations;
using JsonApiDotNetCore.Configuration;
using JsonApiDotNetCore.Middleware;
using JsonApiDotNetCore.Queries;
Expand Down Expand Up @@ -36,6 +37,7 @@ public ServiceDiscoveryFacadeTests()
_services.AddScoped(_ => new Mock<ITargetedFields>().Object);
_services.AddScoped(_ => new Mock<IResourceGraph>().Object);
_services.AddScoped(typeof(IResourceChangeTracker<>), typeof(ResourceChangeTracker<>));
_services.AddScoped(_ => new Mock<IVersionTracker>().Object);
_services.AddScoped(_ => new Mock<IResourceFactory>().Object);
_services.AddScoped(_ => new Mock<IPaginationContext>().Object);
_services.AddScoped(_ => new Mock<IQueryLayerComposer>().Object);
Expand Down
Loading

0 comments on commit 934f96a

Please sign in to comment.