things3-core 1.0.1

Core library for Things 3 database access and data models
Documentation
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
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
//! Caching middleware for MCP (Model Context Protocol) tool results

use anyhow::Result;
use chrono::{DateTime, Utc};
use parking_lot::RwLock;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::sync::Arc;
use std::time::Duration;
use tracing::{debug, info, warn};

/// MCP tool result cache entry
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MCPCacheEntry<T> {
    pub tool_name: String,
    pub parameters: HashMap<String, serde_json::Value>,
    pub result: T,
    pub cached_at: DateTime<Utc>,
    pub expires_at: DateTime<Utc>,
    pub access_count: u64,
    pub last_accessed: DateTime<Utc>,
    pub cache_key: String,
    pub result_size_bytes: usize,
    pub compression_ratio: f64,
}

/// MCP cache configuration
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct MCPCacheConfig {
    /// Maximum number of cached results
    pub max_entries: usize,
    /// Time to live for cache entries
    pub ttl: Duration,
    /// Time to idle for cache entries
    pub tti: Duration,
    /// Enable compression for large results
    pub enable_compression: bool,
    /// Compression threshold in bytes
    pub compression_threshold: usize,
    /// Maximum result size to cache
    pub max_result_size: usize,
    /// Enable cache warming for frequently used tools
    pub enable_cache_warming: bool,
    /// Cache warming interval
    pub warming_interval: Duration,
}

impl Default for MCPCacheConfig {
    fn default() -> Self {
        Self {
            max_entries: 1000,
            ttl: Duration::from_secs(3600), // 1 hour
            tti: Duration::from_secs(300),  // 5 minutes
            enable_compression: true,
            compression_threshold: 1024,       // 1KB
            max_result_size: 10 * 1024 * 1024, // 10MB
            enable_cache_warming: true,
            warming_interval: Duration::from_secs(60), // 1 minute
        }
    }
}

/// MCP cache statistics
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct MCPCacheStats {
    pub total_entries: u64,
    pub hits: u64,
    pub misses: u64,
    pub hit_rate: f64,
    pub total_size_bytes: u64,
    pub compressed_entries: u64,
    pub uncompressed_entries: u64,
    pub evictions: u64,
    pub warming_entries: u64,
    pub average_access_time_ms: f64,
}

impl MCPCacheStats {
    pub fn calculate_hit_rate(&mut self) {
        let total = self.hits + self.misses;
        self.hit_rate = if total > 0 {
            #[allow(clippy::cast_precision_loss)]
            {
                self.hits as f64 / total as f64
            }
        } else {
            0.0
        };
    }
}

/// MCP tool cache middleware
pub struct MCPCacheMiddleware<T> {
    /// Cache entries by tool name and parameters
    cache: Arc<RwLock<HashMap<String, MCPCacheEntry<T>>>>,
    /// Configuration
    config: MCPCacheConfig,
    /// Statistics
    stats: Arc<RwLock<MCPCacheStats>>,
    /// Cache warming entries (key -> priority)
    warming_entries: Arc<RwLock<HashMap<String, u32>>>,
    /// Cache warming task handle
    warming_task: Option<tokio::task::JoinHandle<()>>,
}

