rustberg 0.0.5

A production-grade, cross-platform, single-binary Apache Iceberg REST Catalog
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
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
//! Persistent idempotency store for safe retries across restarts.
//!
//! This module provides a persistent backing store for idempotency keys,
//! preventing replay attacks after server restart.
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────┐
//! │                    IdempotencyCache                         │
//! │  ┌─────────────────────┐   ┌─────────────────────────────┐  │
//! │  │   L1: DashMap       │   │   L2: IdempotencyStore      │  │
//! │  │   (fast lookups)    │───│   (persistent backing)      │  │
//! │  └─────────────────────┘   └─────────────────────────────┘  │
//! └─────────────────────────────────────────────────────────────┘
//! ```
//!
//! - **L1 Cache**: In-memory DashMap for sub-millisecond lookups
//! - **L2 Store**: SlateDB persistence for durability across restarts
//! - **Write-through**: Both layers updated on set
//! - **Read-through**: L1 miss falls back to L2

use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::time::{Duration, SystemTime, UNIX_EPOCH};

use parking_lot::RwLock;

use crate::error::Result;

// ============================================================================
// IdempotencyStore Trait
// ============================================================================

/// Persistent entry for an idempotency key.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct IdempotencyEntry {
    /// The idempotency key value.
    pub key: String,
    /// The scope (method:path).
    pub scope: String,
    /// HTTP status code of the cached response.
    pub status_code: u16,
    /// Response body (typically small JSON).
    pub response_body: Vec<u8>,
    /// Content type of the response.
    pub content_type: Option<String>,
    /// Unix timestamp (millis) when this entry was created.
    pub created_at_ms: u64,
    /// TTL in milliseconds.
    pub ttl_ms: u64,
}

impl IdempotencyEntry {
    /// Creates a new idempotency entry.
    pub fn new(
        key: String,
        scope: String,
        status_code: u16,
        response_body: Vec<u8>,
        content_type: Option<String>,
        ttl: Duration,
    ) -> Self {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis() as u64;

        Self {
            key,
            scope,
            status_code,
            response_body,
            content_type,
            created_at_ms: now,
            ttl_ms: ttl.as_millis() as u64,
        }
    }

    /// Checks if this entry has expired.
    pub fn is_expired(&self) -> bool {
        let now = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap_or_default()
            .as_millis() as u64;

        now > self.created_at_ms + self.ttl_ms
    }

    /// Returns the storage key for this entry.
    pub fn storage_key(&self) -> String {
        format!("idempotency:{}:{}", self.scope, self.key)
    }
}

/// Trait for idempotency key storage backends.
#[async_trait]
pub trait IdempotencyStore: Send + Sync + std::fmt::Debug {
    /// Gets an idempotency entry by key and scope.
    /// Returns None if not found or expired.
    async fn get(&self, scope: &str, key: &str) -> Result<Option<IdempotencyEntry>>;

    /// Stores an idempotency entry.
    async fn set(&self, entry: IdempotencyEntry) -> Result<()>;

    /// Removes an idempotency entry.
    async fn remove(&self, scope: &str, key: &str) -> Result<()>;

    /// Cleans up all expired entries.
    /// Returns the number of entries removed.
    async fn cleanup_expired(&self) -> Result<usize>;

    /// Returns the count of stored entries.
    async fn count(&self) -> Result<usize>;
}

// ============================================================================
// In-Memory Implementation
// ============================================================================

/// Type alias for the in-memory map.
type IdempotencyMap = HashMap<String, IdempotencyEntry>;

/// In-memory idempotency store for development/testing.
///
/// **Warning:** Entries are lost on server restart.
#[derive(Debug, Default)]
pub struct MemoryIdempotencyStore {
    entries: RwLock<IdempotencyMap>,
}

impl MemoryIdempotencyStore {
    /// Creates a new in-memory idempotency store.
    pub fn new() -> Self {
        Self {
            entries: RwLock::new(HashMap::new()),
        }
    }

    fn make_key(scope: &str, key: &str) -> String {
        format!("{}:{}", scope, key)
    }
}

#[async_trait]
impl IdempotencyStore for MemoryIdempotencyStore {
    async fn get(&self, scope: &str, key: &str) -> Result<Option<IdempotencyEntry>> {
        let map_key = Self::make_key(scope, key);
        let entries = self.entries.read();

        match entries.get(&map_key) {
            Some(entry) if !entry.is_expired() => Ok(Some(entry.clone())),
            Some(_) => {
                // Entry is expired, remove it
                drop(entries);
                let mut entries = self.entries.write();
                entries.remove(&map_key);
                Ok(None)
            }
            None => Ok(None),
        }
    }

