Skip to main content

DocumentCache

Struct DocumentCache 

Source
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

Source

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);
Source

pub fn statistics(&self) -> CacheStatistics

Get current cache statistics.

This method provides a snapshot of cache performance metrics.

Source

pub fn set_max_cache_size(&self, new_max: usize)

Update maximum cache size (can be called during runtime).

Source

pub fn max_cache_size(&self) -> usize

Get current maximum cache size.

Source

pub fn set_max_document_size(&self, new_max: usize)

Update maximum document size (can be called during runtime).

Source

pub fn max_document_size(&self) -> usize

Get current maximum document size.

Source

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
Source

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
Source

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.

Source

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.

Source

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.

Source

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
Source

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.

Source

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.

Source

pub fn for_each<F>(&self, f: F)
where F: FnMut(&Url, &Arc<Mutex<DocumentState>>),

Iterate over all documents with a function.

This provides a safe way to iterate over all documents without exposing the internal DashMap structure.

Source

pub fn clear(&self)

Clear all documents from the cache.

This is primarily useful for testing or when resetting the server state.

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> IntoEither for T

Source§

fn into_either(self, into_left: bool) -> Either<Self, Self>

Converts 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 more
Source§

fn into_either_with<F>(self, into_left: F) -> Either<Self, Self>
where F: FnOnce(&Self) -> bool,

Converts 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
Source§

impl<T> Pointable for T

Source§

const ALIGN: usize

The alignment of pointer.
Source§

type Init = T

The type for initializers.
Source§

unsafe fn init(init: <T as Pointable>::Init) -> usize

Initializes a with the given initializer. Read more
Source§

unsafe fn deref<'a>(ptr: usize) -> &'a T

Dereferences the given pointer. Read more
Source§

unsafe fn deref_mut<'a>(ptr: usize) -> &'a mut T

Mutably dereferences the given pointer. Read more
Source§

unsafe fn drop(ptr: usize)

Drops the object pointed to by the given pointer. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more