Skip to content
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

feat: add on_piece_completed method on TorrentStorage #219

Merged
merged 2 commits into from
Aug 28, 2024

Conversation

cocool97
Copy link

@cocool97 cocool97 commented Aug 27, 2024

As discussed in #211, a custom TorrentStorage that would remove blocks when read by pread_exact() is currently not working as this method is also called when checking torrent piece hashes.

This PR adds a new method on TorrentStorage trait, on_piece_completed() that is automatically called by rqbit when a piece has been downloaded and checked. This allows implementations to deal with this new information as they think is best.

InMemoryExampleStorage structure has been updated to remove blocks when they have been read by a stream.

For all other implementations this new method only returns Ok(()) as I don't think that this has to be handled explicitly.

Note: I think there is however still an issue but apart from this new method added as blocks are loop-downloaded as they are no more available in the TorrentStorage

crates/librqbit/src/storage/examples/inmemory.rs Outdated Show resolved Hide resolved
crates/librqbit/src/storage/examples/inmemory.rs Outdated Show resolved Hide resolved
Ok(())
}

fn remove_directory_if_empty(&self, _path: &std::path::Path) -> anyhow::Result<()> {
Ok(())
}

fn on_piece_completed(&self, _file_id: usize, _offset: u64) -> anyhow::Result<()> {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It would probably make sense to implement default in the trait itself not to add this to every single storage example that exists.

There's also "storage_middleware" feature that will break if turned on (although I do understand it will break already anyway because ManagedTorrentInfo was renamed, but that's a separate thing)

Copy link
Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added default (empty) implementation in the trait itself, can still be override in trait implementations.

I checked and a cargo check --all-features build (apart from an error of multiple definition of Sha1

error[E0428]: the name `Sha1` is defined multiple times
  --> crates/sha1w/src/lib.rs:79:1
   |
76 | pub type Sha1 = crypto_hash_impl::Sha1CryptoHash;
   | ------------------------------------------------- previous definition of the type `Sha1` here
...
79 | pub type Sha1 = ring_impl::Sha1Ring;
   | ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ `Sha1` redefined here
   |
   = note: `Sha1` must be defined only once in the type namespace of this module

- Reset previous implementation of InMemoryExampleStorage
- Implement default (empty) behaviour of on_piece_completed in trait
  itself
- Now passing a ValidPieceIndex in on_piece_completed
@ikatson ikatson merged commit 92951dc into ikatson:main Aug 28, 2024
5 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants