kaccy-db 0.2.0

Database layer for Kaccy Protocol - PostgreSQL, Redis, and distributed caching
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
//! Cache invalidation patterns for distributed systems.
//!
//! This module provides:
//! - Redis pub/sub for cross-instance invalidation
//! - Tag-based invalidation
//! - Cascade invalidation rules

use futures::StreamExt;
use parking_lot::RwLock;
use redis::Client;
use serde::{Deserialize, Serialize};
use std::collections::{HashMap, HashSet};
use std::sync::Arc;
use tokio::sync::mpsc;
use tracing::{debug, error, info, warn};

use crate::cache::RedisCache;
use crate::error::{DbError, Result};

/// Invalidation event
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct InvalidationEvent {
    /// Type of invalidation
    pub event_type: InvalidationType,
    /// Keys to invalidate
    pub keys: Vec<String>,
    /// Tags to invalidate
    pub tags: Vec<String>,
    /// Timestamp of the event
    pub timestamp: chrono::DateTime<chrono::Utc>,
    /// Source instance that triggered the invalidation
    pub source_instance: String,
}

/// Type of invalidation
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum InvalidationType {
    /// Invalidate specific keys
    Keys,
    /// Invalidate all keys with a tag
    Tags,
    /// Invalidate with cascade rules
    Cascade,
    /// Invalidate all keys matching a pattern
    Pattern,
}

/// Configuration for invalidation manager
#[derive(Debug, Clone)]
pub struct InvalidationConfig {
    /// Redis pub/sub channel name
    pub pubsub_channel: String,
    /// Instance identifier (for preventing self-invalidation loops)
    pub instance_id: String,
    /// Enable cascade invalidation
    pub enable_cascade: bool,
    /// Maximum cascade depth
    pub max_cascade_depth: usize,
}

impl Default for InvalidationConfig {
    fn default() -> Self {
        Self {
            pubsub_channel: "cache:invalidation".to_string(),
            instance_id: uuid::Uuid::new_v4().to_string(),
            enable_cascade: true,
            max_cascade_depth: 5,
        }
    }
}

/// Tag registry for managing key-tag relationships
#[derive(Debug, Clone)]
pub struct TagRegistry {
    /// Map of tags to keys
    tags: Arc<RwLock<HashMap<String, HashSet<String>>>>,
    /// Map of keys to tags
    keys: Arc<RwLock<HashMap<String, HashSet<String>>>>,
}

impl Default for TagRegistry {
    fn default() -> Self {
        Self::new()
    }
}

impl TagRegistry {
    /// Create a new tag registry
    pub fn new() -> Self {
        Self {
            tags: Arc::new(RwLock::new(HashMap::new())),
            keys: Arc::new(RwLock::new(HashMap::new())),
        }
    }

    /// Register a key with tags
    pub fn register(&self, key: String, tags: Vec<String>) {
        let mut tag_map = self.tags.write();
        let mut key_map = self.keys.write();

        for tag in &tags {
            tag_map.entry(tag.clone()).or_default().insert(key.clone());
        }

        key_map.insert(key, tags.into_iter().collect());
    }

    /// Get all keys for a tag
    pub fn get_keys_for_tag(&self, tag: &str) -> Vec<String> {
        self.tags
            .read()
            .get(tag)
            .map(|keys| keys.iter().cloned().collect())
            .unwrap_or_default()
    }

    /// Get all tags for a key
    pub fn get_tags_for_key(&self, key: &str) -> Vec<String> {
        self.keys
            .read()
            .get(key)
            .map(|tags| tags.iter().cloned().collect())
            .unwrap_or_default()
    }

    /// Unregister a key
    pub fn unregister(&self, key: &str) {
        let mut key_map = self.keys.write();
        if let Some(tags) = key_map.remove(key) {
            let mut tag_map = self.tags.write();
            for tag in tags {
                if let Some(keys) = tag_map.get_mut(&tag) {
                    keys.remove(key);
                }
            }
        }
    }
}

/// Cascade rule for invalidation
#[derive(Debug, Clone)]
pub struct CascadeRule {
    /// Source tag that triggers the cascade
    pub source_tag: String,
    /// Target tags to invalidate
    pub target_tags: Vec<String>,
}

/// Invalidation manager
pub struct InvalidationManager {
    cache: Arc<RedisCache>,
    config: InvalidationConfig,
    tag_registry: TagRegistry,
    cascade_rules: Arc<RwLock<Vec<CascadeRule>>>,
    pubsub_tx: mpsc::UnboundedSender<InvalidationEvent>,
}

impl InvalidationManager {
    /// Create a new invalidation manager
    pub fn new(
        cache: Arc<RedisCache>,
        config: InvalidationConfig,
    ) -> (Self, mpsc::UnboundedReceiver<InvalidationEvent>) {
        let (tx, rx) = mpsc::unbounded_channel();

        let manager = Self {
            cache,
            config,
            tag_registry: TagRegistry::new(),
            cascade_rules: Arc::new(RwLock::new(Vec::new())),
            pubsub_tx: tx,
        };

        (manager, rx)
    }

    /// Register a cascade rule
    pub fn add_cascade_rule(&self, rule: CascadeRule) {
        info!(
            source = %rule.source_tag,
            targets = ?rule.target_tags,
            "Added cascade invalidation rule"
        );
        self.cascade_rules.write().push(rule);
    }

    /// Register a key with tags
    pub fn register_key(&self, key: String, tags: Vec<String>) {
        self.tag_registry.register(key, tags);
    }

    /// Invalidate specific keys
    pub async fn invalidate_keys(&self, keys: Vec<String>) -> Result<()> {
        debug!(count = keys.len(), "Invalidating keys");

        for key in &keys {
            if let Err(e) = self.cache.delete(key).await {
                error!(key = %key, error = %e, "Failed to invalidate key");
            }
        }

        // Publish invalidation event
        let event = InvalidationEvent {
            event_type: InvalidationType::Keys,
            keys,
            tags: Vec::new(),
            timestamp: chrono::Utc::now(),
            source_instance: self.config.instance_id.clone(),
        };

        self.publish_event(event).await?;

        Ok(())
    }

    /// Invalidate all keys with a specific tag
    pub async fn invalidate_tag(&self, tag: String) -> Result<()> {
        let keys = self.tag_registry.get_keys_for_tag(&tag);

        debug!(tag = %tag, key_count = keys.len(), "Invalidating tag");

        for key in &keys {
            if let Err(e) = self.cache.delete(key).await {
                error!(key = %key, error = %e, "Failed to invalidate key");
            }
        }

        // Apply cascade rules if enabled
        if self.config.enable_cascade {
            self.apply_cascade_rules(&tag, 0).await?;
        }

        // Publish invalidation event
        let event = InvalidationEvent {
            event_type: InvalidationType::Tags,
            keys: Vec::new(),
            tags: vec![tag],
            timestamp: chrono::Utc::now(),
            source_instance: self.config.instance_id.clone(),
        };

        self.publish_event(event).await?;

        Ok(())
    }

    /// Invalidate keys matching a pattern
    pub async fn invalidate_pattern(&self, pattern: String) -> Result<()> {
        debug!(pattern = %pattern, "Invalidating pattern");

        let deleted = self.cache.delete_pattern(&pattern).await?;

        info!(pattern = %pattern, deleted = deleted, "Pattern invalidation completed");

        // Publish invalidation event
        let event = InvalidationEvent {
            event_type: InvalidationType::Pattern,
            keys: vec![pattern],
            tags: Vec::new(),
            timestamp: chrono::Utc::now(),
            source_instance: self.config.instance_id.clone(),
        };

        self.publish_event(event).await?;

        Ok(())
    }

