Skip to content

Commit

Permalink
* add type alias NullFieldsBitMask for ushort to silent ReSharper…
Browse files Browse the repository at this point in the history
… inspection `UseSymbolAlias` @ GlobalUsings.cs

@ crawler
* fix all violations of Roslyn rule `RCS1077: Optimize LINQ method call`
@ c#
  • Loading branch information
n0099 committed Mar 23, 2024
1 parent 63eba2d commit 46f0f66
Show file tree
Hide file tree
Showing 9 changed files with 11 additions and 11 deletions.
2 changes: 1 addition & 1 deletion c#/crawler/src/GlobalUsings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,6 @@
global using tbm.Crawler.Worker;
global using tbm.Shared;

global using TbClient;
global using TbClient.Api.Request;
global using TbClient.Api.Response;
global using TbClient.Post;
Expand All @@ -59,3 +58,4 @@
global using Time = System.UInt32;
global using Page = System.UInt32;
global using FailureCount = System.UInt16;
global using NullFieldsBitMask = System.UInt16;
4 changes: 2 additions & 2 deletions c#/crawler/src/Tieba/Crawl/CrawlerLocks.cs
Original file line number Diff line number Diff line change
Expand Up @@ -104,9 +104,9 @@ protected override void LogTrace()
lock (_failed)
{
logger.LogTrace("Lock: type={} crawlingIdCount={} crawlingPageCount={} crawlingPageCountsKeyById={} failedIdCount={} failedPageCount={} failures={}", LockType,
_crawling.Count, _crawling.Values.Select(d => d.Count).Sum(),
_crawling.Count, _crawling.Values.Sum(d => d.Count),
Helper.UnescapedJsonSerialize(_crawling.ToDictionary(pair => pair.Key.ToString(), pair => pair.Value.Count)),
_failed.Count, _failed.Values.Select(d => d.Count).Sum(),
_failed.Count, _failed.Values.Sum(d => d.Count),
Helper.UnescapedJsonSerialize(_failed.ToDictionary(pair => pair.Key.ToString(), pair => pair.Value)));
}
}
Expand Down
4 changes: 2 additions & 2 deletions c#/crawler/src/Tieba/Crawl/Saver/CommonInSavers.cs
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,7 @@ bool IsTimestampingFieldName(string name) => name is nameof(IPost.LastSeenAt)
revisionNullFieldsBitMask |= whichBitToMask; // mask the corresponding field bit with 1
}
}
if (revision != null) revision.NullFieldsBitMask = (ushort?)revisionNullFieldsBitMask.NullIfZero();
if (revision != null) revision.NullFieldsBitMask = (NullFieldsBitMask?)revisionNullFieldsBitMask.NullIfZero();
return revision;
}).OfType<TRevision>().ToList();
if (newRevisions.Count == 0) return; // quick exit to prevent execute sql with WHERE FALSE clause
Expand All @@ -109,7 +109,7 @@ bool IsTimestampingFieldName(string name) => name is nameof(IPost.LastSeenAt)
.ForEach(g => RevisionUpsertDelegatesKeyBySplitEntityType[g.Key](db, g));
}

protected virtual ushort GetRevisionNullFieldBitMask(string fieldName) => throw new NotImplementedException();
protected virtual NullFieldsBitMask GetRevisionNullFieldBitMask(string fieldName) => throw new NotImplementedException();

private static bool IsLatestReplierUser(string pName, PropertyEntry p, EntityEntry entry)
{
Expand Down
2 changes: 1 addition & 1 deletion c#/crawler/src/Tieba/Crawl/Saver/ReplySaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ public override SaverChangeSet<ReplyPost> SavePosts(CrawlerDbContext db)
}

[SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1025:Code should not contain multiple whitespace in a row")]
protected override ushort GetRevisionNullFieldBitMask(string fieldName) => fieldName switch
protected override NullFieldsBitMask GetRevisionNullFieldBitMask(string fieldName) => fieldName switch
{
nameof(ReplyPost.IsFold) => 1 << 2,
nameof(ReplyPost.DisagreeCount) => 1 << 4,
Expand Down
2 changes: 1 addition & 1 deletion c#/crawler/src/Tieba/Crawl/Saver/SubReplySaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -53,5 +53,5 @@ public override SaverChangeSet<SubReplyPost> SavePosts(CrawlerDbContext db)
return changeSet;
}

protected override ushort GetRevisionNullFieldBitMask(string fieldName) => 0;
protected override NullFieldsBitMask GetRevisionNullFieldBitMask(string fieldName) => 0;
}
2 changes: 1 addition & 1 deletion c#/crawler/src/Tieba/Crawl/Saver/ThreadSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ public override SaverChangeSet<ThreadPost> SavePosts(CrawlerDbContext db) =>
PredicateBuilder.New<ThreadPost>(th => Posts.Keys.Contains(th.Tid)));

[SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1025:Code should not contain multiple whitespace in a row")]
protected override ushort GetRevisionNullFieldBitMask(string fieldName) => fieldName switch
protected override NullFieldsBitMask GetRevisionNullFieldBitMask(string fieldName) => fieldName switch
{
nameof(ThreadPost.StickyType) => 1,
nameof(ThreadPost.TopicType) => 1 << 1,
Expand Down
2 changes: 1 addition & 1 deletion c#/crawler/src/Tieba/Crawl/UserParserAndSaver.cs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ protected override Dictionary<Type, RevisionUpsertDelegate>
};

[SuppressMessage("StyleCop.CSharp.SpacingRules", "SA1025:Code should not contain multiple whitespace in a row")]
protected override ushort GetRevisionNullFieldBitMask(string fieldName) => fieldName switch
protected override NullFieldsBitMask GetRevisionNullFieldBitMask(string fieldName) => fieldName switch
{
nameof(User.Name) => 1,
nameof(User.Gender) => 1 << 3,
Expand Down
2 changes: 1 addition & 1 deletion c#/crawler/src/Worker/ArchiveCrawlWorker.cs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ string GetHumanizedElapsedTimeThenRestart()

if (cancellationToken.IsCancellationRequested) return;
var savedReplies = await CrawlReplies(savedThreads, _fid, cancellationToken);
var savedReplyCount = savedReplies.Select(pair => pair.Value.AllAfter.Count).Sum();
var savedReplyCount = savedReplies.Sum(pair => pair.Value.AllAfter.Count);
logger.LogInformation("Archive for {} replies within {} threads in the page {} of forum {} finished after {:F2}s",
savedReplyCount, savedThreadCount, page, _forumName, GetHumanizedElapsedTimeThenRestart());
_ = Interlocked.Add(ref totalSavedReplyCount, savedReplyCount);
Expand Down
2 changes: 1 addition & 1 deletion c#/imagePipeline/src/Ocr/TesseractRecognizer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ public Func<PreprocessedTextBox, TesseractRecognitionResult> RecognizePreprocess
var text = string.Concat(components.Select(t => t.Text)).Trim();
if (text == "") shouldFallbackToPaddleOcr = true;
var averageConfidence = components.Count != 0
? components.Select(c => c.Confidence).Average().RoundToByte()
? components.Average(c => c.Confidence).RoundToByte()
: (byte)0;

return new(imageKey, box, text, averageConfidence, isVertical, shouldFallbackToPaddleOcr);
Expand Down

0 comments on commit 46f0f66

Please sign in to comment.