-
Notifications
You must be signed in to change notification settings - Fork 212
Trivial consistency check #898
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
Merged
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
ccfec46
Add basic db <-> index consistency check
Nemo157 88188b4
Improve debugging while walking crates
Nemo157 9bb4cfb
Rename CrateId to CrateName
Nemo157 d8fb3fa
Improve name comparison while loading db data
Nemo157 a056129
Concretely type the iterable diffs
Nemo157 306d1ed
Use new Crate::from_slice constructor
Nemo157 d650155
Apply suggestions from code review
Nemo157 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
use crates_index::Crate; | ||
use failure::ResultExt; | ||
|
||
pub(crate) struct Crates { | ||
repo: git2::Repository, | ||
} | ||
|
||
impl Crates { | ||
pub(super) fn new(repo: git2::Repository) -> Self { | ||
Self { repo } | ||
} | ||
|
||
pub(crate) fn walk(&self, mut f: impl FnMut(Crate)) -> Result<(), failure::Error> { | ||
log::debug!("Walking crates in index"); | ||
let tree = self | ||
.repo | ||
.find_commit(self.repo.refname_to_id("refs/remotes/origin/master")?)? | ||
.tree()?; | ||
|
||
let mut result = Ok(()); | ||
|
||
tree.walk(git2::TreeWalkMode::PreOrder, |_, entry| { | ||
result = (|| { | ||
if let Some(blob) = entry.to_object(&self.repo)?.as_blob() { | ||
if let Ok(krate) = Crate::from_slice(blob.content()) { | ||
f(krate); | ||
} else { | ||
log::warn!("not a crate '{}'", entry.name().unwrap()); | ||
} | ||
} | ||
Result::<(), failure::Error>::Ok(()) | ||
})() | ||
.with_context(|_| { | ||
format!( | ||
"loading crate details from '{}'", | ||
entry.name().unwrap_or("<unknown>") | ||
) | ||
}); | ||
match result { | ||
Ok(_) => git2::TreeWalkResult::Ok, | ||
Err(_) => git2::TreeWalkResult::Abort, | ||
} | ||
})?; | ||
|
||
Ok(result?) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use std::{ | ||
cmp::PartialEq, | ||
collections::BTreeMap, | ||
fmt::{self, Debug, Display, Formatter}, | ||
}; | ||
|
||
#[derive(Default, Debug)] | ||
pub(crate) struct Data { | ||
pub(crate) crates: BTreeMap<CrateName, Crate>, | ||
} | ||
|
||
#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Default, Debug)] | ||
pub(crate) struct CrateName(pub(crate) String); | ||
|
||
#[derive(Default, Debug)] | ||
pub(crate) struct Crate { | ||
pub(crate) releases: BTreeMap<Version, Release>, | ||
} | ||
|
||
#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Default, Debug)] | ||
pub(crate) struct Version(pub(crate) String); | ||
|
||
#[derive(Default, Debug)] | ||
pub(crate) struct Release {} | ||
|
||
impl PartialEq<String> for CrateName { | ||
fn eq(&self, other: &String) -> bool { | ||
self.0 == *other | ||
} | ||
} | ||
|
||
impl PartialEq<String> for Version { | ||
fn eq(&self, other: &String) -> bool { | ||
self.0 == *other | ||
} | ||
} | ||
|
||
impl Display for CrateName { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | ||
Display::fmt(&self.0, f) | ||
} | ||
} | ||
|
||
impl Display for Version { | ||
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result { | ||
Display::fmt(&self.0, f) | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use super::data::{Crate, CrateName, Data, Release, Version}; | ||
use std::collections::BTreeMap; | ||
|
||
pub(crate) fn load(conn: &mut postgres::Client) -> Result<Data, failure::Error> { | ||
let rows = conn.query( | ||
" | ||
SELECT | ||
crates.name, | ||
releases.version | ||
FROM crates | ||
INNER JOIN releases ON releases.crate_id = crates.id | ||
ORDER BY crates.id, releases.id | ||
", | ||
&[], | ||
)?; | ||
|
||
let mut data = Data { | ||
crates: BTreeMap::new(), | ||
}; | ||
|
||
let mut rows = rows.iter(); | ||
|
||
struct Current { | ||
name: CrateName, | ||
krate: Crate, | ||
} | ||
|
||
let mut current = if let Some(row) = rows.next() { | ||
Current { | ||
name: CrateName(row.get("name")), | ||
krate: Crate { | ||
releases: { | ||
let mut releases = BTreeMap::new(); | ||
releases.insert(Version(row.get("version")), Release {}); | ||
releases | ||
}, | ||
}, | ||
} | ||
} else { | ||
return Ok(data); | ||
}; | ||
|
||
for row in rows { | ||
let name = row.get("name"); | ||
if current.name != name { | ||
data.crates.insert( | ||
std::mem::replace(&mut current.name, CrateName(name)), | ||
std::mem::take(&mut current.krate), | ||
); | ||
} | ||
current | ||
.krate | ||
.releases | ||
.insert(Version(row.get("version")), Release::default()); | ||
} | ||
|
||
data.crates.insert(current.name, current.krate); | ||
|
||
Ok(data) | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.