Skip to content

Commit b9f949c

Browse files
authored
Merge pull request #292 from ikatson/deferred-torrents-2
[breaking] multiple refactorings in preparation for deferring torrent metadata resolution
2 parents fce467e + 9e4c465 commit b9f949c

21 files changed

Lines changed: 815 additions & 561 deletions

File tree

crates/librqbit/examples/custom_storage.rs

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,7 +20,11 @@ struct CustomStorage {
2020
impl StorageFactory for CustomStorageFactory {
2121
type Storage = CustomStorage;
2222

23-
fn create(&self, _info: &librqbit::ManagedTorrentShared) -> anyhow::Result<Self::Storage> {
23+
fn create(
24+
&self,
25+
_: &librqbit::ManagedTorrentShared,
26+
_: &librqbit::TorrentMetadata,
27+
) -> anyhow::Result<Self::Storage> {
2428
Ok(CustomStorage::default())
2529
}
2630

@@ -54,7 +58,11 @@ impl TorrentStorage for CustomStorage {
5458
anyhow::bail!("not implemented")
5559
}
5660

57-
fn init(&mut self, _meta: &librqbit::ManagedTorrentShared) -> anyhow::Result<()> {
61+
fn init(
62+
&mut self,
63+
_meta: &librqbit::ManagedTorrentShared,
64+
_: &librqbit::TorrentMetadata,
65+
) -> anyhow::Result<()> {
5866
anyhow::bail!("not implemented")
5967
}
6068
}

crates/librqbit/examples/ubuntu.rs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,9 @@ async fn main() -> Result<(), anyhow::Error> {
4949
_ => unreachable!(),
5050
};
5151

52-
info!("Details: {:?}", &handle.shared().info);
52+
handle.with_metadata(|r| {
53+
info!("Details: {:?}", &r.info);
54+
})?;
5355

5456
// Print stats periodically.
5557
tokio::spawn({

crates/librqbit/src/api.rs

Lines changed: 40 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -209,7 +209,7 @@ impl Api {
209209
let mut r = TorrentDetailsResponse {
210210
id: Some(id),
211211
info_hash: mgr.shared().info_hash.as_string(),
212-
name: mgr.shared().info.name.as_ref().map(|n| n.to_string()),
212+
name: mgr.name(),
213213
output_folder: mgr
214214
.shared()
215215
.options
@@ -245,7 +245,8 @@ impl Api {
245245
make_torrent_details(
246246
Some(handle.id()),
247247
&info_hash,
248-
&handle.shared().info,
248+
handle.metadata.load().as_ref().map(|r| &r.info),
249+
handle.name().as_deref(),
249250
only_files.as_deref(),
250251
output_folder,
251252
)
@@ -261,8 +262,7 @@ impl Api {
261262
file_idx: usize,
262263
) -> Result<&'static str> {
263264
let handle = self.mgr_handle(idx)?;
264-
let info = &handle.shared().info;
265-
torrent_file_mime_type(info, file_idx)
265+
handle.with_metadata(|r| torrent_file_mime_type(&r.info, file_idx))?
266266
}
267267

268268
pub fn api_peer_stats(
@@ -380,7 +380,8 @@ impl Api {
380380
let details = make_torrent_details(
381381
Some(id),
382382
&handle.info_hash(),
383-
&handle.shared().info,
383+
handle.metadata.load().as_ref().map(|r| &r.info),
384+
handle.name().as_deref(),
384385
handle.only_files().as_deref(),
385386
handle
386387
.shared()
@@ -416,7 +417,8 @@ impl Api {
416417
details: make_torrent_details(
417418
None,
418419
&info_hash,
419-
&info,
420+
Some(&info),
421+
None,
420422
only_files.as_deref(),
421423
output_folder.to_string_lossy().into_owned().to_string(),
422424
)
@@ -426,7 +428,8 @@ impl Api {
426428
let details = make_torrent_details(
427429
Some(id),
428430
&handle.info_hash(),
429-
&handle.shared().info,
431+
handle.metadata.load().as_ref().map(|r| &r.info),
432+
handle.name().as_deref(),
430433
handle.only_files().as_deref(),
431434
handle
432435
.shared()
@@ -529,37 +532,43 @@ pub struct ApiAddTorrentResponse {
529532
fn make_torrent_details(
530533
id: Option<TorrentId>,
531534
info_hash: &Id20,
532-
info: &TorrentMetaV1Info<ByteBufOwned>,
535+
info: Option<&TorrentMetaV1Info<ByteBufOwned>>,
536+
name: Option<&str>,
533537
only_files: Option<&[usize]>,
534538
output_folder: String,
535539
) -> Result<TorrentDetailsResponse> {
536-
let files = info
537-
.iter_file_details()
538-
.context("error iterating filenames and lengths")?
539-
.enumerate()
540-
.map(|(idx, d)| {
541-
let name = match d.filename.to_string() {
542-
Ok(s) => s,
543-
Err(err) => {
544-
warn!("error reading filename: {:?}", err);
545-
"<INVALID NAME>".to_string()
540+
let files = match info {
541+
Some(info) => info
542+
.iter_file_details()
543+
.context("error iterating filenames and lengths")?
544+
.enumerate()
545+
.map(|(idx, d)| {
546+
let name = match d.filename.to_string() {
547+
Ok(s) => s,
548+
Err(err) => {
549+
warn!("error reading filename: {:?}", err);
550+
"<INVALID NAME>".to_string()
551+
}
552+
};
553+
let components = d.filename.to_vec().unwrap_or_default();
554+
let included = only_files.map(|o| o.contains(&idx)).unwrap_or(true);
555+
TorrentDetailsResponseFile {
556+
name,
557+
components,
558+
length: d.len,
559+
included,
560+
attributes: d.attrs(),
546561
}
547-
};
548-
let components = d.filename.to_vec().unwrap_or_default();
549-
let included = only_files.map(|o| o.contains(&idx)).unwrap_or(true);
550-
TorrentDetailsResponseFile {
551-
name,
552-
components,
553-
length: d.len,
554-
included,
555-
attributes: d.attrs(),
556-
}
557-
})
558-
.collect();
562+
})
563+
.collect(),
564+
None => Default::default(),
565+
};
559566
Ok(TorrentDetailsResponse {
560567
id,
561568
info_hash: info_hash.as_string(),
562-
name: info.name.as_ref().map(|b| b.to_string()),
569+
name: name
570+
.map(|s| s.to_owned())
571+
.or_else(|| info.and_then(|i| i.name.as_ref().map(|b| b.to_string()))),
563572
files: Some(files),
564573
output_folder,
565574
stats: None,

crates/librqbit/src/http_api.rs

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -262,7 +262,10 @@ impl HttpApi {
262262

263263
fn torrent_playlist_items(handle: &ManagedTorrent) -> Result<Vec<(usize, String)>> {
264264
let mut playlist_items = handle
265-
.shared()
265+
.metadata
266+
.load()
267+
.as_ref()
268+
.context("torrent metadata not resolved")?
266269
.info
267270
.iter_file_details()?
268271
.enumerate()
@@ -340,10 +343,9 @@ impl HttpApi {
340343
.context("timeout")??;
341344

342345
let (info, content) = match added {
343-
crate::AddTorrentResponse::AlreadyManaged(_, handle) => (
344-
handle.shared().info.clone(),
345-
handle.shared().torrent_bytes.clone(),
346-
),
346+
crate::AddTorrentResponse::AlreadyManaged(_, handle) => {
347+
handle.with_metadata(|r| (r.info.clone(), r.torrent_bytes.clone()))?
348+
}
347349
crate::AddTorrentResponse::ListOnly(ListOnlyResponse {
348350
info,
349351
torrent_bytes,

crates/librqbit/src/lib.rs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,8 @@ pub use session::{
8282
};
8383
pub use spawn_utils::spawn as librqbit_spawn;
8484
pub use torrent_state::{
85-
ManagedTorrent, ManagedTorrentShared, ManagedTorrentState, TorrentStats, TorrentStatsState,
85+
ManagedTorrent, ManagedTorrentShared, ManagedTorrentState, TorrentMetadata, TorrentStats,
86+
TorrentStatsState,
8687
};
8788
pub use type_aliases::FileInfos;
8889

0 commit comments

Comments
 (0)