/** * Prediction Engine Implementation * Analyzes access patterns to predict future key accesses * Requirements: 8.1, 8.2, 8.3, 8.4 */ import { AccessTracker } from '../tracking/AccessTracker.js'; import { TierManager, MovementResult } from '../tier/TierManager.js'; /** * Time-based access pattern storage * Tracks when keys are typically accessed based on time of day/week */ export interface TimePattern { /** The cache key this pattern applies to */ key: string; /** Hour of day (0-23) when access typically occurs */ hourOfDay: number; /** Day of week (0-6, 0=Sunday) when access typically occurs */ dayOfWeek: number; /** How often this pattern has been observed */ frequency: number; /** Confidence score (0-1) based on observation consistency */ confidence: number; } /** * Sequence-based access pattern storage * Tracks correlated key accesses (key A often followed by key B) */ export interface SequencePattern { /** The key that triggers this pattern */ triggerKey: string; /** Keys that typically follow the trigger key */ followingKeys: string[]; /** Probability (0-1) that following keys will be accessed */ probability: number; /** Number of times this sequence has been observed */ observationCount: number; } /** * Result of a prediction for a specific key */ export interface PredictionResult { /** The key predicted to be accessed */ key: string; /** Probability (0-1) of the prediction being correct */ probability: number; /** Predicted timestamp when access will occur */ predictedTime: number; /** Type of pattern that generated this prediction */ patternType: 'time-based' | 'sequence-based'; } /** * Configuration options for PredictionEngine */ export interface PredictionEngineConfig { /** Minimum observations required to establish a pattern */ minObservations?: number; /** Minimum confidence threshold for time patterns */ minTimePatternConfidence?: number; /** Minimum probability threshold for sequence patterns */ minSequenceProbability?: number; /** Maximum number of keys to prefetch per cycle */ maxPrefetchCount?: number; /** Time window (ms) for considering patterns relevant */ patternWindowMs?: number; /** Maximum sequence length to track */ maxSequenceLength?: number; } /** * PredictionEngine analyzes historical access patterns to predict future key accesses. * Supports both time-based patterns (periodic access) and sequence-based patterns * (correlated key access). * * Requirements: * - 8.1: Analyze historical access patterns to identify recurring patterns * - 8.3: Support time-based pattern recognition for periodic access patterns * - 8.4: Support sequence-based pattern recognition for correlated key access */ export declare class PredictionEngine { private readonly accessTracker; private readonly config; /** Storage for detected time-based patterns */ private timePatterns; /** Storage for detected sequence patterns */ private sequencePatterns; /** Recent access sequence for pattern detection */ private recentAccessSequence; /** Whether prediction is enabled */ private enabled; /** Tracks time-slot access counts for pattern detection: key -> Map */ private timeSlotAccesses; /** Tracks sequence co-occurrences: triggerKey -> Map */ private sequenceCooccurrences; /** Total access count per key for probability calculation */ private keyAccessCounts; /** TierManager for proactive promotion - Requirement 8.2 */ private tierManager; constructor(accessTracker: AccessTracker, config?: PredictionEngineConfig); /** * Analyze historical access patterns to identify recurring patterns. * Updates both time-based and sequence-based pattern storage. * * Requirement 8.1: Analyze historical access patterns */ analyzePatterns(): void; /** * Record an access sequence for pattern learning. * Called when keys are accessed to build pattern data. * * @param keys - Array of keys accessed in sequence */ recordAccessSequence(keys: string[]): void; /** * Predict the next access for a specific key. * * @param key - The key to predict access for * @returns Prediction result or null if no prediction available */ predictNextAccess(key: string): PredictionResult | null; /** * Get a list of keys that should be proactively prefetched. * Based on current time and recent access patterns. * * @returns Array of keys to prefetch */ getProactivePrefetchList(): string[]; /** * Enable the prediction engine. * * Requirement 8.5: Configurable enable/disable */ enable(): void; /** * Disable the prediction engine. * * Requirement 8.5: Configurable enable/disable */ disable(): void; /** * Check if prediction is enabled. */ isEnabled(): boolean; /** * Set the maximum number of keys to prefetch per cycle. * * @param count - Maximum prefetch count * * Requirement 8.6: Limit proactive promotions */ setMaxPrefetchCount(count: number): void; /** * Get the current max prefetch count. */ getMaxPrefetchCount(): number; /** * Get all detected time patterns. */ getTimePatterns(): Map; /** * Get all detected sequence patterns. */ getSequencePatterns(): Map; /** * Get time patterns for a specific key. */ getTimePatternsForKey(key: string): TimePattern[]; /** * Get sequence pattern for a specific trigger key. */ getSequencePatternForKey(key: string): SequencePattern | undefined; /** * Clear all pattern data. */ clear(): void; /** * Set the TierManager for proactive promotion. * When set, the prediction engine can trigger proactive promotions to RAM tier. * * Requirement 8.2: Wire to TierManager for proactive promotion * * @param tierManager - The TierManager instance to use for promotions */ setTierManager(tierManager: TierManager): void; /** * Get the configured TierManager. * * @returns The TierManager instance or null if not configured */ getTierManager(): TierManager | null; /** * Trigger proactive prefetch based on current predictions. * Gets the prefetch list and queues promotions to RAM tier via TierManager. * * Requirement 8.2: When the Prediction_Engine identifies a high-probability future access, * THE Tier_Manager SHALL proactively promote the data to RAM_Tier * * @returns Result of the promotion processing, or null if TierManager not configured */ triggerProactivePrefetch(): Promise; /** * Queue proactive prefetch without immediately processing. * Useful when you want to batch prefetch with other movements. * * Requirement 8.2: Proactive promotion based on predictions * * @returns Number of keys queued for promotion, or 0 if TierManager not configured */ queueProactivePrefetch(): number; /** * Record a time-slot access for pattern detection. */ private recordTimeSlotAccess; /** * Record sequence co-occurrences for pattern detection. */ private recordSequenceCooccurrences; /** * Analyze accumulated data to detect time-based patterns. * * Requirement 8.3: Time-based pattern recognition */ private analyzeTimePatterns; /** * Analyze accumulated data to detect sequence-based patterns. * * Requirement 8.4: Sequence-based pattern recognition */ private analyzeSequencePatterns; /** * Predict access based on time patterns. */ private predictFromTimePattern; /** * Predict access based on sequence patterns. */ private predictFromSequencePattern; /** * Calculate the next occurrence of a time pattern. */ private calculateNextOccurrence; } //# sourceMappingURL=PredictionEngine.d.ts.map