    async fn set(&self, entry: IdempotencyEntry) -> Result<()> {
        let map_key = format!("{}:{}", entry.scope, entry.key);
        let mut entries = self.entries.write();
        entries.insert(map_key, entry);
        Ok(())
    }

    async fn remove(&self, scope: &str, key: &str) -> Result<()> {
        let map_key = Self::make_key(scope, key);
        let mut entries = self.entries.write();
        entries.remove(&map_key);
        Ok(())
    }

    async fn cleanup_expired(&self) -> Result<usize> {
        let mut entries = self.entries.write();
        let before = entries.len();
        entries.retain(|_, entry| !entry.is_expired());
        Ok(before - entries.len())
    }

    async fn count(&self) -> Result<usize> {
        Ok(self.entries.read().len())
    }
}

// ============================================================================
// SlateDB Implementation
// ============================================================================

#[cfg(feature = "slatedb-storage")]
mod slatedb_impl {
    use super::*;
    use crate::error::AppError;
    use slatedb::Db;
    use std::sync::Arc;

    /// Persistent idempotency store backed by SlateDB.
    ///
    /// Entries are stored with key pattern: `idempotency:{scope}:{key}`
    pub struct SlateDbIdempotencyStore {
        /// SlateDB instance.
        db: Arc<Db>,
    }

    impl std::fmt::Debug for SlateDbIdempotencyStore {
        fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
            f.debug_struct("SlateDbIdempotencyStore")
                .finish_non_exhaustive()
        }
    }

    impl SlateDbIdempotencyStore {
        /// Creates a new SlateDB-backed idempotency store.
        pub fn new(db: Arc<Db>) -> Self {
            Self { db }
        }

        /// Generates a storage key for an idempotency entry.
        fn storage_key(scope: &str, key: &str) -> Vec<u8> {
            format!("idempotency:{}:{}", scope, key).into_bytes()
        }

        /// Prefix for scanning all idempotency entries.
        fn prefix() -> &'static [u8] {
            b"idempotency:"
        }
    }

    #[async_trait]
    impl IdempotencyStore for SlateDbIdempotencyStore {
        async fn get(&self, scope: &str, key: &str) -> Result<Option<IdempotencyEntry>> {
            let storage_key = Self::storage_key(scope, key);

            match self.db.get(&storage_key).await {
                Ok(Some(value)) => {
                    let entry: IdempotencyEntry = serde_json::from_slice(&value).map_err(|e| {
                        AppError::Internal(format!(
                            "Failed to deserialize idempotency entry: {}",
                            e
                        ))
                    })?;

                    if entry.is_expired() {
                        // Remove expired entry
                        self.db
                            .delete(&storage_key)
                            .await
                            .map_err(|e| AppError::Internal(format!("SlateDB error: {}", e)))?;
                        Ok(None)
                    } else {
                        Ok(Some(entry))
                    }
                }
                Ok(None) => Ok(None),
                Err(e) => Err(AppError::Internal(format!("SlateDB error: {}", e))),
            }
        }

        async fn set(&self, entry: IdempotencyEntry) -> Result<()> {
            let storage_key = Self::storage_key(&entry.scope, &entry.key);
            let value = serde_json::to_vec(&entry).map_err(|e| {
                AppError::Internal(format!("Failed to serialize idempotency entry: {}", e))
            })?;

            self.db
                .put(&storage_key, &value)
                .await
                .map_err(|e| AppError::Internal(format!("SlateDB error: {}", e)))?;

            Ok(())
        }

        async fn remove(&self, scope: &str, key: &str) -> Result<()> {
            let storage_key = Self::storage_key(scope, key);
            self.db
                .delete(&storage_key)
                .await
                .map_err(|e| AppError::Internal(format!("SlateDB error: {}", e)))?;
            Ok(())
        }

        async fn cleanup_expired(&self) -> Result<usize> {
            let prefix = Self::prefix();
            let mut removed = 0;

            // Scan all idempotency entries
            let mut iter = self
                .db
                .scan_prefix(prefix)
                .await
                .map_err(|e| AppError::Internal(format!("SlateDB error: {}", e)))?;

            let mut keys_to_delete = Vec::new();

            while let Some(kv) = iter
                .next()
                .await
                .map_err(|e| AppError::Internal(format!("SlateDB error: {}", e)))?
            {
                if let Ok(entry) = serde_json::from_slice::<IdempotencyEntry>(&kv.value) {
                    if entry.is_expired() {
                        keys_to_delete.push(kv.key.to_vec());
                    }
                }
            }

            // Delete expired entries
            for key in keys_to_delete {
                if self.db.delete(&key).await.is_ok() {
                    removed += 1;
                }
            }

            tracing::debug!(removed = removed, "Cleaned up expired idempotency entries");
            Ok(removed)
        }

        async fn count(&self) -> Result<usize> {
            let prefix = Self::prefix();
            let mut count = 0;

            let mut iter = self
                .db
                .scan_prefix(prefix)
                .await
                .map_err(|e| AppError::Internal(format!("SlateDB error: {}", e)))?;

            while iter
                .next()
                .await
                .map_err(|e| AppError::Internal(format!("SlateDB error: {}", e)))?
                .is_some()
            {
                count += 1;
            }

            Ok(count)
        }
    }
}

