chiark / gitweb /
Revert "Revert "libraries: Prepare for multiple libraries with the same name""
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 15 May 2021 10:53:18 +0000 (11:53 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 15 May 2021 10:59:06 +0000 (11:59 +0100)
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 <ijackson@chiark.greenend.org.uk>
daemon/cmdlistener.rs
src/bin/otterlib.rs
src/shapelib.rs

index 4b5bb8a43ec3be902bb3bad690d5a1fe5791d80b..fce4a8abda5111b6bb44178152abd726c3127d53 100644 (file)
@@ -311,8 +311,11 @@ fn execute_and_respond<R,W>(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<shapelib::ItemEnquiryData> = default();
+      for lib in &*libs {
+        results.extend(lib.list_glob(&spec.item)?);
+      }
       MR::LibraryItems(results)
     }
 
index 3b22dbe906487fe1c087584fdb645c8b90f67785..0c97a54891a741684de5a7445226a7aa27f9e719 100644 (file)
@@ -233,11 +233,12 @@ fn main() {
     load_global_libs(&vec![tlibs.clone()])?;
   }
   let mut items: Vec<ItemForOutput> = 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))
+        }
       }
     }
   }
index f115bdbc5866d0e5ecd98b960ff23fa00241218e..9d240b37f1f471847fab65938eaaac1416b42ba7 100644 (file)
@@ -17,7 +17,7 @@ use parking_lot::{MappedRwLockReadGuard, RwLockReadGuard};
 
 #[derive(Default)]
 pub struct Registry {
-  libs: HashMap<String, shapelib::Contents>,
+  libs: HashMap<String, Vec<shapelib::Contents>>,
 }
 
 #[derive(Debug)]
@@ -347,7 +347,7 @@ impl OccultedPieceTrait for Item {
 
 static SHAPELIBS: RwLock<Option<Registry>> = const_rwlock(None);
 
-pub fn libs_list() -> Vec<String> {
+pub fn lib_name_list() -> Vec<String> {
   let reg = SHAPELIBS.read();
   reg.as_ref().map(
     |reg| reg.libs.keys().cloned().collect()
@@ -355,12 +355,13 @@ pub fn libs_list() -> Vec<String> {
 }
 
 #[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<Registry>| -> Option<_> {
     (|| Some({
       reg.as_ref()?.libs.get(libname)?
+        .as_slice()
     }))()
   })
     .map_err(|_| SpE::LibraryNotFound)
@@ -382,8 +383,10 @@ impl From<ItemSpecLoaded> 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::<str>(), 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);
 }