Skip to content

Commit

Permalink
* fix all violation of Roslyn analyzer rules and ReSharper inspections
Browse files Browse the repository at this point in the history
* rename variable `nonTracked` to `notTracked` in linq query syntax @ `SaverChangeSet.AllAfter`
* fix outdated comments @ SaverWithRevision.cs
@ crawler

* rename params of `(Except|Intersect)ByKey()` from `first` & `second` to `pairs` & `keys` @ shared/ExtensionMethods.cs
@ c#
  • Loading branch information
n0099 committed Jul 24, 2024
1 parent 2ed6374 commit a87f2d6
Show file tree
Hide file tree
Showing 6 changed files with 20 additions and 23 deletions.
2 changes: 0 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Saver/Post/PostSaver.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using LinqKit;

namespace tbm.Crawler.Tieba.Crawl.Saver.Post;

public abstract class PostSaver<TPost, TBaseRevision, TPostId>(
Expand Down
2 changes: 0 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Saver/Post/ThreadSaver.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
using LinqKit;

namespace tbm.Crawler.Tieba.Crawl.Saver.Post;

public partial class ThreadSaver(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,7 @@ private void Save<TPost, TRevision, TValue>(

// 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 once NotAccessedPositionalProperty.Global
public record UniqueAuthorRevision(Fid Fid, Uid Uid);

Check warning on line 95 in c#/crawler/src/Tieba/Crawl/Saver/Related/AuthorRevisionSaver.cs

View workflow job for this annotation

GitHub Actions / runs-on (macos-latest) / ReSharper

"[NotAccessedPositionalProperty.Global] Positional property 'tbm.Crawler.Tieba.Crawl.Saver.Related.AuthorRevisionSaver.UniqueAuthorRevision.Uid' is never accessed (except in implicit Equals/ToString implementations)" on /Users/runner/work/open-tbm/open-tbm/c#/crawler/src/Tieba/Crawl/Saver/Related/AuthorRevisionSaver.cs(95,4765)

Check warning on line 95 in c#/crawler/src/Tieba/Crawl/Saver/Related/AuthorRevisionSaver.cs

View workflow job for this annotation

GitHub Actions / runs-on (ubuntu-latest) / ReSharper

"[NotAccessedPositionalProperty.Global] Positional property 'tbm.Crawler.Tieba.Crawl.Saver.Related.AuthorRevisionSaver.UniqueAuthorRevision.Uid' is never accessed (except in implicit Equals/ToString implementations)" on /home/runner/work/open-tbm/open-tbm/c#/crawler/src/Tieba/Crawl/Saver/Related/AuthorRevisionSaver.cs(95,4765)

Check warning on line 95 in c#/crawler/src/Tieba/Crawl/Saver/Related/AuthorRevisionSaver.cs

View workflow job for this annotation

GitHub Actions / runs-on (windows-latest) / ReSharper

"[NotAccessedPositionalProperty.Global] Positional property 'tbm.Crawler.Tieba.Crawl.Saver.Related.AuthorRevisionSaver.UniqueAuthorRevision.Uid' is never accessed (except in implicit Equals/ToString implementations)" on D:\a\open-tbm\open-tbm\c#\crawler\src\Tieba\Crawl\Saver\Related\AuthorRevisionSaver.cs(95,4765)

private sealed class LatestAuthorRevisionProjection<TValue>
Expand Down
6 changes: 3 additions & 3 deletions c#/crawler/src/Tieba/Crawl/Saver/SaverChangeSet.cs
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,9 @@ public class SaverChangeSet<TPost>(

// https://stackoverflow.com/questions/3404975/left-outer-join-in-linq/23558389#23558389
public IReadOnlyCollection<TPost> 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();
}
20 changes: 10 additions & 10 deletions c#/crawler/src/Tieba/Crawl/Saver/SaverWithRevision.cs
Original file line number Diff line number Diff line change
Expand Up @@ -62,14 +62,8 @@ public partial class SaverWithRevision<TBaseRevision, TRevisionId>
}
public partial class SaverWithRevision<TBaseRevision, TRevisionId>
{
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>(TEntity Existing, TEntity New) where TEntity : RowVersionedEntity;
protected record MaybeExistingAndNewEntity<TEntity>(TEntity? Existing, TEntity New) where TEntity : RowVersionedEntity;

protected IEnumerable<ExistingAndNewEntity<TEntity>> SaveNewEntities<TEntity>(
CrawlerDbContext db,
IReadOnlyCollection<MaybeExistingAndNewEntity<TEntity>> maybeEntities)
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -141,7 +135,7 @@ protected void SaveExistingEntityRevisions<TEntity, TRevision>(
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)
Expand All @@ -151,7 +145,7 @@ protected void SaveExistingEntityRevisions<TEntity, TRevision>(
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={}",
Expand All @@ -163,7 +157,7 @@ protected void SaveExistingEntityRevisions<TEntity, TRevision>(
{
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
Expand Down Expand Up @@ -195,4 +189,10 @@ protected void SaveExistingEntityRevisions<TEntity, TRevision>(
.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>(TEntity Existing, TEntity New) where TEntity : RowVersionedEntity;
protected record MaybeExistingAndNewEntity<TEntity>(TEntity? Existing, TEntity New) where TEntity : RowVersionedEntity;
}
12 changes: 6 additions & 6 deletions c#/shared/src/ExtensionMethods.cs
Original file line number Diff line number Diff line change
Expand Up @@ -39,13 +39,13 @@ public static IEnumerable<TValue> Values<TKey, TValue>(this IEnumerable<KeyValue
pairs.Select(i => i.Value);

public static IEnumerable<KeyValuePair<TKey, TValue>> ExceptByKey<TKey, TValue>(
this IEnumerable<KeyValuePair<TKey, TValue>> first,
IEnumerable<TKey> second) =>
first.ExceptBy(second, pair => pair.Key);
this IEnumerable<KeyValuePair<TKey, TValue>> pairs,
IEnumerable<TKey> keys) =>
pairs.ExceptBy(keys, pair => pair.Key);
public static IEnumerable<KeyValuePair<TKey, TValue>> IntersectByKey<TKey, TValue>(
this IEnumerable<KeyValuePair<TKey, TValue>> first,
IEnumerable<TKey> second) =>
first.IntersectBy(second, pair => pair.Key);
this IEnumerable<KeyValuePair<TKey, TValue>> pairs,
IEnumerable<TKey> keys) =>
pairs.IntersectBy(keys, pair => pair.Key);

public static IEnumerable<T> Flatten2<T>(this IEnumerable<IEnumerable<T>> source) =>
source.SelectMany(i => i);
Expand Down

0 comments on commit a87f2d6

Please sign in to comment.