impl<T> MCPCacheMiddleware<T>
where
    T: Clone + Serialize + for<'de> Deserialize<'de> + Send + Sync + 'static,
{
    /// Create a new MCP cache middleware
    #[must_use]
    pub fn new(config: &MCPCacheConfig) -> Self {
        let mut middleware = Self {
            cache: Arc::new(RwLock::new(HashMap::new())),
            config: config.clone(),
            stats: Arc::new(RwLock::new(MCPCacheStats::default())),
            warming_entries: Arc::new(RwLock::new(HashMap::new())),
            warming_task: None,
        };

        // Start cache warming task if enabled
        if config.enable_cache_warming {
            middleware.start_cache_warming();
        }

        middleware
    }

    /// Create a new middleware with default configuration
    #[must_use]
    pub fn new_default() -> Self {
        Self::new(&MCPCacheConfig::default())
    }

    /// Execute a tool with caching
    ///
    /// # Errors
    ///
    /// This function will return an error if:
    /// - Tool execution fails
    /// - Cache operations fail
    /// - Serialization/deserialization fails
    pub async fn execute_tool<F, Fut>(
        &self,
        tool_name: &str,
        parameters: HashMap<String, serde_json::Value>,
        tool_executor: F,
    ) -> Result<T>
    where
        F: FnOnce(HashMap<String, serde_json::Value>) -> Fut,
        Fut: std::future::Future<Output = Result<T>>,
    {
        let cache_key = Self::generate_cache_key(tool_name, &parameters);

        // Check cache first
        if let Some(cached_entry) = self.get_cached_entry(&cache_key) {
            if !cached_entry.is_expired() && !cached_entry.is_idle(self.config.tti) {
                self.record_hit();
                debug!(
                    "MCP cache hit for tool: {} with key: {}",
                    tool_name, cache_key
                );
                return Ok(cached_entry.result);
            }
        }

        // Cache miss - execute tool
        self.record_miss();
        let start_time = std::time::Instant::now();

        let result = tool_executor(parameters.clone()).await?;
        let execution_time = start_time.elapsed();

        // Check if result is too large to cache
        let result_size = Self::calculate_result_size(&result);
        if result_size > self.config.max_result_size {
            warn!("MCP tool result too large to cache: {} bytes", result_size);
            return Ok(result);
        }

        // Cache the result
        self.cache_result(
            tool_name,
            parameters,
            result.clone(),
            &cache_key,
            result_size,
        );

        debug!(
            "MCP tool executed and cached: {} ({}ms, {} bytes)",
            tool_name,
            execution_time.as_millis(),
            result_size
        );

        Ok(result)
    }

    /// Get a cached result without executing the tool
    #[must_use]
    pub fn get_cached_result(
        &self,
        tool_name: &str,
        parameters: &HashMap<String, serde_json::Value>,
    ) -> Option<T> {
        let cache_key = Self::generate_cache_key(tool_name, parameters);

        if let Some(cached_entry) = self.get_cached_entry(&cache_key) {
            if !cached_entry.is_expired() && !cached_entry.is_idle(self.config.tti) {
                self.record_hit();
                return Some(cached_entry.result);
            }
        }

        self.record_miss();
        None
    }

    /// Invalidate cache entries for a specific tool
    pub fn invalidate_tool(&self, tool_name: &str) {
        let mut cache = self.cache.write();
        let keys_to_remove: Vec<String> = cache
            .iter()
            .filter(|(_, entry)| entry.tool_name == tool_name)
            .map(|(key, _)| key.clone())
            .collect();

        let count = keys_to_remove.len();
        for key in keys_to_remove {
            cache.remove(&key);
        }

        debug!(
            "Invalidated {} cache entries for tool: {}",
            count, tool_name
        );
    }

    /// Invalidate all cache entries
    pub fn invalidate_all(&self) {
        let mut cache = self.cache.write();
        cache.clear();
        info!("Invalidated all MCP cache entries");
    }

    /// Get cache statistics
    #[must_use]
    pub fn get_stats(&self) -> MCPCacheStats {
        let mut stats = self.stats.read().clone();
        stats.calculate_hit_rate();
        stats
    }

    /// Get cache size in bytes
    #[must_use]
    pub fn get_cache_size(&self) -> usize {
        let cache = self.cache.read();
        cache.values().map(|entry| entry.result_size_bytes).sum()
    }

    /// Get cache utilization percentage
    #[must_use]
    #[allow(clippy::cast_precision_loss)]
    pub fn get_utilization(&self) -> f64 {
        let current_size = self.get_cache_size();
        let max_size = self.config.max_entries * self.config.max_result_size;
        (current_size as f64 / max_size as f64) * 100.0
    }

    /// Generate cache key from tool name and parameters
    fn generate_cache_key(
        tool_name: &str,
        parameters: &HashMap<String, serde_json::Value>,
    ) -> String {
        use std::collections::hash_map::DefaultHasher;
        use std::hash::{Hash, Hasher};

        let mut key_parts = vec![tool_name.to_string()];

        // Sort parameters for consistent key generation
        let mut sorted_params: Vec<_> = parameters.iter().collect();
        sorted_params.sort_by_key(|(k, _)| *k);

        for (param_name, param_value) in sorted_params {
            key_parts.push(format!("{param_name}:{param_value}"));
        }

        // Use a hash of the key parts to keep it manageable
        let mut hasher = DefaultHasher::new();
        key_parts.join("|").hash(&mut hasher);
        format!("mcp:{}:{}", tool_name, hasher.finish())
    }

    /// Get a cached entry
    fn get_cached_entry(&self, cache_key: &str) -> Option<MCPCacheEntry<T>> {
        let mut cache = self.cache.write();
        if let Some(entry) = cache.get_mut(cache_key) {
            entry.access_count += 1;
            entry.last_accessed = Utc::now();
            Some(entry.clone())
        } else {
            None
        }
    }

    /// Cache a tool result
    fn cache_result(
        &self,
        tool_name: &str,
        parameters: HashMap<String, serde_json::Value>,
        result: T,
        cache_key: &str,
        result_size: usize,
    ) {
        let now = Utc::now();
        let expires_at = now + chrono::Duration::from_std(self.config.ttl).unwrap_or_default();

        let entry = MCPCacheEntry {
            tool_name: tool_name.to_string(),
            parameters,
            result,
            cached_at: now,
            expires_at,
            access_count: 0,
            last_accessed: now,
            cache_key: cache_key.to_string(),
            result_size_bytes: result_size,
            compression_ratio: 1.0, // TODO: Implement compression
        };

        // Check if we need to evict entries
        self.evict_if_needed();

        let mut cache = self.cache.write();
        cache.insert(cache_key.to_string(), entry);

        // Update statistics
        {
            let mut stats = self.stats.write();
            stats.total_entries += 1;
            stats.total_size_bytes += result_size as u64;
        }
    }

    /// Calculate result size in bytes
    fn calculate_result_size(result: &T) -> usize {
        serde_json::to_vec(result).map_or(0, |bytes| bytes.len())
    }

    /// Evict entries if cache is full
    fn evict_if_needed(&self) {
        let mut cache = self.cache.write();

        if cache.len() >= self.config.max_entries {
            // Remove oldest entries (LRU)
            let mut entries: Vec<_> = cache
                .iter()
                .map(|(k, v)| (k.clone(), v.last_accessed))
                .collect();
            entries.sort_by_key(|(_, last_accessed)| *last_accessed);

            let entries_to_remove = cache.len() - self.config.max_entries + 1;
            for (key, _) in entries.iter().take(entries_to_remove) {
                cache.remove(key);
            }

            // Update statistics
            {
                let mut stats = self.stats.write();
                stats.evictions += entries_to_remove as u64;
            }
        }
    }

    /// Start cache warming background task
    fn start_cache_warming(&mut self) {
        let warming_entries = Arc::clone(&self.warming_entries);
        let warming_interval = self.config.warming_interval;

        let handle = tokio::spawn(async move {
            let mut interval = tokio::time::interval(warming_interval);
            loop {
                interval.tick().await;

                // In a real implementation, you would warm frequently accessed entries
                // by calling the appropriate tool executors
                let entries_count = {
                    let entries = warming_entries.read();
                    entries.len()
                };

                if entries_count > 0 {
                    debug!("MCP cache warming {} entries", entries_count);
                }
            }
        });

        self.warming_task = Some(handle);
    }

    /// Record a cache hit
    fn record_hit(&self) {
        let mut stats = self.stats.write();
        stats.hits += 1;
    }

    /// Record a cache miss
    fn record_miss(&self) {
        let mut stats = self.stats.write();
        stats.misses += 1;
    }
}

