marvinlanhenke commented on code in PR #386:
URL: https://github.com/apache/iceberg-rust/pull/386#discussion_r1621824738
##########
crates/iceberg/src/spec/values.rs:
##########
@@ -713,8 +866,117 @@ impl Datum {
}
}
+/// Map is a collection of key-value pairs with a key type and a value type.
+/// It used in Literal::Map, to make it hashable, the order of key-value pairs
is stored in a separate vector
+/// so that we can hash the map in a deterministic way. But it also means that
the order of key-value pairs is matter
+/// for the hash value.
+#[derive(Clone, Debug, PartialEq, Eq)]
+pub struct Map {
+ index: HashMap<Literal, usize>,
+ pair: Vec<(Literal, Option<Literal>)>,
+}
+
+impl Map {
+ /// Creates a new empty map.
+ pub fn new() -> Self {
+ Self {
+ index: HashMap::new(),
+ pair: Vec::new(),
+ }
+ }
+
+ /// Return the number of key-value pairs in the map.
+ pub fn len(&self) -> usize {
+ self.pair.len()
+ }
+
+ /// Returns true if the map contains no elements.
+ pub fn is_empty(&self) -> bool {
+ self.pair.is_empty()
+ }
+
+ /// Inserts a key-value pair into the map.
+ /// If the map did not have this key present, None is returned.
+ /// If the map did have this key present, the value is updated, and the
old value is returned.
+ pub fn insert(&mut self, key: Literal, value: Option<Literal>) ->
Option<Option<Literal>> {
+ if let Some(index) = self.index.get(&key) {
+ let old_value = std::mem::replace(&mut self.pair[*index].1, value);
+ Some(old_value)
+ } else {
+ self.pair.push((key.clone(), value));
+ self.index.insert(key, self.pair.len() - 1);
+ None
+ }
+ }
+
+ /// Returns a reference to the value corresponding to the key.
+ /// If the key is not present in the map, None is returned.
+ pub fn get(&self, key: &Literal) -> Option<&Option<Literal>> {
+ self.index.get(key).map(|index| &self.pair[*index].1)
+ }
+
+ /// The order of map is matter, so this method used to compare two maps
has same key-value pairs without considering the order.
+ pub fn has_same_content(&self, other: &Map) -> bool {
+ if self.len() != other.len() {
+ return false;
+ }
+
+ for (key, value) in &self.pair {
+ if let Some(other_value) = other.get(key) {
+ if value != other_value {
+ return false;
+ }
+ } else {
+ return false;
+ }
+ }
Review Comment:
minor nit here:
```suggestion
for (key, value) in &self.pair {
match other.get(key) {
Some(other_value) if value == other_value => (),
_ => return false,
}
}
```
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]