use lance_core::deepsize::DeepSizeOf;
#[derive(Debug, Clone, DeepSizeOf, PartialEq)]
pub struct UpdateMapEntry {
pub key: String,
pub value: Option<String>,
}
impl From<(String, Option<String>)> for UpdateMapEntry {
fn from((key, value): (String, Option<String>)) -> Self {
Self { key, value }
}
}
impl From<(String, String)> for UpdateMapEntry {
fn from((key, value): (String, String)) -> Self {
Self::from((key, Some(value)))
}
}
impl From<(&str, Option<&str>)> for UpdateMapEntry {
fn from((key, value): (&str, Option<&str>)) -> Self {
Self {
key: key.to_string(),
value: value.map(str::to_owned),
}
}
}
impl From<(&str, &str)> for UpdateMapEntry {
fn from((key, value): (&str, &str)) -> Self {
Self::from((key, Some(value)))
}
}
#[derive(Debug, Clone, DeepSizeOf, PartialEq)]
pub struct UpdateMap {
pub update_entries: Vec<UpdateMapEntry>,
pub replace: bool,
}
pub(super) fn apply_update_map(
target: &mut std::collections::HashMap<String, String>,
update_map: &UpdateMap,
) {
if update_map.replace {
target.clear();
for entry in &update_map.update_entries {
if let Some(value) = &entry.value {
target.insert(entry.key.clone(), value.clone());
}
}
} else {
for entry in &update_map.update_entries {
if let Some(value) = &entry.value {
target.insert(entry.key.clone(), value.clone());
} else {
target.remove(&entry.key);
}
}
}
}
pub fn translate_config_updates(
upsert_values: &std::collections::HashMap<String, String>,
delete_keys: &[String],
) -> UpdateMap {
let mut update_entries = Vec::new();
for (key, value) in upsert_values {
update_entries.push(UpdateMapEntry {
key: key.clone(),
value: Some(value.clone()),
});
}
for key in delete_keys {
update_entries.push(UpdateMapEntry {
key: key.clone(),
value: None,
});
}
UpdateMap {
update_entries,
replace: false, }
}
pub fn translate_schema_metadata_updates(
schema_metadata: &std::collections::HashMap<String, String>,
) -> UpdateMap {
let update_entries = schema_metadata
.iter()
.map(|(key, value)| UpdateMapEntry {
key: key.clone(),
value: Some(value.clone()),
})
.collect();
UpdateMap {
update_entries,
replace: true, }
}