-
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.
* prevent selecting all fields of entity
TRevision
by casting `IQue…
…ryable<TRevision>` to `IQueryable<TBaseRevision>` then project to `RevisionIdWithDuplicateIndexProjectionFactory()` * quick exit when param `newRevisions` is empty to prevent execute sql with WHERE FALSE clause @ `AddRevisionsWithDuplicateIndex()` + method `RevisionIdWithDuplicateIndexProjectionFactory()` & nested class `RevisionIdWithDuplicateIndexProjection` * rename method `IsRevisionEntityIdEqualsExpression()` & `RevisionEntityIdSelector()` to `IsRevisionIdEqualsExpression()` & `RevisionIdSelector()` * rename type param `TEntityId` to `TRevisionId` and constrained to `struct` @ SaverWithRevision.cs @ c#/crawler
- Loading branch information
Showing
6 changed files
with
41 additions
and
15 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
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 |
---|---|---|
|
@@ -2,10 +2,11 @@ | |
|
||
namespace tbm.Crawler.Tieba.Crawl.Saver; | ||
|
||
public abstract partial class SaverWithRevision<TBaseRevision, TEntityId>( | ||
ILogger<SaverWithRevision<TBaseRevision, TEntityId>> logger) | ||
public abstract partial class SaverWithRevision<TBaseRevision, TRevisionId>( | ||
ILogger<SaverWithRevision<TBaseRevision, TRevisionId>> logger) | ||
: IRevisionProperties | ||
where TBaseRevision : BaseRevisionWithSplitting | ||
where TRevisionId : struct | ||
{ | ||
protected delegate void AddSplitRevisionsDelegate(CrawlerDbContext db, IEnumerable<TBaseRevision> revisions); | ||
protected abstract Lazy<Dictionary<Type, AddSplitRevisionsDelegate>> | ||
|
@@ -15,6 +16,7 @@ protected void AddRevisionsWithDuplicateIndex<TRevision>(CrawlerDbContext db, IE | |
where TRevision : TBaseRevision | ||
{ | ||
var newRevisions = revisions.OfType<TRevision>().ToList(); | ||
if (newRevisions.Count == 0) return; // quick exit to prevent execute sql with WHERE FALSE clause | ||
var dbSet = db.Set<TRevision>(); | ||
var visitor = new ReplaceParameterTypeVisitor<TBaseRevision, TRevision>(); | ||
|
||
|
@@ -26,11 +28,13 @@ protected void AddRevisionsWithDuplicateIndex<TRevision>(CrawlerDbContext db, IE | |
(predicate, newRevision) => predicate.Or(LinqKit.PredicateBuilder | ||
.New<TRevision>(existingRevision => existingRevision.TakenAt == newRevision.TakenAt) | ||
.And((Expression<Func<TRevision, bool>>)visitor | ||
.Visit(IsRevisionEntityIdEqualsExpression(newRevision)))))) | ||
.Visit(IsRevisionIdEqualsExpression(newRevision)))))) | ||
.Cast<TBaseRevision>() | ||
.Select(RevisionIdWithDuplicateIndexProjectionFactory()) | ||
.ToList(); | ||
(from existingRevision in existingRevisions | ||
join newRevision in newRevisions | ||
on RevisionEntityIdSelector(existingRevision) equals RevisionEntityIdSelector(newRevision) | ||
on existingRevision.RevisionId equals RevisionIdSelector(newRevision) | ||
select (existingRevision, newRevision)) | ||
.ForEach(t => | ||
t.newRevision.DuplicateIndex = (ushort)(t.existingRevision.DuplicateIndex + 1)); | ||
|
@@ -39,9 +43,18 @@ on RevisionEntityIdSelector(existingRevision) equals RevisionEntityIdSelector(ne | |
|
||
protected abstract NullFieldsBitMask GetRevisionNullFieldBitMask(string fieldName); | ||
|
||
protected abstract TEntityId RevisionEntityIdSelector(TBaseRevision entity); | ||
protected abstract TRevisionId RevisionIdSelector(TBaseRevision entity); | ||
protected abstract Expression<Func<TBaseRevision, bool>> | ||
IsRevisionEntityIdEqualsExpression(TBaseRevision newRevision); | ||
IsRevisionIdEqualsExpression(TBaseRevision newRevision); | ||
|
||
protected abstract Expression<Func<TBaseRevision, RevisionIdWithDuplicateIndexProjection>> | ||
RevisionIdWithDuplicateIndexProjectionFactory(); | ||
[SuppressMessage("ReSharper", "PropertyCanBeMadeInitOnly.Global")] | ||
protected class RevisionIdWithDuplicateIndexProjection | ||
{ | ||
public TRevisionId RevisionId { get; set; } | ||
public ushort DuplicateIndex { get; set; } | ||
} | ||
|
||
protected virtual bool ShouldIgnoreEntityRevision(string propName, PropertyEntry propEntry, EntityEntry entityEntry) => false; | ||
Check failure on line 59 in c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs GitHub Actions / build (crawler)
Check failure on line 59 in c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs GitHub Actions / build (crawler)
|
||
protected virtual bool FieldUpdateIgnorance(string propName, object? oldValue, object? newValue) => false; | ||
|
@@ -52,7 +65,7 @@ protected abstract Expression<Func<TBaseRevision, bool>> | |
_ => false | ||
}; | ||
} | ||
public abstract partial class SaverWithRevision<TBaseRevision, TEntityId> | ||
public abstract partial class SaverWithRevision<TBaseRevision, TRevisionId> | ||
{ | ||
protected void SaveEntitiesWithRevision<TEntity, TRevision>( | ||
CrawlerDbContext db, | ||
|
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