agent-diva-files 0.5.0

File management system for agent-diva with deduplication and reference counting
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
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
//! SQLite-based file index
//!
//! This module provides a persistent file index using SQLite.
//! It replaces the in-memory HashMap with a proper database for:
//! - Better query performance with large datasets
//! - ACID transactions
//! - SQL-based filtering and aggregation
//! - Automatic persistence

use crate::handle::{FileIndexEntry, FileMetadata};
use crate::Result;
use chrono::{DateTime, Utc};
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
use sqlx::{Row, SqlitePool};
use std::path::{Path, PathBuf};

/// SQLite-based file index
pub struct SqliteIndex {
    pool: SqlitePool,
    db_path: PathBuf,
}

impl SqliteIndex {
    /// Create a new SQLite index at the given path
    pub async fn new(db_path: PathBuf) -> Result<Self> {
        // Ensure parent directory exists
        if let Some(parent) = db_path.parent() {
            tokio::fs::create_dir_all(parent).await?;
        }

        // Use SqliteConnectOptions for proper path handling on Windows
        let options = SqliteConnectOptions::new()
            .filename(&db_path)
            .create_if_missing(true);

        let pool = SqlitePoolOptions::new()
            .max_connections(5)
            .connect_with(options)
            .await?;

        let index = Self { pool, db_path };
        index.init().await?;

        Ok(index)
    }

    /// Initialize the database schema
    async fn init(&self) -> Result<()> {
        // Create main files table with soft delete support
        sqlx::query(
            r#"
            CREATE TABLE IF NOT EXISTS files (
                id TEXT PRIMARY KEY NOT NULL,
                path TEXT NOT NULL,
                size INTEGER NOT NULL DEFAULT 0,
                ref_count INTEGER NOT NULL DEFAULT 1,
                created_at TEXT NOT NULL,
                last_accessed_at TEXT,
                deleted_at TEXT,          -- NEW: soft delete timestamp
                deleted_by TEXT,          -- NEW: who deleted the file
                metadata_json TEXT NOT NULL
            );

            CREATE INDEX IF NOT EXISTS idx_files_ref_count ON files(ref_count);
            CREATE INDEX IF NOT EXISTS idx_files_last_accessed ON files(last_accessed_at);
            CREATE INDEX IF NOT EXISTS idx_files_deleted_at ON files(deleted_at) WHERE deleted_at IS NOT NULL;
            "#,
        )
        .execute(&self.pool)
        .await?;

        // Migration: Add deleted_at column if not exists (for existing databases)
        let columns: Vec<String> =
            sqlx::query_scalar("SELECT name FROM pragma_table_info('files')")
                .fetch_all(&self.pool)
                .await?;

        if !columns.contains(&"deleted_at".to_string()) {
            let _ = sqlx::query("ALTER TABLE files ADD COLUMN deleted_at TEXT")
                .execute(&self.pool)
                .await;
        }
        if !columns.contains(&"deleted_by".to_string()) {
            let _ = sqlx::query("ALTER TABLE files ADD COLUMN deleted_by TEXT")
                .execute(&self.pool)
                .await;
        }

        tracing::info!("SQLite index initialized at {:?}", self.db_path);
        Ok(())
    }

    /// Load index from existing database (alias for new)
    pub async fn load(db_path: PathBuf) -> Result<Self> {
        Self::new(db_path).await
    }

    /// Get an entry by ID
    ///
    /// # Arguments
    /// * `id` - The file ID
    /// * `include_deleted` - If true, include soft-deleted files
    pub async fn get(&self, id: &str, include_deleted: bool) -> Result<Option<FileIndexEntry>> {
        let row = if include_deleted {
            sqlx::query(
                r#"
                SELECT id, path, size, ref_count, created_at, last_accessed_at, metadata_json
                FROM files WHERE id = ?
                "#,
            )
            .bind(id)
            .fetch_optional(&self.pool)
            .await?
        } else {
            sqlx::query(
                r#"
                SELECT id, path, size, ref_count, created_at, last_accessed_at, metadata_json
                FROM files WHERE id = ? AND deleted_at IS NULL
                "#,
            )
            .bind(id)
            .fetch_optional(&self.pool)
            .await?
        };

        match row {
            Some(row) => Ok(Some(self.row_to_entry(&row)?)),
            None => Ok(None),
        }
    }

