Skip to content

Commit

Permalink
fix(serivces/memory) blocking scan right range
Browse files Browse the repository at this point in the history
  • Loading branch information
meteorgan committed Aug 27, 2024
1 parent 8bc883f commit 5b55889
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 13 deletions.
2 changes: 1 addition & 1 deletion core/src/raw/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ pub fn normalize_path(path: &str) -> String {
/// - Add leading `/` if not starts with: `abc/` => `/abc/`
/// - Add trailing `/` if not ends with: `/abc` => `/abc/`
///
/// Finally, we will got path like `/path/to/root/`.
/// Finally, we will get path like `/path/to/root/`.
pub fn normalize_root(v: &str) -> String {
let mut v = v
.split('/')
Expand Down
43 changes: 31 additions & 12 deletions core/src/services/memory/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -125,25 +125,26 @@ impl typed_kv::Adapter for Adapter {

fn blocking_scan(&self, path: &str) -> Result<Vec<String>> {
let inner = self.inner.lock().unwrap();
let keys: Vec<_> = if path.is_empty() {
inner.keys().cloned().collect()
} else {
let right_range = if let Some(path) = path.strip_suffix('/') {
format!("{}0", path)
} else {
format!("{}{}", path, std::char::MAX)
};
inner
.range(path.to_string()..right_range)
.map(|(k, _)| k.to_string())
.collect()

if path.is_empty() {
return Ok(inner.keys().cloned().collect());
}

let mut keys = Vec::new();
for (key, _) in inner.range(path.to_string()..) {
if !key.starts_with(path) {
break;
}
keys.push(key.to_string());
};
Ok(keys)
}
}

#[cfg(test)]
mod tests {
use crate::raw::adapters::typed_kv::{Adapter, Value};
use crate::services::memory::backend;
use super::*;

#[test]
Expand All @@ -154,4 +155,22 @@ mod tests {
let b2 = MemoryBuilder::default().build().unwrap();
assert_ne!(b1.info().name(), b2.info().name())
}

#[test]
fn test_blocking_scan() {
let adapter = backend::Adapter { inner: Arc::new(Mutex::new(BTreeMap::default())) };

adapter.blocking_set("aaa/bbb/", Value::new_dir()).unwrap();
adapter.blocking_set("aab/bbb/", Value::new_dir()).unwrap();
adapter.blocking_set("aab/ccc/", Value::new_dir()).unwrap();
adapter.blocking_set(&format!("aab{}aaa/", std::char::MAX), Value::new_dir()).unwrap();
adapter.blocking_set("aac/bbb/", Value::new_dir()).unwrap();

let data = adapter.blocking_scan("aab").unwrap();
assert_eq!(data.len(), 3);
for path in data {
assert_eq!(path.starts_with("aab"), true);
}
}
}

0 comments on commit 5b55889

Please sign in to comment.