From 6925c0812add9075dc3749ef84454f70821abc59 Mon Sep 17 00:00:00 2001 From: n0099 Date: Sat, 11 May 2024 08:37:40 +0800 Subject: [PATCH] * rename delegate `RevisionUpsertDelegate` to `AddRevisionDelegate` * rename prop `RevisionUpsertDelegatesKeyBySplitEntityType` to `AddRevisionDelegatesKeyBySplitEntityType` @ SaverWithRevision.cs * replace all invokes to `DbSet<>.UpsertRange().NoUpdate().Run()` with `DbSet<>.AddRange()` @ Tieba/Crawl - NuGet package `FlexLabs.EntityFrameworkCore.Upsert` @ c#/crawler --- c#/crawler/packages.lock.json | 9 --------- c#/crawler/src/Tieba/Crawl/Facade/ThreadCrawlFacade.cs | 2 +- c#/crawler/src/Tieba/Crawl/Saver/BaseSaver.cs | 7 +++---- c#/crawler/src/Tieba/Crawl/Saver/Post/ReplySaver.cs | 10 +++++----- c#/crawler/src/Tieba/Crawl/Saver/Post/SubReplySaver.cs | 8 ++++---- c#/crawler/src/Tieba/Crawl/Saver/Post/ThreadSaver.cs | 6 +++--- c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs | 4 ++-- c#/crawler/src/Tieba/Crawl/Saver/UserSaver.cs | 10 +++++----- c#/crawler/tbm.Crawler.csproj | 1 - 9 files changed, 23 insertions(+), 34 deletions(-) diff --git a/c#/crawler/packages.lock.json b/c#/crawler/packages.lock.json index 3d19b874..1a310929 100644 --- a/c#/crawler/packages.lock.json +++ b/c#/crawler/packages.lock.json @@ -17,15 +17,6 @@ "resolved": "2.1.35", "contentHash": "YKRwjVfrG7GYOovlGyQoMvr1/IJdn+7QzNXJxyMh0YfFF5yvDmTYaJOVYWsckreNjGsGSEtrMTpnzxTUq/tZQw==" }, - "FlexLabs.EntityFrameworkCore.Upsert": { - "type": "Direct", - "requested": "[8.0.0, )", - "resolved": "8.0.0", - "contentHash": "Z7KFJhVEuhLQMK763gXTaqfYFd3MknU0Du9IngXEtFXaEP4rKMxRlaWYntv58z3DEj1/UqBjLxojKMrUEDwNcw==", - "dependencies": { - "Microsoft.EntityFrameworkCore.Relational": "8.0.0" - } - }, "Humanizer.Core.zh-Hans": { "type": "Direct", "requested": "[2.14.1, )", diff --git a/c#/crawler/src/Tieba/Crawl/Facade/ThreadCrawlFacade.cs b/c#/crawler/src/Tieba/Crawl/Facade/ThreadCrawlFacade.cs index 404b5821..3838adc5 100644 --- a/c#/crawler/src/Tieba/Crawl/Facade/ThreadCrawlFacade.cs +++ b/c#/crawler/src/Tieba/Crawl/Facade/ThreadCrawlFacade.cs @@ -39,7 +39,7 @@ protected override void BeforeCommitSaveHook(CrawlerDbContext db, UserSaver user u.CreatedAt = Helper.GetNowTimestamp(); return u; }); - _ = db.Users.UpsertRange(newLatestRepliersExceptLocked).NoUpdate().Run(); + db.Users.AddRange(newLatestRepliersExceptLocked); } protected override void PostParseHook( diff --git a/c#/crawler/src/Tieba/Crawl/Saver/BaseSaver.cs b/c#/crawler/src/Tieba/Crawl/Saver/BaseSaver.cs index e81954f5..6bd591af 100644 --- a/c#/crawler/src/Tieba/Crawl/Saver/BaseSaver.cs +++ b/c#/crawler/src/Tieba/Crawl/Saver/BaseSaver.cs @@ -98,13 +98,12 @@ bool IsTimestampingFieldName(string name) => name is nameof(BasePost.LastSeenAt) }).OfType().ToList(); if (newRevisions.Count == 0) return; // quick exit to prevent execute sql with WHERE FALSE clause - _ = db.Set().UpsertRange( - newRevisions.Where(rev => !rev.IsAllFieldsIsNullExceptSplit())) - .NoUpdate().Run(); + db.Set().AddRange( + newRevisions.Where(rev => !rev.IsAllFieldsIsNullExceptSplit())); newRevisions.OfType>() .SelectMany(rev => rev.SplitEntities) .GroupBy(pair => pair.Key, pair => pair.Value) - .ForEach(g => RevisionUpsertDelegatesKeyBySplitEntityType[g.Key](db, g)); + .ForEach(g => AddRevisionDelegatesKeyBySplitEntityType[g.Key](db, g)); } private static bool IsLatestReplierUser(string pName, PropertyEntry p, EntityEntry entry) diff --git a/c#/crawler/src/Tieba/Crawl/Saver/Post/ReplySaver.cs b/c#/crawler/src/Tieba/Crawl/Saver/Post/ReplySaver.cs index e9e3661f..c89cf756 100644 --- a/c#/crawler/src/Tieba/Crawl/Saver/Post/ReplySaver.cs +++ b/c#/crawler/src/Tieba/Crawl/Saver/Post/ReplySaver.cs @@ -25,23 +25,23 @@ public override IFieldChangeIgnorance.FieldChangeIgnoranceDelegates _ => false }); - protected override Dictionary - RevisionUpsertDelegatesKeyBySplitEntityType { get; } = new() + protected override Dictionary + AddRevisionDelegatesKeyBySplitEntityType { get; } = new() { { typeof(ReplyRevision.SplitFloor), (db, revisions) => db.Set() - .UpsertRange(revisions.OfType()).NoUpdate().Run() + .AddRange(revisions.OfType()) }, { typeof(ReplyRevision.SplitSubReplyCount), (db, revisions) => db.Set() - .UpsertRange(revisions.OfType()).NoUpdate().Run() + .AddRange(revisions.OfType()) }, { typeof(ReplyRevision.SplitAgreeCount), (db, revisions) => db.Set() - .UpsertRange(revisions.OfType()).NoUpdate().Run() + .AddRange(revisions.OfType()) } }; diff --git a/c#/crawler/src/Tieba/Crawl/Saver/Post/SubReplySaver.cs b/c#/crawler/src/Tieba/Crawl/Saver/Post/SubReplySaver.cs index 209011f9..b0406327 100644 --- a/c#/crawler/src/Tieba/Crawl/Saver/Post/SubReplySaver.cs +++ b/c#/crawler/src/Tieba/Crawl/Saver/Post/SubReplySaver.cs @@ -26,18 +26,18 @@ public override IFieldChangeIgnorance.FieldChangeIgnoranceDelegates _ => false }, (_, _, _, _) => false); - protected override Dictionary - RevisionUpsertDelegatesKeyBySplitEntityType { get; } = new() + protected override Dictionary + AddRevisionDelegatesKeyBySplitEntityType { get; } = new() { { typeof(SubReplyRevision.SplitAgreeCount), (db, revisions) => db.Set() - .UpsertRange(revisions.OfType()).NoUpdate().Run() + .AddRange(revisions.OfType()) }, { typeof(SubReplyRevision.SplitDisagreeCount), (db, revisions) => db.Set() - .UpsertRange(revisions.OfType()).NoUpdate().Run() + .AddRange(revisions.OfType()) } }; diff --git a/c#/crawler/src/Tieba/Crawl/Saver/Post/ThreadSaver.cs b/c#/crawler/src/Tieba/Crawl/Saver/Post/ThreadSaver.cs index 639e9890..00894192 100644 --- a/c#/crawler/src/Tieba/Crawl/Saver/Post/ThreadSaver.cs +++ b/c#/crawler/src/Tieba/Crawl/Saver/Post/ThreadSaver.cs @@ -22,13 +22,13 @@ public override IFieldChangeIgnorance.FieldChangeIgnoranceDelegates _ => false }, (_, _, _, _) => false); - protected override Dictionary - RevisionUpsertDelegatesKeyBySplitEntityType { get; } = new() + protected override Dictionary + AddRevisionDelegatesKeyBySplitEntityType { get; } = new() { { typeof(ThreadRevision.SplitViewCount), (db, revisions) => db.Set() - .UpsertRange(revisions.OfType()).NoUpdate().Run() + .AddRange(revisions.OfType()) } }; diff --git a/c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs b/c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs index 78336ea5..8590fc93 100644 --- a/c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs +++ b/c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs @@ -3,9 +3,9 @@ namespace tbm.Crawler.Tieba.Crawl.Saver; public abstract class SaverWithRevision : IRevisionProperties where TBaseRevision : BaseRevisionWithSplitting { - protected delegate void RevisionUpsertDelegate(CrawlerDbContext db, IEnumerable revision); + protected delegate void AddRevisionDelegate(CrawlerDbContext db, IEnumerable revision); - protected virtual IReadOnlyDictionary RevisionUpsertDelegatesKeyBySplitEntityType => + protected virtual IReadOnlyDictionary AddRevisionDelegatesKeyBySplitEntityType => throw new NotSupportedException(); protected virtual NullFieldsBitMask GetRevisionNullFieldBitMask(string fieldName) => diff --git a/c#/crawler/src/Tieba/Crawl/Saver/UserSaver.cs b/c#/crawler/src/Tieba/Crawl/Saver/UserSaver.cs index b80893b1..738c99fb 100644 --- a/c#/crawler/src/Tieba/Crawl/Saver/UserSaver.cs +++ b/c#/crawler/src/Tieba/Crawl/Saver/UserSaver.cs @@ -2,23 +2,23 @@ namespace tbm.Crawler.Tieba.Crawl.Saver; public partial class UserSaver { - protected override Dictionary - RevisionUpsertDelegatesKeyBySplitEntityType { get; } = new() + protected override Dictionary + AddRevisionDelegatesKeyBySplitEntityType { get; } = new() { { typeof(UserRevision.SplitDisplayName), (db, revisions) => db.Set() - .UpsertRange(revisions.OfType()).NoUpdate().Run() + .AddRange(revisions.OfType()) }, { typeof(UserRevision.SplitPortraitUpdatedAt), (db, revisions) => db.Set() - .UpsertRange(revisions.OfType()).NoUpdate().Run() + .AddRange(revisions.OfType()) }, { typeof(UserRevision.SplitIpGeolocation), (db, revisions) => db.Set() - .UpsertRange(revisions.OfType()).NoUpdate().Run() + .AddRange(revisions.OfType()) } }; diff --git a/c#/crawler/tbm.Crawler.csproj b/c#/crawler/tbm.Crawler.csproj index 7adf4aa8..bc8a94ba 100644 --- a/c#/crawler/tbm.Crawler.csproj +++ b/c#/crawler/tbm.Crawler.csproj @@ -9,7 +9,6 @@ -