siftdb_core/
index.rs

1use crate::types::{FileHandle, HandleMetadata};
2use anyhow::Result;
3use serde::{Serialize, Deserialize};
4use std::collections::HashMap;
5use std::path::Path;
6use serde_json;
7use crate::fst_index::PathFSTIndex;
8
9/// Path index supporting both JSON (0.1) and FST (0.2) formats
10#[derive(Serialize, Deserialize)]
11pub struct PathIndex {
12    pub paths: HashMap<String, FileHandle>,
13    pub next_handle: FileHandle,
14    #[serde(skip)]
15    pub fst_index: Option<PathFSTIndex>, // 0.2 feature: FST-based fast path queries
16}
17
18impl PathIndex {
19    pub fn new() -> Self {
20        Self {
21            paths: HashMap::new(),
22            next_handle: 1,
23            fst_index: None,
24        }
25    }
26    
27    pub fn add_path(&mut self, path: String) -> FileHandle {
28        if let Some(&handle) = self.paths.get(&path) {
29            return handle;
30        }
31        
32        let handle = self.next_handle;
33        self.paths.insert(path, handle);
34        self.next_handle += 1;
35        handle
36    }
37    
38    pub fn get_handle(&self, path: &str) -> Option<FileHandle> {
39        self.paths.get(path).copied()
40    }
41    
42    pub fn get_path(&self, handle: FileHandle) -> Option<String> {
43        self.paths.iter()
44            .find(|(_, &h)| h == handle)
45            .map(|(path, _)| path.clone())
46    }
47    
48    pub fn write_to_file(&self, path: &Path) -> Result<()> {
49        let json = serde_json::to_string_pretty(self)?;
50        std::fs::write(path, json)?;
51        Ok(())
52    }
53    
54    pub fn read_from_file(path: &Path) -> Result<Self> {
55        if !path.exists() {
56            return Ok(Self::new());
57        }
58        let json = std::fs::read_to_string(path)?;
59        let index = serde_json::from_str(&json)?;
60        Ok(index)
61    }
62}
63
64/// Handle metadata map
65#[derive(Debug, Clone, Serialize, Deserialize)]
66pub struct HandlesMap {
67    pub handles: HashMap<FileHandle, HandleMetadata>,
68}
69
70impl HandlesMap {
71    pub fn new() -> Self {
72        Self {
73            handles: HashMap::new(),
74        }
75    }
76    
77    pub fn add_handle(&mut self, handle: FileHandle, metadata: HandleMetadata) {
78        self.handles.insert(handle, metadata);
79    }
80    
81    pub fn get_metadata(&self, handle: FileHandle) -> Option<&HandleMetadata> {
82        self.handles.get(&handle)
83    }
84    
85    pub fn write_to_file(&self, path: &Path) -> Result<()> {
86        let json = serde_json::to_string_pretty(self)?;
87        std::fs::write(path, json)?;
88        Ok(())
89    }
90    
91    pub fn read_from_file(path: &Path) -> Result<Self> {
92        if !path.exists() {
93            return Ok(Self::new());
94        }
95        let json = std::fs::read_to_string(path)?;
96        let index = serde_json::from_str(&json)?;
97        Ok(index)
98    }
99}
100
101impl Default for PathIndex {
102    fn default() -> Self {
103        Self::new()
104    }
105}
106
107impl Default for HandlesMap {
108    fn default() -> Self {
109        Self::new()
110    }
111}