-
Notifications
You must be signed in to change notification settings - Fork 529
New issue
Have a question about this project? # for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “#”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? # to your account
Remove +1 assumption for input versions in $import #3685
Conversation
@@ -164,12 +164,12 @@ internal class SqlServerFhirDataStore : IFhirDataStore, IProvideCapability | |||
(var transactionId, var minSequenceId) = await StoreClient.MergeResourcesBeginTransactionAsync(resources.Count, cancellationToken); | |||
|
|||
var index = 0; | |||
var mergeWrappers = new List<MergeResourceWrapper>(); | |||
var mergeWrappersPlus = new List<(MergeResourceWrapper Wrapper, bool KeepVersion, int ResourceVersion, int? ExistingVersion)>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Can we use the same name than before? "plus" without context could lead confusion, perhaps, adding a comment? #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Plus indicates that this variable contains not only merge wrappers but other data points too. There are 3 other data points.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mergeWrappersAndVersions :)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fine.
// In each group find the smallest version higher then existing | ||
prevResourceId = string.Empty; | ||
var notSetInResoureGroup = false; | ||
foreach (var mergeWrapperPlus in mergeWrappersPlus.Where(_ => _.KeepVersion && _.ExistingVersion != 0).OrderBy(_ => _.Wrapper.ResourceWrapper.ResourceId).ThenBy(_ => _.ResourceVersion)) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: _
as a variable confuses tooling, x
is just as short ;) #Closed
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
:-) I like _ as it denotes that it is derived from the definition of enumerable, nothing else. In complex cases when there is more than one thing derived, I use meaningful variable names to differentiate between cases. BTW All code scanner are happy...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When hovering over _, my VS shows correct type details.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This one is also documented in the C# fundamentals: https://learn.microsoft.com/en-us/dotnet/csharp/fundamentals/functional/discards. I'd suggest that this would break existing assumptions that someone might have when reading this code.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Replaced _ by x.
BTW In this article they are talking about treating _ as discards in the outputs of some methods. I don't see _ used inside LINQ expression as an output. So, I don't see a violation here.
if (mergeWrappers.Count > 0) // Do not call DB with empty input | ||
// Resources with input versions (keepVersion=true) might not have hasVersionToCompare set. Fix it here. | ||
// Resources with keepVersion=true must be in separate call, and not mixed with keepVersion=false ones. | ||
// Sort them in groups by resource id and order by version. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could this be done with Linq to simplify? Something like:
mergeWrappersPlus
.Where(x => x.KeepVersion && x.ExistingVersion != 0)
.GroupBy(key => key.Wrapper.ResourceWrapper.ResourceId, items => items.OrderBy(y =>y.ResourceVersion))
``` #ByDesign
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was not able to find a clear way to apply required logic only using Linq. So I did what I did. I hope it is obvious, hence simple.
@@ -164,12 +164,12 @@ internal class SqlServerFhirDataStore : IFhirDataStore, IProvideCapability | |||
(var transactionId, var minSequenceId) = await StoreClient.MergeResourcesBeginTransactionAsync(resources.Count, cancellationToken); | |||
|
|||
var index = 0; | |||
var mergeWrappers = new List<MergeResourceWrapper>(); | |||
var mergeWrappersPlus = new List<(MergeResourceWrapper Wrapper, bool KeepVersion, int ResourceVersion, int? ExistingVersion)>(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
mergeWrappersAndVersions :)
prevResourceId = string.Empty; | ||
var notSetInResoureGroup = false; | ||
foreach (var mergeWrapper in mergeWrappersWithVersions.Where(_ => _.KeepVersion && _.ExistingVersion != 0).OrderBy(_ => _.Wrapper.ResourceWrapper.ResourceId).ThenBy(_ => _.ResourceVersion)) | ||
{ | ||
if (prevResourceId != mergeWrapper.Wrapper.ResourceWrapper.ResourceId) // this should reset flag on each resource id group including first. | ||
{ | ||
notSetInResoureGroup = true; | ||
} | ||
|
||
prevResourceId = mergeWrapper.Wrapper.ResourceWrapper.ResourceId; | ||
|
||
if (notSetInResoureGroup && mergeWrapper.ResourceVersion > mergeWrapper.ExistingVersion) | ||
{ | ||
mergeWrapper.Wrapper.HasVersionToCompare = true; | ||
notSetInResoureGroup = false; | ||
} | ||
} |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The linq version would look like this:
foreach (var wrapperWithVersionToCompare in mergeWrappersWithVersions
.Where(x => x.KeepVersion && x.ExistingVersion != 0)
.GroupBy(key => key.Wrapper.ResourceWrapper.ResourceId)
.Select(group => group.MinBy(y => y.ResourceVersion))
.Where(x => x.ResourceVersion > x.ExistingVersion))
{
wrapperWithVersionToCompare.Wrapper.HasVersionToCompare = true;
}
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Cool. Your version is more difficult for me to understand, so I want to stick to my current one.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sergey, I understand you have code that functions as intended. In this case though, .net has a GroupBy
function that we can leverage instead of needed to code it ourselves.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Brendan,
I agree that this code can be rewritten. I still prefer my simple loop because it seems more transparent to me.
I already ran large scale perf test on my version before submitting PR, and I would like to avoid time waste.
If you are comfortable with your logic, please make changes directly in the PR.
There is demand to honor arbitrary non sequential resource versions provided for $import. Currently versions are assumed to be sequential ints. This PR enables any int version.