From 0611046523eae72d2300d53cf935431b38d210e7 Mon Sep 17 00:00:00 2001 From: Ian Jackson Date: Sat, 15 May 2021 11:53:18 +0100 Subject: [PATCH] Revert "Revert "libraries: Prepare for multiple libraries with the same name"" This reverts commit 9c68f0377f6b9b5ff27fe9ad2bb47206b6e5dd99. It seems I must have accidentally reverted 5148d6ae3d2bedffcc35e7d0528d81fe0f8dbffe libraries: Prepare for multiple libraries with the same name when I must have intended to revert f580502d464611e7c0a5c3085f5a9be71979bfdf libraries: LibraryItemNotPrepared: Make this a separate error Signed-off-by: Ian Jackson --- daemon/cmdlistener.rs | 7 +++++-- src/bin/otterlib.rs | 11 ++++++----- src/shapelib.rs | 18 +++++++++++------- 3 files changed, 22 insertions(+), 14 deletions(-) diff --git a/daemon/cmdlistener.rs b/daemon/cmdlistener.rs index 4b5bb8a4..fce4a8ab 100644 --- a/daemon/cmdlistener.rs +++ b/daemon/cmdlistener.rs @@ -311,8 +311,11 @@ fn execute_and_respond(cs: &mut CommandStreamData, cmd: MgmtCommand, } MC::LibraryListByGlob { glob: spec } => { - let lib = shapelib::libs_lookup(&spec.lib)?; - let results = lib.list_glob(&spec.item)?; + let libs = shapelib::lib_name_lookup(&spec.lib)?; + let mut results: Vec = default(); + for lib in &*libs { + results.extend(lib.list_glob(&spec.item)?); + } MR::LibraryItems(results) } diff --git a/src/bin/otterlib.rs b/src/bin/otterlib.rs index 3b22dbe9..0c97a548 100644 --- a/src/bin/otterlib.rs +++ b/src/bin/otterlib.rs @@ -233,11 +233,12 @@ fn main() { load_global_libs(&vec![tlibs.clone()])?; } let mut items: Vec = default(); - for lib in libs_list() { - let contents = libs_lookup(&lib)?; - for pat in opts.items.split(SPLIT) { - for item in contents.list_glob(pat)? { - items.push((lib.clone(), item)) + for lib in lib_name_list() { + for contents in &*lib_name_lookup(&lib)? { + for pat in opts.items.split(SPLIT) { + for item in contents.list_glob(pat)? { + items.push((lib.clone(), item)) + } } } } diff --git a/src/shapelib.rs b/src/shapelib.rs index f115bdbc..9d240b37 100644 --- a/src/shapelib.rs +++ b/src/shapelib.rs @@ -17,7 +17,7 @@ use parking_lot::{MappedRwLockReadGuard, RwLockReadGuard}; #[derive(Default)] pub struct Registry { - libs: HashMap, + libs: HashMap>, } #[derive(Debug)] @@ -347,7 +347,7 @@ impl OccultedPieceTrait for Item { static SHAPELIBS: RwLock> = const_rwlock(None); -pub fn libs_list() -> Vec { +pub fn lib_name_list() -> Vec { let reg = SHAPELIBS.read(); reg.as_ref().map( |reg| reg.libs.keys().cloned().collect() @@ -355,12 +355,13 @@ pub fn libs_list() -> Vec { } #[throws(SpecError)] -pub fn libs_lookup(libname: &str) - -> MappedRwLockReadGuard<'static, Contents> { +pub fn lib_name_lookup(libname: &str) + -> MappedRwLockReadGuard<'static, [Contents]> { let reg = SHAPELIBS.read(); RwLockReadGuard::try_map( reg, |reg: &Option| -> Option<_> { (|| Some({ reg.as_ref()?.libs.get(libname)? + .as_slice() }))() }) .map_err(|_| SpE::LibraryNotFound) @@ -382,8 +383,10 @@ impl From for PieceSpecLoaded { impl ItemSpec { #[throws(SpecError)] pub fn find_load(&self, ig: &Instance, depth: SpecDepth) -> ItemSpecLoaded { - let lib = libs_lookup(&self.lib)?; - let (item, idata) = lib.items.get_key_value(self.item.as_str()) + let libs = lib_name_lookup(&self.lib)?; + let (lib, (item, idata)) = libs.iter().rev().find_map( + |lib| Some((lib, lib.items.get_key_value(self.item.as_str())?)) + ) .ok_or(SpE::LibraryItemNotFound(self.clone()))?; lib.load1(idata, &self.lib, item.unnest::(), ig, depth)? } @@ -784,7 +787,8 @@ pub fn load_1_library(l: &Explicit1) { SHAPELIBS.write() .get_or_insert_with(default) .libs - .insert(l.name.clone(), data); + .entry(l.name.clone()).or_default() + .push(data); info!("loaded {} shapes in library {:?} from {:?} and {:?}", count, &l.name, &l.catalogue, &l.dirname); } -- 2.30.2