chore: finalize redb migration for all memory stores
This commit is contained in:
1 parent
9a8b6e52e9
commit
721f564910
4 files changed
+107
-88
No files matched your search
+35
-5
@@ -1,6 +1,6 @@
|
||||
use crate::models::*;
|
||||
use crate::search::MemoryIndex;
|
||||
use crate::store::{Store, read_json_file, write_json_atomic};
|
||||
use crate::store::Store;
|
||||
use std::collections::{HashMap, HashSet};
|
||||
use std::fs;
|
||||
use std::path::PathBuf;
|
||||
@@ -76,7 +76,21 @@ impl MemoryState {
|
||||
let mut lock = self.master_cache.write().unwrap();
|
||||
let new_mtime = self.master_mtime();
|
||||
if lock.1 != new_mtime {
|
||||
lock.0 = read_json_file(&self.master_path);
|
||||
if let Ok(data) = fs::read(&self.master_path)
|
||||
&& let Ok(parsed) = serde_json::from_slice(&data)
|
||||
{
|
||||
lock.0 = parsed;
|
||||
} else {
|
||||
let bak_path = self.master_path.with_extension("json.bak");
|
||||
if let Ok(data) = fs::read(&bak_path)
|
||||
&& let Ok(parsed) = serde_json::from_slice(&data)
|
||||
{
|
||||
let _ = fs::write(&self.master_path, data);
|
||||
lock.0 = parsed;
|
||||
} else {
|
||||
lock.0 = KnowledgeGraph::default();
|
||||
}
|
||||
}
|
||||
lock.1 = new_mtime;
|
||||
}
|
||||
lock.0.clone()
|
||||
@@ -133,7 +147,18 @@ impl MemoryState {
|
||||
let _ = fs::remove_file(&wal_path);
|
||||
*self.session_graph.write().unwrap() = KnowledgeGraph::default();
|
||||
update_fn(&mut master);
|
||||
let _ = write_json_atomic(&self.master_path, &master);
|
||||
|
||||
let write_json = |path: &std::path::Path, data: &KnowledgeGraph| -> std::io::Result<()> {
|
||||
if path.exists() {
|
||||
let bak_path = path.with_extension("json.bak");
|
||||
let _ = fs::copy(path, &bak_path);
|
||||
}
|
||||
let tmp_path = path.with_extension("json.tmp");
|
||||
let json_data = serde_json::to_string_pretty(data)?;
|
||||
fs::write(&tmp_path, json_data)?;
|
||||
fs::rename(&tmp_path, path)
|
||||
};
|
||||
let _ = write_json(&self.master_path, &master);
|
||||
|
||||
let mut cache_lock = self.master_cache.write().unwrap();
|
||||
cache_lock.0 = master;
|
||||
@@ -143,8 +168,13 @@ impl MemoryState {
|
||||
}
|
||||
|
||||
pub fn rebuild_index(&self) {
|
||||
if let Ok(new_idx) = MemoryIndex::new() {
|
||||
let full = self.get_full_graph();
|
||||
if let Ok(new_idx) = MemoryIndex::new(&self.base_dir) {
|
||||
let session = self.session_graph.read().unwrap();
|
||||
let mut full = {
|
||||
let cache = self.master_cache.read().unwrap();
|
||||
cache.0.clone()
|
||||
};
|
||||
Self::merge_graphs(&mut full, &session);
|
||||
for e in full.entities.values() {
|
||||
let _ = new_idx.index_entity(e);
|
||||
}
|
||||
|
||||
Reference in new issue
Block a user