[pbs-devel] [RFC PATCH proxmox v2 13/15] section-config: add method to retrieve case-insensitive entries

Christoph Heiss c.heiss at proxmox.com
Wed Aug 16 16:47:43 CEST 2023


Add a new `SectionConfigData::lookup_icase()` method, to lookup	sections
which - after casefolding - might have the same names. Returned as a
list, the caller has to take take responsibility how to handle such
cases.

To have the above a) with no impact on existing code-paths and b) still
have reasonable runtime in the worse case, use a separate hashmap for the
casefolded section ids, which only contains the names of the
non-case-folded.

Signed-off-by: Christoph Heiss <c.heiss at proxmox.com>
---
Biggest concern might be the increased memory usage - approximate
doubles the space needed for section names.

Changes v1 -> v2:
  * New patch

 proxmox-section-config/Cargo.toml |   3 +
 proxmox-section-config/src/lib.rs | 115 +++++++++++++++++++++++++++++-
 2 files changed, 117 insertions(+), 1 deletion(-)

diff --git a/proxmox-section-config/Cargo.toml b/proxmox-section-config/Cargo.toml
index 4da63f3..2c5b2b3 100644
--- a/proxmox-section-config/Cargo.toml
+++ b/proxmox-section-config/Cargo.toml
@@ -18,3 +18,6 @@ serde_json.workspace = true
 proxmox-schema.workspace = true
 # FIXME: remove!
 proxmox-lang.workspace = true
+
+[dev-dependencies]
+serde = { workspace = true, features = [ "derive" ] }
diff --git a/proxmox-section-config/src/lib.rs b/proxmox-section-config/src/lib.rs
index 4441df1..96d824f 100644
--- a/proxmox-section-config/src/lib.rs
+++ b/proxmox-section-config/src/lib.rs
@@ -104,6 +104,7 @@ enum ParseState<'a> {
 #[derive(Debug, Clone)]
 pub struct SectionConfigData {
     pub sections: HashMap<String, (String, Value)>,
+    sections_lowercase: HashMap<String, Vec<String>>,
     pub order: Vec<String>,
 }

@@ -118,6 +119,7 @@ impl SectionConfigData {
     pub fn new() -> Self {
         Self {
             sections: HashMap::new(),
+            sections_lowercase: HashMap::new(),
             order: Vec::new(),
         }
     }
@@ -135,6 +137,15 @@ impl SectionConfigData {
         let json = serde_json::to_value(config)?;
         self.sections
             .insert(section_id.to_string(), (type_name.to_string(), json));
+
+        let section_id_lc = section_id.to_lowercase();
+        if let Some(entry) = self.sections_lowercase.get_mut(&section_id_lc) {
+            entry.push(section_id.to_owned());
+        } else {
+            self.sections_lowercase
+                .insert(section_id_lc, vec![section_id.to_owned()]);
+        }
+
         Ok(())
     }

@@ -158,13 +169,53 @@ impl SectionConfigData {
         }
     }

-    /// Lookup section data as native rust data type.
+    pub fn lookup_json_icase(&self, type_name: &str, id: &str) -> Result<Vec<Value>, Error> {
+        let sections = self
+            .sections_lowercase
+            .get(&id.to_lowercase())
+            .ok_or_else(|| format_err!("no such {} '{}'", type_name, id))?;
+
+        let mut result = Vec::with_capacity(sections.len());
+        for section in sections {
+            let config = self.lookup_json(type_name, section)?;
+            result.push(config.clone());
+        }
+
+        Ok(result)
+    }
+
+    /// Lookup section data as native rust data type, with the section id being compared
+    /// case-sensitive.
     pub fn lookup<T: DeserializeOwned>(&self, type_name: &str, id: &str) -> Result<T, Error> {
         let config = self.lookup_json(type_name, id)?;
         let data = T::deserialize(config)?;
         Ok(data)
     }

+    /// Lookup section data as native rust data type, with the section id being compared
+    /// case-insensitive.
+    pub fn lookup_icase<T: DeserializeOwned>(
+        &self,
+        type_name: &str,
+        id: &str,
+    ) -> Result<Vec<T>, Error> {
+        let config = self.lookup_json_icase(type_name, id)?;
+        let data = config
+            .iter()
+            .fold(Ok(Vec::with_capacity(config.len())), |mut acc, c| {
+                if let Ok(acc) = &mut acc {
+                    match T::deserialize(c) {
+                        Ok(data) => acc.push(data),
+                        Err(err) => return Err(err),
+                    }
+                }
+
+                acc
+            })?;
+
+        Ok(data)
+    }
+
     /// Record section ordering
     ///
     /// Sections are written in the recorder order.
@@ -911,6 +962,68 @@ group: mygroup
     println!("CONFIG:\n{}", raw.unwrap());
 }

+#[test]
+fn test_section_config_id_case_sensitivity() {
+    let filename = "user.cfg";
+
+    const ID_SCHEMA: Schema = StringSchema::new("default id schema.")
+        .min_length(3)
+        .schema();
+    let mut config = SectionConfig::new(&ID_SCHEMA);
+
+    #[derive(Debug, PartialEq, Eq, serde::Deserialize)]
+    struct User {}
+
+    const USER_PROPERTIES: ObjectSchema = ObjectSchema::new(
+        "user properties",
+        &[(
+            "userid",
+            true,
+            &StringSchema::new("The id of the user (name at realm).")
+                .min_length(3)
+                .schema(),
+        )],
+    );
+
+    let plugin = SectionConfigPlugin::new(
+        "user".to_string(),
+        Some("userid".to_string()),
+        &USER_PROPERTIES,
+    );
+    config.register_plugin(plugin);
+
+    let raw = r"
+user: root at pam
+
+user: ambiguous at pam
+
+user: AMBIguous at pam
+";
+
+    let res = config.parse(filename, raw).unwrap();
+
+    assert_eq!(
+        res.lookup::<User>("user", "root at pam")
+            .map_err(|err| format!("{err}")),
+        Ok(User {})
+    );
+    assert_eq!(
+        res.lookup::<User>("user", "ambiguous at pam")
+            .map_err(|err| format!("{err}")),
+        Ok(User {})
+    );
+    assert_eq!(
+        res.lookup::<User>("user", "AMBIguous at pam")
+            .map_err(|err| format!("{err}")),
+        Ok(User {})
+    );
+    assert_eq!(
+        res.lookup_icase::<User>("user", "ambiguous at pam")
+            .map_err(|err| format!("{err}")),
+        Ok(vec![User {}, User {}])
+    );
+}
+
 #[test]
 fn test_section_config_with_all_of_schema() {
     let filename = "storage.cfg";
--
2.41.0






More information about the pbs-devel mailing list