|
1 | 1 | use super::*; |
2 | 2 | use std::collections::HashMap; |
| 3 | +use std::sync::{LazyLock, RwLock}; |
3 | 4 |
|
4 | 5 | const CACHE_VERSION: u64 = 24; |
5 | 6 |
|
6 | 7 | lazy_static! { |
7 | 8 | static ref DB: std::sync::Mutex<Option<sled::Db>> = std::sync::Mutex::new(None); |
8 | | - static ref REF_CACHE: std::sync::Mutex<HashMap<git2::Oid, HashMap<git2::Oid, git2::Oid>>> = |
9 | | - std::sync::Mutex::new(HashMap::new()); |
10 | | - static ref POPULATE_MAP: std::sync::Mutex<HashMap<(git2::Oid, git2::Oid), git2::Oid>> = |
11 | | - std::sync::Mutex::new(HashMap::new()); |
12 | | - static ref GLOB_MAP: std::sync::Mutex<HashMap<(git2::Oid, git2::Oid), git2::Oid>> = |
13 | | - std::sync::Mutex::new(HashMap::new()); |
14 | 9 | } |
15 | 10 |
|
| 11 | +static REF_CACHE: LazyLock<RwLock<HashMap<git2::Oid, HashMap<git2::Oid, git2::Oid>>>> = |
| 12 | + LazyLock::new(Default::default); |
| 13 | + |
| 14 | +static POPULATE_MAP: LazyLock<RwLock<HashMap<(git2::Oid, git2::Oid), git2::Oid>>> = |
| 15 | + LazyLock::new(Default::default); |
| 16 | + |
| 17 | +static GLOB_MAP: LazyLock<RwLock<HashMap<(git2::Oid, git2::Oid), git2::Oid>>> = |
| 18 | + LazyLock::new(Default::default); |
| 19 | + |
16 | 20 | pub fn load(path: &std::path::Path) -> JoshResult<()> { |
17 | 21 | *DB.lock()? = Some( |
18 | 22 | sled::Config::default() |
@@ -272,32 +276,32 @@ impl Transaction { |
272 | 276 | } |
273 | 277 |
|
274 | 278 | pub fn insert_populate(&self, tree: (git2::Oid, git2::Oid), result: git2::Oid) { |
275 | | - POPULATE_MAP.lock().unwrap().entry(tree).or_insert(result); |
| 279 | + POPULATE_MAP.write().unwrap().entry(tree).or_insert(result); |
276 | 280 | } |
277 | 281 |
|
278 | 282 | pub fn get_populate(&self, tree: (git2::Oid, git2::Oid)) -> Option<git2::Oid> { |
279 | | - return POPULATE_MAP.lock().unwrap().get(&tree).cloned(); |
| 283 | + POPULATE_MAP.read().unwrap().get(&tree).cloned() |
280 | 284 | } |
281 | 285 |
|
282 | 286 | pub fn insert_glob(&self, tree: (git2::Oid, git2::Oid), result: git2::Oid) { |
283 | | - GLOB_MAP.lock().unwrap().entry(tree).or_insert(result); |
| 287 | + GLOB_MAP.write().unwrap().entry(tree).or_insert(result); |
284 | 288 | } |
285 | 289 |
|
286 | 290 | pub fn get_glob(&self, tree: (git2::Oid, git2::Oid)) -> Option<git2::Oid> { |
287 | | - return GLOB_MAP.lock().unwrap().get(&tree).cloned(); |
| 291 | + GLOB_MAP.read().unwrap().get(&tree).cloned() |
288 | 292 | } |
289 | 293 |
|
290 | 294 | pub fn insert_ref(&self, filter: filter::Filter, from: git2::Oid, to: git2::Oid) { |
291 | 295 | REF_CACHE |
292 | | - .lock() |
| 296 | + .write() |
293 | 297 | .unwrap() |
294 | 298 | .entry(filter.id()) |
295 | 299 | .or_default() |
296 | 300 | .insert(from, to); |
297 | 301 | } |
298 | 302 |
|
299 | 303 | pub fn get_ref(&self, filter: filter::Filter, from: git2::Oid) -> Option<git2::Oid> { |
300 | | - if let Some(m) = REF_CACHE.lock().unwrap().get(&filter.id()) { |
| 304 | + if let Some(m) = REF_CACHE.read().unwrap().get(&filter.id()) { |
301 | 305 | if let Some(oid) = m.get(&from) { |
302 | 306 | if self.repo.odb().unwrap().exists(*oid) { |
303 | 307 | return Some(*oid); |
|
0 commit comments