pub struct DocumentCache { /* private fields */ }Expand description
Document manager with LRU caching and dirty tracking.
The DocumentCache is the single source of truth for all document state
in the LSP server. It handles document lifecycle, caching, and eviction.
§Thread Safety
The DocumentCache uses DashMap for concurrent access and parking_lot::Mutex
for fine-grained locking. It can be safely shared across threads.
§Example
use hedl_lsp::document_manager::DocumentCache;
let manager = DocumentCache::new(1000, 500 * 1024 * 1024);
// Insert a document
// manager.insert_or_update(uri, content);
// Get a document
// let doc = manager.get(&uri);Implementations§
Source§impl DocumentCache
impl DocumentCache
Sourcepub fn new(max_cache_size: usize, max_document_size: usize) -> Self
pub fn new(max_cache_size: usize, max_document_size: usize) -> Self
Create a new document manager with specified limits.
§Parameters
max_cache_size: Maximum number of documents to cache (default: 1000)max_document_size: Maximum document size in bytes (default: 500 MB)
§Example
use hedl_lsp::document_manager::DocumentCache;
// Create with custom limits
let manager = DocumentCache::new(2000, 1024 * 1024 * 1024);Sourcepub fn statistics(&self) -> CacheStatistics
pub fn statistics(&self) -> CacheStatistics
Get current cache statistics.
This method provides a snapshot of cache performance metrics.
Sourcepub fn set_max_cache_size(&self, new_max: usize)
pub fn set_max_cache_size(&self, new_max: usize)
Update maximum cache size (can be called during runtime).
Sourcepub fn max_cache_size(&self) -> usize
pub fn max_cache_size(&self) -> usize
Get current maximum cache size.
Sourcepub fn set_max_document_size(&self, new_max: usize)
pub fn set_max_document_size(&self, new_max: usize)
Update maximum document size (can be called during runtime).
Sourcepub fn max_document_size(&self) -> usize
pub fn max_document_size(&self) -> usize
Get current maximum document size.
Sourcepub fn insert_or_update(&self, uri: &Url, content: &str) -> bool
pub fn insert_or_update(&self, uri: &Url, content: &str) -> bool
Insert or update a document.
If the document already exists, updates its content and marks it as dirty if the content changed. If it’s a new document, performs initial analysis.
§Memory Management
This method enforces the maximum document size limit. Documents exceeding
the limit are rejected and this method returns false.
§Returns
Returns true if the document was successfully inserted/updated,
false if rejected due to size constraints.
§Error Handling
- Size limit violations: Logged as warnings and rejected
- Cache eviction: Logged with LRU document details
- Content hashing: Hash collisions are statistically impossible but detected
Sourcepub fn get(&self, uri: &Url) -> Option<(String, Arc<AnalyzedDocument>)>
pub fn get(&self, uri: &Url) -> Option<(String, Arc<AnalyzedDocument>)>
Get document content and analysis.
This method returns the document content and an Arc to the analysis. It also updates the last access time for LRU tracking.
§Returns
Returns Some((content, analysis)) if the document exists, None otherwise.
§Error Handling
- Missing document: Returns None (logged at call site)
- Access tracking: Always updates last access time for LRU
Sourcepub fn get_state(&self, uri: &Url) -> Option<Arc<Mutex<DocumentState>>>
pub fn get_state(&self, uri: &Url) -> Option<Arc<Mutex<DocumentState>>>
Get document state reference for in-place operations.
This method returns an Arc to the document state, allowing for more efficient operations that need to inspect or modify state without cloning the entire content.
§Returns
Returns Some(Arc<Mutex<DocumentState>>) if the document exists, None otherwise.
Sourcepub fn is_dirty(&self, uri: &Url) -> bool
pub fn is_dirty(&self, uri: &Url) -> bool
Check if a document is dirty (needs re-analysis).
§Returns
Returns true if the document exists and is dirty, false otherwise.
Sourcepub fn mark_clean(&self, uri: &Url)
pub fn mark_clean(&self, uri: &Url)
Mark a document as clean (analysis is up-to-date).
This method should be called after successfully analyzing a document.
Sourcepub fn update_analysis(&self, uri: &Url, analysis: Arc<AnalyzedDocument>)
pub fn update_analysis(&self, uri: &Url, analysis: Arc<AnalyzedDocument>)
Update analysis for a document and mark it as clean.
This is a convenience method that combines updating the analysis and marking the document as clean.
§Error Handling
- Missing document: Silently ignored (document may have been closed/evicted)
- Analysis update: Atomic with dirty flag clearing
Sourcepub fn remove(&self, uri: &Url) -> bool
pub fn remove(&self, uri: &Url) -> bool
Remove a document from the cache.
This is typically called when a document is closed in the editor.
§Returns
Returns true if the document was removed, false if it didn’t exist.
Sourcepub fn all_uris(&self) -> Vec<Url>
pub fn all_uris(&self) -> Vec<Url>
Get all document URIs currently in the cache.
This is useful for workspace-wide operations like workspace symbols.
Auto Trait Implementations§
impl Freeze for DocumentCache
impl !RefUnwindSafe for DocumentCache
impl Send for DocumentCache
impl Sync for DocumentCache
impl Unpin for DocumentCache
impl !UnwindSafe for DocumentCache
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Source§impl<T> Instrument for T
impl<T> Instrument for T
Source§fn instrument(self, span: Span) -> Instrumented<Self>
fn instrument(self, span: Span) -> Instrumented<Self>
Source§fn in_current_span(self) -> Instrumented<Self>
fn in_current_span(self) -> Instrumented<Self>
Source§impl<T> IntoEither for T
impl<T> IntoEither for T
Source§fn into_either(self, into_left: bool) -> Either<Self, Self>
fn into_either(self, into_left: bool) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left is true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read moreSource§fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
self into a Left variant of Either<Self, Self>
if into_left(&self) returns true.
Converts self into a Right variant of Either<Self, Self>
otherwise. Read more