From 47e1148283cd8e185569771a02be4225feca3763 Mon Sep 17 00:00:00 2001 From: Matt Corallo Date: Thu, 28 Sep 2023 17:28:04 +0000 Subject: [PATCH] Update remaining references to primary/secondary namespaces Update various variables, error strings, and the pending changelog entry to refer to new namespace terminology. --- lightning-background-processor/src/lib.rs | 36 ++++++++--------- lightning-persister/src/fs_store.rs | 46 +++++++++++----------- lightning-persister/src/test_utils.rs | 25 ++++++------ lightning-persister/src/utils.rs | 48 ++++++++++++----------- lightning/src/util/test_utils.rs | 32 +++++++-------- pending_changelog/kvstore.txt | 2 +- 6 files changed, 96 insertions(+), 93 deletions(-) diff --git a/lightning-background-processor/src/lib.rs b/lightning-background-processor/src/lib.rs index e0c71bfcf..efa1a4214 100644 --- a/lightning-background-processor/src/lib.rs +++ b/lightning-background-processor/src/lib.rs @@ -506,10 +506,10 @@ use core::task; /// # use lightning_background_processor::{process_events_async, GossipSync}; /// # struct MyStore {} /// # impl lightning::util::persist::KVStore for MyStore { -/// # fn read(&self, namespace: &str, sub_namespace: &str, key: &str) -> io::Result> { Ok(Vec::new()) } -/// # fn write(&self, namespace: &str, sub_namespace: &str, key: &str, buf: &[u8]) -> io::Result<()> { Ok(()) } -/// # fn remove(&self, namespace: &str, sub_namespace: &str, key: &str, lazy: bool) -> io::Result<()> { Ok(()) } -/// # fn list(&self, namespace: &str, sub_namespace: &str) -> io::Result> { Ok(Vec::new()) } +/// # fn read(&self, primary_namespace: &str, secondary_namespace: &str, key: &str) -> io::Result> { Ok(Vec::new()) } +/// # fn write(&self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: &[u8]) -> io::Result<()> { Ok(()) } +/// # fn remove(&self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool) -> io::Result<()> { Ok(()) } +/// # fn list(&self, primary_namespace: &str, secondary_namespace: &str) -> io::Result> { Ok(Vec::new()) } /// # } /// # struct MyEventHandler {} /// # impl MyEventHandler { @@ -986,13 +986,13 @@ mod tests { } impl KVStore for Persister { - fn read(&self, namespace: &str, sub_namespace: &str, key: &str) -> lightning::io::Result> { - self.kv_store.read(namespace, sub_namespace, key) + fn read(&self, primary_namespace: &str, secondary_namespace: &str, key: &str) -> lightning::io::Result> { + self.kv_store.read(primary_namespace, secondary_namespace, key) } - fn write(&self, namespace: &str, sub_namespace: &str, key: &str, buf: &[u8]) -> lightning::io::Result<()> { - if namespace == CHANNEL_MANAGER_PERSISTENCE_PRIMARY_NAMESPACE && - sub_namespace == CHANNEL_MANAGER_PERSISTENCE_SECONDARY_NAMESPACE && + fn write(&self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: &[u8]) -> lightning::io::Result<()> { + if primary_namespace == CHANNEL_MANAGER_PERSISTENCE_PRIMARY_NAMESPACE && + secondary_namespace == CHANNEL_MANAGER_PERSISTENCE_SECONDARY_NAMESPACE && key == CHANNEL_MANAGER_PERSISTENCE_KEY { if let Some((error, message)) = self.manager_error { @@ -1000,8 +1000,8 @@ mod tests { } } - if namespace == NETWORK_GRAPH_PERSISTENCE_PRIMARY_NAMESPACE && - sub_namespace == NETWORK_GRAPH_PERSISTENCE_SECONDARY_NAMESPACE && + if primary_namespace == NETWORK_GRAPH_PERSISTENCE_PRIMARY_NAMESPACE && + secondary_namespace == NETWORK_GRAPH_PERSISTENCE_SECONDARY_NAMESPACE && key == NETWORK_GRAPH_PERSISTENCE_KEY { if let Some(sender) = &self.graph_persistence_notifier { @@ -1016,8 +1016,8 @@ mod tests { } } - if namespace == SCORER_PERSISTENCE_PRIMARY_NAMESPACE && - sub_namespace == SCORER_PERSISTENCE_SECONDARY_NAMESPACE && + if primary_namespace == SCORER_PERSISTENCE_PRIMARY_NAMESPACE && + secondary_namespace == SCORER_PERSISTENCE_SECONDARY_NAMESPACE && key == SCORER_PERSISTENCE_KEY { if let Some((error, message)) = self.scorer_error { @@ -1025,15 +1025,15 @@ mod tests { } } - self.kv_store.write(namespace, sub_namespace, key, buf) + self.kv_store.write(primary_namespace, secondary_namespace, key, buf) } - fn remove(&self, namespace: &str, sub_namespace: &str, key: &str, lazy: bool) -> lightning::io::Result<()> { - self.kv_store.remove(namespace, sub_namespace, key, lazy) + fn remove(&self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool) -> lightning::io::Result<()> { + self.kv_store.remove(primary_namespace, secondary_namespace, key, lazy) } - fn list(&self, namespace: &str, sub_namespace: &str) -> lightning::io::Result> { - self.kv_store.list(namespace, sub_namespace) + fn list(&self, primary_namespace: &str, secondary_namespace: &str) -> lightning::io::Result> { + self.kv_store.list(primary_namespace, secondary_namespace) } } diff --git a/lightning-persister/src/fs_store.rs b/lightning-persister/src/fs_store.rs index 42b28018f..347554484 100644 --- a/lightning-persister/src/fs_store.rs +++ b/lightning-persister/src/fs_store.rs @@ -67,7 +67,7 @@ impl FilesystemStore { } } - fn get_dest_dir_path(&self, namespace: &str, sub_namespace: &str) -> std::io::Result { + fn get_dest_dir_path(&self, primary_namespace: &str, secondary_namespace: &str) -> std::io::Result { let mut dest_dir_path = { #[cfg(target_os = "windows")] { @@ -81,9 +81,9 @@ impl FilesystemStore { } }; - dest_dir_path.push(namespace); - if !sub_namespace.is_empty() { - dest_dir_path.push(sub_namespace); + dest_dir_path.push(primary_namespace); + if !secondary_namespace.is_empty() { + dest_dir_path.push(secondary_namespace); } Ok(dest_dir_path) @@ -91,10 +91,10 @@ impl FilesystemStore { } impl KVStore for FilesystemStore { - fn read(&self, namespace: &str, sub_namespace: &str, key: &str) -> std::io::Result> { - check_namespace_key_validity(namespace, sub_namespace, Some(key), "read")?; + fn read(&self, primary_namespace: &str, secondary_namespace: &str, key: &str) -> std::io::Result> { + check_namespace_key_validity(primary_namespace, secondary_namespace, Some(key), "read")?; - let mut dest_file_path = self.get_dest_dir_path(namespace, sub_namespace)?; + let mut dest_file_path = self.get_dest_dir_path(primary_namespace, secondary_namespace)?; dest_file_path.push(key); let mut buf = Vec::new(); @@ -114,10 +114,10 @@ impl KVStore for FilesystemStore { Ok(buf) } - fn write(&self, namespace: &str, sub_namespace: &str, key: &str, buf: &[u8]) -> std::io::Result<()> { - check_namespace_key_validity(namespace, sub_namespace, Some(key), "write")?; + fn write(&self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: &[u8]) -> std::io::Result<()> { + check_namespace_key_validity(primary_namespace, secondary_namespace, Some(key), "write")?; - let mut dest_file_path = self.get_dest_dir_path(namespace, sub_namespace)?; + let mut dest_file_path = self.get_dest_dir_path(primary_namespace, secondary_namespace)?; dest_file_path.push(key); let parent_directory = dest_file_path @@ -201,10 +201,10 @@ impl KVStore for FilesystemStore { res } - fn remove(&self, namespace: &str, sub_namespace: &str, key: &str, lazy: bool) -> std::io::Result<()> { - check_namespace_key_validity(namespace, sub_namespace, Some(key), "remove")?; + fn remove(&self, primary_namespace: &str, secondary_namespace: &str, key: &str, lazy: bool) -> std::io::Result<()> { + check_namespace_key_validity(primary_namespace, secondary_namespace, Some(key), "remove")?; - let mut dest_file_path = self.get_dest_dir_path(namespace, sub_namespace)?; + let mut dest_file_path = self.get_dest_dir_path(primary_namespace, secondary_namespace)?; dest_file_path.push(key); if !dest_file_path.is_file() { @@ -290,10 +290,10 @@ impl KVStore for FilesystemStore { Ok(()) } - fn list(&self, namespace: &str, sub_namespace: &str) -> std::io::Result> { - check_namespace_key_validity(namespace, sub_namespace, None, "list")?; + fn list(&self, primary_namespace: &str, secondary_namespace: &str) -> std::io::Result> { + check_namespace_key_validity(primary_namespace, secondary_namespace, None, "list")?; - let prefixed_dest = self.get_dest_dir_path(namespace, sub_namespace)?; + let prefixed_dest = self.get_dest_dir_path(primary_namespace, secondary_namespace)?; let mut keys = Vec::new(); if !Path::new(&prefixed_dest).exists() { @@ -320,7 +320,7 @@ impl KVStore for FilesystemStore { let metadata = p.metadata()?; - // We allow the presence of directories in the empty namespace and just skip them. + // We allow the presence of directories in the empty primary namespace and just skip them. if metadata.is_dir() { continue; } @@ -328,9 +328,9 @@ impl KVStore for FilesystemStore { // If we otherwise don't find a file at the given path something went wrong. if !metadata.is_file() { debug_assert!(false, "Failed to list keys of {}/{}: file couldn't be accessed.", - PrintableString(namespace), PrintableString(sub_namespace)); + PrintableString(primary_namespace), PrintableString(secondary_namespace)); let msg = format!("Failed to list keys of {}/{}: file couldn't be accessed.", - PrintableString(namespace), PrintableString(sub_namespace)); + PrintableString(primary_namespace), PrintableString(secondary_namespace)); return Err(std::io::Error::new(std::io::ErrorKind::Other, msg)); } @@ -342,17 +342,17 @@ impl KVStore for FilesystemStore { } } else { debug_assert!(false, "Failed to list keys of {}/{}: file path is not valid UTF-8", - PrintableString(namespace), PrintableString(sub_namespace)); + PrintableString(primary_namespace), PrintableString(secondary_namespace)); let msg = format!("Failed to list keys of {}/{}: file path is not valid UTF-8", - PrintableString(namespace), PrintableString(sub_namespace)); + PrintableString(primary_namespace), PrintableString(secondary_namespace)); return Err(std::io::Error::new(std::io::ErrorKind::Other, msg)); } } Err(e) => { debug_assert!(false, "Failed to list keys of {}/{}: {}", - PrintableString(namespace), PrintableString(sub_namespace), e); + PrintableString(primary_namespace), PrintableString(secondary_namespace), e); let msg = format!("Failed to list keys of {}/{}: {}", - PrintableString(namespace), PrintableString(sub_namespace), e); + PrintableString(primary_namespace), PrintableString(secondary_namespace), e); return Err(std::io::Error::new(std::io::ErrorKind::Other, msg)); } } diff --git a/lightning-persister/src/test_utils.rs b/lightning-persister/src/test_utils.rs index 91557500f..360fa3492 100644 --- a/lightning-persister/src/test_utils.rs +++ b/lightning-persister/src/test_utils.rs @@ -12,34 +12,35 @@ use std::panic::RefUnwindSafe; pub(crate) fn do_read_write_remove_list_persist(kv_store: &K) { let data = [42u8; 32]; - let namespace = "testspace"; - let sub_namespace = "testsubspace"; + let primary_namespace = "testspace"; + let secondary_namespace = "testsubspace"; let key = "testkey"; // Test the basic KVStore operations. - kv_store.write(namespace, sub_namespace, key, &data).unwrap(); + kv_store.write(primary_namespace, secondary_namespace, key, &data).unwrap(); - // Test empty namespace/sub_namespace is allowed, but not empty namespace and non-empty - // sub-namespace, and not empty key. + // Test empty primary_namespace/secondary_namespace is allowed, but not empty primary_namespace + // and non-empty secondary_namespace, and not empty key. kv_store.write("", "", key, &data).unwrap(); - let res = std::panic::catch_unwind(|| kv_store.write("", sub_namespace, key, &data)); + let res = std::panic::catch_unwind(|| kv_store.write("", secondary_namespace, key, &data)); assert!(res.is_err()); - let res = std::panic::catch_unwind(|| kv_store.write(namespace, sub_namespace, "", &data)); + let res = std::panic::catch_unwind(|| kv_store.write(primary_namespace, secondary_namespace, "", &data)); assert!(res.is_err()); - let listed_keys = kv_store.list(namespace, sub_namespace).unwrap(); + let listed_keys = kv_store.list(primary_namespace, secondary_namespace).unwrap(); assert_eq!(listed_keys.len(), 1); assert_eq!(listed_keys[0], key); - let read_data = kv_store.read(namespace, sub_namespace, key).unwrap(); + let read_data = kv_store.read(primary_namespace, secondary_namespace, key).unwrap(); assert_eq!(data, &*read_data); - kv_store.remove(namespace, sub_namespace, key, false).unwrap(); + kv_store.remove(primary_namespace, secondary_namespace, key, false).unwrap(); - let listed_keys = kv_store.list(namespace, sub_namespace).unwrap(); + let listed_keys = kv_store.list(primary_namespace, secondary_namespace).unwrap(); assert_eq!(listed_keys.len(), 0); - // Ensure we have no issue operating with namespace/sub_namespace/key being KVSTORE_NAMESPACE_KEY_MAX_LEN + // Ensure we have no issue operating with primary_namespace/secondary_namespace/key being + // KVSTORE_NAMESPACE_KEY_MAX_LEN let max_chars: String = std::iter::repeat('A').take(KVSTORE_NAMESPACE_KEY_MAX_LEN).collect(); kv_store.write(&max_chars, &max_chars, &max_chars, &data).unwrap(); diff --git a/lightning-persister/src/utils.rs b/lightning-persister/src/utils.rs index 54ec230de..59a615937 100644 --- a/lightning-persister/src/utils.rs +++ b/lightning-persister/src/utils.rs @@ -6,51 +6,53 @@ pub(crate) fn is_valid_kvstore_str(key: &str) -> bool { key.len() <= KVSTORE_NAMESPACE_KEY_MAX_LEN && key.chars().all(|c| KVSTORE_NAMESPACE_KEY_ALPHABET.contains(c)) } -pub(crate) fn check_namespace_key_validity(namespace: &str, sub_namespace: &str, key: Option<&str>, operation: &str) -> Result<(), std::io::Error> { +pub(crate) fn check_namespace_key_validity( + primary_namespace: &str, secondary_namespace: &str, key: Option<&str>, operation: &str) +-> Result<(), std::io::Error> { if let Some(key) = key { if key.is_empty() { debug_assert!(false, "Failed to {} {}/{}/{}: key may not be empty.", operation, - PrintableString(namespace), PrintableString(sub_namespace), PrintableString(key)); + PrintableString(primary_namespace), PrintableString(secondary_namespace), PrintableString(key)); let msg = format!("Failed to {} {}/{}/{}: key may not be empty.", operation, - PrintableString(namespace), PrintableString(sub_namespace), PrintableString(key)); + PrintableString(primary_namespace), PrintableString(secondary_namespace), PrintableString(key)); return Err(std::io::Error::new(std::io::ErrorKind::Other, msg)); } - if namespace.is_empty() && !sub_namespace.is_empty() { + if primary_namespace.is_empty() && !secondary_namespace.is_empty() { debug_assert!(false, - "Failed to {} {}/{}/{}: namespace may not be empty if a non-empty sub-namespace is given.", + "Failed to {} {}/{}/{}: primary namespace may not be empty if a non-empty secondary namespace is given.", operation, - PrintableString(namespace), PrintableString(sub_namespace), PrintableString(key)); + PrintableString(primary_namespace), PrintableString(secondary_namespace), PrintableString(key)); let msg = format!( - "Failed to {} {}/{}/{}: namespace may not be empty if a non-empty sub-namespace is given.", operation, - PrintableString(namespace), PrintableString(sub_namespace), PrintableString(key)); + "Failed to {} {}/{}/{}: primary namespace may not be empty if a non-empty secondary namespace is given.", operation, + PrintableString(primary_namespace), PrintableString(secondary_namespace), PrintableString(key)); return Err(std::io::Error::new(std::io::ErrorKind::Other, msg)); } - if !is_valid_kvstore_str(namespace) || !is_valid_kvstore_str(sub_namespace) || !is_valid_kvstore_str(key) { - debug_assert!(false, "Failed to {} {}/{}/{}: namespace, sub-namespace, and key must be valid.", + if !is_valid_kvstore_str(primary_namespace) || !is_valid_kvstore_str(secondary_namespace) || !is_valid_kvstore_str(key) { + debug_assert!(false, "Failed to {} {}/{}/{}: primary namespace, secondary namespace, and key must be valid.", operation, - PrintableString(namespace), PrintableString(sub_namespace), PrintableString(key)); - let msg = format!("Failed to {} {}/{}/{}: namespace, sub-namespace, and key must be valid.", + PrintableString(primary_namespace), PrintableString(secondary_namespace), PrintableString(key)); + let msg = format!("Failed to {} {}/{}/{}: primary namespace, secondary namespace, and key must be valid.", operation, - PrintableString(namespace), PrintableString(sub_namespace), PrintableString(key)); + PrintableString(primary_namespace), PrintableString(secondary_namespace), PrintableString(key)); return Err(std::io::Error::new(std::io::ErrorKind::Other, msg)); } } else { - if namespace.is_empty() && !sub_namespace.is_empty() { + if primary_namespace.is_empty() && !secondary_namespace.is_empty() { debug_assert!(false, - "Failed to {} {}/{}: namespace may not be empty if a non-empty sub-namespace is given.", - operation, PrintableString(namespace), PrintableString(sub_namespace)); + "Failed to {} {}/{}: primary namespace may not be empty if a non-empty secondary namespace is given.", + operation, PrintableString(primary_namespace), PrintableString(secondary_namespace)); let msg = format!( - "Failed to {} {}/{}: namespace may not be empty if a non-empty sub-namespace is given.", - operation, PrintableString(namespace), PrintableString(sub_namespace)); + "Failed to {} {}/{}: primary namespace may not be empty if a non-empty secondary namespace is given.", + operation, PrintableString(primary_namespace), PrintableString(secondary_namespace)); return Err(std::io::Error::new(std::io::ErrorKind::Other, msg)); } - if !is_valid_kvstore_str(namespace) || !is_valid_kvstore_str(sub_namespace) { - debug_assert!(false, "Failed to {} {}/{}: namespace and sub-namespace must be valid.", - operation, PrintableString(namespace), PrintableString(sub_namespace)); - let msg = format!("Failed to {} {}/{}: namespace and sub-namespace must be valid.", - operation, PrintableString(namespace), PrintableString(sub_namespace)); + if !is_valid_kvstore_str(primary_namespace) || !is_valid_kvstore_str(secondary_namespace) { + debug_assert!(false, "Failed to {} {}/{}: primary namespace and secondary namespace must be valid.", + operation, PrintableString(primary_namespace), PrintableString(secondary_namespace)); + let msg = format!("Failed to {} {}/{}: primary namespace and secondary namespace must be valid.", + operation, PrintableString(primary_namespace), PrintableString(secondary_namespace)); return Err(std::io::Error::new(std::io::ErrorKind::Other, msg)); } } diff --git a/lightning/src/util/test_utils.rs b/lightning/src/util/test_utils.rs index d53cd39b1..c5e37ca90 100644 --- a/lightning/src/util/test_utils.rs +++ b/lightning/src/util/test_utils.rs @@ -440,12 +440,12 @@ impl TestStore { } impl KVStore for TestStore { - fn read(&self, namespace: &str, sub_namespace: &str, key: &str) -> io::Result> { + fn read(&self, primary_namespace: &str, secondary_namespace: &str, key: &str) -> io::Result> { let persisted_lock = self.persisted_bytes.lock().unwrap(); - let prefixed = if sub_namespace.is_empty() { - namespace.to_string() + let prefixed = if secondary_namespace.is_empty() { + primary_namespace.to_string() } else { - format!("{}/{}", namespace, sub_namespace) + format!("{}/{}", primary_namespace, secondary_namespace) }; if let Some(outer_ref) = persisted_lock.get(&prefixed) { @@ -460,7 +460,7 @@ impl KVStore for TestStore { } } - fn write(&self, namespace: &str, sub_namespace: &str, key: &str, buf: &[u8]) -> io::Result<()> { + fn write(&self, primary_namespace: &str, secondary_namespace: &str, key: &str, buf: &[u8]) -> io::Result<()> { if self.read_only { return Err(io::Error::new( io::ErrorKind::PermissionDenied, @@ -469,10 +469,10 @@ impl KVStore for TestStore { } let mut persisted_lock = self.persisted_bytes.lock().unwrap(); - let prefixed = if sub_namespace.is_empty() { - namespace.to_string() + let prefixed = if secondary_namespace.is_empty() { + primary_namespace.to_string() } else { - format!("{}/{}", namespace, sub_namespace) + format!("{}/{}", primary_namespace, secondary_namespace) }; let outer_e = persisted_lock.entry(prefixed).or_insert(HashMap::new()); let mut bytes = Vec::new(); @@ -481,7 +481,7 @@ impl KVStore for TestStore { Ok(()) } - fn remove(&self, namespace: &str, sub_namespace: &str, key: &str, _lazy: bool) -> io::Result<()> { + fn remove(&self, primary_namespace: &str, secondary_namespace: &str, key: &str, _lazy: bool) -> io::Result<()> { if self.read_only { return Err(io::Error::new( io::ErrorKind::PermissionDenied, @@ -491,10 +491,10 @@ impl KVStore for TestStore { let mut persisted_lock = self.persisted_bytes.lock().unwrap(); - let prefixed = if sub_namespace.is_empty() { - namespace.to_string() + let prefixed = if secondary_namespace.is_empty() { + primary_namespace.to_string() } else { - format!("{}/{}", namespace, sub_namespace) + format!("{}/{}", primary_namespace, secondary_namespace) }; if let Some(outer_ref) = persisted_lock.get_mut(&prefixed) { outer_ref.remove(&key.to_string()); @@ -503,13 +503,13 @@ impl KVStore for TestStore { Ok(()) } - fn list(&self, namespace: &str, sub_namespace: &str) -> io::Result> { + fn list(&self, primary_namespace: &str, secondary_namespace: &str) -> io::Result> { let mut persisted_lock = self.persisted_bytes.lock().unwrap(); - let prefixed = if sub_namespace.is_empty() { - namespace.to_string() + let prefixed = if secondary_namespace.is_empty() { + primary_namespace.to_string() } else { - format!("{}/{}", namespace, sub_namespace) + format!("{}/{}", primary_namespace, secondary_namespace) }; match persisted_lock.entry(prefixed) { hash_map::Entry::Occupied(e) => Ok(e.get().keys().cloned().collect()), diff --git a/pending_changelog/kvstore.txt b/pending_changelog/kvstore.txt index d96fd6937..3fe949500 100644 --- a/pending_changelog/kvstore.txt +++ b/pending_changelog/kvstore.txt @@ -1,3 +1,3 @@ ## Backwards Compatibility -* Users migrating custom persistence backends from the pre-v0.0.117 `KVStorePersister` interface can use a concatenation of `[{namespace}/[{sub_namespace}/]]{key}` to recover a `key` compatible with the data model previously assumed by `KVStorePersister::persist`. +* Users migrating custom persistence backends from the pre-v0.0.117 `KVStorePersister` interface can use a concatenation of `[{primary_namespace}/[{secondary_namespace}/]]{key}` to recover a `key` compatible with the data model previously assumed by `KVStorePersister::persist`. -- 2.39.5