-
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.
+ class
SaverLocks
for AuthorRevisionSaver
, ReplySignatureSaver
…
… & `UserSaver` * move class `CrawlerLocks` from namespace `tbm.Crawler.Tieba.Crawl` into its child`.Crawler` @ c#/crawler
- Loading branch information
Showing
6 changed files
with
83 additions
and
83 deletions.
There are no files selected for viewing
2 changes: 1 addition & 1 deletion
2
c#/crawler/src/Tieba/Crawl/CrawlerLocks.cs → ...r/src/Tieba/Crawl/Crawler/CrawlerLocks.cs
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 |
---|---|---|
@@ -0,0 +1,30 @@ | ||
namespace tbm.Crawler.Tieba.Crawl.Saver; | ||
|
||
public class SaverLocks<TKey> | ||
{ | ||
public delegate T NewLocksFactory<out T>(IReadOnlySet<TKey> alreadyLocked); | ||
public delegate IEnumerable<TKey> LockingKeysSelector<in T>(T newlyLocked); | ||
|
||
private static readonly HashSet<TKey> GlobalLocks = []; | ||
private readonly List<TKey> _localLocks = []; | ||
|
||
public void AcquireLocksThen<TNewLock>( | ||
Action<IReadOnlyCollection<TNewLock>> payload, | ||
NewLocksFactory<IReadOnlyCollection<TNewLock>> newLocksFactory, | ||
LockingKeysSelector<IReadOnlyCollection<TNewLock>> lockingKeysSelector) | ||
{ | ||
lock (GlobalLocks) | ||
{ | ||
var newLocks = newLocksFactory(GlobalLocks); | ||
if (newLocks.Count == 0) return; | ||
_localLocks.AddRange(lockingKeysSelector(newLocks)); | ||
GlobalLocks.UnionWith(_localLocks); | ||
payload(newLocks); | ||
} | ||
} | ||
|
||
public void ReleaseLocalLocked() | ||
{ | ||
lock (GlobalLocks) GlobalLocks.ExceptWith(_localLocks); | ||
} | ||
} |
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