    /// Soft delete a file by ID
    ///
    /// Marks the file as deleted but does not remove it physically.
    /// The file can be restored until the retention period expires.
    pub async fn soft_delete(&self, id: &str, deleted_by: Option<&str>) -> Result<bool> {
        let result = sqlx::query(
            r#"
            UPDATE files
            SET deleted_at = ?, deleted_by = ?
            WHERE id = ? AND deleted_at IS NULL
            "#,
        )
        .bind(Utc::now().to_rfc3339())
        .bind(deleted_by)
        .bind(id)
        .execute(&self.pool)
        .await?;

        Ok(result.rows_affected() > 0)
    }

    /// Restore a soft-deleted file
    ///
    /// Clears the deleted_at timestamp, making the file accessible again.
    pub async fn restore(&self, id: &str) -> Result<bool> {
        let result = sqlx::query(
            r#"
            UPDATE files
            SET deleted_at = NULL, deleted_by = NULL
            WHERE id = ? AND deleted_at IS NOT NULL
            "#,
        )
        .bind(id)
        .execute(&self.pool)
        .await?;

        Ok(result.rows_affected() > 0)
    }

    /// List all soft-deleted files
    pub async fn list_deleted(&self) -> Result<Vec<FileIndexEntry>> {
        let rows = sqlx::query(
            r#"
            SELECT id, path, size, ref_count, created_at, last_accessed_at, metadata_json
            FROM files
            WHERE deleted_at IS NOT NULL
            ORDER BY deleted_at DESC
            "#,
        )
        .fetch_all(&self.pool)
        .await?;

        let mut entries = Vec::new();
        for row in rows {
            entries.push(self.row_to_entry(&row)?);
        }

        Ok(entries)
    }

    /// Get soft-deleted files that are ready for permanent deletion
    ///
    /// # Arguments
    /// * `retention_days` - Files deleted more than this many days ago
    pub async fn get_expired_deletions(&self, retention_days: u32) -> Result<Vec<FileIndexEntry>> {
        let cutoff = Utc::now() - chrono::Duration::days(retention_days as i64);
        let cutoff_str = cutoff.to_rfc3339();

        let rows = sqlx::query(
            r#"
            SELECT id, path, size, ref_count, created_at, last_accessed_at, metadata_json
            FROM files
            WHERE deleted_at IS NOT NULL AND deleted_at < ?
            "#,
        )
        .bind(cutoff_str)
        .fetch_all(&self.pool)
        .await?;

        let mut entries = Vec::new();
        for row in rows {
            entries.push(self.row_to_entry(&row)?);
        }

        Ok(entries)
    }

    /// Hard delete a file (physical removal from database)
    ///
    /// Use with caution - this permanently removes the index entry.
    /// The actual file data should be deleted separately.
    pub async fn hard_delete(&self, id: &str) -> Result<Option<FileIndexEntry>> {
        // First get the entry to return it
        let entry = self.get(id, true).await?;

        if entry.is_some() {
            sqlx::query("DELETE FROM files WHERE id = ?")
                .bind(id)
                .execute(&self.pool)
                .await?;
        }

        Ok(entry)
    }

    /// Insert or update an entry
    pub async fn insert(&self, entry: FileIndexEntry) -> Result<()> {
        let metadata_json = serde_json::to_string(&entry.metadata)?;

        sqlx::query(
            r#"
            INSERT INTO files (id, path, size, ref_count, created_at, last_accessed_at, metadata_json)
            VALUES (?, ?, ?, ?, ?, ?, ?)
            ON CONFLICT(id) DO UPDATE SET
                path = excluded.path,
                size = excluded.size,
                ref_count = excluded.ref_count,
                last_accessed_at = excluded.last_accessed_at,
                metadata_json = excluded.metadata_json,
                deleted_at = NULL,
                deleted_by = NULL
            "#,
        )
        .bind(&entry.id)
        .bind(entry.path.to_string_lossy().to_string())
        .bind(entry.size as i64)
        .bind(entry.ref_count as i64)
        .bind(entry.created_at.to_rfc3339())
        .bind(entry.last_accessed_at.map(|t| t.to_rfc3339()))
        .bind(metadata_json)
        .execute(&self.pool)
        .await?;

        Ok(())
    }

