Skip to content

Commit

Permalink
* fix still saving changes and revisions for existing users whose id …
Browse files Browse the repository at this point in the history
…already get locked @ `UserSaver.Save()`

+ record `MaybeExistingAndNewEntity` & method `SaveNewEntities()` to reuse parts in `(User|Post)Saver.Save()`
* rename record `ExistingAndNewEntities` to `ExistingAndNewEntity` and remove suffix `-Entity` from its props
@ SaverWithRevision.cs
@ c#/crawler
  • Loading branch information
n0099 committed Jul 24, 2024
1 parent 2341c3e commit 2ed6374
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 23 deletions.
12 changes: 3 additions & 9 deletions c#/crawler/src/Tieba/Crawl/Saver/Post/PostSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ protected SaverChangeSet<TPost> Save<TRevision>(
Func<TPost, PostId> postIdSelector,
Func<TPost, TRevision> revisionFactory,
Func<IQueryable<TPost>, IQueryable<TPost>> postQueryTransformer,
Action<IEnumerable<ExistingAndNewEntities<TPost>>>? onBeforeSaveRevision = null)
Action<IEnumerable<ExistingAndNewEntity<TPost>>>? onBeforeSaveRevision = null)
where TRevision : TBaseRevision
{
var existingPosts = postQueryTransformer(db.Set<TPost>().AsTracking()).ToList();
Expand All @@ -39,15 +39,9 @@ protected SaverChangeSet<TPost> Save<TRevision>(
join existingPost in existingPosts
on postIdSelector(newPost) equals postIdSelector(existingPost) into existingPostsWithSameId
from existingPost in existingPostsWithSameId.DefaultIfEmpty()
select (existingPost, newPost)).ToList();

db.Set<TPost>().AddRange(maybeExistingAndNewPosts
.Where(t => t.existingPost == null).Select(t => t.newPost));
var existingAndNewPosts = maybeExistingAndNewPosts
.Where(t => t.existingPost != null)
.Select(t => new ExistingAndNewEntities<TPost>(t.existingPost, t.newPost))
.ToList();
select new MaybeExistingAndNewEntity<TPost>(existingPost, newPost)).ToList();

