diff --git a/c#/crawler/src/Tieba/Crawl/Saver/Post/PostSaver.cs b/c#/crawler/src/Tieba/Crawl/Saver/Post/PostSaver.cs index 3bf71776..9c8aebed 100644 --- a/c#/crawler/src/Tieba/Crawl/Saver/Post/PostSaver.cs +++ b/c#/crawler/src/Tieba/Crawl/Saver/Post/PostSaver.cs @@ -1,5 +1,3 @@ -using LinqKit; - namespace tbm.Crawler.Tieba.Crawl.Saver.Post; public abstract class PostSaver( diff --git a/c#/crawler/src/Tieba/Crawl/Saver/Post/ThreadSaver.cs b/c#/crawler/src/Tieba/Crawl/Saver/Post/ThreadSaver.cs index 757bbdcc..a5cab2a7 100644 --- a/c#/crawler/src/Tieba/Crawl/Saver/Post/ThreadSaver.cs +++ b/c#/crawler/src/Tieba/Crawl/Saver/Post/ThreadSaver.cs @@ -1,5 +1,3 @@ -using LinqKit; - namespace tbm.Crawler.Tieba.Crawl.Saver.Post; public partial class ThreadSaver( diff --git a/c#/crawler/src/Tieba/Crawl/Saver/Related/AuthorRevisionSaver.cs b/c#/crawler/src/Tieba/Crawl/Saver/Related/AuthorRevisionSaver.cs index 43cead46..378c2c86 100644 --- a/c#/crawler/src/Tieba/Crawl/Saver/Related/AuthorRevisionSaver.cs +++ b/c#/crawler/src/Tieba/Crawl/Saver/Related/AuthorRevisionSaver.cs @@ -91,7 +91,9 @@ private void Save( // locking key only using AuthorRevision.Fid and Uid, ignoring TriggeredBy // this prevents inserting multiple entities with similar time and other fields with the same values + // ReSharper disable NotAccessedPositionalProperty.Global public record UniqueAuthorRevision(Fid Fid, Uid Uid); + // ReSharper restore NotAccessedPositionalProperty.Global private sealed class LatestAuthorRevisionProjection { diff --git a/c#/crawler/src/Tieba/Crawl/Saver/SaverChangeSet.cs b/c#/crawler/src/Tieba/Crawl/Saver/SaverChangeSet.cs index 87a83a0b..52070ed3 100644 --- a/c#/crawler/src/Tieba/Crawl/Saver/SaverChangeSet.cs +++ b/c#/crawler/src/Tieba/Crawl/Saver/SaverChangeSet.cs @@ -21,9 +21,9 @@ public class SaverChangeSet( // https://stackoverflow.com/questions/3404975/left-outer-join-in-linq/23558389#23558389 public IReadOnlyCollection AllAfter { get; } = ( - from nonTracked in existingAfterAndNewlyAdded + from notTracked in existingAfterAndNewlyAdded join inTracking in existingAfterInTracking - on postIdSelector(nonTracked) equals postIdSelector(inTracking) into inTrackings + on postIdSelector(notTracked) equals postIdSelector(inTracking) into inTrackings from inTracking in inTrackings.DefaultIfEmpty() - select inTracking ?? nonTracked).ToList().AsReadOnly(); + select inTracking ?? notTracked).ToList().AsReadOnly(); } diff --git a/c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs b/c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs index e30f2829..c3b446ec 100644 --- a/c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs +++ b/c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs @@ -62,14 +62,8 @@ public partial class SaverWithRevision } public partial class SaverWithRevision { - private static bool IsTimestampingFieldName(string name) => name is nameof(BasePost.LastSeenAt) - or nameof(TimestampedEntity.CreatedAt) or nameof(TimestampedEntity.UpdatedAt); - protected abstract NullFieldsBitMask GetRevisionNullFieldBitMask(string fieldName); - protected record ExistingAndNewEntity(TEntity Existing, TEntity New) where TEntity : RowVersionedEntity; - protected record MaybeExistingAndNewEntity(TEntity? Existing, TEntity New) where TEntity : RowVersionedEntity; - protected IEnumerable> SaveNewEntities( CrawlerDbContext db, IReadOnlyCollection> maybeEntities) @@ -103,7 +97,7 @@ on newNavigation.Metadata.Name equals existingNavigation.Metadata.Name // rollback changes that overwrite original values with the default value 0 or null // for all fields of TimestampedEntity and BasePost.LastSeenAt - // this will also affect the entity instance which entityInTracking references to it + // this will also affect the entity instance which existingEntity references to it entityEntry.Properties .Where(prop => prop.IsModified && IsTimestampingFieldName(prop.Metadata.Name)) .ForEach(prop => prop.IsModified = false); @@ -141,7 +135,7 @@ protected void SaveExistingEntityRevisions( pName, p.OriginalValue, p.CurrentValue))) { p.IsModified = false; - continue; // skip following revision check + continue; // skip any further revision check } if (FieldRevisionIgnorance( pName, p.OriginalValue, p.CurrentValue) @@ -151,7 +145,7 @@ protected void SaveExistingEntityRevisions( if (!IRevisionProperties.Cache[typeof(TRevision)].TryGetValue(pName, out var revisionProp)) { - object? ToHexWhenByteArray(object? value) => + static object? ToHexWhenByteArray(object? value) => value is byte[] bytes ? bytes.ToHex() : value; logger.LogWarning("Updating field {} is not existing in revision table, " + "newValue={}, oldValue={}, newObject={}, oldObject={}", @@ -163,7 +157,7 @@ protected void SaveExistingEntityRevisions( { revision ??= revisionFactory(existingEntity); - // quote from MSDN https://learn.microsoft.com/en-us/dotnet/api/system.reflection.propertyinfo.setvalue + // https://learn.microsoft.com/en-us/dotnet/api/system.reflection.propertyinfo.setvalue // If the property type of this PropertyInfo object is a value type and value is null // the property will be set to the default value for that type. // https://stackoverflow.com/questions/3049477/propertyinfo-setvalue-and-nulls @@ -195,4 +189,10 @@ protected void SaveExistingEntityRevisions( .GroupBy(pair => pair.Key, pair => pair.Value) .ForEach(g => AddSplitRevisionsDelegatesKeyByEntityType.Value[g.Key](db, g)); } + + private static bool IsTimestampingFieldName(string name) => name is nameof(BasePost.LastSeenAt) + or nameof(TimestampedEntity.CreatedAt) or nameof(TimestampedEntity.UpdatedAt); + + protected record ExistingAndNewEntity(TEntity Existing, TEntity New) where TEntity : RowVersionedEntity; + protected record MaybeExistingAndNewEntity(TEntity? Existing, TEntity New) where TEntity : RowVersionedEntity; } diff --git a/c#/shared/src/ExtensionMethods.cs b/c#/shared/src/ExtensionMethods.cs index e52d82f7..d55cd741 100644 --- a/c#/shared/src/ExtensionMethods.cs +++ b/c#/shared/src/ExtensionMethods.cs @@ -39,13 +39,13 @@ public static IEnumerable Values(this IEnumerable i.Value); public static IEnumerable> ExceptByKey( - this IEnumerable> first, - IEnumerable second) => - first.ExceptBy(second, pair => pair.Key); + this IEnumerable> pairs, + IEnumerable keys) => + pairs.ExceptBy(keys, pair => pair.Key); public static IEnumerable> IntersectByKey( - this IEnumerable> first, - IEnumerable second) => - first.IntersectBy(second, pair => pair.Key); + this IEnumerable> pairs, + IEnumerable keys) => + pairs.IntersectBy(keys, pair => pair.Key); public static IEnumerable Flatten2(this IEnumerable> source) => source.SelectMany(i => i); diff --git a/c#/shared/tbm.Shared.csproj b/c#/shared/tbm.Shared.csproj index 9f009742..add07017 100644 --- a/c#/shared/tbm.Shared.csproj +++ b/c#/shared/tbm.Shared.csproj @@ -8,7 +8,7 @@ - + @@ -18,12 +18,12 @@ - + - +