Skip to content

Commit bf96f15

Browse files
committed
f Account for sub-namespaces
1 parent 7a27261 commit bf96f15

1 file changed

Lines changed: 32 additions & 8 deletions

File tree

lightning/src/util/test_utils.rs

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -439,9 +439,15 @@ impl TestStore {
439439
}
440440

441441
impl KVStore for TestStore {
442-
fn read(&self, namespace: &str, key: &str) -> io::Result<Vec<u8>> {
442+
fn read(&self, namespace: &str, sub_namespace: &str, key: &str) -> io::Result<Vec<u8>> {
443443
let persisted_lock = self.persisted_bytes.lock().unwrap();
444-
if let Some(outer_ref) = persisted_lock.get(namespace) {
444+
let prefixed = if sub_namespace.is_empty() {
445+
namespace.to_string()
446+
} else {
447+
format!("{}/{}", namespace, sub_namespace)
448+
};
449+
450+
if let Some(outer_ref) = persisted_lock.get(&prefixed) {
445451
if let Some(inner_ref) = outer_ref.get(key) {
446452
let bytes = inner_ref.clone();
447453
Ok(bytes)
@@ -453,22 +459,28 @@ impl KVStore for TestStore {
453459
}
454460
}
455461

456-
fn write(&self, namespace: &str, key: &str, buf: &[u8]) -> io::Result<()> {
462+
fn write(&self, namespace: &str, sub_namespace: &str, key: &str, buf: &[u8]) -> io::Result<()> {
457463
if self.read_only {
458464
return Err(io::Error::new(
459465
io::ErrorKind::PermissionDenied,
460466
"Cannot modify read-only store",
461467
));
462468
}
463469
let mut persisted_lock = self.persisted_bytes.lock().unwrap();
464-
let outer_e = persisted_lock.entry(namespace.to_string()).or_insert(HashMap::new());
470+
471+
let prefixed = if sub_namespace.is_empty() {
472+
namespace.to_string()
473+
} else {
474+
format!("{}/{}", namespace, sub_namespace)
475+
};
476+
let outer_e = persisted_lock.entry(prefixed).or_insert(HashMap::new());
465477
let mut bytes = Vec::new();
466478
bytes.write_all(buf)?;
467479
outer_e.insert(key.to_string(), bytes);
468480
Ok(())
469481
}
470482

471-
fn remove(&self, namespace: &str, key: &str) -> io::Result<()> {
483+
fn remove(&self, namespace: &str, sub_namespace: &str, key: &str) -> io::Result<()> {
472484
if self.read_only {
473485
return Err(io::Error::new(
474486
io::ErrorKind::PermissionDenied,
@@ -477,16 +489,28 @@ impl KVStore for TestStore {
477489
}
478490

479491
let mut persisted_lock = self.persisted_bytes.lock().unwrap();
480-
if let Some(outer_ref) = persisted_lock.get_mut(namespace) {
492+
493+
let prefixed = if sub_namespace.is_empty() {
494+
namespace.to_string()
495+
} else {
496+
format!("{}/{}", namespace, sub_namespace)
497+
};
498+
if let Some(outer_ref) = persisted_lock.get_mut(&prefixed) {
481499
outer_ref.remove(&key.to_string());
482500
}
483501

484502
Ok(())
485503
}
486504

487-
fn list(&self, namespace: &str) -> io::Result<Vec<String>> {
505+
fn list(&self, namespace: &str, sub_namespace: &str) -> io::Result<Vec<String>> {
488506
let mut persisted_lock = self.persisted_bytes.lock().unwrap();
489-
match persisted_lock.entry(namespace.to_string()) {
507+
508+
let prefixed = if sub_namespace.is_empty() {
509+
namespace.to_string()
510+
} else {
511+
format!("{}/{}", namespace, sub_namespace)
512+
};
513+
match persisted_lock.entry(prefixed) {
490514
hash_map::Entry::Occupied(e) => Ok(e.get().keys().cloned().collect()),
491515
hash_map::Entry::Vacant(_) => Ok(Vec::new()),
492516
}

0 commit comments

Comments
 (0)