impl<T> MCPCacheEntry<T> {
    /// Check if the cache entry is expired
    pub fn is_expired(&self) -> bool {
        Utc::now() > self.expires_at
    }

    /// Check if the cache entry is idle
    pub fn is_idle(&self, tti: Duration) -> bool {
        let now = Utc::now();
        let idle_duration = now - self.last_accessed;
        idle_duration > chrono::Duration::from_std(tti).unwrap_or_default()
    }
}

impl<T> Drop for MCPCacheMiddleware<T> {
    fn drop(&mut self) {
        if let Some(handle) = self.warming_task.take() {
            handle.abort();
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::collections::HashMap;

    #[tokio::test]
    async fn test_mcp_cache_basic_operations() {
        let middleware = MCPCacheMiddleware::<String>::new_default();

        let mut parameters = HashMap::new();
        parameters.insert(
            "query".to_string(),
            serde_json::Value::String("test".to_string()),
        );

        // First call - should be a cache miss
        let result1 = middleware
            .execute_tool("test_tool", parameters.clone(), |_| async {
                Ok("test_result".to_string())
            })
            .await
            .unwrap();

        assert_eq!(result1, "test_result");

        // Second call - should be a cache hit
        let result2 = middleware
            .execute_tool("test_tool", parameters, |_| async {
                panic!("Should not execute on cache hit")
            })
            .await
            .unwrap();

        assert_eq!(result2, "test_result");

        let stats = middleware.get_stats();
        assert_eq!(stats.hits, 1);
        assert_eq!(stats.misses, 1);
        assert!((stats.hit_rate - 0.5).abs() < 1e-9);
    }

    #[tokio::test]
    async fn test_mcp_cache_invalidation() {
        let middleware = MCPCacheMiddleware::<String>::new_default();

        let mut parameters = HashMap::new();
        parameters.insert(
            "query".to_string(),
            serde_json::Value::String("test".to_string()),
        );

        // Cache a result
        middleware
            .execute_tool("test_tool", parameters.clone(), |_| async {
                Ok("test_result".to_string())
            })
            .await
            .unwrap();

        // Verify it's cached
        let cached = middleware.get_cached_result("test_tool", &parameters);
        assert!(cached.is_some());

        // Invalidate the tool
        middleware.invalidate_tool("test_tool");

        // Verify it's no longer cached
        let cached = middleware.get_cached_result("test_tool", &parameters);
        assert!(cached.is_none());
    }

    #[tokio::test]
    async fn test_mcp_cache_key_generation() {
        let _middleware = MCPCacheMiddleware::<String>::new_default();

        let mut params1 = HashMap::new();
        params1.insert("a".to_string(), serde_json::Value::String("1".to_string()));
        params1.insert("b".to_string(), serde_json::Value::String("2".to_string()));

        let mut params2 = HashMap::new();
        params2.insert("b".to_string(), serde_json::Value::String("2".to_string()));
        params2.insert("a".to_string(), serde_json::Value::String("1".to_string()));

        // Same parameters in different order should generate same key
        let key1 = MCPCacheMiddleware::<String>::generate_cache_key("test_tool", &params1);
        let key2 = MCPCacheMiddleware::<String>::generate_cache_key("test_tool", &params2);
        assert_eq!(key1, key2);
    }
}