use std::collections::HashMap;
use std::sync::atomic::{AtomicU64, Ordering};
use std::sync::{Arc, Mutex, OnceLock};
use crate::Layer;
pub const VECTOR_MEMORY_PREFIX: &str = "memory://vector/";
static NEXT_ID: AtomicU64 = AtomicU64::new(1);
static VECTOR_STORE: OnceLock<Mutex<HashMap<String, Arc<Layer>>>> = OnceLock::new();
fn store() -> &'static Mutex<HashMap<String, Arc<Layer>>> {
VECTOR_STORE.get_or_init(|| Mutex::new(HashMap::new()))
}
pub fn vector_is_memory_path(path: &str) -> bool {
path.starts_with(VECTOR_MEMORY_PREFIX)
}
pub fn vector_path_to_id(path: &str) -> Option<&str> {
path.strip_prefix(VECTOR_MEMORY_PREFIX)
}
pub fn make_vector_memory_path(id: &str) -> String {
format!("{VECTOR_MEMORY_PREFIX}{id}")
}
pub fn put_vector(vector: Layer) -> String {
let id = NEXT_ID.fetch_add(1, Ordering::Relaxed).to_string();
if let Ok(mut map) = store().lock() {
map.insert(id.clone(), Arc::new(vector));
}
id
}
pub fn put_vector_arc(vector: Arc<Layer>) -> String {
let id = NEXT_ID.fetch_add(1, Ordering::Relaxed).to_string();
if let Ok(mut map) = store().lock() {
map.insert(id.clone(), vector);
}
id
}
pub fn get_vector_arc_by_id(id: &str) -> Option<Arc<Layer>> {
store().lock().ok().and_then(|map| map.get(id).cloned())
}
pub fn get_vector_arc_by_path(path: &str) -> Option<Arc<Layer>> {
vector_path_to_id(path).and_then(get_vector_arc_by_id)
}
pub fn get_vector_by_id(id: &str) -> Option<Layer> {
get_vector_arc_by_id(id).map(|v| (*v).clone())
}
pub fn replace_vector_by_id(id: &str, vector: Layer) -> bool {
store()
.lock()
.map(|mut map| map.insert(id.to_string(), Arc::new(vector)).is_some())
.unwrap_or(false)
}
pub fn replace_vector_by_path(path: &str, vector: Layer) -> bool {
vector_path_to_id(path)
.map(|id| replace_vector_by_id(id, vector))
.unwrap_or(false)
}
pub fn remove_vector_by_id(id: &str) -> Option<Layer> {
store()
.lock()
.ok()
.and_then(|mut map| map.remove(id))
.map(|v| Arc::try_unwrap(v).unwrap_or_else(|shared| (*shared).clone()))
}
pub fn remove_vector_by_path(path: &str) -> Option<Layer> {
vector_path_to_id(path).and_then(remove_vector_by_id)
}
pub fn clear_vectors() -> usize {
store()
.lock()
.map(|mut map| {
let count = map.len();
map.clear();
count
})
.unwrap_or(0)
}
pub fn vector_count() -> usize {
store().lock().map(|map| map.len()).unwrap_or(0)
}
#[cfg(test)]
mod tests {
use super::*;
use std::sync::{Mutex, MutexGuard, OnceLock};
fn memory_store_test_guard() -> MutexGuard<'static, ()> {
static TEST_LOCK: OnceLock<Mutex<()>> = OnceLock::new();
TEST_LOCK
.get_or_init(|| Mutex::new(()))
.lock()
.expect("memory_store test lock poisoned")
}
#[test]
fn remove_vector_by_id_removes_only_target_entry() {
let _guard = memory_store_test_guard();
clear_vectors();
let id1 = put_vector(Layer::new("a"));
let id2 = put_vector(Layer::new("b"));
assert!(get_vector_by_id(&id1).is_some());
assert!(get_vector_by_id(&id2).is_some());
let removed = remove_vector_by_id(&id1).expect("vector should be removed by id");
assert_eq!(removed.name, "a");
assert!(get_vector_by_id(&id1).is_none());
assert!(get_vector_by_id(&id2).is_some());
clear_vectors();
}
#[test]
fn remove_vector_by_path_and_clear_vectors_work() {
let _guard = memory_store_test_guard();
clear_vectors();
let id1 = put_vector(Layer::new("x"));
let id2 = put_vector(Layer::new("y"));
let path1 = make_vector_memory_path(&id1);
let removed = remove_vector_by_path(&path1).expect("vector should be removed by path");
assert_eq!(removed.name, "x");
assert!(get_vector_by_id(&id1).is_none());
assert!(get_vector_by_id(&id2).is_some());
assert!(vector_count() >= 1);
let cleared = clear_vectors();
assert!(cleared >= 1);
assert!(get_vector_by_id(&id2).is_none());
}
}