mirror of
https://github.com/YGGverse/aquatic-crawler.git
synced 2026-03-31 17:15:35 +00:00
wrap index implementation, skip rss file update if the index is not changed (safes ssd life by prevent extra write operations)
This commit is contained in:
parent
ff7bb4c94f
commit
b2b69ca9e7
2 changed files with 73 additions and 33 deletions
63
src/index.rs
63
src/index.rs
|
|
@ -1,7 +1,68 @@
|
|||
use chrono::{DateTime, Utc};
|
||||
use std::collections::HashMap;
|
||||
|
||||
pub struct Index {
|
||||
pub struct Value {
|
||||
pub time: DateTime<Utc>,
|
||||
pub node: u64,
|
||||
pub name: Option<String>,
|
||||
}
|
||||
|
||||
/// Collect processed info hashes to skip on the next iterations (for this session)
|
||||
/// * also contains optional meta info to export index as RSS or any other format
|
||||
pub struct Index {
|
||||
index: HashMap<String, Value>,
|
||||
/// Track index changes to prevent extra disk write operations (safe SSD life)
|
||||
/// * useful in the static RSS feed generation case, if enabled.
|
||||
is_changed: bool,
|
||||
}
|
||||
|
||||
impl Index {
|
||||
pub fn init(capacity: usize) -> Self {
|
||||
Self {
|
||||
index: HashMap::with_capacity(capacity),
|
||||
is_changed: false,
|
||||
}
|
||||
}
|
||||
|
||||
pub fn has(&self, infohash: &str) -> bool {
|
||||
self.index.contains_key(infohash)
|
||||
}
|
||||
|
||||
pub fn is_changed(&self) -> bool {
|
||||
self.is_changed
|
||||
}
|
||||
|
||||
pub fn list(&self) -> &HashMap<String, Value> {
|
||||
&self.index
|
||||
}
|
||||
|
||||
pub fn len(&self) -> usize {
|
||||
self.index.len()
|
||||
}
|
||||
|
||||
pub fn nodes(&self) -> u64 {
|
||||
self.index.values().map(|i| i.node).sum::<u64>()
|
||||
}
|
||||
|
||||
pub fn insert(&mut self, infohash: String, node: u64, name: Option<String>) {
|
||||
if self
|
||||
.index
|
||||
.insert(
|
||||
infohash,
|
||||
Value {
|
||||
time: Utc::now(),
|
||||
node,
|
||||
name,
|
||||
},
|
||||
)
|
||||
.is_none()
|
||||
{
|
||||
self.is_changed = true
|
||||
}
|
||||
}
|
||||
|
||||
pub fn refresh(&mut self) {
|
||||
self.is_changed = false
|
||||
// @TODO implement also index cleanup by Value timeout
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue