-
Notifications
You must be signed in to change notification settings - Fork 0
fix: give every item its own share of a shared peer directory #86
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
70 changes: 70 additions & 0 deletions
70
src/Sleezer/Download/Clients/Soulseek/SlskdDirectoryPartitioner.cs
This file contains hidden or 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,70 @@ | ||
| using NzbDrone.Plugin.Sleezer.Download.Clients.Soulseek.Models; | ||
|
|
||
| namespace NzbDrone.Plugin.Sleezer.Download.Clients.Soulseek; | ||
|
|
||
| /// <summary>One item's share of a peer directory, and the files no tracked item claimed.</summary> | ||
| public record SlskdDirectoryPartition( | ||
| List<(SlskdDownloadItem Item, SlskdDownloadDirectory Slice)> Owners, | ||
| SlskdDownloadDirectory? Unclaimed); | ||
|
|
||
| /// <summary> | ||
| /// Splits one peer directory's transfers into per-item slices. Two Lidarr albums | ||
| /// can share a peer directory, so the group belongs to every item that enqueued | ||
| /// part of it — not to whichever item happens to own the first file. | ||
| /// </summary> | ||
| public static class SlskdDirectoryPartitioner | ||
| { | ||
| /// <summary>Splits a peer directory into one slice per owning item, plus the rest.</summary> | ||
| public static SlskdDirectoryPartition Partition( | ||
| SlskdDownloadDirectory directory, | ||
| IEnumerable<SlskdDownloadItem> candidates, | ||
| string username) | ||
| { | ||
| List<SlskdDownloadFile> files = directory.Files ?? []; | ||
| if (files.Count == 0) | ||
| return new SlskdDirectoryPartition([], null); | ||
|
|
||
| List<(SlskdDownloadItem Item, SlskdDownloadDirectory Slice)> owners = []; | ||
| HashSet<string> claimed = new(StringComparer.OrdinalIgnoreCase); | ||
|
|
||
| foreach (SlskdDownloadItem candidate in candidates) | ||
| { | ||
| // An item with no username yet is still a candidate for any peer. | ||
| if (candidate.Username != null && | ||
| !string.Equals(candidate.Username, username, StringComparison.OrdinalIgnoreCase)) | ||
| continue; | ||
|
|
||
| // Deliberately not deduped against `claimed`: two items can legitimately | ||
| // enqueue the same remote file, and one transfer serves both. | ||
| List<SlskdDownloadFile> owned = files.Where(f => candidate.OwnsAcceptedFile(f.Filename)).ToList(); | ||
| if (owned.Count == 0) | ||
| continue; | ||
|
|
||
| owners.Add((candidate, new SlskdDownloadDirectory(directory.Directory, owned.Count, owned))); | ||
| foreach (SlskdDownloadFile file in owned) | ||
| claimed.Add(file.Filename); | ||
| } | ||
|
|
||
| List<SlskdDownloadFile> unclaimed = files.Where(f => !claimed.Contains(f.Filename)).ToList(); | ||
|
|
||
| return new SlskdDirectoryPartition( | ||
| owners, | ||
| unclaimed.Count > 0 ? new SlskdDownloadDirectory(directory.Directory, unclaimed.Count, unclaimed) : null); | ||
| } | ||
|
|
||
| /// <summary>Adds the item whose ID hashes the whole directory, if it isn't already an owner.</summary> | ||
| public static List<(SlskdDownloadItem Item, SlskdDownloadDirectory Slice)> WithKeyedOwner( | ||
| SlskdDirectoryPartition partition, | ||
| SlskdDownloadItem? keyed, | ||
| SlskdDownloadDirectory directory) | ||
| { | ||
| List<(SlskdDownloadItem Item, SlskdDownloadDirectory Slice)> owners = [.. partition.Owners]; | ||
|
|
||
| // Returning early on the hash match would starve a co-owner that enqueued | ||
| // only part of the directory — the same starvation this class exists to fix. | ||
| if (keyed != null && !owners.Any(owner => ReferenceEquals(owner.Item, keyed))) | ||
| owners.Add((keyed, directory)); | ||
|
|
||
| return owners; | ||
| } | ||
| } |
This file contains hidden or 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 hidden or 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
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.