diff options
| author | gennyble <gen@nyble.dev> | 2026-09-27 02:59:18 -0500 |
|---|---|---|
| committer | gennyble <gen@nyble.dev> | 2026-09-27 02:59:25 -0500 |
| commit | 92f9c5dd277e0a7f0a9bf30fc6d52c47e779b9fd (patch) | |
| tree | ddeb52a5ed4bf42f39692f78471f24b27706c29c /wikarden/src | |
| parent | 17b673ed5e02c378b1f3ee86e8e9715574627430 (diff) | |
rework journaling system
Diffstat (limited to 'wikarden/src')
| -rw-r--r-- | wikarden/src/fs/mod.rs | 9 | ||||
| -rw-r--r-- | wikarden/src/journal.rs | 21 | ||||
| -rw-r--r-- | wikarden/src/lib.rs | 6 |
3 files changed, 33 insertions, 3 deletions
diff --git a/wikarden/src/fs/mod.rs b/wikarden/src/fs/mod.rs index 7d7f57d..c917586 100644 --- a/wikarden/src/fs/mod.rs +++ b/wikarden/src/fs/mod.rs @@ -111,6 +111,15 @@ impl WikFile { } } + bufwrite + .flush() + .await + .map_err(|e| WikError::FileOperation { + op: FileOp::Write, + path: self.path.clone(), + io: e, + })?; + Ok(()) } diff --git a/wikarden/src/journal.rs b/wikarden/src/journal.rs index 70a076c..731a0b5 100644 --- a/wikarden/src/journal.rs +++ b/wikarden/src/journal.rs @@ -30,7 +30,7 @@ pub struct JournalFile { impl JournalFile { pub async fn init(name: JournalName, root: &Utf8Path) -> Result<Self, WikError> { let path = root.join(name.filename()); - let file = WikFile::open(path.clone(), crate::Open::Create).await?; + let file = WikFile::open(path.clone(), crate::Open::CreateAppend).await?; let mut entries = vec![]; for line in file.read_to_string().await?.lines() { @@ -49,7 +49,12 @@ impl JournalFile { pub async fn push(&self, path: WikPath) { let line = JournalLine::now(path.as_ref()); let mut lock = self.entries.write().await; - lock.push(line); + + if let Some(idx) = lock.iter().position(|jl| jl.path == path.as_ref()) { + lock[idx] = line; + } else { + lock.push(line); + } //TODO: insert the line at the correct position to begin with Self::sort(&mut lock); @@ -96,6 +101,13 @@ impl Journal { self.files.iter().find(|jf| jf.name == name).unwrap() } + pub async fn journal_tail(&self, name: JournalName, count: usize) -> Vec<JournalLine> { + let lock = self.journal(name).entries.read().await; + let len = lock.len(); + let start = len.saturating_sub(count); + lock[start..len].to_vec() + } + pub async fn push(&self, name: JournalName, path: WikPath) -> Result<(), WikError> { let journal = self.journal(name); journal.push(path).await; @@ -109,6 +121,7 @@ impl Journal { } } +#[derive(Clone, Debug)] pub struct JournalLine { path: Utf8PathBuf, time: OffsetDateTime, @@ -122,6 +135,10 @@ impl JournalLine { } } + pub fn path(&self) -> String { + self.path.to_string() + } + pub fn epoch(&self) -> i64 { (self.time - OffsetDateTime::UNIX_EPOCH).whole_seconds() } diff --git a/wikarden/src/lib.rs b/wikarden/src/lib.rs index 64eafb9..490a7af 100644 --- a/wikarden/src/lib.rs +++ b/wikarden/src/lib.rs @@ -7,6 +7,7 @@ mod journal; mod table; pub use error::WikError; +pub use journal::{Journal, JournalName}; pub use table::PageTableType; use std::borrow::Cow; @@ -18,7 +19,6 @@ use tokio::sync::RwLock; use crate::{ fs::{EitherPath, Open, SafePath, WikFile, WikPath}, git::GitContext, - journal::{Journal, JournalName}, table::{PageTables, TableEntry}, }; @@ -59,6 +59,10 @@ impl Wikarden { } } + pub fn journal(&self) -> &Journal { + &self.journal + } + pub async fn exists<P: Into<WikPath>>( &self, path: P, |
