update redb api

This commit is contained in:
yggverse 2025-08-03 13:52:11 +03:00
parent b4d379c650
commit 66c9e35481
2 changed files with 36 additions and 79 deletions

View file

@ -23,6 +23,6 @@ urlencoding = "2.1"
yggtracker-redb = "0.1" yggtracker-redb = "0.1"
[patch.crates-io] [patch.crates-io]
librqbit = { git = "https://github.com/ikatson/rqbit.git", rev="b580a9610ae7c6eaacd305a3905f7e2d3202ca69" }
yggtracker-redb = { git = "https://github.com/yggverse/yggtracker-redb.git", rev="9910e0b157591f2ba064aa53bd2720c247485243" }
# yggtracker-redb = { path = "../yggtracker-redb" } # yggtracker-redb = { path = "../yggtracker-redb" }
yggtracker-redb = { git = "https://github.com/YGGverse/yggtracker-redb.git", rev="ca9ebedd790b67caf64c049a26d3f45f8e6529d1" }
librqbit = { git = "https://github.com/ikatson/rqbit.git", rev="b580a9610ae7c6eaacd305a3905f7e2d3202ca69" }

View file

@ -12,14 +12,12 @@ use librqbit::{
}; };
use peers::Peers; use peers::Peers;
use preload::Preload; use preload::Preload;
use std::{ use std::{collections::HashSet, num::NonZero, os::unix::ffi::OsStrExt, time::Duration};
collections::HashSet, num::NonZero, os::unix::ffi::OsStrExt, path::PathBuf, time::Duration,
};
use trackers::Trackers; use trackers::Trackers;
use url::Url; use url::Url;
use yggtracker_redb::{ use yggtracker_redb::{
Database, Database,
torrent::{Torrent, meta::*}, torrent::{Image, Torrent},
}; };
#[tokio::main] #[tokio::main]
@ -139,20 +137,20 @@ async fn main() -> Result<()> {
{ {
Ok(r) => match r { Ok(r) => match r {
Ok(AddTorrentResponse::Added(id, mt)) => { Ok(AddTorrentResponse::Added(id, mt)) => {
let mut images: HashSet<PathBuf> = HashSet::with_capacity( let mut only_files = HashSet::with_capacity(
config config
.preload_max_filecount .preload_max_filecount
.unwrap_or(config.index_capacity), .unwrap_or(config.index_capacity),
); );
let mut only_files: HashSet<usize> = HashSet::with_capacity( let mut images = Vec::with_capacity(
config config
.preload_max_filecount .preload_max_filecount
.unwrap_or(config.index_capacity), .unwrap_or(config.index_capacity),
); );
mt.wait_until_initialized().await?; mt.wait_until_initialized().await?;
let (name, files, is_private, length, bytes) = mt.with_metadata(|m| { let bytes = mt.with_metadata(|m| {
for info in &m.file_infos { for info in &m.file_infos {
if preload.max_filecount.is_some_and(|limit| images.len() + 1 > limit) { if preload.max_filecount.is_some_and(|limit| only_files.len() + 1 > limit) {
if config.debug { if config.debug {
println!( println!(
"\t\t\ttotal files count limit ({}) for `{i}` reached!", "\t\t\ttotal files count limit ({}) for `{i}` reached!",
@ -173,77 +171,37 @@ async fn main() -> Result<()> {
} }
continue; continue;
} }
assert!(images.insert(info.relative_filename.clone())); assert!(only_files.insert(id));
assert!(only_files.insert(id)) images.push(info.relative_filename.clone());
} }
let mi = m.info.info(); m.info_bytes.to_vec()
(
mi.name.as_ref().map(|s| s.to_string()),
mi.files.as_ref().map(|f| {
let mut b = Vec::with_capacity(f.len());
let mut i = f.iter();
for f in i.by_ref() {
b.push(File {
path: String::from_utf8(
f.path
.iter()
.enumerate()
.flat_map(|(n, b)| {
if n == 0 {
b.0.to_vec()
} else {
let mut p = vec![b'/'];
p.extend(b.0.to_vec());
p
}
})
.collect(),
)
.ok(),
length: f.length,
});
}
b.sort_by(|a, b| a.path.cmp(&b.path)); // @TODO optional
b
}),
mi.private,
mi.length,
m.torrent_bytes.clone().into()
)
})?; })?;
session.update_only_files(&mt, &only_files).await?; session.update_only_files(&mt, &only_files).await?;
session.unpause(&mt).await?; session.unpause(&mt).await?;
mt.wait_until_completed().await?; mt.wait_until_completed().await?;
assert!(
database // persist torrent data resolved
.set_torrent( database.set_torrent(
&i, &i,
Torrent { Torrent {
bytes, bytes,
meta: Meta { images: if images.is_empty() {
comment: None, // @TODO None
files, } else {
images: if images.is_empty() { Some(
None images
} else { .into_iter()
let mut b = Vec::with_capacity(images.len()); .map(|i| Image {
for p in images { alt: i.to_str().map(|p| p.to_string()),
b.push(Image { bytes: preload.bytes(&i).unwrap(),
bytes: preload.bytes(&p)?, })
path: p.to_string_lossy().to_string(), .collect(),
}) )
} },
Some(b) time: chrono::Utc::now(),
}, },
is_private, )?;
name,
length,
time: chrono::Utc::now(),
},
},
)?
.is_none()
);
// remove torrent from session as indexed // remove torrent from session as indexed
session session
.delete(librqbit::api::TorrentIdOrHash::Id(id), false) .delete(librqbit::api::TorrentIdOrHash::Id(id), false)
@ -254,7 +212,7 @@ async fn main() -> Result<()> {
preload.clear_output_folder(&i)?; preload.clear_output_folder(&i)?;
if config.debug { if config.debug {
println!("\t\t\tadd `{i}` to index.") println!("\t\t\ttorrent data successfully resolved.")
} }
} }
Ok(_) => panic!(), Ok(_) => panic!(),
@ -270,8 +228,7 @@ async fn main() -> Result<()> {
} }
if config.debug { if config.debug {
println!( println!(
"Queue completed on {time_queue}\n\ttotal: {}\n\ttime: {} s\n\tuptime: {} s\n\tawait {} seconds to continue...", "Queue completed on {time_queue}\n\ttime: {} s\n\tuptime: {} s\n\tawait {} seconds to continue...",
database.torrents_total()?,
Local::now() Local::now()
.signed_duration_since(time_queue) .signed_duration_since(time_queue)
.as_seconds_f32(), .as_seconds_f32(),