Skip to content

Commit

Permalink
extract method loadOverallContribution
Browse files Browse the repository at this point in the history
  • Loading branch information
anthonyraymond committed Aug 16, 2024
1 parent a55c8d0 commit 9157a49
Showing 1 changed file with 15 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -31,18 +31,7 @@ public SharedTorrentService(SharedTorrentRepository torrents, DomainEventPublish
public void create(TorrentMetadata metadata) {
var events = new ArrayList<DomainEvent>();

var overallContributions = this.overallContributions.load(metadata.infoHash())
.orElseGet(() -> {
Contribution overall = Contribution.ZERO;
if (config.skipDownload().get()) {
// return a fully Downloaded contribution when the torrent is not yet known and skip download is true
overall = new Contribution(new DownloadAmount(metadata.size().bytes()), new UploadAmount(0));
}
this.overallContributions.save(metadata.infoHash(), overall);

return overall;
});

var overallContributions = loadOverallContribution(metadata);
var left = new Left(Math.max(metadata.size().bytes() - overallContributions.downloaded().bytes(), 0));

var torrent = new SharedTorrent(metadata.infoHash(), overallContributions, left);
Expand All @@ -56,6 +45,20 @@ public void create(TorrentMetadata metadata) {
publish(events);
}

private Contribution loadOverallContribution(TorrentMetadata metadata) {
return this.overallContributions.load(metadata.infoHash())
.orElseGet(() -> {
Contribution overall = Contribution.ZERO;
if (config.skipDownload().get()) {
// return a fully Downloaded contribution when the torrent is not yet known and skip download is true
overall = new Contribution(new DownloadAmount(metadata.size().bytes()), new UploadAmount(0));
}
this.overallContributions.save(metadata.infoHash(), overall);

return overall;
});
}

public void registerPeers(InfoHash infoHash, Swarm.TrackerUniqueIdentifier identifier, Peers peers) {
var torrent = torrents.findByTorrentInfoHash(infoHash).orElseThrow(() -> new UnknownSharedTorrentException("No torrent found for %s".formatted(infoHash)));

Expand Down

0 comments on commit 9157a49

Please sign in to comment.