pub mod related;
pub mod report;
mod rows;
#[cfg(test)]
mod tests;
use std::collections::{BTreeMap, HashMap, HashSet};
use std::env;
use std::fs;
use std::io;
use std::path::{Path, PathBuf};
use std::sync::{Mutex, PoisonError};
use rows::{Row, load};
use crate::home::home;
use crate::store::fileio::{append_line, atomic_write};
use crate::store::time::Stamp;
pub const SWITCH_ENV: &str = "NEOSIAN_HEAT";
const DIRECTORY: &str = "heat";
const DAY: i64 = 86_400;
pub const WEEK_DAYS: i64 = 7;
pub const MONTH_DAYS: i64 = 30;
const COMPACT_BYTES: u64 = 2 * 1024 * 1024;
const SEEN_CEILING: usize = 100_000;
pub fn directory() -> Option<PathBuf> {
if env::var(SWITCH_ENV).is_ok_and(|value| value.eq_ignore_ascii_case("off")) {
return None;
}
home().ok().map(|home| home.join(DIRECTORY))
}
pub fn file(directory: &Path, scope: &str) -> PathBuf {
let name = scope.replace(':', "%3A").replace('/', "%2F");
directory.join(format!("{name}.jsonl"))
}
#[derive(Clone, Debug, Default, PartialEq, Eq)]
pub struct Counts {
pub week: u64,
pub month: u64,
pub total: u64,
pub last: Option<Stamp>,
}
fn later(held: Option<Stamp>, seen: Stamp) -> Option<Stamp> {
match held {
Some(held) if held.instant() >= seen.instant() => Some(held),
_ => Some(seen),
}
}
pub fn counts_in(directory: &Path, scope: &str, now: Stamp) -> BTreeMap<String, Counts> {
let week = now.after(-WEEK_DAYS * DAY).instant();
let month = now.after(-MONTH_DAYS * DAY).instant();
let mut found: BTreeMap<String, Counts> = BTreeMap::new();
let mut readers: HashMap<(String, String), i64> = HashMap::new();
for row in load(&file(directory, scope)).unwrap_or_default() {
match row {
Row::Read { at, reader, path } => {
let latest = readers.entry((path, reader)).or_insert(i64::MIN);
*latest = (*latest).max(at.instant());
}
Row::Fold {
path,
readers,
last,
} => {
let counts = found.entry(path).or_default();
counts.total += readers;
counts.last = later(counts.last, last);
}
}
}
for ((path, _), at) in readers {
let counts = found.entry(path).or_default();
counts.total += 1;
counts.month += u64::from(at >= month);
counts.week += u64::from(at >= week);
counts.last = later(counts.last, Stamp::from_micros(at));
}
found
}
pub fn reads_in(directory: &Path, scope: &str) -> Vec<(String, String)> {
load(&file(directory, scope))
.unwrap_or_default()
.into_iter()
.filter_map(|row| match row {
Row::Read { reader, path, .. } => Some((reader, path)),
Row::Fold { .. } => None,
})
.collect()
}
pub fn counts(scope: &str, now: Stamp) -> BTreeMap<String, Counts> {
directory().map_or_else(BTreeMap::new, |directory| counts_in(&directory, scope, now))
}
#[derive(Default)]
pub struct Heat {
seen: Mutex<HashSet<(String, String, String)>>,
}
impl Heat {
pub fn observe(&self, reader: &str, scope: &str, path: &str) {
if let Some(directory) = directory() {
self.observe_in(&directory, reader, scope, path, Stamp::now());
}
}
pub fn observe_in(&self, directory: &Path, reader: &str, scope: &str, path: &str, at: Stamp) {
let key = (reader.to_string(), scope.to_string(), path.to_string());
{
let mut seen = self.seen.lock().unwrap_or_else(PoisonError::into_inner);
if seen.len() >= SEEN_CEILING {
seen.clear();
}
if !seen.insert(key) {
return;
}
}
let file = file(directory, scope);
let row = Row::Read {
at,
reader: reader.to_string(),
path: path.to_string(),
};
if append_line(&file, &row.line(), false).is_ok()
&& fs::metadata(&file).is_ok_and(|meta| meta.len() > COMPACT_BYTES)
{
let _ = compact(&file, at);
}
}
}
fn compact(file: &Path, now: Stamp) -> io::Result<()> {
let month = now.after(-MONTH_DAYS * DAY).instant();
let mut folds: BTreeMap<String, (HashSet<String>, u64, Stamp)> = BTreeMap::new();
let mut kept = Vec::new();
for row in load(file)? {
match row {
Row::Read { at, reader, path } if at.instant() < month => {
let fold = folds.entry(path).or_insert((HashSet::new(), 0, at));
fold.0.insert(reader);
fold.2 = later(Some(fold.2), at).expect("one of two stamps");
}
Row::Fold {
path,
readers,
last,
} => {
let fold = folds.entry(path).or_insert((HashSet::new(), 0, last));
fold.1 += readers;
fold.2 = later(Some(fold.2), last).expect("one of two stamps");
}
row => kept.push(row),
}
}
let folded = folds
.into_iter()
.map(|(path, (readers, counted, last))| Row::Fold {
path,
readers: readers.len() as u64 + counted,
last,
});
let text: String = folded.chain(kept).map(|row| row.line()).collect();
atomic_write(file, &text, false)
}
fn rewrite(file: &Path, keep: impl FnMut(Row) -> Option<Row>) -> io::Result<()> {
if !file.exists() {
return Ok(());
}
let text: String = load(file)?
.into_iter()
.filter_map(keep)
.map(|row| row.line())
.collect();
atomic_write(file, &text, false)
}
pub fn forget(scope: &str, path: &str) {
if let Some(directory) = directory() {
let _ = forget_in(&directory, scope, path);
}
}
pub fn forget_in(directory: &Path, scope: &str, path: &str) -> io::Result<()> {
rewrite(&file(directory, scope), |row| {
(row.path() != path).then_some(row)
})
}
pub fn forget_scope(scope: &str) {
if let Some(directory) = directory() {
let file = file(&directory, scope);
if file.exists() {
let _ = fs::remove_file(file);
}
}
}
pub fn rename(scope: &str, old: &str, new_scope: &str, new: &str) {
if let Some(directory) = directory() {
let _ = rename_in(&directory, scope, old, new_scope, new);
}
}
pub fn rename_in(
directory: &Path,
scope: &str,
old: &str,
new_scope: &str,
new: &str,
) -> io::Result<()> {
let mut moved = Vec::new();
rewrite(&file(directory, scope), |row| {
if row.path() == old {
moved.push(row.relabel(new));
None
} else {
Some(row)
}
})?;
let text: String = moved.iter().map(Row::line).collect();
if text.is_empty() {
return Ok(());
}
append_line(&file(directory, new_scope), &text, false)
}