-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
+ class
ReplySignatureSaver
which split from partial class `ReplySa…
…ver` @ c#/crawler
- Loading branch information
Showing
3 changed files
with
78 additions
and
73 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using System.IO.Hashing; | ||
|
||
namespace tbm.Crawler.Tieba.Crawl.Saver; | ||
|
||
public class ReplySignatureSaver | ||
{ | ||
private static readonly HashSet<UniqueSignature> GlobalLocks = []; | ||
private readonly List<UniqueSignature> _localLocks = []; | ||
|
||
public Action SaveReplySignatures(CrawlerDbContext db, IEnumerable<ReplyPost> replies) | ||
{ | ||
SharedHelper.GetNowTimestamp(out var now); | ||
var signatures = replies | ||
.Where(r => r is {SignatureId: not null, Signature: not null}) | ||
.DistinctBy(r => r.SignatureId) | ||
.Select(r => new ReplySignature | ||
{ | ||
UserId = r.AuthorUid, | ||
SignatureId = (uint)r.SignatureId!, | ||
XxHash3 = XxHash3.Hash(r.Signature!), | ||
ProtoBufBytes = r.Signature!, | ||
FirstSeenAt = now, | ||
LastSeenAt = now | ||
}).ToList(); | ||
if (signatures.Count == 0) return () => { }; | ||
|
||
var uniqueSignatures = signatures | ||
.ConvertAll(s => new UniqueSignature(s.SignatureId, s.XxHash3)); | ||
var existingSignatures = ( | ||
from s in db.ReplySignatures.AsTracking() | ||
where uniqueSignatures.Select(us => us.Id).Contains(s.SignatureId) | ||
|
||
// server side eval doesn't need ByteArrayEqualityComparer | ||
&& uniqueSignatures.Select(us => us.XxHash3).Contains(s.XxHash3) | ||
select s | ||
).ToList(); | ||
(from existing in existingSignatures | ||
join newInReply in signatures on existing.SignatureId equals newInReply.SignatureId | ||
select (existing, newInReply)) | ||
.ForEach(t => t.existing.LastSeenAt = t.newInReply.LastSeenAt); | ||
|
||
lock (GlobalLocks) | ||
{ | ||
var newSignaturesExceptLocked = signatures | ||
.ExceptBy(existingSignatures.Select(s => s.SignatureId), s => s.SignatureId) | ||
.ExceptBy(GlobalLocks, s => new(s.SignatureId, s.XxHash3)) | ||
.ToList(); | ||
if (newSignaturesExceptLocked.Count == 0) return () => { }; | ||
|
||
_localLocks.AddRange(newSignaturesExceptLocked | ||
.Select(s => new UniqueSignature(s.SignatureId, s.XxHash3))); | ||
GlobalLocks.UnionWith(_localLocks); | ||
db.ReplySignatures.AddRange(newSignaturesExceptLocked); | ||
} | ||
return () => | ||
{ | ||
lock (GlobalLocks) GlobalLocks.ExceptWith(_localLocks); | ||
}; | ||
} | ||
|
||
private sealed record UniqueSignature(uint Id, byte[] XxHash3) | ||
{ | ||
public bool Equals(UniqueSignature? other) => | ||
other != null && Id == other.Id && new ByteArrayEqualityComparer().Equals(XxHash3, other.XxHash3); | ||
|
||
public override int GetHashCode() | ||
{ | ||
var hash = default(HashCode); | ||
hash.Add(Id); | ||
hash.AddBytes(XxHash3); | ||
return hash.ToHashCode(); | ||
} | ||
} | ||
} |