Skip to main content

json_eval_rs/jsoneval/
eval_cache.rs

1use indexmap::IndexSet;
2use serde_json::Value;
3use std::collections::{HashMap, HashSet};
4
5/// Token-version tracker for json paths
6#[derive(Default, Clone)]
7pub struct VersionTracker {
8    versions: HashMap<String, u64>,
9}
10
11impl VersionTracker {
12    pub fn new() -> Self {
13        Self {
14            versions: HashMap::new(),
15        }
16    }
17
18    #[inline]
19    pub fn get(&self, path: &str) -> u64 {
20        self.versions.get(path).copied().unwrap_or(0)
21    }
22
23    #[inline]
24    pub fn bump(&mut self, path: &str) {
25        let current = self.get(path);
26        // We use actual data pointers here
27        self.versions.insert(path.to_string(), current + 1);
28    }
29
30    /// Merge version counters from `other`, taking the **maximum** for each path.
31    /// Using max (not insert) ensures that if this tracker already saw a higher version
32    /// for a path (e.g., from a previous subform evaluation round), it is never downgraded.
33    pub fn merge_from(&mut self, other: &VersionTracker) {
34        for (k, v) in &other.versions {
35            let current = self.versions.get(k).copied().unwrap_or(0);
36            self.versions.insert(k.clone(), current.max(*v));
37        }
38    }
39
40    /// Merge only `/$params`-prefixed version counters from `other` (max strategy).
41    /// Used when giving a per-item tracker the latest schema-level param versions
42    /// without absorbing data-path bumps that belong to other items.
43    pub fn merge_from_params(&mut self, other: &VersionTracker) {
44        for (k, v) in &other.versions {
45            if k.starts_with("/$params") {
46                let current = self.versions.get(k).copied().unwrap_or(0);
47                self.versions.insert(k.clone(), current.max(*v));
48            }
49        }
50    }
51
52    /// Returns true if any tracked path with the given prefix has been bumped (version > 0).
53    /// Used to gate table re-evaluation when item fields change without the item being new.
54    pub fn any_bumped_with_prefix(&self, prefix: &str) -> bool {
55        self.versions
56            .iter()
57            .any(|(k, &v)| k.starts_with(prefix) && v > 0)
58    }
59
60    /// Returns true if any path with the given prefix has a **higher** version than in `baseline`.
61    /// Unlike `any_bumped_with_prefix`, this detects only brand-new bumps from a specific diff
62    /// pass, ignoring historical bumps that were already present in the baseline.
63    pub fn any_newly_bumped_with_prefix(&self, prefix: &str, baseline: &VersionTracker) -> bool {
64        self.versions
65            .iter()
66            .any(|(k, &v)| k.starts_with(prefix) && v > baseline.get(k))
67    }
68
69    /// Returns an iterator over all (path, version) pairs, for targeted bump enumeration.
70    pub fn versions(&self) -> impl Iterator<Item = (&str, &u64)> {
71        self.versions.iter().map(|(k, v)| (k.as_str(), v))
72    }
73}
74
75/// A cached evaluation result with the specific dependency versions it was evaluated against
76#[derive(Clone)]
77pub struct CacheEntry {
78    pub dep_versions: HashMap<String, u64>,
79    pub result: Value,
80    /// The `active_item_index` this entry was computed under.
81    /// `None` = computed during main-form evaluation (safe to reuse across all items
82    /// provided the dep versions match). `Some(idx)` = computed for a specific item;
83    /// Tier-2 reuse is restricted to entries whose deps are entirely `$params`-scoped.
84    pub computed_for_item: Option<usize>,
85}
86
87/// Independent cache state for a single item in a subform array
88#[derive(Default, Clone)]
89pub struct SubformItemCache {
90    pub data_versions: VersionTracker,
91    pub entries: HashMap<String, CacheEntry>,
92    pub item_snapshot: Value,
93}
94
95impl SubformItemCache {
96    pub fn new() -> Self {
97        Self {
98            data_versions: VersionTracker::new(),
99            entries: HashMap::new(),
100            item_snapshot: Value::Null,
101        }
102    }
103}
104
105/// Primary cache structure for a JSON evaluation instance
106#[derive(Clone)]
107pub struct EvalCache {
108    pub data_versions: VersionTracker,
109    pub params_versions: VersionTracker,
110    pub entries: HashMap<String, CacheEntry>,
111
112    pub active_item_index: Option<usize>,
113    pub subform_caches: HashMap<usize, SubformItemCache>,
114
115    /// Monotonically increasing counter bumped whenever data_versions or params_versions change.
116    /// When `eval_generation == last_evaluated_generation`, all cache entries are guaranteed valid
117    /// and `evaluate_internal` can skip the full tree traversal.
118    pub eval_generation: u64,
119    pub last_evaluated_generation: u64,
120
121    /// Snapshot of the last fully-diffed main-form data payload.
122    /// Stored after each successful `evaluate_internal_with_new_data` call so the next
123    /// invocation can avoid an extra `snapshot_data_clone()` when computing the diff.
124    pub main_form_snapshot: Option<Value>,
125}
126
127impl Default for EvalCache {
128    fn default() -> Self {
129        Self::new()
130    }
131}
132
133impl EvalCache {
134    pub fn new() -> Self {
135        Self {
136            data_versions: VersionTracker::new(),
137            params_versions: VersionTracker::new(),
138            entries: HashMap::new(),
139            active_item_index: None,
140            subform_caches: HashMap::new(),
141            eval_generation: 0,
142            last_evaluated_generation: u64::MAX, // force first evaluate_internal to run
143            main_form_snapshot: None,
144        }
145    }
146
147    pub fn clear(&mut self) {
148        self.data_versions = VersionTracker::new();
149        self.params_versions = VersionTracker::new();
150        self.entries.clear();
151        self.active_item_index = None;
152        self.subform_caches.clear();
153        self.eval_generation = 0;
154        self.last_evaluated_generation = u64::MAX;
155        self.main_form_snapshot = None;
156    }
157
158    /// Remove item caches for indices >= `current_count`.
159    /// Call this whenever the subform array length is known to have shrunk so that
160    /// stale per-item version trackers and cached entries do not linger in memory.
161    pub fn prune_subform_caches(&mut self, current_count: usize) {
162        self.subform_caches.retain(|&idx, _| idx < current_count);
163    }
164
165    /// Invalidate all `$params`-scoped table cache entries for a specific item.
166    ///
167    /// Called when a brand-new subform item is introduced so that `$params` tables
168    /// that aggregate array data (e.g. WOP_RIDERS) are forced to recompute instead
169    /// of returning stale results cached from a prior main-form evaluation that ran
170    /// when the item was absent (and thus saw zero/null for that item's values).
171    pub fn invalidate_params_tables_for_item(&mut self, idx: usize, table_keys: &[String]) {
172        // Bump params_versions so T2 global entries for these tables are stale.
173        for key in table_keys {
174            let data_path = crate::jsoneval::path_utils::normalize_to_json_pointer(key)
175                .replace("/properties/", "/");
176            let data_path = data_path.trim_start_matches('#');
177            let data_path = if data_path.starts_with('/') {
178                data_path.to_string()
179            } else {
180                format!("/{}", data_path)
181            };
182            self.params_versions.bump(&data_path);
183            self.eval_generation += 1;
184        }
185
186        // Evict matching T1 (item-level) entries so they are not reused.
187        if let Some(item_cache) = self.subform_caches.get_mut(&idx) {
188            for key in table_keys {
189                item_cache.entries.remove(key);
190            }
191        }
192    }
193
194    /// Returns true if evaluate_internal must run (versions changed since last full evaluation)
195    pub fn needs_full_evaluation(&self) -> bool {
196        self.eval_generation != self.last_evaluated_generation
197    }
198
199    /// Call after evaluate_internal completes successfully to mark the generation stable
200    pub fn mark_evaluated(&mut self) {
201        self.last_evaluated_generation = self.eval_generation;
202    }
203
204    pub(crate) fn ensure_active_item_cache(&mut self, idx: usize) {
205        self.subform_caches
206            .entry(idx)
207            .or_insert_with(SubformItemCache::new);
208    }
209
210    pub fn set_active_item(&mut self, idx: usize) {
211        self.active_item_index = Some(idx);
212        self.ensure_active_item_cache(idx);
213    }
214
215    pub fn clear_active_item(&mut self) {
216        self.active_item_index = None;
217    }
218
219    /// Recursively diffs `old` against `new` and bumps version for every changed data path scalar.
220    pub fn store_snapshot_and_diff_versions(&mut self, old: &Value, new: &Value) {
221        if let Some(idx) = self.active_item_index {
222            self.ensure_active_item_cache(idx);
223            let sub_cache = self.subform_caches.get_mut(&idx).unwrap();
224            diff_and_update_versions(&mut sub_cache.data_versions, "", old, new);
225            sub_cache.item_snapshot = new.clone();
226        } else {
227            diff_and_update_versions(&mut self.data_versions, "", old, new);
228        }
229    }
230
231    pub fn get_active_snapshot(&self) -> Value {
232        if let Some(idx) = self.active_item_index {
233            self.subform_caches
234                .get(&idx)
235                .map(|c| c.item_snapshot.clone())
236                .unwrap_or(Value::Null)
237        } else {
238            Value::Null
239        }
240    }
241
242    pub fn diff_active_item(
243        &mut self,
244        field_key: &str,
245        old_sub_data: &Value,
246        new_sub_data: &Value,
247    ) {
248        if let Some(idx) = self.active_item_index {
249            self.ensure_active_item_cache(idx);
250            let sub_cache = self.subform_caches.get_mut(&idx).unwrap();
251
252            // Diff ONLY the localized item part, skipping the massive parent tree
253            let empty = Value::Null;
254            let old_item = old_sub_data.get(field_key).unwrap_or(&empty);
255            let new_item = new_sub_data.get(field_key).unwrap_or(&empty);
256
257            diff_and_update_versions(
258                &mut sub_cache.data_versions,
259                &format!("/{}", field_key),
260                old_item,
261                new_item,
262            );
263            sub_cache.item_snapshot = new_sub_data.clone();
264        }
265    }
266
267    pub fn bump_data_version(&mut self, data_path: &str) {
268        // Always signal that something changed so the parent's needs_full_evaluation()
269        // returns true even when the bump was item-scoped.
270        self.eval_generation += 1;
271        if let Some(idx) = self.active_item_index {
272            if let Some(cache) = self.subform_caches.get_mut(&idx) {
273                cache.data_versions.bump(data_path);
274            }
275        } else {
276            self.data_versions.bump(data_path);
277        }
278    }
279
280    pub fn bump_params_version(&mut self, data_path: &str) {
281        self.params_versions.bump(data_path);
282        self.eval_generation += 1;
283    }
284
285    /// Check if the `eval_key` result can be safely bypassed because dependencies are unchanged.
286    ///
287    /// Two-tier lookup:
288    /// - Tier 1: item-scoped entries in `subform_caches[idx]` — checked first when an active item is set
289    /// - Tier 2: global `self.entries` — allows Run 1 (main form) results to be reused in Run 2 (subform)
290    pub fn check_cache(&self, eval_key: &str, deps: &IndexSet<String>) -> Option<Value> {
291        if let Some(idx) = self.active_item_index {
292            // Tier 1: item-specific entries (always safe to reuse for the same index)
293            if let Some(cache) = self.subform_caches.get(&idx) {
294                if let Some(hit) =
295                    self.validate_entry(eval_key, deps, &cache.entries, &cache.data_versions)
296                {
297                    if std::env::var("JSONEVAL_DEBUG_CACHE").is_ok() {
298                        println!("Cache HIT [T1 idx={}] {}", idx, eval_key);
299                    }
300                    return Some(hit);
301                }
302            }
303
304            // Tier 2: global entries (may have been stored by main-form Run 1).
305            // Only reuse if the entry is index-safe:
306            //   (a) computed with no active item (main-form result), OR
307            //   (b) computed for the same item index, OR
308            //   (c) all deps are $params-scoped (truly index-independent)
309            let item_data_versions = self
310                .subform_caches
311                .get(&idx)
312                .map(|c| &c.data_versions)
313                .unwrap_or(&self.data_versions);
314
315            if let Some(entry) = self.entries.get(eval_key) {
316                let index_safe = match entry.computed_for_item {
317                    // Main-form entry (no active item when stored): only safe if ALL its deps
318                    // are $params-scoped. Non-$params deps (like /riders/prem_pay_period) mean
319                    // the formula result is rider-specific — using it for a different rider via
320                    // the batch fast path would corrupt eval_data and poison subsequent formulas.
321                    None => entry.dep_versions.keys().all(|p| p.starts_with("/$params")),
322                    Some(stored_idx) if stored_idx == idx => true,
323                    _ => entry.dep_versions.keys().all(|p| p.starts_with("/$params")),
324                };
325                if index_safe {
326                    let result =
327                        self.validate_entry(eval_key, deps, &self.entries, item_data_versions);
328                    if result.is_some() {
329                        if std::env::var("JSONEVAL_DEBUG_CACHE").is_ok() {
330                            println!(
331                                "Cache HIT [T2 idx={} for={:?}] {}",
332                                idx, entry.computed_for_item, eval_key
333                            );
334                        }
335                    }
336                    return result;
337                }
338            }
339
340            None
341        } else {
342            self.validate_entry(eval_key, deps, &self.entries, &self.data_versions)
343        }
344    }
345
346    fn validate_entry(
347        &self,
348        eval_key: &str,
349        deps: &IndexSet<String>,
350        entries: &HashMap<String, CacheEntry>,
351        data_versions: &VersionTracker,
352    ) -> Option<Value> {
353        let entry = entries.get(eval_key)?;
354        for dep in deps {
355            let data_dep_path = crate::jsoneval::path_utils::normalize_to_json_pointer(dep)
356                .replace("/properties/", "/");
357
358            let current_ver = if data_dep_path.starts_with("/$params") {
359                self.params_versions.get(&data_dep_path)
360            } else {
361                data_versions.get(&data_dep_path)
362            };
363
364            if let Some(&cached_ver) = entry.dep_versions.get(&data_dep_path) {
365                if current_ver != cached_ver {
366                    if std::env::var("JSONEVAL_DEBUG_CACHE").is_ok() {
367                        println!(
368                            "Cache MISS {}: dep {} changed ({} -> {})",
369                            eval_key, data_dep_path, cached_ver, current_ver
370                        );
371                    }
372                    return None;
373                }
374            } else {
375                if std::env::var("JSONEVAL_DEBUG_CACHE").is_ok() {
376                    println!(
377                        "Cache MISS {}: dep {} missing from cache entry",
378                        eval_key, data_dep_path
379                    );
380                }
381                return None;
382            }
383        }
384        if std::env::var("JSONEVAL_DEBUG_CACHE").is_ok() {
385            println!("Cache HIT {}", eval_key);
386        }
387        Some(entry.result.clone())
388    }
389
390    /// Store the newly evaluated value and snapshot the dependency versions.
391    ///
392    /// Storage strategy:
393    /// - When an active item is set, store into `subform_caches[idx].entries` (item-scoped).
394    ///   This isolates per-rider results so different items with different data don't collide.
395    /// - The global `self.entries` is written only from the main form (no active item).
396    ///   Subforms can reuse these via the Tier 2 fallback in `check_cache`.
397    pub fn store_cache(&mut self, eval_key: &str, deps: &IndexSet<String>, result: Value) {
398        // Phase 1: snapshot dep versions using the correct data_versions tracker
399        let mut dep_versions = HashMap::with_capacity(deps.len());
400        {
401            let data_versions = if let Some(idx) = self.active_item_index {
402                self.ensure_active_item_cache(idx);
403                &self.subform_caches[&idx].data_versions
404            } else {
405                &self.data_versions
406            };
407
408            for dep in deps {
409                let data_dep_path = crate::jsoneval::path_utils::normalize_to_json_pointer(dep)
410                    .replace("/properties/", "/");
411                let ver = if data_dep_path.starts_with("/$params") {
412                    self.params_versions.get(&data_dep_path)
413                } else {
414                    data_versions.get(&data_dep_path)
415                };
416                dep_versions.insert(data_dep_path, ver);
417            }
418        }
419
420        // Phase 2: insert into the correct tier, tagging with the current item index.
421        let computed_for_item = self.active_item_index;
422
423        // For $params-scoped entries, only bump params_versions when the result value
424        // actually changed relative to the canonical cached entry.
425        //
426        // For T1 stores (active_item set), we compare against T2 (global) first.
427        // T2 is the authoritative reference: if T2 already holds the same value,
428        // params_versions was already bumped for it — bumping again per-rider causes
429        // an O(riders × $params_formulas) version explosion that makes every downstream
430        // formula (TOTAL_WOP_SA, WOP_MULTIPLIER, COMMISSION_FACTOR…) miss on each rider.
431        if eval_key.starts_with("#/$params") {
432            let existing_result: Option<&Value> = if let Some(idx) = self.active_item_index {
433                // Check T2 (global) first — if T2 has same value, no need to bump again.
434                self.entries.get(eval_key).map(|e| &e.result).or_else(|| {
435                    self.subform_caches
436                        .get(&idx)
437                        .and_then(|c| c.entries.get(eval_key))
438                        .map(|e| &e.result)
439                })
440            } else {
441                self.entries.get(eval_key).map(|e| &e.result)
442            };
443
444            let value_changed = existing_result.map_or(true, |r| r != &result);
445
446            if value_changed {
447                let data_path = crate::jsoneval::path_utils::normalize_to_json_pointer(eval_key)
448                    .replace("/properties/", "/");
449                let data_path = data_path.trim_start_matches('#').to_string();
450                let data_path = if data_path.starts_with('/') {
451                    data_path
452                } else {
453                    format!("/{}", data_path)
454                };
455
456                // Bump the explicit path and its table-level parent.
457                // Stop at slash_count < 3 — never bump /$params/others or /$params itself.
458                let mut current_path = data_path.as_str();
459                let mut slash_count = current_path.matches('/').count();
460
461                while slash_count >= 3 {
462                    self.params_versions.bump(current_path);
463                    if let Some(last_slash) = current_path.rfind('/') {
464                        current_path = &current_path[..last_slash];
465                        slash_count -= 1;
466                    } else {
467                        break;
468                    }
469                }
470
471                self.eval_generation += 1;
472            }
473        }
474
475        let entry = CacheEntry {
476            dep_versions,
477            result,
478            computed_for_item,
479        };
480
481        if let Some(idx) = self.active_item_index {
482            // Store item-scoped: isolates per-rider entries so riders with different data don't collide
483            self.subform_caches
484                .get_mut(&idx)
485                .unwrap()
486                .entries
487                .insert(eval_key.to_string(), entry.clone());
488
489            // For $params-scoped formulas, also promote to T2 (global entries).
490            // $params formulas are index-independent: all riders produce the same result.
491            // Without T2 promotion, each rider's first store compares against stale/missing T2
492            // and sees the value as "new" → bumps params_versions → O(riders) cascade.
493            // With T2 promotion, rider 1 finds rider 0's result in T2 → no bump → no cascade.
494            if eval_key.starts_with("#/$params") {
495                self.entries.insert(eval_key.to_string(), entry);
496            }
497        } else {
498            self.entries.insert(eval_key.to_string(), entry);
499        }
500    }
501}
502
503/// Recursive helper to walk JSON structures and bump specific leaf versions where they differ
504pub(crate) fn diff_and_update_versions(
505    tracker: &mut VersionTracker,
506    pointer: &str,
507    old: &Value,
508    new: &Value,
509) {
510    if pointer.is_empty() {
511        diff_and_update_versions_internal(tracker, "", old, new);
512    } else {
513        diff_and_update_versions_internal(tracker, pointer, old, new);
514    }
515}
516
517fn diff_and_update_versions_internal(
518    tracker: &mut VersionTracker,
519    pointer: &str,
520    old: &Value,
521    new: &Value,
522) {
523    if old == new {
524        return;
525    }
526
527    match (old, new) {
528        (Value::Object(a), Value::Object(b)) => {
529            let mut keys = HashSet::new();
530            for k in a.keys() {
531                keys.insert(k.as_str());
532            }
533            for k in b.keys() {
534                keys.insert(k.as_str());
535            }
536
537            for key in keys {
538                // Do not deep-diff $params at any nesting level — it is manually tracked
539                // via bump_params_version on evaluations. Skipping at root-only was insufficient
540                // when item data is diffed via a non-empty pointer prefix.
541                if key == "$params" {
542                    continue;
543                }
544
545                let a_val = a.get(key).unwrap_or(&Value::Null);
546                let b_val = b.get(key).unwrap_or(&Value::Null);
547
548                let escaped_key = key.replace('~', "~0").replace('/', "~1");
549                let next_path = format!("{}/{}", pointer, escaped_key);
550                diff_and_update_versions_internal(tracker, &next_path, a_val, b_val);
551            }
552        }
553        (Value::Array(a), Value::Array(b)) => {
554            let max_len = a.len().max(b.len());
555            for i in 0..max_len {
556                let a_val = a.get(i).unwrap_or(&Value::Null);
557                let b_val = b.get(i).unwrap_or(&Value::Null);
558                let next_path = format!("{}/{}", pointer, i);
559                diff_and_update_versions_internal(tracker, &next_path, a_val, b_val);
560            }
561        }
562        (old_val, new_val) => {
563            if old_val != new_val {
564                tracker.bump(pointer);
565
566                // If either side contains nested structures (e.g. Object replaced by Null, or vice versa)
567                // we must recursively bump all paths inside them so targeted cache entries invalidate.
568                if old_val.is_object() || old_val.is_array() {
569                    traverse_and_bump(tracker, pointer, old_val);
570                }
571                if new_val.is_object() || new_val.is_array() {
572                    traverse_and_bump(tracker, pointer, new_val);
573                }
574            }
575        }
576    }
577}
578
579/// Recursively traverses a value and bumps the version for every nested path.
580/// Used when a structural type mismatch occurs (e.g., Object -> Null) so that
581/// cache entries depending on nested fields are correctly invalidated.
582fn traverse_and_bump(tracker: &mut VersionTracker, pointer: &str, val: &Value) {
583    match val {
584        Value::Object(map) => {
585            for (key, v) in map {
586                if key == "$params" {
587                    continue; // Skip the special top-level params branch if it leaked here
588                }
589                let escaped_key = key.replace('~', "~0").replace('/', "~1");
590                let next_path = format!("{}/{}", pointer, escaped_key);
591                tracker.bump(&next_path);
592                traverse_and_bump(tracker, &next_path, v);
593            }
594        }
595        Value::Array(arr) => {
596            for (i, v) in arr.iter().enumerate() {
597                let next_path = format!("{}/{}", pointer, i);
598                tracker.bump(&next_path);
599                traverse_and_bump(tracker, &next_path, v);
600            }
601        }
602        _ => {}
603    }
604}