/** * Base Storage Adapter * Provides common functionality for all storage adapters, including statistics tracking */ import { StatisticsData, StorageAdapter } from '../../coreTypes.js' /** * Base class for storage adapters that implements statistics tracking */ export abstract class BaseStorageAdapter implements StorageAdapter { // Abstract methods that must be implemented by subclasses abstract init(): Promise abstract saveNoun(noun: any): Promise abstract getNoun(id: string): Promise abstract getAllNouns(): Promise abstract getNounsByNounType(nounType: string): Promise abstract deleteNoun(id: string): Promise abstract saveVerb(verb: any): Promise abstract getVerb(id: string): Promise abstract getAllVerbs(): Promise abstract getVerbsBySource(sourceId: string): Promise abstract getVerbsByTarget(targetId: string): Promise abstract getVerbsByType(type: string): Promise abstract deleteVerb(id: string): Promise abstract saveMetadata(id: string, metadata: any): Promise abstract getMetadata(id: string): Promise abstract clear(): Promise abstract getStorageStatus(): Promise<{ type: string used: number quota: number | null details?: Record }> // Statistics-specific methods protected abstract saveStatisticsData(statistics: StatisticsData): Promise protected abstract getStatisticsData(): Promise /** * Save statistics data * @param statistics The statistics data to save */ async saveStatistics(statistics: StatisticsData): Promise { await this.saveStatisticsData(statistics) } /** * Get statistics data * @returns Promise that resolves to the statistics data */ async getStatistics(): Promise { return await this.getStatisticsData() } /** * Increment a statistic counter * @param type The type of statistic to increment ('noun', 'verb', 'metadata') * @param service The service that inserted the data * @param amount The amount to increment by (default: 1) */ async incrementStatistic( type: 'noun' | 'verb' | 'metadata', service: string, amount: number = 1 ): Promise { // Get current statistics or create default if not exists let statistics = await this.getStatisticsData() if (!statistics) { statistics = this.createDefaultStatistics() } // Increment the appropriate counter const counterMap = { noun: statistics.nounCount, verb: statistics.verbCount, metadata: statistics.metadataCount } const counter = counterMap[type] counter[service] = (counter[service] || 0) + amount // Update timestamp statistics.lastUpdated = new Date().toISOString() // Save updated statistics await this.saveStatisticsData(statistics) } /** * Decrement a statistic counter * @param type The type of statistic to decrement ('noun', 'verb', 'metadata') * @param service The service that inserted the data * @param amount The amount to decrement by (default: 1) */ async decrementStatistic( type: 'noun' | 'verb' | 'metadata', service: string, amount: number = 1 ): Promise { // Get current statistics or create default if not exists let statistics = await this.getStatisticsData() if (!statistics) { statistics = this.createDefaultStatistics() } // Decrement the appropriate counter const counterMap = { noun: statistics.nounCount, verb: statistics.verbCount, metadata: statistics.metadataCount } const counter = counterMap[type] counter[service] = Math.max(0, (counter[service] || 0) - amount) // Update timestamp statistics.lastUpdated = new Date().toISOString() // Save updated statistics await this.saveStatisticsData(statistics) } /** * Update the HNSW index size statistic * @param size The new size of the HNSW index */ async updateHnswIndexSize(size: number): Promise { // Get current statistics or create default if not exists let statistics = await this.getStatisticsData() if (!statistics) { statistics = this.createDefaultStatistics() } // Update HNSW index size statistics.hnswIndexSize = size // Update timestamp statistics.lastUpdated = new Date().toISOString() // Save updated statistics await this.saveStatisticsData(statistics) } /** * Create default statistics data * @returns Default statistics data */ protected createDefaultStatistics(): StatisticsData { return { nounCount: {}, verbCount: {}, metadataCount: {}, hnswIndexSize: 0, lastUpdated: new Date().toISOString() } } }