/** * 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 declare abstract class BaseStorageAdapter implements StorageAdapter { abstract init(): Promise; abstract saveNoun(noun: any): Promise; abstract getNoun(id: string): Promise; abstract getNounsByNounType(nounType: string): Promise; abstract deleteNoun(id: string): Promise; abstract saveVerb(verb: any): Promise; abstract getVerb(id: string): 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 saveVerbMetadata(id: string, metadata: any): Promise; abstract getVerbMetadata(id: string): Promise; abstract clear(): Promise; abstract getStorageStatus(): Promise<{ type: string; used: number; quota: number | null; details?: Record; }>; /** * Get nouns with pagination and filtering * @param options Pagination and filtering options * @returns Promise that resolves to a paginated result of nouns */ abstract getNouns(options?: { pagination?: { offset?: number; limit?: number; cursor?: string; }; filter?: { nounType?: string | string[]; service?: string | string[]; metadata?: Record; }; }): Promise<{ items: any[]; totalCount?: number; hasMore: boolean; nextCursor?: string; }>; /** * Get verbs with pagination and filtering * @param options Pagination and filtering options * @returns Promise that resolves to a paginated result of verbs */ abstract getVerbs(options?: { pagination?: { offset?: number; limit?: number; cursor?: string; }; filter?: { verbType?: string | string[]; sourceId?: string | string[]; targetId?: string | string[]; service?: string | string[]; metadata?: Record; }; }): Promise<{ items: any[]; totalCount?: number; hasMore: boolean; nextCursor?: string; }>; protected statisticsCache: StatisticsData | null; protected statisticsBatchUpdateTimerId: NodeJS.Timeout | null; protected statisticsModified: boolean; protected lastStatisticsFlushTime: number; protected readonly MIN_FLUSH_INTERVAL_MS = 5000; protected readonly MAX_FLUSH_DELAY_MS = 30000; protected throttlingDetected: boolean; protected throttlingBackoffMs: number; protected maxBackoffMs: number; protected consecutiveThrottleEvents: number; protected lastThrottleTime: number; protected totalThrottleEvents: number; protected throttleEventsByHour: number[]; protected throttleReasons: Record; protected lastThrottleHourIndex: number; protected delayedOperations: number; protected retriedOperations: number; protected failedDueToThrottling: number; protected totalDelayMs: number; protected serviceThrottling: Map; protected abstract saveStatisticsData(statistics: StatisticsData): Promise; protected abstract getStatisticsData(): Promise; /** * Save statistics data * @param statistics The statistics data to save */ saveStatistics(statistics: StatisticsData): Promise; /** * Get statistics data * @returns Promise that resolves to the statistics data */ getStatistics(): Promise; /** * Schedule a batch update of statistics */ protected scheduleBatchUpdate(): void; /** * Flush statistics to storage */ protected flushStatistics(): Promise; /** * 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) */ incrementStatistic(type: 'noun' | 'verb' | 'metadata', service: string, amount?: number): Promise; /** * Track service activity (first/last activity, operation counts) * @param service The service name * @param operation The operation type */ protected trackServiceActivity(service: string, operation: 'add' | 'update' | 'delete'): void; /** * 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) */ decrementStatistic(type: 'noun' | 'verb' | 'metadata', service: string, amount?: number): Promise; /** * Update the HNSW index size statistic * @param size The new size of the HNSW index */ updateHnswIndexSize(size: number): Promise; /** * Force an immediate flush of statistics to storage * This ensures that any pending statistics updates are written to persistent storage */ flushStatisticsToStorage(): Promise; /** * Track field names from a JSON document * @param jsonDocument The JSON document to extract field names from * @param service The service that inserted the data */ trackFieldNames(jsonDocument: any, service: string): Promise; /** * Get available field names by service * @returns Record of field names by service */ getAvailableFieldNames(): Promise>; /** * Get standard field mappings * @returns Record of standard field mappings */ getStandardFieldMappings(): Promise>>; /** * Create default statistics data * @returns Default statistics data */ protected createDefaultStatistics(): StatisticsData; /** * Detect if an error is a throttling error * Override this method in specific adapters for custom detection */ protected isThrottlingError(error: any): boolean; /** * Track a throttling event * @param error The error that caused throttling * @param service Optional service that was throttled */ protected trackThrottlingEvent(error: any, service?: string): void; /** * Get the reason for throttling from an error */ protected getThrottleReason(error: any): string; /** * Clear throttling state after successful operations */ protected clearThrottlingState(): void; /** * Handle throttling by implementing exponential backoff * @param error The error that triggered throttling * @param service Optional service that was throttled */ handleThrottling(error: any, service?: string): Promise; /** * Track a retried operation */ protected trackRetriedOperation(): void; /** * Track an operation that failed due to throttling */ protected trackFailedDueToThrottling(): void; /** * Get current throttling metrics */ protected getThrottlingMetrics(): StatisticsData['throttlingMetrics']; /** * Include throttling metrics in statistics */ getStatisticsWithThrottling(): Promise; }