#[cfg(feature = "slatedb-storage")]
pub use slatedb_impl::SlateDbIdempotencyStore;

// ============================================================================
// Tests
// ============================================================================

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

    fn create_test_entry() -> IdempotencyEntry {
        IdempotencyEntry::new(
            "test-key-123".to_string(),
            "POST:/tables".to_string(),
            201,
            b"{\"name\": \"my_table\"}".to_vec(),
            Some("application/json".to_string()),
            Duration::from_secs(3600),
        )
    }

    #[tokio::test]
    async fn test_memory_store_set_and_get() {
        let store = MemoryIdempotencyStore::new();
        let entry = create_test_entry();

        store.set(entry.clone()).await.unwrap();

        let retrieved = store
            .get(&entry.scope, &entry.key)
            .await
            .unwrap()
            .expect("entry should exist");

        assert_eq!(retrieved.key, entry.key);
        assert_eq!(retrieved.status_code, 201);
    }

    #[tokio::test]
    async fn test_memory_store_expiry() {
        let store = MemoryIdempotencyStore::new();

        // Create entry with very short TTL
        let entry = IdempotencyEntry::new(
            "expiring-key".to_string(),
            "POST:/test".to_string(),
            200,
            vec![],
            None,
            Duration::from_millis(1), // 1ms TTL
        );

        store.set(entry.clone()).await.unwrap();

        // Wait for expiry
        tokio::time::sleep(Duration::from_millis(10)).await;

        let result = store.get(&entry.scope, &entry.key).await.unwrap();
        assert!(result.is_none(), "expired entry should not be returned");
    }

    #[tokio::test]
    async fn test_memory_store_remove() {
        let store = MemoryIdempotencyStore::new();
        let entry = create_test_entry();

        store.set(entry.clone()).await.unwrap();
        assert!(store.get(&entry.scope, &entry.key).await.unwrap().is_some());

        store.remove(&entry.scope, &entry.key).await.unwrap();
        assert!(store.get(&entry.scope, &entry.key).await.unwrap().is_none());
    }

    #[tokio::test]
    async fn test_memory_store_cleanup() {
        let store = MemoryIdempotencyStore::new();

        // Add expired and non-expired entries
        let expired = IdempotencyEntry::new(
            "expired".to_string(),
            "POST:/a".to_string(),
            200,
            vec![],
            None,
            Duration::from_millis(1),
        );

        let valid = IdempotencyEntry::new(
            "valid".to_string(),
            "POST:/b".to_string(),
            200,
            vec![],
            None,
            Duration::from_secs(3600),
        );

        store.set(expired).await.unwrap();
        store.set(valid.clone()).await.unwrap();

        // Wait for first to expire
        tokio::time::sleep(Duration::from_millis(10)).await;

        let removed = store.cleanup_expired().await.unwrap();
        assert_eq!(removed, 1);
        assert_eq!(store.count().await.unwrap(), 1);
    }

    #[tokio::test]
    async fn test_entry_is_expired() {
        let expired = IdempotencyEntry {
            key: "test".to_string(),
            scope: "POST:/test".to_string(),
            status_code: 200,
            response_body: vec![],
            content_type: None,
            created_at_ms: 0, // Epoch = definitely expired
            ttl_ms: 1000,
        };

        assert!(expired.is_expired());

        let valid = IdempotencyEntry::new(
            "test".to_string(),
            "POST:/test".to_string(),
            200,
            vec![],
            None,
            Duration::from_secs(3600),
        );

        assert!(!valid.is_expired());
    }
}