    /// Apply cascade invalidation rules
    fn apply_cascade_rules<'a>(
        &'a self,
        tag: &'a str,
        depth: usize,
    ) -> std::pin::Pin<Box<dyn std::future::Future<Output = Result<()>> + Send + 'a>> {
        Box::pin(async move {
            if depth >= self.config.max_cascade_depth {
                warn!(tag = %tag, depth = depth, "Max cascade depth reached");
                return Ok(());
            }

            let matching_rules: Vec<_> = {
                let rules = self.cascade_rules.read();
                rules
                    .iter()
                    .filter(|rule| rule.source_tag == tag)
                    .cloned()
                    .collect()
            };

            for rule in matching_rules {
                debug!(
                    source = %rule.source_tag,
                    targets = ?rule.target_tags,
                    depth = depth,
                    "Applying cascade rule"
                );

                for target_tag in rule.target_tags {
                    let keys = self.tag_registry.get_keys_for_tag(&target_tag);

                    for key in keys {
                        if let Err(e) = self.cache.delete(&key).await {
                            error!(key = %key, error = %e, "Failed to cascade invalidate key");
                        }
                    }

                    // Recursively apply cascade rules
                    self.apply_cascade_rules(&target_tag, depth + 1).await?;
                }
            }

            Ok(())
        })
    }

    /// Publish invalidation event via Redis pub/sub
    async fn publish_event(&self, event: InvalidationEvent) -> Result<()> {
        let _json = serde_json::to_string(&event)
            .map_err(|e| DbError::Cache(format!("Serialization error: {}", e)))?;

        if let Err(e) = self.pubsub_tx.send(event) {
            error!(error = %e, "Failed to send invalidation event to channel");
        }

        debug!("Published invalidation event");

        Ok(())
    }

    /// Start pub/sub subscriber
    pub async fn start_subscriber(self: Arc<Self>, redis_url: String) -> Result<()> {
        let client = Client::open(redis_url.as_str())
            .map_err(|e| DbError::Connection(format!("Redis client error: {}", e)))?;

        let mut pubsub = client
            .get_async_pubsub()
            .await
            .map_err(|e| DbError::Connection(format!("Redis pubsub error: {}", e)))?;
        pubsub
            .subscribe(&self.config.pubsub_channel)
            .await
            .map_err(|e| DbError::Cache(format!("Subscribe error: {}", e)))?;

        info!(channel = %self.config.pubsub_channel, "Started invalidation subscriber");

        tokio::spawn(async move {
            loop {
                match pubsub.on_message().next().await {
                    Some(msg) => {
                        let payload: String = match msg.get_payload() {
                            Ok(p) => p,
                            Err(e) => {
                                error!(error = %e, "Failed to get message payload");
                                continue;
                            }
                        };

                        let event: InvalidationEvent = match serde_json::from_str(&payload) {
                            Ok(e) => e,
                            Err(e) => {
                                error!(error = %e, "Failed to deserialize event");
                                continue;
                            }
                        };

                        // Skip events from self
                        if event.source_instance == self.config.instance_id {
                            continue;
                        }

                        debug!(
                            event_type = ?event.event_type,
                            source = %event.source_instance,
                            "Received invalidation event"
                        );

                        // Process the event
                        match event.event_type {
                            InvalidationType::Keys => {
                                for key in &event.keys {
                                    if let Err(e) = self.cache.delete(key).await {
                                        error!(key = %key, error = %e, "Failed to invalidate key");
                                    }
                                }
                            }
                            InvalidationType::Tags => {
                                for tag in &event.tags {
                                    let keys = self.tag_registry.get_keys_for_tag(tag);
                                    for key in keys {
                                        if let Err(e) = self.cache.delete(&key).await {
                                            error!(key = %key, error = %e, "Failed to invalidate key");
                                        }
                                    }
                                }
                            }
                            InvalidationType::Pattern => {
                                for pattern in &event.keys {
                                    if let Err(e) = self.cache.delete_pattern(pattern).await {
                                        error!(pattern = %pattern, error = %e, "Failed to invalidate pattern");
                                    }
                                }
                            }
                            InvalidationType::Cascade => {
                                // Cascade handled by publisher
                            }
                        }
                    }
                    None => {
                        warn!("Pub/sub connection closed, reconnecting...");
                        tokio::time::sleep(tokio::time::Duration::from_secs(1)).await;
                    }
                }
            }
        });

        Ok(())
    }
}

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

    #[test]
    fn test_invalidation_config_default() {
        let config = InvalidationConfig::default();
        assert_eq!(config.pubsub_channel, "cache:invalidation");
        assert!(config.enable_cascade);
        assert_eq!(config.max_cascade_depth, 5);
    }

    #[test]
    fn test_tag_registry_register() {
        let registry = TagRegistry::new();

        registry.register(
            "key1".to_string(),
            vec!["tag1".to_string(), "tag2".to_string()],
        );

        let keys = registry.get_keys_for_tag("tag1");
        assert_eq!(keys.len(), 1);
        assert!(keys.contains(&"key1".to_string()));
    }

    #[test]
    fn test_tag_registry_get_tags() {
        let registry = TagRegistry::new();

        registry.register(
            "key1".to_string(),
            vec!["tag1".to_string(), "tag2".to_string()],
        );

        let tags = registry.get_tags_for_key("key1");
        assert_eq!(tags.len(), 2);
        assert!(tags.contains(&"tag1".to_string()));
        assert!(tags.contains(&"tag2".to_string()));
    }

    #[test]
    fn test_tag_registry_unregister() {
        let registry = TagRegistry::new();

        registry.register("key1".to_string(), vec!["tag1".to_string()]);
        registry.unregister("key1");

        let keys = registry.get_keys_for_tag("tag1");
        assert_eq!(keys.len(), 0);
    }

    #[test]
    fn test_cascade_rule_creation() {
        let rule = CascadeRule {
            source_tag: "user".to_string(),
            target_tags: vec!["user_profile".to_string(), "user_orders".to_string()],
        };

        assert_eq!(rule.source_tag, "user");
        assert_eq!(rule.target_tags.len(), 2);
    }

    #[test]
    fn test_invalidation_event_serialization() {
        let event = InvalidationEvent {
            event_type: InvalidationType::Keys,
            keys: vec!["key1".to_string()],
            tags: vec![],
            timestamp: chrono::Utc::now(),
            source_instance: "instance1".to_string(),
        };

        let json = serde_json::to_string(&event).unwrap();
        let deserialized: InvalidationEvent = serde_json::from_str(&json).unwrap();

        assert_eq!(deserialized.event_type, InvalidationType::Keys);
        assert_eq!(deserialized.keys.len(), 1);
    }
}