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

Index::Tree::File #55

Draft
wants to merge 10 commits into
base: master
Choose a base branch
from
Draft

Conversation

danieleades
Copy link
Contributor

@Hirevo
this is a first draft of what it might look like to refactor the tree into a 'File' object (for managing specific index files)

this is where it gets a bit murky, as without any tests, the intended semantics are slightly opaque.

also there's a few design decisions in there i'd love to chat about. using a BTreeMap instead of a vector for storing crate records, for example.

@codecov
Copy link

codecov bot commented Apr 16, 2020

Codecov Report

Merging #55 into master will increase coverage by 0.09%.
The diff coverage is 1.25%.

Impacted file tree graph

@@            Coverage Diff            @@
##           master     #55      +/-   ##
=========================================
+ Coverage    0.00%   0.09%   +0.09%     
=========================================
  Files          50      50              
  Lines        1988    2028      +40     
=========================================
+ Hits            0       2       +2     
- Misses       1988    2026      +38     
Impacted Files Coverage Δ
src/api/unyank.rs 0.00% <0.00%> (ø)
src/api/yank.rs 0.00% <0.00%> (ø)
src/config/mod.rs 0.00% <0.00%> (ø)
src/index/cli.rs 0.00% <0.00%> (ø)
src/index/models.rs 0.00% <0.00%> (ø)
src/index/repository/cli.rs 0.00% <0.00%> (ø)
src/index/repository/git2.rs 0.00% <0.00%> (ø)
src/index/repository/mod.rs 0.00% <0.00%> (ø)
src/index/tree.rs 0.00% <0.00%> (ø)
src/index/mod.rs 6.66% <8.33%> (+6.66%) ⬆️
... and 4 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update ff77d0c...e6fa0f2. Read the comment docs.

Copy link
Owner

@Hirevo Hirevo left a comment

Choose a reason for hiding this comment

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

This is a very neat abstraction, not having to manually deal with the filesystem and the JSON (de)serialization, just to make a quick change (like yank a crate) is a great step forward.
I'd say using a map is indeed the better choice here than a Vec because accessing records based on the version is quite frequent in our case (more than sequentially iterating them).
According to the documentation page about when to use BTreeMap, it seems the better choice, since we tend to access the latest version the most, currently.
So, yeah, I would agree about using a BTreeMap here.

.collect::<Result<Vec<String>, json::Error>>()
.unwrap(); // we unwrap because any error here is a logic bug in the implementation

let text = lines.join("\n");
Copy link
Owner

Choose a reason for hiding this comment

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

I have a doubt about whether we need to also add a new line at the very end of the file.
In the previous version, there was an additional writeln!(file); to add this final newline.
You're right that not having tests makes this hard to validate.
End-to-end tests are coming very soon (I think I have a good model on how to do them and implementation work has begun), but if it doesn't land when this PR is ready to be merged, we'll have to manually test that.
(That review comment should remind me of that.)

let path = self.compute_record_path(name);
File::create(path)
}

pub fn match_record(&self, name: &str, req: VersionReq) -> Result<CrateVersion, Error> {
Copy link
Owner

Choose a reason for hiding this comment

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

This could also be implemented with File:

self.file(name)
    .records()
    .iter()
    .filter(|(vers, _)| req.matches(vers))
    .max_by(|(v1, _), (v2, _)| v1.cmp(v2))
    .map(|(_, record)| record.clone())
    .ok_or_else(|| AlexError::CrateNotFound {
        name: String::from(name),
    })

Copy link
Contributor Author

Choose a reason for hiding this comment

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

i'll take a look at this. my inclination would be to push this down to the File. So the tree would be responsible for checking the name exists, and the file for doing the version matching.

also it seems like a mistake to clone in the method. better to return the reference, and let the caller clone it (if they need to)

Copy link
Contributor Author

Choose a reason for hiding this comment

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

quick question- what are you using the match record method for?

I had considered this 'dependency resolution' task to be Cargo's job

@Hirevo Hirevo added the C-refactor Category: Refactor label Apr 25, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
C-refactor Category: Refactor
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants