-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
…` that sharing the same key `AuthorUid` of `BasePost` entities with a invoking to `Helper.LogDifferentValuesSharingTheSameKeyInEntities()` @ `AuthorRevisionSaver.Save()` * replace logging different values of `Signature` that sharing the same key `SignatureId` of `ReplyPost` entities with a invoking to `Helper.LogDifferentValuesSharingTheSameKeyInEntities()` @ `Save()` * turn record `ReplySignatureProjection` into class `SignatureIdAndValueEqualityComparer` that implements `EqualityComparer<ValueTuple<Nullable<uint>, Array<byte>>` @ ReplySignatureSaver.cs @ c#/crawler
- Loading branch information
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,20 +26,15 @@ public Action Save(CrawlerDbContext db, IEnumerable<ReplyPost> replies) | |
}).ToList(); | ||
if (signatures.Count == 0) return () => { }; | ||
if (signatures.Count != repliesWithSignature | ||
.GroupBy(r => new ReplySignatureProjection(r.SignatureId!.Value, r.Signature!)) | ||
.Count()) ( | ||
from r in repliesWithSignature | ||
group r by r.SignatureId into g | ||
where g.Count() > 1 | ||
from r in g | ||
group r by new ReplySignatureProjection(r.SignatureId!.Value, r.Signature!) into g | ||
group g by g.Key.SignatureId into gg | ||
where gg.Count() > 1 | ||
from g in gg | ||
select g) | ||
.ForEach(g => logger.LogWarning( | ||
"Multiple entities with different value of revisioning field sharing the same signature id {}: {}", | ||
g.Key.SignatureId, SharedHelper.UnescapedJsonSerialize(g))); | ||
.GroupBy(r => (r.SignatureId!.Value, r.Signature!), | ||
comparer: SignatureIdAndValueEqualityComparer.Instance) | ||
.Count()) | ||
Helper.LogDifferentValuesSharingTheSameKeyInEntities(logger, | ||
repliesWithSignature, | ||
nameof(ReplyPost.SignatureId), | ||
r => r.SignatureId, | ||
r => r.Signature, | ||
SignatureIdAndValueEqualityComparer.Instance); | ||
|
||
var existingSignatures = ( | ||
from s in db.ReplySignatures.AsTracking() | ||
|
@@ -63,18 +58,20 @@ join newInReply in signatures on existing.SignatureId equals newInReply.Signatur | |
return locks.Dispose; | ||
} | ||
|
||
private sealed record ReplySignatureProjection(uint SignatureId, byte[] Signature) | ||
private class SignatureIdAndValueEqualityComparer : EqualityComparer<(uint? SignatureId, byte[]? Signature)> | ||
Check failure on line 61 in c#/crawler/src/Tieba/Crawl/Saver/ReplySignatureSaver.cs GitHub Actions / build (crawler)
Check failure on line 61 in c#/crawler/src/Tieba/Crawl/Saver/ReplySignatureSaver.cs GitHub Actions / build (crawler)
Check failure on line 61 in c#/crawler/src/Tieba/Crawl/Saver/ReplySignatureSaver.cs GitHub Actions / build (crawler)
Check failure on line 61 in c#/crawler/src/Tieba/Crawl/Saver/ReplySignatureSaver.cs GitHub Actions / build (crawler)
Check failure on line 61 in c#/crawler/src/Tieba/Crawl/Saver/ReplySignatureSaver.cs GitHub Actions / build (crawler)
Check failure on line 61 in c#/crawler/src/Tieba/Crawl/Saver/ReplySignatureSaver.cs GitHub Actions / build (crawler)
|
||
{ | ||
public bool Equals(ReplySignatureProjection? other) => | ||
other != null | ||
&& SignatureId == other.SignatureId | ||
&& ByteArrayEqualityComparer.Instance.Equals(Signature, other.Signature); | ||
public static SignatureIdAndValueEqualityComparer Instance { get; } = new(); | ||
|
||
public override int GetHashCode() | ||
public override bool Equals((uint? SignatureId, byte[]? Signature) x, (uint? SignatureId, byte[]? Signature) y) => | ||
x == y || | ||
(x.SignatureId == y.SignatureId | ||
&& ByteArrayEqualityComparer.Instance.Equals(x.Signature, y.Signature)); | ||
|
||
public override int GetHashCode((uint? SignatureId, byte[]? Signature) obj) | ||
{ | ||
var hash = default(HashCode); | ||
hash.Add(SignatureId); | ||
hash.AddBytes(Signature); | ||
hash.Add(obj.SignatureId); | ||
hash.AddBytes(obj.Signature); | ||
return hash.ToHashCode(); | ||
} | ||
} | ||
|