    /// Remove an entry by ID
    ///
    /// This physically removes the entry from the database.
    /// Note: For soft delete, use `soft_delete()` instead.
    pub async fn remove(&self, id: &str) -> Result<Option<FileIndexEntry>> {
        // First get the entry to return it (include deleted since we're removing it)
        let entry = self.get(id, true).await?;

        if entry.is_some() {
            sqlx::query("DELETE FROM files WHERE id = ?")
                .bind(id)
                .execute(&self.pool)
                .await?;
        }

        Ok(entry)
    }

    /// Update reference count for an entry
    ///
    /// # Arguments
    /// * `id` - The file ID
    /// * `delta` - Change in reference count (+1 to add, -1 to release)
    ///
    /// # Returns
    /// The new reference count, or None if the entry doesn't exist
    pub async fn update_ref_count(&self, id: &str, delta: i32) -> Result<Option<usize>> {
        // Get the current entry (only non-deleted files should have their ref count updated)
        let current = self.get(id, false).await?;

        if let Some(entry) = current {
            let new_count = if delta < 0 {
                entry
                    .ref_count
                    .saturating_sub(delta.unsigned_abs() as usize)
            } else {
                entry.ref_count.saturating_add(delta as usize)
            };

            sqlx::query(
                r#"
                UPDATE files
                SET ref_count = ?, last_accessed_at = ?
                WHERE id = ?
                "#,
            )
            .bind(new_count as i64)
            .bind(Utc::now().to_rfc3339())
            .bind(id)
            .execute(&self.pool)
            .await?;

            Ok(Some(new_count))
        } else {
            Ok(None)
        }
    }

    /// Get entries suitable for cleanup
    pub async fn get_candidates_for_cleanup(
        &self,
        threshold: i32,
        max_age_days: u32,
    ) -> Result<Vec<FileIndexEntry>> {
        let cutoff = Utc::now() - chrono::Duration::days(max_age_days as i64);
        let cutoff_str = cutoff.to_rfc3339();

        let rows = sqlx::query(
            r#"
            SELECT id, path, size, ref_count, created_at, last_accessed_at, metadata_json
            FROM files
            WHERE ref_count <= ?
              AND (last_accessed_at IS NULL OR last_accessed_at < ?)
            "#,
        )
        .bind(threshold as i64)
        .bind(cutoff_str)
        .fetch_all(&self.pool)
        .await?;

        let mut entries = Vec::new();
        for row in rows {
            entries.push(self.row_to_entry(&row)?);
        }

        Ok(entries)
    }

    /// Get total entry count
    pub async fn len(&self) -> Result<usize> {
        let count: i64 = sqlx::query_scalar("SELECT COUNT(*) FROM files")
            .fetch_one(&self.pool)
            .await?;
        Ok(count as usize)
    }

    /// Check if index is empty
    pub async fn is_empty(&self) -> Result<bool> {
        Ok(self.len().await? == 0)
    }

    /// Get all entries (use sparingly with large datasets)
    pub async fn entries(&self) -> Result<Vec<FileIndexEntry>> {
        let rows = sqlx::query(
            r#"
            SELECT id, path, size, ref_count, created_at, last_accessed_at, metadata_json
            FROM files
            "#,
        )
        .fetch_all(&self.pool)
        .await?;

        let mut entries = Vec::new();
        for row in rows {
            entries.push(self.row_to_entry(&row)?);
        }

        Ok(entries)
    }

