1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
//! Node store caching layer for performance optimization.
//!
//! This module provides a simple LRU cache for NodeRecord instances to avoid
//! repeated deserialization during graph traversal operations.
use crate::backend::native::types::*;
use std::collections::HashMap;
/// Simple LRU cache for node records with bounded size
pub struct NodeRecordCache {
cache: HashMap<NativeNodeId, NodeRecord>,
access_order: Vec<NativeNodeId>,
max_size: usize,
}
impl NodeRecordCache {
/// Create a new cache with the specified maximum size
pub fn new(max_size: usize) -> Self {
Self {
cache: HashMap::with_capacity(max_size),
access_order: Vec::with_capacity(max_size),
max_size,
}
}
/// Get a cached node record, returning None if not found
pub fn get(&mut self, node_id: NativeNodeId) -> Option<NodeRecord> {
if let Some(node) = self.cache.remove(&node_id) {
// Move to end (most recently used)
self.access_order.retain(|&id| id != node_id);
self.access_order.push(node_id);
self.cache.insert(node_id, node.clone());
Some(node)
} else {
None
}
}
/// Insert a node record into the cache
pub fn put(&mut self, node_id: NativeNodeId, node: NodeRecord) {
// Remove existing entry if present
if self.cache.contains_key(&node_id) {
self.access_order.retain(|&id| id != node_id);
}
// Evict oldest entries if necessary
while self.cache.len() >= self.max_size {
if let Some(oldest_id) = self.access_order.first() {
let oldest_id = *oldest_id;
self.cache.remove(&oldest_id);
self.access_order.remove(0);
} else {
break;
}
}
// Insert new entry
self.access_order.push(node_id);
self.cache.insert(node_id, node);
}
/// Clear the cache
pub fn clear(&mut self) {
self.cache.clear();
self.access_order.clear();
}
/// Get the current cache size
pub fn size(&self) -> usize {
self.cache.len()
}
/// Check if the cache contains the given node ID
pub fn contains(&self, node_id: NativeNodeId) -> bool {
self.cache.contains_key(&node_id)
}
/// Remove a specific node from the cache
pub fn remove(&mut self, node_id: NativeNodeId) -> Option<NodeRecord> {
if self.cache.remove(&node_id).is_some() {
self.access_order.retain(|&id| id != node_id);
// Note: We can't return the node because it would require cloning
None
} else {
None
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_cache_basic_operations() {
let mut cache = NodeRecordCache::new(3);
// Test empty cache
assert!(cache.get(1).is_none());
assert_eq!(cache.size(), 0);
// Test insert and get
let node = NodeRecord::new(
1,
"Test".to_string(),
"test".to_string(),
serde_json::json!({}),
);
cache.put(1, node.clone());
assert_eq!(cache.size(), 1);
let retrieved = cache.get(1).unwrap();
assert_eq!(retrieved.id, node.id);
assert_eq!(retrieved.kind, node.kind);
}
#[test]
fn test_lru_eviction() {
let mut cache = NodeRecordCache::new(2);
// Insert two nodes
let node1 = NodeRecord::new(
1,
"Test".to_string(),
"test1".to_string(),
serde_json::json!({}),
);
let node2 = NodeRecord::new(
2,
"Test".to_string(),
"test2".to_string(),
serde_json::json!({}),
);
cache.put(1, node1);
cache.put(2, node2);
assert_eq!(cache.size(), 2);
// Insert third node, should evict first
let node3 = NodeRecord::new(
3,
"Test".to_string(),
"test3".to_string(),
serde_json::json!({}),
);
cache.put(3, node3);
assert_eq!(cache.size(), 2);
// First node should be evicted
assert!(cache.get(1).is_none());
// Second and third should be available
assert!(cache.get(2).is_some());
assert!(cache.get(3).is_some());
}
#[test]
fn test_access_order_update() {
let mut cache = NodeRecordCache::new(3);
// Insert three nodes
let node1 = NodeRecord::new(
1,
"Test".to_string(),
"test1".to_string(),
serde_json::json!({}),
);
let node2 = NodeRecord::new(
2,
"Test".to_string(),
"test2".to_string(),
serde_json::json!({}),
);
let node3 = NodeRecord::new(
3,
"Test".to_string(),
"test3".to_string(),
serde_json::json!({}),
);
cache.put(1, node1);
cache.put(2, node2);
cache.put(3, node3);
// Access first node, should make it most recently used
cache.get(1);
// Insert fourth node, should evict the least recently used (node2)
let node4 = NodeRecord::new(
4,
"Test".to_string(),
"test4".to_string(),
serde_json::json!({}),
);
cache.put(4, node4);
// Node2 should be evicted, nodes 1, 3, 4 should be available
assert!(cache.get(2).is_none());
assert!(cache.get(1).is_some());
assert!(cache.get(3).is_some());
assert!(cache.get(4).is_some());
}
}