f Move empty-key checks before taking locks
authorElias Rohrer <dev@tnull.de>
Mon, 21 Aug 2023 12:07:16 +0000 (14:07 +0200)
committerElias Rohrer <dev@tnull.de>
Wed, 23 Aug 2023 10:37:23 +0000 (12:37 +0200)
lightning-persister/src/fs_store.rs

index 3c55e5f824d9dfe449a13e0e1679a2705262f170..cd3e9dda5c6e5bc2a213ee3af039d197f856f384 100644 (file)
@@ -52,15 +52,15 @@ impl KVStore for FilesystemStore {
        type Reader = FilesystemReader;
 
        fn read(&self, namespace: &str, key: &str) -> std::io::Result<Self::Reader> {
-               let mut outer_lock = self.locks.lock().unwrap();
-               let lock_key = (namespace.to_string(), key.to_string());
-               let inner_lock_ref = Arc::clone(&outer_lock.entry(lock_key).or_default());
-
                if key.is_empty() {
                        let msg = format!("Failed to read {}/{}: key may not be empty.", namespace, key);
                        return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
                }
 
+               let mut outer_lock = self.locks.lock().unwrap();
+               let lock_key = (namespace.to_string(), key.to_string());
+               let inner_lock_ref = Arc::clone(&outer_lock.entry(lock_key).or_default());
+
                let mut dest_file_path = self.data_dir.clone();
                dest_file_path.push(namespace);
                dest_file_path.push(key);
@@ -68,16 +68,16 @@ impl KVStore for FilesystemStore {
        }
 
        fn write(&self, namespace: &str, key: &str, buf: &[u8]) -> std::io::Result<()> {
-               let mut outer_lock = self.locks.lock().unwrap();
-               let lock_key = (namespace.to_string(), key.to_string());
-               let inner_lock_ref = Arc::clone(&outer_lock.entry(lock_key).or_default());
-               let _guard = inner_lock_ref.write().unwrap();
-
                if key.is_empty() {
                        let msg = format!("Failed to write {}/{}: key may not be empty.", namespace, key);
                        return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
                }
 
+               let mut outer_lock = self.locks.lock().unwrap();
+               let lock_key = (namespace.to_string(), key.to_string());
+               let inner_lock_ref = Arc::clone(&outer_lock.entry(lock_key).or_default());
+               let _guard = inner_lock_ref.write().unwrap();
+
                let mut dest_file_path = self.data_dir.clone();
                dest_file_path.push(namespace);
                dest_file_path.push(key);
@@ -143,17 +143,17 @@ impl KVStore for FilesystemStore {
        }
 
        fn remove(&self, namespace: &str, key: &str) -> std::io::Result<()> {
+               if key.is_empty() {
+                       let msg = format!("Failed to remove {}/{}: key may not be empty.", namespace, key);
+                       return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
+               }
+
                let mut outer_lock = self.locks.lock().unwrap();
                let lock_key = (namespace.to_string(), key.to_string());
                let inner_lock_ref = Arc::clone(&outer_lock.entry(lock_key.clone()).or_default());
 
                let _guard = inner_lock_ref.write().unwrap();
 
-               if key.is_empty() {
-                       let msg = format!("Failed to remove {}/{}: key may not be empty.", namespace, key);
-                       return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
-               }
-
                let mut dest_file_path = self.data_dir.clone();
                dest_file_path.push(namespace);
                dest_file_path.push(key);