    /// Get statistics about the index
    pub async fn stats(&self) -> Result<IndexStats> {
        let row = sqlx::query(
            r#"
            SELECT
                COUNT(*) as total_files,
                COALESCE(SUM(size), 0) as total_size,
                COALESCE(SUM(ref_count), 0) as total_refs
            FROM files
            "#,
        )
        .fetch_one(&self.pool)
        .await?;

        Ok(IndexStats {
            total_files: row.get::<i64, _>("total_files") as usize,
            total_size: row.get::<i64, _>("total_size") as u64,
            total_refs: row.get::<i64, _>("total_refs") as usize,
        })
    }

    /// Close the database connection
    pub async fn close(&self) {
        self.pool.close().await;
    }

    /// Convert a database row to FileIndexEntry
    fn row_to_entry(&self, row: &sqlx::sqlite::SqliteRow) -> Result<FileIndexEntry> {
        let metadata_json: String = row.get("metadata_json");
        let metadata: FileMetadata = serde_json::from_str(&metadata_json)?;

        Ok(FileIndexEntry {
            id: row.get("id"),
            path: PathBuf::from(row.get::<String, _>("path")),
            size: row.get::<i64, _>("size") as u64,
            ref_count: row.get::<i64, _>("ref_count") as usize,
            created_at: DateTime::parse_from_rfc3339(&row.get::<String, _>("created_at"))?
                .with_timezone(&Utc),
            last_accessed_at: row
                .get::<Option<String>, _>("last_accessed_at")
                .map(|s| DateTime::parse_from_rfc3339(&s).map(|dt| dt.with_timezone(&Utc)))
                .transpose()?,
            metadata,
        })
    }

    /// Migrate from old JSONL format
    pub async fn migrate_from_jsonl(&self, jsonl_path: &Path) -> Result<usize> {
        if !jsonl_path.exists() {
            return Ok(0);
        }

        let content = tokio::fs::read_to_string(jsonl_path).await?;
        let mut count = 0;

        for line in content.lines() {
            if line.trim().is_empty() {
                continue;
            }
            if let Ok(entry) = serde_json::from_str::<FileIndexEntry>(line) {
                self.insert(entry).await?;
                count += 1;
            } else {
                tracing::warn!("Failed to parse index entry during migration: {}", line);
            }
        }

        tracing::info!("Migrated {} entries from JSONL to SQLite", count);
        Ok(count)
    }
}

/// Statistics for the index
#[derive(Debug, Clone, Default)]
pub struct IndexStats {
    pub total_files: usize,
    pub total_size: u64,
    pub total_refs: usize,
}