var existingAndNewPosts = SaveNewEntities(db, maybeExistingAndNewPosts).ToList();
SaveExistingEntities(db, existingAndNewPosts);
onBeforeSaveRevision?.Invoke(existingAndNewPosts);
SaveExistingEntityRevisions(db, revisionFactory, existingAndNewPosts);
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 @@ -20,7 +20,7 @@ public override SaverChangeSet<ThreadPost> Save(CrawlerDbContext db) => Save(db,
.Include(th => th.LatestReplier),
existingAndNewPosts =>
PostSaveHandlers += threadLatestReplierSaver.SaveFromThread(db,
existingAndNewPosts.Select(t => t.ExistingEntity).ToList()));
existingAndNewPosts.Select(t => t.Existing).ToList()));
}
public partial class ThreadSaver
{
Expand Down
20 changes: 16 additions & 4 deletions c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -67,12 +67,24 @@ private static bool IsTimestampingFieldName(string name) => name is nameof(BaseP

protected abstract NullFieldsBitMask GetRevisionNullFieldBitMask(string fieldName);

Check failure on line 68 in c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs

View workflow job for this annotation

GitHub Actions / runs-on (macos-latest) / build (crawler)

'protected' members should come before 'private' members (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1202.md)

Check failure on line 68 in c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs

View workflow job for this annotation

GitHub Actions / runs-on (macos-latest) / build (crawler)

'protected' members should come before 'private' members (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1202.md)

Check failure on line 68 in c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs

View workflow job for this annotation

GitHub Actions / runs-on (ubuntu-latest) / build (crawler)

'protected' members should come before 'private' members (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1202.md)

Check failure on line 68 in c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs

View workflow job for this annotation

GitHub Actions / runs-on (ubuntu-latest) / build (crawler)

'protected' members should come before 'private' members (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1202.md)

Check failure on line 68 in c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs

View workflow job for this annotation

GitHub Actions / runs-on (windows-latest) / build (crawler)

'protected' members should come before 'private' members (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1202.md)

Check failure on line 68 in c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs

View workflow job for this annotation

GitHub Actions / runs-on (windows-latest) / build (crawler)

'protected' members should come before 'private' members (https://github.com/DotNetAnalyzers/StyleCopAnalyzers/blob/master/documentation/SA1202.md)

protected record ExistingAndNewEntities<TEntity>
(TEntity ExistingEntity, TEntity NewEntity) where TEntity : RowVersionedEntity;
protected record ExistingAndNewEntity<TEntity>(TEntity Existing, TEntity New) where TEntity : RowVersionedEntity;
protected record MaybeExistingAndNewEntity<TEntity>(TEntity? Existing, TEntity New) where TEntity : RowVersionedEntity;

protected IEnumerable<ExistingAndNewEntity<TEntity>> SaveNewEntities<TEntity>(

Check failure on line 73 in c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs

View workflow job for this annotation

GitHub Actions / runs-on (macos-latest) / build (crawler)

Check failure on line 73 in c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs

View workflow job for this annotation

GitHub Actions / runs-on (macos-latest) / build (crawler)

Check failure on line 73 in c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs

View workflow job for this annotation

GitHub Actions / runs-on (ubuntu-latest) / build (crawler)

Check failure on line 73 in c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs

View workflow job for this annotation

GitHub Actions / runs-on (ubuntu-latest) / build (crawler)

Check failure on line 73 in c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs

View workflow job for this annotation

GitHub Actions / runs-on (windows-latest) / build (crawler)

Check failure on line 73 in c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs

View workflow job for this annotation

GitHub Actions / runs-on (windows-latest) / build (crawler)

CrawlerDbContext db,
IReadOnlyCollection<MaybeExistingAndNewEntity<TEntity>> maybeEntities)
where TEntity : RowVersionedEntity
{
db.Set<TEntity>().AddRange(maybeEntities
.Where(entity => entity.Existing == null).Select(entity => entity.New));
return maybeEntities
.Where(entity => entity.Existing != null)
.Select(entity => new ExistingAndNewEntity<TEntity>(entity.Existing!, entity.New));
}

protected void SaveExistingEntities<TEntity>(
CrawlerDbContext db,
IEnumerable<ExistingAndNewEntities<TEntity>> existingAndNewEntities)
IEnumerable<ExistingAndNewEntity<TEntity>> existingAndNewEntities)
where TEntity : RowVersionedEntity =>
existingAndNewEntities.ForEach(existingAndNew =>
{
Expand Down Expand Up @@ -100,7 +112,7 @@ on newNavigation.Metadata.Name equals existingNavigation.Metadata.Name
protected void SaveExistingEntityRevisions<TEntity, TRevision>(
CrawlerDbContext db,
Func<TEntity, TRevision> revisionFactory,
IEnumerable<ExistingAndNewEntities<TEntity>> existingAndNewEntities,
IEnumerable<ExistingAndNewEntity<TEntity>> existingAndNewEntities,
UserSaver.FieldChangeIgnorance? userFieldUpdateIgnorance = null,
UserSaver.FieldChangeIgnorance? userFieldRevisionIgnorance = null)
where TEntity : RowVersionedEntity
Expand Down
13 changes: 4 additions & 9 deletions c#/crawler/src/Tieba/Crawl/Saver/UserSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -32,19 +32,14 @@ public void Save(
var existingUsers = (from user in db.Users.AsTracking()
where newlyLocked.Contains(user.Uid)
select user).ToList();
var maybeExistingAndNewUsers = (from newUser in users.Values
var maybeExistingAndNewUsers = (
from newUser in users.IntersectByKey(newlyLocked).Values()
join existingUser in existingUsers
on newUser.Uid equals existingUser.Uid into existingUsersWithSameId
from existingUser in existingUsersWithSameId.DefaultIfEmpty()
select (existingUser, newUser)).ToList();

db.Users.AddRange(maybeExistingAndNewUsers
.Where(t => t.existingUser == null).Select(t => t.newUser));
var existingAndNewUsers = maybeExistingAndNewUsers
.Where(t => t.existingUser != null)
.Select(t => new ExistingAndNewEntities<User>(t.existingUser, t.newUser))
.ToList();
select new MaybeExistingAndNewEntity<User>(existingUser, newUser)).ToList();

var existingAndNewUsers = SaveNewEntities(db, maybeExistingAndNewUsers).ToList();
SaveExistingEntities(db, existingAndNewUsers);
SaveExistingEntityRevisions(db,
u => new UserRevision
Expand Down

0 comments on commit 2ed6374

Please sign in to comment.