Skip to content

Commit

Permalink
* fix non-split revision entities will always be inserted with the de…
Browse files Browse the repository at this point in the history
…fault value of field `DuplicateIndex`

* change the constraint of type param `TRevision` from `BaseRevisionWithSplitting` to `class, TBaseRevision`
@ `SaveEntitiesWithRevision()`

* opt-out EFCore change tracker for existing revision entities
* renamed from `AddSplitRevisions()`
@ `AddRevisionsWithDuplicateIndex()`
@ SaverWithRevision.cs

* change the constraint of type param `TRevision` from `BaseRevisionWithSplitting` to `TBaseRevision` @ `PostSaver.Save()`
* rename method `SplitToTable()` to `HasSplitTable()` @ `RevisionWithSplitting.ModelBuilder`
@ c#/crawler
  • Loading branch information
n0099 committed Jun 1, 2024
1 parent 44a35a8 commit 522f9b3
Show file tree
Hide file tree
Showing 8 changed files with 24 additions and 24 deletions.
18 changes: 9 additions & 9 deletions c#/crawler/src/Db/CrawlerDbContext.cs
Original file line number Diff line number Diff line change
Expand Up @@ -80,27 +80,27 @@ protected override void OnModelCreating(ModelBuilder b)
_ = new RevisionWithSplitting<BaseThreadRevision>
.ModelBuilder(b, "tbmcr_thread", e => new {e.Tid, e.TakenAt, e.DuplicateIndex})
.HasBaseTable<ThreadRevision>()
.SplitToTable<SplitViewCount>("viewCount");
.HasSplitTable<SplitViewCount>("viewCount");

_ = new RevisionWithSplitting<BaseReplyRevision>
.ModelBuilder(b, "tbmcr_reply", e => new {e.Pid, e.TakenAt, e.DuplicateIndex})
.HasBaseTable<ReplyRevision>()
.SplitToTable<ReplyRevision.SplitAgreeCount>("agreeCount")
.SplitToTable<SplitSubReplyCount>("subReplyCount")
.SplitToTable<SplitFloor>("floor");
.HasSplitTable<ReplyRevision.SplitAgreeCount>("agreeCount")
.HasSplitTable<SplitSubReplyCount>("subReplyCount")
.HasSplitTable<SplitFloor>("floor");

_ = new RevisionWithSplitting<BaseSubReplyRevision>
.ModelBuilder(b, "tbmcr_subReply", e => new {e.Spid, e.TakenAt, e.DuplicateIndex})
.HasBaseTable<SubReplyRevision>()
.SplitToTable<SubReplyRevision.SplitAgreeCount>("agreeCount")
.SplitToTable<SplitDisagreeCount>("disagreeCount");
.HasSplitTable<SubReplyRevision.SplitAgreeCount>("agreeCount")
.HasSplitTable<SplitDisagreeCount>("disagreeCount");

_ = new RevisionWithSplitting<BaseUserRevision>
.ModelBuilder(b, "tbmcr_user", e => new {e.Uid, e.TakenAt, e.DuplicateIndex})
.HasBaseTable<UserRevision>()
.SplitToTable<SplitIpGeolocation>("ipGeolocation")
.SplitToTable<SplitPortraitUpdatedAt>("portraitUpdatedAt")
.SplitToTable<SplitDisplayName>("displayName");
.HasSplitTable<SplitIpGeolocation>("ipGeolocation")
.HasSplitTable<SplitPortraitUpdatedAt>("portraitUpdatedAt")
.HasSplitTable<SplitDisplayName>("displayName");

b.Entity<SplitDisplayName>().Property(e => e.DisplayName).HasConversion<byte[]>();
b.Entity<User>().Property(e => e.DisplayName).HasConversion<byte[]>();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public ModelBuilder HasBaseTable<TRevision>() where TRevision : TBaseRevision
return this;
}

