Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions crates/frilvault-core/src/cache/note_cache.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
//! In-memory cache for note files.
//!
//! Used by long-running runtimes such as
//! VSCode extensions to reduce filesystem access.

use std::collections::HashMap;
use std::path::PathBuf;
use std::path::{Path, PathBuf};

use crate::NoteFile;

Expand All @@ -9,19 +14,23 @@ pub struct NoteCache {
}

impl NoteCache {
pub fn get(&self, path: &PathBuf) -> Option<&NoteFile> {
pub fn get(&self, path: &Path) -> Option<&NoteFile> {
self.files.get(path)
}

pub fn insert(&mut self, path: PathBuf, note_file: NoteFile) {
self.files.insert(path, note_file);
}

pub fn invalidate(&mut self, path: &PathBuf) {
pub fn invalidate(&mut self, path: &Path) {
self.files.remove(path);
}

pub fn clear(&mut self) {
self.files.clear();
}

pub fn contains(&self, source_file: &Path) -> bool {
self.files.contains_key(source_file)
}
}
33 changes: 24 additions & 9 deletions crates/frilvault-core/src/cache/vault_context.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,21 @@
use std::path::Path;

use crate::{FrilVaultResult, NoteCache, NoteFile, WorkspaceIndexRepository, YamlNoteRepository};

/// Runtime container for FrilVault.
///
/// VaultContext owns shared runtime resources:
///
/// - repositories
/// - caches
/// - indexes
///
/// Services should use VaultContext instead of
/// accessing repositories directly.
pub struct VaultContext {
pub note_repository: YamlNoteRepository,
pub workspace_index_repository: WorkspaceIndexRepository,
pub cache: NoteCache,
pub note_cache: NoteCache,
}

impl VaultContext {
Expand All @@ -14,28 +26,31 @@ impl VaultContext {
Self {
note_repository,
workspace_index_repository,
cache: NoteCache::default(),
note_cache: NoteCache::default(),
}
}

pub fn load_notes(&mut self, source_file: &std::path::Path) -> FrilVaultResult<NoteFile> {
let key = source_file.to_path_buf();

pub fn load_notes(&mut self, source_file: &Path) -> FrilVaultResult<NoteFile> {
// 1. CACHE HIT
if let Some(cached) = self.cache.get(&key) {
if let Some(cached) = self.note_cache.get(source_file) {
return Ok(cached.clone());
}

// 2. REPOSITORY LOAD
let note_file = self.note_repository.load_by_source_file(source_file)?;

// 3. CACHE STORE
self.cache.insert(key, note_file.clone());
self.note_cache
.insert(source_file.to_path_buf(), note_file.clone());

Ok(note_file)
}

pub fn invalidate_notes(&mut self, source_file: &std::path::Path) {
self.cache.invalidate(&source_file.to_path_buf());
pub fn invalidate_notes(&mut self, source_file: &Path) {
self.note_cache.invalidate(source_file);
}

pub fn contains_cached_notes(&self, source_file: &Path) -> bool {
self.note_cache.contains(source_file)
}
}
8 changes: 4 additions & 4 deletions crates/frilvault-core/src/note/mod.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
mod dto;
mod entity;
mod repository;
mod service;
mod note_repository;
mod note_service;

pub use dto::*;
pub use entity::*;
pub use repository::*;
pub use service::*;
pub use note_repository::*;
pub use note_service::*;
Original file line number Diff line number Diff line change
@@ -1,3 +1,12 @@
//! Application services for note operations.
//!
//! This module contains high-level note workflows
//! such as CRUD operations and searching.
//!
//! Services should access storage through
//! VaultContext rather than directly interacting
//! with repositories.

use std::path::Path;

use chrono::Utc;
Expand All @@ -9,6 +18,10 @@ use crate::{
note_view::NoteView,
};

/// Application service responsible for note operations.
///
/// Coordinates repositories, caching,
/// and future runtime behaviors.
pub struct NoteService {
pub vault_context: VaultContext,
}
Expand All @@ -31,9 +44,9 @@ impl NoteService {

self.vault_context
.note_repository
.replace_notes(source_file, notes)?;
.replace_notes(source_file.as_ref(), notes)?;

self.vault_context.invalidate_notes(source_file);
self.vault_context.invalidate_notes(source_file.as_ref());

Ok(())
}
Expand Down Expand Up @@ -115,54 +128,55 @@ impl NoteService {
Ok(())
}

pub fn search_notes(&mut self, keyword: &str) -> FrilVaultResult<Vec<NoteView>> {
fn all_note_views(&self) -> FrilVaultResult<Vec<NoteView>> {
let records = self.vault_context.note_repository.list_all_note_files()?;

let keyword = keyword.to_lowercase();

let mut results = Vec::new();

for record in records {
for note in record.note_file.notes {
let content_match = note.content.to_lowercase().contains(&keyword);

let symbol_match = match &note.anchor {
NoteAnchor::Symbol(anchor) => anchor.name.to_lowercase().contains(&keyword),
_ => false,
};

if content_match || symbol_match {
results.push(NoteView {
source_file: record.source_file.clone(),
note,
});
}
results.push(NoteView {
source_file: record.source_file.clone(),
note,
});
}
}

Ok(results)
}

pub fn search_by_symbol(&mut self, symbol: &str) -> FrilVaultResult<Vec<NoteView>> {
let symbol = symbol.to_lowercase();
pub fn search_notes(&mut self, keyword: &str) -> FrilVaultResult<Vec<NoteView>> {
let keyword = keyword.to_lowercase();

let records = self.vault_context.note_repository.list_all_note_files()?;
Ok(self
.all_note_views()?
.into_iter()
.filter(|view| {
let content_match = view.note.content.to_lowercase().contains(&keyword);

let mut results = Vec::new();
let symbol_match = match &view.note.anchor {
NoteAnchor::Symbol(anchor) => anchor.name.to_lowercase().contains(&keyword),
_ => false,
};

for record in records {
for note in record.note_file.notes {
if let NoteAnchor::Symbol(anchor) = &note.anchor
&& anchor.name.to_lowercase().contains(&symbol)
{
results.push(NoteView {
source_file: record.source_file.clone(),
note,
});
}
}
}
content_match || symbol_match
})
.collect())
}

Ok(results)
pub fn search_by_symbol(&mut self, symbol: &str) -> FrilVaultResult<Vec<NoteView>> {
let symbol = symbol.to_lowercase();

Ok(self
.all_note_views()?
.into_iter()
.filter(|view| {
matches!(
&view.note.anchor,
NoteAnchor::Symbol(anchor)
if anchor.name.to_lowercase().contains(&symbol)
)
})
.collect())
}
}
6 changes: 6 additions & 0 deletions crates/frilvault-core/src/parser/note_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,13 @@ use crate::FrilVaultResult;
use crate::note::NoteFile;

pub trait NoteParser {
/// Converts a NoteFile into a serialized representation.
///
/// Implementations may choose YAML, JSON, or any other format.
fn serialize(&self, note_file: &NoteFile) -> FrilVaultResult<String>;

/// Converts serialized content back into a NoteFile.
///
/// Returns an error if the content is invalid or unsupported.
fn deserialize(&self, content: &str) -> FrilVaultResult<NoteFile>;
}
5 changes: 5 additions & 0 deletions crates/frilvault-core/src/storage/yaml_repository.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
//! YAML-backed note repository.
//!
//! Notes are persisted as YAML files
//! inside the `.vault/notes` directory.

use crate::note::{Note, NoteFile};
use crate::parser::{NoteParser, YamlParser};
use crate::workspace::PathResolver;
Expand Down
2 changes: 1 addition & 1 deletion crates/frilvault-core/src/tests/helper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub fn create_test_note_service(workspace_root: &Path) -> NoteService {
NoteService::new(vault_context)
}

fn create_test_vault_context(workspace_root: &Path) -> VaultContext {
pub fn create_test_vault_context(workspace_root: &Path) -> VaultContext {
let resolver = PathResolver::new(workspace_root);
let workspace_repository = WorkspaceRepository::new(resolver.clone());
workspace_repository.create_if_missing().unwrap();
Expand Down
3 changes: 3 additions & 0 deletions crates/frilvault-core/src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,6 @@ mod workspace_index_test;

#[cfg(test)]
mod workspace_index_repository_test;

#[cfg(test)]
mod vault_context_test;
70 changes: 70 additions & 0 deletions crates/frilvault-core/src/tests/note_service_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -306,3 +306,73 @@ fn search_finds_symbol_anchor() {

fs::remove_dir_all(workspace_root).unwrap();
}

#[test]
fn search_by_symbol_returns_matching_notes() {
let workspace_root =
std::env::temp_dir().join(format!("frilvault-test-{}", uuid::Uuid::new_v4()));

fs::create_dir_all(&workspace_root).unwrap();

let mut service = create_test_note_service(&workspace_root);

service
.add_note(AddNoteInput {
source_file: "src/main.rs".into(),

anchor: NoteAnchor::Symbol(SymbolAnchor {
name: "main".to_string(),

kind: SymbolKind::Function,

signature: None,

line_hint: None,
}),

content: "main symbol note".to_string(),
})
.unwrap();

let results = service.search_by_symbol("main").unwrap();

assert_eq!(results.len(), 1);

assert_eq!(results[0].note.content, "main symbol note");

fs::remove_dir_all(workspace_root).unwrap();
}

#[test]
fn search_by_symbol_returns_empty_when_not_found() {
let workspace_root =
std::env::temp_dir().join(format!("frilvault-test-{}", uuid::Uuid::new_v4()));

fs::create_dir_all(&workspace_root).unwrap();

let mut service = create_test_note_service(&workspace_root);

service
.add_note(AddNoteInput {
source_file: "src/main.rs".into(),

anchor: NoteAnchor::Symbol(SymbolAnchor {
name: "main".to_string(),

kind: SymbolKind::Function,

signature: None,

line_hint: None,
}),

content: "main symbol note".to_string(),
})
.unwrap();

let results = service.search_by_symbol("parser").unwrap();

assert!(results.is_empty());

fs::remove_dir_all(workspace_root).unwrap();
}
33 changes: 33 additions & 0 deletions crates/frilvault-core/src/tests/vault_context_test.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use std::{fs, path::Path};

use crate::{AddNoteInput, LineAnchor, Note, NoteAnchor, tests::helper::create_test_vault_context};

#[test]
fn load_notes_populates_cache() {
let workspace_root =
std::env::temp_dir().join(format!("frilvault-test-{}", uuid::Uuid::new_v4()));

fs::create_dir_all(&workspace_root).unwrap();

let mut vault_context = create_test_vault_context(&workspace_root);

vault_context
.note_repository
.append_note(
Path::new("src/main.rs"),
&Note::new(AddNoteInput {
source_file: "src/main.rs".into(),
anchor: NoteAnchor::Line(LineAnchor { line: 1, column: 1 }),
content: "test".to_string(),
}),
)
.unwrap();

assert!(!vault_context.contains_cached_notes(Path::new("src/main.rs")));

vault_context.load_notes(Path::new("src/main.rs")).unwrap();

assert!(vault_context.contains_cached_notes(Path::new("src/main.rs")));

fs::remove_dir_all(workspace_root).unwrap();
}
Loading