Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: add merge feature #155

Closed
wants to merge 1 commit into from
Closed

feat: add merge feature #155

wants to merge 1 commit into from

Conversation

louib
Copy link
Collaborator

@louib louib commented Mar 15, 2023

This is a very early version of a merge feature. KeePassXC defines 5 merge modes, but I don't think we need all the 5 modes in the first version of this feature, if ever

TODOs

  • handle location update
  • handle deleted objects
  • handle group updates
  • move the feature behind a feature flags

@louib louib changed the title feat: add a basic merge function feat: add merge feature Mar 15, 2023
src/db/entry.rs Outdated Show resolved Hide resolved
@louib louib force-pushed the add_merge_feature branch 3 times, most recently from 7753a02 to 141a21c Compare March 18, 2023 19:41
Copy link

@phoerious phoerious left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I haven't checked the correctness of what this is doing yet, but I think the code can be streamlined quite a lot.

src/db/entry.rs Outdated
@@ -41,6 +41,44 @@ impl Entry {
..Default::default()
}
}

pub(crate) fn merge(&self, other: &Entry) -> Entry {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You are using unwrap() so often, this method should return a Result type instead.

src/db/entry.rs Outdated
if destination_modification_time > source_modification_time {
response = self.clone();
// TODO we could just return if the other entry doesn't have a history.
let mut source_history = other.history.clone().unwrap_or(History::default());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See comment above, then

Suggested change
let mut source_history = other.history.clone().unwrap_or(History::default());
let mut source_history = other.history.clone().ok_or("No history")?;

src/db/entry.rs Outdated
// TODO we could just return if the other entry doesn't have a history.
let mut source_history = other.history.clone().unwrap_or(History::default());
source_history.add_entry(other.clone());
let mut new_history = self.history.clone().unwrap_or(History::default()).clone();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let mut new_history = self.history.clone().unwrap_or(History::default()).clone();
let mut new_history = self.history.clone().unwrap_or_else(|| History::default()).clone();

src/db/entry.rs Outdated
Comment on lines 68 to 71
let mut destination_history = self.history.clone().unwrap_or(History::default());
destination_history.add_entry(self.clone());
let mut new_history = other.history.clone().unwrap_or(History::default()).clone();

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

See above.


// Determines if the entries of the history are
// ordered by last modification time.
pub(crate) fn is_ordered(&self) -> bool {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should probably be Option<bool>, then you can save a lot of boilerplate in the function body.

Comment on lines +173 to +239
for node in &mut self.children {
if let Node::Entry(e) = node {
response.push(e)
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for node in &mut self.children {
if let Node::Entry(e) = node {
response.push(e)
}
}
self.children.iter_mut().flat_map(|node| {
match node {
Node::Entry(e) => Some(e),
_ => None
}
}).collect()

Comment on lines +183 to +249
for node in &mut self.children {
if let Node::Group(g) = node {
response.push(g);
}
}

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for node in &mut self.children {
if let Node::Group(g) = node {
response.push(g);
}
}
self.children.iter_mut().flat_map(|node| {
match node {
Node::Group(g) => Some(g),
_ => None
}
}).collect()

Comment on lines +207 to +417
for node in &self.children {
match node {
Node::Group(g) => {
if let Some(e) = g.find_entry_by_uuid(id) {
return Some(e);
}
}
Node::Entry(e) => {
if e.uuid == id {
return Some(e);
}
}
}
}
None

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You should probably implement a recursive iterator, so you can do this:

Suggested change
for node in &self.children {
match node {
Node::Group(g) => {
if let Some(e) = g.find_entry_by_uuid(id) {
return Some(e);
}
}
Node::Entry(e) => {
if e.uuid == id {
return Some(e);
}
}
}
}
None
self.iter_recursive()
.flat_map(|c| {
match c {
Node::Entry(e) => Some(e),
_ => None
}
})
.find(|e| e.uuid == id)

}

pub fn find_entry_by_uuid(&self, id: Uuid) -> Option<&Entry> {
for node in &self.children {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
for node in &self.children {
self.children.iter_mut().for_each(|node| {

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure what's the advantage of this proposition. We don't need a mutable reference to the node, and the proposed syntax is more verbose.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The mut was a bit of an oversight. iter() should be sufficient. Overall, the iterator syntax should be preferred. It's cleaner and often faster than the for-loop.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@phoerious

often faster than the for-loop.

Do you have a source for that? I would like to know why that's the case.

It's cleaner

I personally prefer the for syntax, especially in Rust. Notice the absence of parentheses on the line where the for is declared ✨

Copy link

@phoerious phoerious Mar 27, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have a source for that? I would like to know why that's the case.

The iterators are usually as fast, sometimes faster. The difference isn't huge, but I guess it comes mostly from Rust's automatic SIMD vectorization. The same happens for for loops, but I guess they are sometimes not as easy to optimize.

I personally prefer the for syntax, especially in Rust. Notice the absence of parentheses on the line where the for is declared

I would say the functional approach is definitely more idiomatic, especially if you have loops without side effects. This is different in Javascript, where iterators are eagerly executed, and so you loop over your data multiple times if you do filter().map(), but not in Rust. Here I would avoid explicit for loops if possible and also avoid side effects if possible, which for loops tend to invite you to introduce.

To be honest, the for_each() iterator was lazy by me. You could rewrite the whole function like that:

    pub fn find_entry_by_uuid(&self, id: Uuid) -> Option<&Entry> {
        self.children.iter().flat_map(|node| {
            match node {
                Node::Group(g) => g.find_entry_by_uuid(id),
                Node::Entry(e) => Some(e)
            }
        }).find(|entry| entry.uuid == id)
    }

Not only shorter, but also easier to read and understand. You don't come up with this kind of solution if you think in for loops, though.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Another way to write it:

    pub fn find_entry_by_uuid(&self, id: Uuid) -> Option<&Entry> {
        self.children.iter().flat_map(|node| {
            match node {
                Node::Group(g) => g.find_entry_by_uuid(id),
                Node::Entry(e) => if e.uuid == id {
                    Some(e)
                } else {
                    None
                }
            }
        }).next()
    }

It's slightly longer, but avoids comparing uuids multiple times when walking up the recursion chain. Not sure if it makes a huge difference.

Comment on lines +286 to +584
pub(crate) fn get_all_entries(
&self,
current_location: &NodeLocation,
) -> Vec<(&Entry, NodeLocation)> {

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be replaced with a recursive iterator implementation as suggested above.

@phoerious
Copy link

phoerious commented Mar 20, 2023

I read through the code a bit more. From what I understood, the NodeIter iterator is already recursive, but the traversal is breadth-first. By using a Vec<NodeRef<'a>> instead of a VecDeque<NodeRef<'a'>> as the internal traversal data structure, this could be changed to depth-first easily. Perhaps this could be extended to also include the full path.

@louib
Copy link
Collaborator Author

louib commented Dec 29, 2023

Superseded by #201

@louib louib closed this Dec 29, 2023
@louib louib mentioned this pull request Dec 29, 2023
3 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants