Skip to content

Commit

Permalink
* rename delegate RevisionUpsertDelegate to AddRevisionDelegate
Browse files Browse the repository at this point in the history
* 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
  • Loading branch information
n0099 committed May 11, 2024
1 parent 8ee3dd1 commit 6925c08
Show file tree
Hide file tree
Showing 9 changed files with 23 additions and 34 deletions.
9 changes: 0 additions & 9 deletions c#/crawler/packages.lock.json
Original file line number Diff line number Diff line change
Expand Up @@ -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, )",
Expand Down
2 changes: 1 addition & 1 deletion c#/crawler/src/Tieba/Crawl/Facade/ThreadCrawlFacade.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
7 changes: 3 additions & 4 deletions c#/crawler/src/Tieba/Crawl/Saver/BaseSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -98,13 +98,12 @@ bool IsTimestampingFieldName(string name) => name is nameof(BasePost.LastSeenAt)
}).OfType<TRevision>().ToList();
if (newRevisions.Count == 0) return; // quick exit to prevent execute sql with WHERE FALSE clause

_ = db.Set<TRevision>().UpsertRange(
newRevisions.Where(rev => !rev.IsAllFieldsIsNullExceptSplit()))
.NoUpdate().Run();
db.Set<TRevision>().AddRange(
newRevisions.Where(rev => !rev.IsAllFieldsIsNullExceptSplit()));
newRevisions.OfType<RevisionWithSplitting<TBaseRevision>>()
.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)
Expand Down
10 changes: 5 additions & 5 deletions c#/crawler/src/Tieba/Crawl/Saver/Post/ReplySaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,23 @@ public override IFieldChangeIgnorance.FieldChangeIgnoranceDelegates
_ => false
});

protected override Dictionary<Type, RevisionUpsertDelegate>
RevisionUpsertDelegatesKeyBySplitEntityType { get; } = new()
protected override Dictionary<Type, AddRevisionDelegate>
AddRevisionDelegatesKeyBySplitEntityType { get; } = new()
{
{
typeof(ReplyRevision.SplitFloor), (db, revisions) =>
db.Set<ReplyRevision.SplitFloor>()
.UpsertRange(revisions.OfType<ReplyRevision.SplitFloor>()).NoUpdate().Run()
.AddRange(revisions.OfType<ReplyRevision.SplitFloor>())
},
{
typeof(ReplyRevision.SplitSubReplyCount), (db, revisions) =>
db.Set<ReplyRevision.SplitSubReplyCount>()
.UpsertRange(revisions.OfType<ReplyRevision.SplitSubReplyCount>()).NoUpdate().Run()
.AddRange(revisions.OfType<ReplyRevision.SplitSubReplyCount>())
},
{
typeof(ReplyRevision.SplitAgreeCount), (db, revisions) =>
db.Set<ReplyRevision.SplitAgreeCount>()
.UpsertRange(revisions.OfType<ReplyRevision.SplitAgreeCount>()).NoUpdate().Run()
.AddRange(revisions.OfType<ReplyRevision.SplitAgreeCount>())
}
};

Expand Down
8 changes: 4 additions & 4 deletions c#/crawler/src/Tieba/Crawl/Saver/Post/SubReplySaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,18 +26,18 @@ public override IFieldChangeIgnorance.FieldChangeIgnoranceDelegates
_ => false
}, (_, _, _, _) => false);

protected override Dictionary<Type, RevisionUpsertDelegate>
RevisionUpsertDelegatesKeyBySplitEntityType { get; } = new()
protected override Dictionary<Type, AddRevisionDelegate>
AddRevisionDelegatesKeyBySplitEntityType { get; } = new()
{
{
typeof(SubReplyRevision.SplitAgreeCount), (db, revisions) =>
db.Set<SubReplyRevision.SplitAgreeCount>()
.UpsertRange(revisions.OfType<SubReplyRevision.SplitAgreeCount>()).NoUpdate().Run()
.AddRange(revisions.OfType<SubReplyRevision.SplitAgreeCount>())
},
{
typeof(SubReplyRevision.SplitDisagreeCount), (db, revisions) =>
db.Set<SubReplyRevision.SplitDisagreeCount>()
.UpsertRange(revisions.OfType<SubReplyRevision.SplitDisagreeCount>()).NoUpdate().Run()
.AddRange(revisions.OfType<SubReplyRevision.SplitDisagreeCount>())
}
};

