mirror of
https://github.com/YGGverse/aquatic-crawler.git
synced 2026-03-31 17:15:35 +00:00
define fixed bytes count for info-hash v1 type
This commit is contained in:
parent
dcbb4108fd
commit
f872e81cff
3 changed files with 12 additions and 11 deletions
19
src/api.rs
19
src/api.rs
|
|
@ -5,21 +5,20 @@ use info_hash::InfoHash;
|
||||||
/// decode hash bytes to `InfoHash` array on success.
|
/// decode hash bytes to `InfoHash` array on success.
|
||||||
///
|
///
|
||||||
/// * return `None` if the `path` is not reachable
|
/// * return `None` if the `path` is not reachable
|
||||||
pub fn get(path: &str) -> Option<Vec<InfoHash>> {
|
pub fn get(path: &str, capacity: usize) -> Option<Vec<InfoHash>> {
|
||||||
use std::io::Read;
|
use std::io::Read;
|
||||||
if path.contains("://") {
|
if path.contains("://") {
|
||||||
todo!("URL sources yet not supported")
|
todo!("URL sources yet not supported")
|
||||||
}
|
}
|
||||||
const L: usize = 20; // v1 only
|
const L: usize = 20; // v1 only
|
||||||
let mut r = Vec::new();
|
let mut r = Vec::with_capacity(capacity);
|
||||||
let mut f = std::fs::File::open(path).ok()?;
|
let mut f = std::fs::File::open(path).ok()?;
|
||||||
loop {
|
loop {
|
||||||
let mut b = vec![0; L];
|
let mut b = [0; L];
|
||||||
let l = f.read(&mut b).ok()?;
|
if f.read(&mut b).ok()? != L {
|
||||||
if l != L {
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
r.push(InfoHash::V1(b[..l].to_vec()))
|
r.push(InfoHash::V1(b))
|
||||||
}
|
}
|
||||||
Some(r)
|
Some(r)
|
||||||
}
|
}
|
||||||
|
|
@ -33,6 +32,8 @@ fn test() {
|
||||||
todo!()
|
todo!()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const C: usize = 2;
|
||||||
|
|
||||||
const P0: &str = "/tmp/aquatic-crawler-api-test-0.bin";
|
const P0: &str = "/tmp/aquatic-crawler-api-test-0.bin";
|
||||||
const P1: &str = "/tmp/aquatic-crawler-api-test-1.bin";
|
const P1: &str = "/tmp/aquatic-crawler-api-test-1.bin";
|
||||||
const P2: &str = "/tmp/aquatic-crawler-api-test-2.bin";
|
const P2: &str = "/tmp/aquatic-crawler-api-test-2.bin";
|
||||||
|
|
@ -40,9 +41,9 @@ fn test() {
|
||||||
fs::write(P0, vec![]).unwrap();
|
fs::write(P0, vec![]).unwrap();
|
||||||
fs::write(P1, vec![1; 40]).unwrap(); // 20 + 20 bytes
|
fs::write(P1, vec![1; 40]).unwrap(); // 20 + 20 bytes
|
||||||
|
|
||||||
assert!(get(P0).is_some_and(|b| b.is_empty()));
|
assert!(get(P0, C).is_some_and(|b| b.is_empty()));
|
||||||
assert!(get(P1).is_some_and(|b| b.len() == 2));
|
assert!(get(P1, C).is_some_and(|b| b.len() == 2));
|
||||||
assert!(get(P2).is_none());
|
assert!(get(P2, C).is_none());
|
||||||
|
|
||||||
fs::remove_file(P0).unwrap();
|
fs::remove_file(P0).unwrap();
|
||||||
fs::remove_file(P1).unwrap();
|
fs::remove_file(P1).unwrap();
|
||||||
|
|
|
||||||
|
|
@ -1,5 +1,5 @@
|
||||||
pub enum InfoHash {
|
pub enum InfoHash {
|
||||||
V1(Vec<u8>),
|
V1([u8; 20]),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::fmt::Display for InfoHash {
|
impl std::fmt::Display for InfoHash {
|
||||||
|
|
|
||||||
|
|
@ -88,7 +88,7 @@ async fn main() -> Result<()> {
|
||||||
debug.info(&format!("Index source `{source}`..."));
|
debug.info(&format!("Index source `{source}`..."));
|
||||||
// grab latest info-hashes from this source
|
// grab latest info-hashes from this source
|
||||||
// * aquatic server may update the stats at this moment, handle result manually
|
// * aquatic server may update the stats at this moment, handle result manually
|
||||||
for i in match api::get(source) {
|
for i in match api::get(source, config.index_capacity) {
|
||||||
Some(i) => i,
|
Some(i) => i,
|
||||||
None => {
|
None => {
|
||||||
// skip without panic
|
// skip without panic
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue