Skip to content

Commit 100e201

Browse files
committed
f Move empty-key checks before taking locks
1 parent f7157c2 commit 100e201

File tree

1 file changed

+14
-14
lines changed

1 file changed

+14
-14
lines changed

lightning-persister/src/fs_store.rs

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -52,32 +52,32 @@ impl KVStore for FilesystemStore {
5252
type Reader = FilesystemReader;
5353

5454
fn read(&self, namespace: &str, key: &str) -> std::io::Result<Self::Reader> {
55-
let mut outer_lock = self.locks.lock().unwrap();
56-
let lock_key = (namespace.to_string(), key.to_string());
57-
let inner_lock_ref = Arc::clone(&outer_lock.entry(lock_key).or_default());
58-
5955
if key.is_empty() {
6056
let msg = format!("Failed to read {}/{}: key may not be empty.", namespace, key);
6157
return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
6258
}
6359

60+
let mut outer_lock = self.locks.lock().unwrap();
61+
let lock_key = (namespace.to_string(), key.to_string());
62+
let inner_lock_ref = Arc::clone(&outer_lock.entry(lock_key).or_default());
63+
6464
let mut dest_file_path = self.data_dir.clone();
6565
dest_file_path.push(namespace);
6666
dest_file_path.push(key);
6767
FilesystemReader::new(dest_file_path, inner_lock_ref)
6868
}
6969

7070
fn write(&self, namespace: &str, key: &str, buf: &[u8]) -> std::io::Result<()> {
71-
let mut outer_lock = self.locks.lock().unwrap();
72-
let lock_key = (namespace.to_string(), key.to_string());
73-
let inner_lock_ref = Arc::clone(&outer_lock.entry(lock_key).or_default());
74-
let _guard = inner_lock_ref.write().unwrap();
75-
7671
if key.is_empty() {
7772
let msg = format!("Failed to write {}/{}: key may not be empty.", namespace, key);
7873
return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
7974
}
8075

76+
let mut outer_lock = self.locks.lock().unwrap();
77+
let lock_key = (namespace.to_string(), key.to_string());
78+
let inner_lock_ref = Arc::clone(&outer_lock.entry(lock_key).or_default());
79+
let _guard = inner_lock_ref.write().unwrap();
80+
8181
let mut dest_file_path = self.data_dir.clone();
8282
dest_file_path.push(namespace);
8383
dest_file_path.push(key);
@@ -143,17 +143,17 @@ impl KVStore for FilesystemStore {
143143
}
144144

145145
fn remove(&self, namespace: &str, key: &str) -> std::io::Result<()> {
146+
if key.is_empty() {
147+
let msg = format!("Failed to remove {}/{}: key may not be empty.", namespace, key);
148+
return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
149+
}
150+
146151
let mut outer_lock = self.locks.lock().unwrap();
147152
let lock_key = (namespace.to_string(), key.to_string());
148153
let inner_lock_ref = Arc::clone(&outer_lock.entry(lock_key.clone()).or_default());
149154

150155
let _guard = inner_lock_ref.write().unwrap();
151156

152-
if key.is_empty() {
153-
let msg = format!("Failed to remove {}/{}: key may not be empty.", namespace, key);
154-
return Err(std::io::Error::new(std::io::ErrorKind::Other, msg));
155-
}
156-
157157
let mut dest_file_path = self.data_dir.clone();
158158
dest_file_path.push(namespace);
159159
dest_file_path.push(key);

0 commit comments

Comments
 (0)