1use crate::dsl::Source;
2use salsa::Database;
3use std::collections::HashMap;
4use std::path::PathBuf;
5use std::time::SystemTime;
6
7pub type DatabaseImpl = salsa::DatabaseImpl;
9
10#[salsa::input]
12pub struct SourceFile {
13 pub path: PathBuf,
14 pub content_hash: u64,
15 pub modified_time: SystemTime,
16}
17
18#[salsa::tracked]
20pub fn source_metadata(db: &dyn salsa::Database, source_file: SourceFile) -> String {
21 let path = source_file.path(db);
22
23 let temp_source = Source {
25 id: "temp".to_string(),
26 path: path.clone(),
27 format: if path.extension().and_then(|s| s.to_str()) == Some("csv") {
28 "csv".to_string()
29 } else {
30 "tsv".to_string()
31 },
32 };
33
34 match crate::engine::DataReader::get_schema(&temp_source) {
35 Ok(schema) => {
36 let schema_map: std::collections::HashMap<String, String> = schema
38 .iter()
39 .map(|(name, dtype)| (name.to_string(), format!("{:?}", dtype)))
40 .collect();
41 serde_json::to_string(&schema_map).unwrap_or_default()
42 },
43 Err(_) => "error".to_string(),
44 }
45}
46
47#[salsa::tracked]
49pub fn parsed_source_id(db: &dyn salsa::Database, source_file: SourceFile) -> String {
50 let _metadata = source_metadata(db, source_file);
52
53 format!("df_{}", source_file.content_hash(db))
55}
56
57#[salsa::tracked]
59pub fn execute_query_cached(
60 db: &dyn salsa::Database,
61 query_json: String,
62 source_files: Vec<SourceFile>,
63) -> String {
64 for source_file in &source_files {
66 let _parsed_id = parsed_source_id(db, *source_file);
67 let _metadata = source_metadata(db, *source_file);
68 }
69
70 use std::collections::hash_map::DefaultHasher;
72 use std::hash::{Hash, Hasher};
73
74 let mut hasher = DefaultHasher::new();
75 query_json.hash(&mut hasher);
76 for sf in &source_files {
77 sf.content_hash(db).hash(&mut hasher);
78 }
79
80 format!("result_{}", hasher.finish())
81}
82
83pub fn invalidate_source_in_db(db: &mut DatabaseImpl, _path: &PathBuf) {
85 db.synthetic_write(salsa::Durability::LOW);
88}
89
90pub fn get_cache_stats(_db: &DatabaseImpl) -> HashMap<String, usize> {
91 let mut stats = HashMap::new();
94 stats.insert("total_inputs".to_string(), 0);
95 stats.insert("computed_values".to_string(), 0);
96 stats
97}