public ModelBuilder SplitToTable<TRevisionWithSplitting>(string tableNameSuffix)
public ModelBuilder HasSplitTable<TRevisionWithSplitting>(string tableNameSuffix)
where TRevisionWithSplitting : RevisionWithSplitting<TBaseRevision>
{
var visitor = new ReplaceParameterTypeVisitor<TBaseRevision, TRevisionWithSplitting>();
Expand Down
2 changes: 1 addition & 1 deletion c#/crawler/src/Tieba/Crawl/Saver/Post/PostSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ protected SaverChangeSet<TPost> Save<TRevision>(
Func<TPost, PostId> postIdSelector,
Func<TPost, TRevision> revisionFactory,
ExpressionStarter<TPost> existingPostPredicate)
where TRevision : BaseRevisionWithSplitting
where TRevision : TBaseRevision
{
var existingPostsKeyById = db.Set<TPost>().AsTracking()
.Where(existingPostPredicate).ToDictionary(postIdSelector);
Expand Down
6 changes: 3 additions & 3 deletions c#/crawler/src/Tieba/Crawl/Saver/Post/ReplySaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,9 @@ protected override Lazy<Dictionary<Type, AddSplitRevisionsDelegate>>
AddSplitRevisionsDelegatesKeyByEntityType =>
_addSplitRevisionsDelegatesKeyByEntityType ??= new(() => new()
{
{typeof(ReplyRevision.SplitFloor), AddSplitRevisions<ReplyRevision.SplitFloor>},
{typeof(ReplyRevision.SplitSubReplyCount), AddSplitRevisions<ReplyRevision.SplitSubReplyCount>},
{typeof(ReplyRevision.SplitAgreeCount), AddSplitRevisions<ReplyRevision.SplitAgreeCount>}
{typeof(ReplyRevision.SplitFloor), AddRevisionsWithDuplicateIndex<ReplyRevision.SplitFloor>},
{typeof(ReplyRevision.SplitSubReplyCount), AddRevisionsWithDuplicateIndex<ReplyRevision.SplitSubReplyCount>},
{typeof(ReplyRevision.SplitAgreeCount), AddRevisionsWithDuplicateIndex<ReplyRevision.SplitAgreeCount>}
});

public override bool UserFieldUpdateIgnorance(string propName, object? oldValue, object? newValue) => propName switch
Expand Down
4 changes: 2 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Saver/Post/SubReplySaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@ protected override Lazy<Dictionary<Type, AddSplitRevisionsDelegate>>
AddSplitRevisionsDelegatesKeyByEntityType =>
_addSplitRevisionsDelegatesKeyByEntityType ??= new(() => new()
{
{typeof(SubReplyRevision.SplitAgreeCount), AddSplitRevisions<SubReplyRevision.SplitAgreeCount>},
{typeof(SubReplyRevision.SplitDisagreeCount), AddSplitRevisions<SubReplyRevision.SplitDisagreeCount>},
{typeof(SubReplyRevision.SplitAgreeCount), AddRevisionsWithDuplicateIndex<SubReplyRevision.SplitAgreeCount>},
{typeof(SubReplyRevision.SplitDisagreeCount), AddRevisionsWithDuplicateIndex<SubReplyRevision.SplitDisagreeCount>},
});

public override bool UserFieldUpdateIgnorance
Expand Down
2 changes: 1 addition & 1 deletion c#/crawler/src/Tieba/Crawl/Saver/Post/ThreadSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ protected override Lazy<Dictionary<Type, AddSplitRevisionsDelegate>>
AddSplitRevisionsDelegatesKeyByEntityType =>
_addSplitRevisionsDelegatesKeyByEntityType ??= new(() => new()
{
{typeof(ThreadRevision.SplitViewCount), AddSplitRevisions<ThreadRevision.SplitViewCount>}
{typeof(ThreadRevision.SplitViewCount), AddRevisionsWithDuplicateIndex<ThreadRevision.SplitViewCount>}
});

public override bool UserFieldUpdateIgnorance
Expand Down
8 changes: 4 additions & 4 deletions c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ public abstract partial class SaverWithRevision<TBaseRevision, TEntityId>(
protected abstract Lazy<Dictionary<Type, AddSplitRevisionsDelegate>>
AddSplitRevisionsDelegatesKeyByEntityType { get; }

protected void AddSplitRevisions<TRevision>(CrawlerDbContext db, IEnumerable<TBaseRevision> revisions)
protected void AddRevisionsWithDuplicateIndex<TRevision>(CrawlerDbContext db, IEnumerable<TBaseRevision> revisions)
where TRevision : TBaseRevision
{
var newRevisions = revisions.OfType<TRevision>().ToList();
Expand All @@ -20,7 +20,7 @@ protected void AddSplitRevisions<TRevision>(CrawlerDbContext db, IEnumerable<TBa

// https://github.com/npgsql/npgsql/issues/4437
// https://github.com/dotnet/efcore/issues/32092
var existingRevisions = dbSet
var existingRevisions = dbSet.AsNoTracking()
.Where(newRevisions.Aggregate(
LinqKit.PredicateBuilder.New<TRevision>(),
(predicate, newRevision) => predicate.Or(LinqKit.PredicateBuilder
Expand Down Expand Up @@ -62,7 +62,7 @@ protected void SaveEntitiesWithRevision<TEntity, TRevision>(
UserSaver.FieldChangeIgnorance? userFieldUpdateIgnorance = null,
UserSaver.FieldChangeIgnorance? userFieldRevisionIgnorance = null)
where TEntity : RowVersionedEntity
where TRevision : BaseRevisionWithSplitting
where TRevision : class, TBaseRevision
{
db.Set<TEntity>().AddRange(isExistingEntityLookup[false]); // newly added
var newRevisions = isExistingEntityLookup[true].Select(newEntity =>
Expand Down Expand Up @@ -147,7 +147,7 @@ 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>().AddRange(
AddRevisionsWithDuplicateIndex<TRevision>(db,
newRevisions.Where(rev => !rev.IsAllFieldsIsNullExceptSplit()));
newRevisions.OfType<RevisionWithSplitting<TBaseRevision>>()
.SelectMany(rev => rev.SplitEntities)
Expand Down
6 changes: 3 additions & 3 deletions c#/crawler/src/Tieba/Crawl/Saver/UserSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,9 @@ protected override Lazy<Dictionary<Type, AddSplitRevisionsDelegate>>
AddSplitRevisionsDelegatesKeyByEntityType =>
_addSplitRevisionsDelegatesKeyByEntityType ??= new(() => new()
{
{typeof(UserRevision.SplitDisplayName), AddSplitRevisions<UserRevision.SplitDisplayName>},
{typeof(UserRevision.SplitPortraitUpdatedAt), AddSplitRevisions<UserRevision.SplitPortraitUpdatedAt>},
{typeof(UserRevision.SplitIpGeolocation), AddSplitRevisions<UserRevision.SplitIpGeolocation>}
{typeof(UserRevision.SplitDisplayName), AddRevisionsWithDuplicateIndex<UserRevision.SplitDisplayName>},
{typeof(UserRevision.SplitPortraitUpdatedAt), AddRevisionsWithDuplicateIndex<UserRevision.SplitPortraitUpdatedAt>},
{typeof(UserRevision.SplitIpGeolocation), AddRevisionsWithDuplicateIndex<UserRevision.SplitIpGeolocation>}
});

protected override Uid RevisionEntityIdSelector(BaseUserRevision entity) => entity.Uid;
Expand Down

0 comments on commit 522f9b3

Please sign in to comment.