#[cfg(test)]
#[cfg(feature = "slatedb-storage")]
mod slatedb_tests {
    use super::*;
    use object_store::local::LocalFileSystem;
    use object_store::ObjectStore;
    use slatedb::Db;
    use std::sync::Arc;
    use tempfile::TempDir;

    async fn create_test_store() -> (SlateDbIdempotencyStore, TempDir) {
        let temp_dir = TempDir::new().expect("failed to create temp dir");
        let object_store: Arc<dyn ObjectStore> =
            Arc::new(LocalFileSystem::new_with_prefix(temp_dir.path()).unwrap());

        let db = Arc::new(
            Db::builder("idempotency", object_store)
                .build()
                .await
                .expect("failed to create SlateDB"),
        );

        (SlateDbIdempotencyStore::new(db), temp_dir)
    }

    fn create_test_entry() -> IdempotencyEntry {
        IdempotencyEntry::new(
            "slatedb-key-456".to_string(),
            "POST:/namespaces".to_string(),
            201,
            b"{\"namespace\": [\"db\"]}".to_vec(),
            Some("application/json".to_string()),
            Duration::from_secs(3600),
        )
    }

    #[tokio::test]
    async fn test_slatedb_store_set_and_get() {
        let (store, _temp) = create_test_store().await;
        let entry = create_test_entry();

        store.set(entry.clone()).await.unwrap();

        let retrieved = store
            .get(&entry.scope, &entry.key)
            .await
            .unwrap()
            .expect("entry should exist");

        assert_eq!(retrieved.key, entry.key);
        assert_eq!(retrieved.status_code, 201);
    }

    #[tokio::test]
    async fn test_slatedb_store_persistence() {
        let temp_dir = TempDir::new().expect("failed to create temp dir");
        let object_store: Arc<dyn ObjectStore> =
            Arc::new(LocalFileSystem::new_with_prefix(temp_dir.path()).unwrap());

        let entry = create_test_entry();

        // Create entry in first instance
        {
            let db = Arc::new(
                Db::builder("idempotency", object_store.clone())
                    .build()
                    .await
                    .expect("failed to create SlateDB"),
            );
            let store = SlateDbIdempotencyStore::new(db.clone());

            store.set(entry.clone()).await.unwrap();

            // Flush to disk
            db.flush().await.expect("flush should succeed");
        }

        // Verify in new instance (simulates restart)
        {
            let db = Arc::new(
                Db::builder("idempotency", object_store.clone())
                    .build()
                    .await
                    .expect("failed to reopen SlateDB"),
            );
            let store = SlateDbIdempotencyStore::new(db);

            let retrieved = store
                .get(&entry.scope, &entry.key)
                .await
                .unwrap()
                .expect("entry should survive restart");

            assert_eq!(retrieved.key, entry.key);
            assert_eq!(retrieved.status_code, 201);
        }
    }

    #[tokio::test]
    async fn test_slatedb_store_cleanup_expired() {
        let (store, _temp) = create_test_store().await;

        // Add expired entry
        let expired = IdempotencyEntry {
            key: "old-key".to_string(),
            scope: "POST:/old".to_string(),
            status_code: 200,
            response_body: vec![],
            content_type: None,
            created_at_ms: 0, // Epoch = definitely expired
            ttl_ms: 1,
        };

        let valid = IdempotencyEntry::new(
            "new-key".to_string(),
            "POST:/new".to_string(),
            200,
            vec![],
            None,
            Duration::from_secs(3600),
        );

        store.set(expired).await.unwrap();
        store.set(valid.clone()).await.unwrap();

        assert_eq!(store.count().await.unwrap(), 2);

        let removed = store.cleanup_expired().await.unwrap();
        assert_eq!(removed, 1);
        assert_eq!(store.count().await.unwrap(), 1);

        // Valid entry still exists
        assert!(store.get(&valid.scope, &valid.key).await.unwrap().is_some());
    }

    #[tokio::test]
    async fn test_slatedb_store_remove() {
        let (store, _temp) = create_test_store().await;
        let entry = create_test_entry();

        store.set(entry.clone()).await.unwrap();
        store.remove(&entry.scope, &entry.key).await.unwrap();

        assert!(store.get(&entry.scope, &entry.key).await.unwrap().is_none());
    }
}