/// Backward-compatible FileIndex wrapper
///
/// This type alias allows existing code to continue working while
/// migrating from the old in-memory HashMap implementation.
pub type FileIndex = SqliteIndex;

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

    fn create_test_metadata() -> FileMetadata {
        FileMetadata {
            name: "test.txt".to_string(),
            size: 100,
            mime_type: Some("text/plain".to_string()),
            source: Some("test".to_string()),
            created_at: Utc::now(),
            last_accessed_at: None,
            preview: None,
        }
    }

    #[tokio::test]
    async fn test_sqlite_index_basic() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("index.db");

        let index = SqliteIndex::new(db_path).await.unwrap();

        // Insert
        let entry = FileIndexEntry {
            id: "abc123".to_string(),
            path: PathBuf::from("ab/c123"),
            size: 100,
            ref_count: 1,
            created_at: Utc::now(),
            last_accessed_at: None,
            metadata: create_test_metadata(),
        };

        index.insert(entry).await.unwrap();
        assert_eq!(index.len().await.unwrap(), 1);

        // Get
        let retrieved = index.get("abc123", false).await.unwrap();
        assert!(retrieved.is_some());
        let retrieved = retrieved.unwrap();
        assert_eq!(retrieved.id, "abc123");
        assert_eq!(retrieved.ref_count, 1);

        // Update ref count
        let new_count = index.update_ref_count("abc123", 1).await.unwrap();
        assert_eq!(new_count, Some(2));

        let updated = index.get("abc123", false).await.unwrap().unwrap();
        assert_eq!(updated.ref_count, 2);

        // Remove
        let removed = index.remove("abc123").await.unwrap();
        assert!(removed.is_some());
        assert_eq!(index.len().await.unwrap(), 0);
    }

    #[tokio::test]
    async fn test_sqlite_index_stats() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("index.db");

        let index = SqliteIndex::new(db_path).await.unwrap();

        // Insert multiple entries
        for i in 0..5 {
            let entry = FileIndexEntry {
                id: format!("file{}", i),
                path: PathBuf::from(format!("f{}/{}", i, i)),
                size: 100 * (i + 1) as u64,
                ref_count: i + 1,
                created_at: Utc::now(),
                last_accessed_at: None,
                metadata: create_test_metadata(),
            };
            index.insert(entry).await.unwrap();
        }

        let stats = index.stats().await.unwrap();
        assert_eq!(stats.total_files, 5);
        assert_eq!(stats.total_size, 100 + 200 + 300 + 400 + 500);
        assert_eq!(stats.total_refs, 1 + 2 + 3 + 4 + 5);
    }

    #[tokio::test]
    async fn test_sqlite_index_cleanup_candidates() {
        let temp_dir = TempDir::new().unwrap();
        let db_path = temp_dir.path().join("index.db");

        let index = SqliteIndex::new(db_path).await.unwrap();

        // Insert entry with old access time and low ref count
        let old_entry = FileIndexEntry {
            id: "old_file".to_string(),
            path: PathBuf::from("old/path"),
            size: 100,
            ref_count: 0,
            created_at: Utc::now() - chrono::Duration::days(100),
            last_accessed_at: Some(Utc::now() - chrono::Duration::days(100)),
            metadata: create_test_metadata(),
        };
        index.insert(old_entry).await.unwrap();

        // Insert entry with recent access time
        let recent_entry = FileIndexEntry {
            id: "recent_file".to_string(),
            path: PathBuf::from("recent/path"),
            size: 100,
            ref_count: 0,
            created_at: Utc::now(),
            last_accessed_at: Some(Utc::now()),
            metadata: create_test_metadata(),
        };
        index.insert(recent_entry).await.unwrap();

        // Get cleanup candidates (older than 30 days, ref_count <= 1)
        let candidates = index.get_candidates_for_cleanup(1, 30).await.unwrap();
        assert_eq!(candidates.len(), 1);
        assert_eq!(candidates[0].id, "old_file");
    }

    #[tokio::test]
    async fn test_migration_from_jsonl() {
        let temp_dir = TempDir::new().unwrap();
        let jsonl_path = temp_dir.path().join("index.jsonl");
        let db_path = temp_dir.path().join("index.db");

        // Create JSONL file
        let entry1 = serde_json::json!({
            "id": "file1",
            "path": "ab/c1",
            "size": 100,
            "ref_count": 1,
            "created_at": Utc::now().to_rfc3339(),
            "last_accessed_at": null,
            "metadata": {
                "name": "test1.txt",
                "size": 100,
                "mime_type": null,
                "source": null,
                "created_at": Utc::now().to_rfc3339(),
                "last_accessed_at": null,
                "preview": null
            }
        });

        let entry2 = serde_json::json!({
            "id": "file2",
            "path": "ab/c2",
            "size": 200,
            "ref_count": 2,
            "created_at": Utc::now().to_rfc3339(),
            "last_accessed_at": null,
            "metadata": {
                "name": "test2.txt",
                "size": 200,
                "mime_type": null,
                "source": null,
                "created_at": Utc::now().to_rfc3339(),
                "last_accessed_at": null,
                "preview": null
            }
        });

        tokio::fs::write(&jsonl_path, format!("{}\n{}\n", entry1, entry2))
            .await
            .unwrap();

        // Migrate
        let index = SqliteIndex::new(db_path).await.unwrap();
        let migrated = index.migrate_from_jsonl(&jsonl_path).await.unwrap();
        assert_eq!(migrated, 2);

        // Verify
        assert_eq!(index.len().await.unwrap(), 2);
        let file1 = index.get("file1", false).await.unwrap().unwrap();
        assert_eq!(file1.size, 100);
        let file2 = index.get("file2", false).await.unwrap().unwrap();
        assert_eq!(file2.size, 200);
    }
}