use std::collections::VecDeque;
use futures::Future;
use mea::mutex::Mutex;
use mea::mutex::MutexGuard;
use moka::sync::Cache;
use crate::raw::*;
use crate::*;
pub trait PathQuery {
fn root(&self) -> impl Future<Output = Result<String>> + MaybeSend;
fn query(
&self,
parent_id: &str,
name: &str,
) -> impl Future<Output = Result<Option<String>>> + MaybeSend;
fn create_dir(
&self,
parent_id: &str,
name: &str,
) -> impl Future<Output = Result<String>> + MaybeSend;
}
pub struct PathCacher<Q: PathQuery> {
query: Q,
cache: Cache<String, String>,
lock: Option<Mutex<()>>,
}
impl<Q: PathQuery> PathCacher<Q> {
pub fn new(query: Q) -> Self {
Self {
query,
cache: Cache::new(64 * 1024),
lock: None,
}
}
pub fn with_lock(mut self) -> Self {
self.lock = Some(Mutex::default());
self
}
async fn lock(&self) -> Option<MutexGuard<'_, ()>> {
if let Some(l) = &self.lock {
Some(l.lock().await)
} else {
None
}
}
pub async fn insert(&self, path: &str, id: &str) {
let _guard = self.lock().await;
if self.cache.contains_key(path) {
debug_assert!(
self.cache.get(path) == Some(id.to_string()),
"path {path} exists but it's value is inconsistent"
);
return;
}
self.cache.insert(path.to_string(), id.to_string());
}
pub async fn remove(&self, path: &str) {
let _guard = self.lock().await;
self.cache.invalidate(path)
}
pub async fn get(&self, path: &str) -> Result<Option<String>> {
let _guard = self.lock().await;
if let Some(id) = self.cache.get(path) {
return Ok(Some(id));
}
let mut paths = VecDeque::new();
let mut current_path = path;
while current_path != "/" && !current_path.is_empty() {
paths.push_front(current_path.to_string());
current_path = get_parent(current_path);
if let Some(id) = self.cache.get(current_path) {
return self.query_down(&id, paths).await;
}
}
let root_id = self.query.root().await?;
self.cache.insert("/".to_string(), root_id.clone());
self.query_down(&root_id, paths).await
}
async fn query_down(&self, start_id: &str, paths: VecDeque<String>) -> Result<Option<String>> {
let mut current_id = start_id.to_string();
for path in paths.into_iter() {
let name = get_basename(&path);
current_id = match self.query.query(¤t_id, name).await? {
Some(id) => {
self.cache.insert(path, id.clone());
id
}
None => return Ok(None),
};
}
Ok(Some(current_id))
}
pub async fn ensure_dir(&self, path: &str) -> Result<String> {
let _guard = self.lock().await;
let mut tmp = "".to_string();
let mut parents = vec![];
for component in path.split('/') {
if component.is_empty() {
continue;
}
tmp.push_str(component);
tmp.push('/');
parents.push(tmp.to_string());
}
let mut parent_id = match self.cache.get("/") {
Some(v) => v,
None => self.query.root().await?,
};
for parent in parents {
parent_id = match self.cache.get(&parent) {
Some(value) => value,
None => {
let value = match self.query.query(&parent_id, get_basename(&parent)).await? {
Some(value) => value,
None => {
self.query
.create_dir(&parent_id, get_basename(&parent))
.await?
}
};
self.cache.insert(parent, value.clone());
value
}
}
}
Ok(parent_id)
}
}
#[cfg(test)]
mod tests {
use crate::raw::PathCacher;
use crate::raw::PathQuery;
use crate::*;
struct TestQuery {}
impl PathQuery for TestQuery {
async fn root(&self) -> Result<String> {
Ok("root/".to_string())
}
async fn query(&self, parent_id: &str, name: &str) -> Result<Option<String>> {
if name.starts_with("not_exist") {
return Ok(None);
}
Ok(Some(format!("{parent_id}{name}")))
}
async fn create_dir(&self, parent_id: &str, name: &str) -> Result<String> {
Ok(format!("{parent_id}{name}"))
}
}
#[tokio::test]
async fn test_path_cacher_get() {
let cases = vec![
("root", "/", Some("root/")),
("normal path", "/a", Some("root/a")),
("not exist normal dir", "/not_exist/a", None),
("not exist normal file", "/a/b/not_exist", None),
("nest path", "/a/b/c/d", Some("root/a/b/c/d")),
];
for (name, input, expect) in cases {
let cache = PathCacher::new(TestQuery {});
let actual = cache.get(input).await.unwrap();
assert_eq!(actual.as_deref(), expect, "{name}")
}
}
}