/**
* @file loda_index.js
* @description Cached document structure index
* @atomic MOLECULE (local state, coordinated operations)
* @component LODA-MCP-COMP-03
*/
class LodaIndex {
constructor(options = {}) {
this.cache = new Map();
this.maxAge = options.maxAge || 60000; // 60s default
this.maxSize = options.maxSize || 100; // Max cached docs
}
/**
* Get document structure (cached)
* @param {string} documentPath - Path to document
* @param {Function} parseFunc - Parser function
* @returns {Object} Document structure
*/
getStructure(documentPath, parseFunc) {
const cached = this.cache.get(documentPath);
if (cached && Date.now() - cached.timestamp < this.maxAge) {
return cached.structure;
}
// Evict oldest if at capacity
if (this.cache.size >= this.maxSize) {
const oldest = [...this.cache.entries()]
.sort((a, b) => a[1].timestamp - b[1].timestamp)[0];
if (oldest) {
this.cache.delete(oldest[0]);
}
}
const structure = parseFunc(documentPath);
this.cache.set(documentPath, {
structure,
timestamp: Date.now()
});
return structure;
}
/**
* Invalidate cache for document
* @param {string} documentPath - Path to invalidate
*/
invalidate(documentPath) {
this.cache.delete(documentPath);
}
/**
* Clear entire cache
*/
clear() {
this.cache.clear();
}
/**
* Get cache statistics
* @returns {Object} Cache stats
*/
getStats() {
return {
size: this.cache.size,
maxSize: this.maxSize,
maxAge: this.maxAge
};
}
}
module.exports = { LodaIndex };