Expand Down
6 changes: 3 additions & 3 deletions c#/crawler/src/Tieba/Crawl/Saver/Post/ThreadSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -22,13 +22,13 @@ public override IFieldChangeIgnorance.FieldChangeIgnoranceDelegates
_ => false
}, (_, _, _, _) => false);

protected override Dictionary<Type, RevisionUpsertDelegate>
RevisionUpsertDelegatesKeyBySplitEntityType { get; } = new()
protected override Dictionary<Type, AddRevisionDelegate>
AddRevisionDelegatesKeyBySplitEntityType { get; } = new()
{
{
typeof(ThreadRevision.SplitViewCount), (db, revisions) =>
db.Set<ThreadRevision.SplitViewCount>()
.UpsertRange(revisions.OfType<ThreadRevision.SplitViewCount>()).NoUpdate().Run()
.AddRange(revisions.OfType<ThreadRevision.SplitViewCount>())
}
};

Expand Down
4 changes: 2 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ namespace tbm.Crawler.Tieba.Crawl.Saver;
public abstract class SaverWithRevision<TBaseRevision> : IRevisionProperties
where TBaseRevision : BaseRevisionWithSplitting
{
protected delegate void RevisionUpsertDelegate(CrawlerDbContext db, IEnumerable<TBaseRevision> revision);
protected delegate void AddRevisionDelegate(CrawlerDbContext db, IEnumerable<TBaseRevision> revision);

protected virtual IReadOnlyDictionary<Type, RevisionUpsertDelegate> RevisionUpsertDelegatesKeyBySplitEntityType =>
protected virtual IReadOnlyDictionary<Type, AddRevisionDelegate> AddRevisionDelegatesKeyBySplitEntityType =>
throw new NotSupportedException();

protected virtual NullFieldsBitMask GetRevisionNullFieldBitMask(string fieldName) =>
Expand Down
10 changes: 5 additions & 5 deletions c#/crawler/src/Tieba/Crawl/Saver/UserSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@ namespace tbm.Crawler.Tieba.Crawl.Saver;

public partial class UserSaver
{
protected override Dictionary<Type, RevisionUpsertDelegate>
RevisionUpsertDelegatesKeyBySplitEntityType { get; } = new()
protected override Dictionary<Type, AddRevisionDelegate>
AddRevisionDelegatesKeyBySplitEntityType { get; } = new()
{
{
typeof(UserRevision.SplitDisplayName), (db, revisions) =>
db.Set<UserRevision.SplitDisplayName>()
.UpsertRange(revisions.OfType<UserRevision.SplitDisplayName>()).NoUpdate().Run()
.AddRange(revisions.OfType<UserRevision.SplitDisplayName>())
},
{
typeof(UserRevision.SplitPortraitUpdatedAt), (db, revisions) =>
db.Set<UserRevision.SplitPortraitUpdatedAt>()
.UpsertRange(revisions.OfType<UserRevision.SplitPortraitUpdatedAt>()).NoUpdate().Run()
.AddRange(revisions.OfType<UserRevision.SplitPortraitUpdatedAt>())
},
{
typeof(UserRevision.SplitIpGeolocation), (db, revisions) =>
db.Set<UserRevision.SplitIpGeolocation>()
.UpsertRange(revisions.OfType<UserRevision.SplitIpGeolocation>()).NoUpdate().Run()
.AddRange(revisions.OfType<UserRevision.SplitIpGeolocation>())
}
};

Expand Down
1 change: 0 additions & 1 deletion c#/crawler/tbm.Crawler.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
<ItemGroup>
<PackageReference Include="AngleSharp" Version="1.1.2" />
<PackageReference Include="Dapper" Version="2.1.35" />
<PackageReference Include="FlexLabs.EntityFrameworkCore.Upsert" Version="8.0.0" />
<PackageReference Include="Humanizer.Core.zh-Hans" Version="2.14.1" />
<PackageReference Include="linq2db.EntityFrameworkCore" Version="8.1.0" />
<PackageReference Include="linq2db.PostgreSQL" Version="5.4.1" />
Expand Down

0 comments on commit 6925c08

Please sign in to comment.