Skip to content

Commit

Permalink
pass average stats through to the top level
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jul 1, 2020
1 parent 4dd9fd4 commit 5b4979c
Show file tree
Hide file tree
Showing 7 changed files with 83 additions and 52 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions git-odb/src/pack/index/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,7 @@ macro_rules! izip {
}

mod file;
mod verify;

pub use file::*;

mod verify;
pub use verify::*;
61 changes: 23 additions & 38 deletions git-odb/src/pack/index/verify.rs
Original file line number Diff line number Diff line change
Expand Up @@ -62,8 +62,8 @@ impl Into<String> for TimeThroughput {

#[derive(Debug, PartialEq, Eq, Hash, Ord, PartialOrd, Clone)]
pub struct PackFileChecksumResult {
average: DecodeEntryResult,
objects_per_chain_length: BTreeMap<u32, u32>,
pub average: DecodeEntryResult,
pub objects_per_chain_length: BTreeMap<u32, u32>,
}

/// Methods to verify and validate the content of the index file
Expand Down Expand Up @@ -138,51 +138,33 @@ impl index::File {
v
};

fn add_decode_result(
DecodeEntryResult {
kind: _,
mut num_deltas,
mut decompressed_size,
mut compressed_size,
mut object_size,
}: &mut DecodeEntryResult,
rhs: DecodeEntryResult,
) {
num_deltas += rhs.num_deltas;
decompressed_size += rhs.decompressed_size;
compressed_size += rhs.compressed_size;
object_size += rhs.object_size;
fn add_decode_result(lhs: &mut DecodeEntryResult, rhs: DecodeEntryResult) {
lhs.num_deltas += rhs.num_deltas;
lhs.decompressed_size += rhs.decompressed_size;
lhs.compressed_size += rhs.compressed_size;
lhs.object_size += rhs.object_size;
}

fn div_decode_result(
DecodeEntryResult {
kind: _,
mut num_deltas,
mut decompressed_size,
mut compressed_size,
mut object_size,
}: &mut DecodeEntryResult,
div: usize,
) {
num_deltas /= div as u32;
decompressed_size /= div as u64;
compressed_size /= div;
object_size /= div as u64;
fn div_decode_result(lhs: &mut DecodeEntryResult, div: usize) {
lhs.num_deltas = (lhs.num_deltas as f32 / div as f32) as u32;
lhs.decompressed_size /= div as u64;
lhs.compressed_size /= div;
lhs.object_size /= div as u64;
}

struct Reducer<'a, P> {
progress: &'a std::sync::Mutex<P>,
entries_seen: u32,
chunks_seen: usize,
average: DecodeEntryResult,
stats: PackFileChecksumResult,
}

impl<'a, P> parallel::Reducer for Reducer<'a, P>
where
P: Progress,
{
type Input = Result<Vec<DecodeEntryResult>, ChecksumError>;
type Output = DecodeEntryResult;
type Output = PackFileChecksumResult;
type Error = ChecksumError;

fn feed(&mut self, input: Self::Input) -> Result<(), Self::Error> {
Expand All @@ -199,16 +181,16 @@ impl index::File {
},
);
div_decode_result(&mut chunk_average, num_entries_in_chunk);
add_decode_result(&mut self.average, chunk_average);
add_decode_result(&mut self.stats.average, chunk_average);

self.progress.lock().unwrap().set(self.entries_seen);
Ok(())
}

fn finalize(mut self) -> Result<Self::Output, Self::Error> {
self.progress.lock().unwrap().done("finished");
div_decode_result(&mut self.average, self.chunks_seen);
Ok(self.average)
div_decode_result(&mut self.stats.average, self.chunks_seen);
Ok(self.stats)
}
}

Expand All @@ -233,7 +215,7 @@ impl index::File {
)
};

in_parallel_if(
let stats = in_parallel_if(
there_are_enough_entries_to_process,
input_chunks,
state_per_thread,
Expand Down Expand Up @@ -312,11 +294,14 @@ impl index::File {
progress: &reduce_progress,
entries_seen: 0,
chunks_seen: 0,
average: DecodeEntryResult::default_from_kind(git_object::Kind::Tree),
stats: PackFileChecksumResult {
average: DecodeEntryResult::default_from_kind(git_object::Kind::Tree),
objects_per_chain_length: Default::default(),
},
},
)?;

Ok((id, None))
Ok((id, Some(stats)))
}
}
}
Expand Down
51 changes: 45 additions & 6 deletions git-odb/tests/pack/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::{
fixture_path, hex_to_id,
pack::{SMALL_PACK, SMALL_PACK_INDEX},
};
use git_odb::pack::{self, index};
use git_odb::pack::{self, index, DecodeEntryResult};
use pretty_assertions::assert_eq;

const INDEX_V2: &str = "packs/pack-11fdfa9e156ab73caae3b6da867192221f2089c2.idx";
Expand Down Expand Up @@ -104,10 +104,49 @@ mod method {
use git_features::progress::Discard;
#[test]
fn pack_lookup() {
for (index_path, pack_path) in &[
(INDEX_V2, PACK_FOR_INDEX_V2),
(INDEX_V1, PACK_FOR_INDEX_V1),
(SMALL_PACK_INDEX, SMALL_PACK),
for (index_path, pack_path, stats) in &[
(
INDEX_V2,
PACK_FOR_INDEX_V2,
index::PackFileChecksumResult {
average: DecodeEntryResult {
kind: git_object::Kind::Tree,
num_deltas: 0,
decompressed_size: 3456,
compressed_size: 1725,
object_size: 9621,
},
objects_per_chain_length: Default::default(),
},
),
(
INDEX_V1,
PACK_FOR_INDEX_V1,
index::PackFileChecksumResult {
average: DecodeEntryResult {
kind: git_object::Kind::Tree,
num_deltas: 0,
decompressed_size: 1982,
compressed_size: 729,
object_size: 2093,
},
objects_per_chain_length: Default::default(),
},
),
(
SMALL_PACK_INDEX,
SMALL_PACK,
index::PackFileChecksumResult {
average: DecodeEntryResult {
kind: git_object::Kind::Tree,
num_deltas: 0,
decompressed_size: 118,
compressed_size: 85,
object_size: 293,
},
objects_per_chain_length: Default::default(),
},
),
] {
let idx = index::File::at(&fixture_path(index_path)).unwrap();
let pack = pack::File::at(&fixture_path(pack_path)).unwrap();
Expand All @@ -117,7 +156,7 @@ fn pack_lookup() {
assert_eq!(
idx.verify_checksum_of_index(Some(&pack), Discard.into())
.unwrap(),
(idx.checksum_of_index(), None)
(idx.checksum_of_index(), Some(stats.to_owned()))
);
for idx_entry in idx.iter() {
let pack_entry = pack.entry(idx_entry.pack_offset);
Expand Down
1 change: 1 addition & 0 deletions gitoxide-core/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ test = false

[dependencies]
git-repository = { version = "0.1.0", path = "../git-repository" }
git-object = { version = "0.1.0", path = "../git-object" }
git-odb = { version = "0.1.0", path = "../git-odb" }
git-features = { version = "0.1.0", path = "../git-features" }
anyhow = "1.0.31"
14 changes: 8 additions & 6 deletions gitoxide-core/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use anyhow::{anyhow, Context, Result};
use git_features::progress::Progress;
use git_odb::pack::index;
use std::{io, path::Path};

pub fn init() -> Result<()> {
Expand All @@ -9,9 +10,10 @@ pub fn init() -> Result<()> {
pub fn verify_pack_or_pack_index<P>(
path: impl AsRef<Path>,
progress: Option<P>,
statistics: bool,
mut out: impl io::Write,
mut err: impl io::Write,
) -> Result<()>
) -> Result<(git_object::Id, Option<index::PackFileChecksumResult>)>
where
P: Progress,
<P as Progress>::SubProgress: Send,
Expand All @@ -20,10 +22,10 @@ where
let ext = path.extension()
.and_then(|ext| ext.to_str())
.ok_or_else(|| anyhow!("Cannot determine file type on path without extension '{}', expecting default extensions 'idx' and 'pack'", path.display()))?;
match ext {
let res = match ext {
"pack" => {
let pack = git_odb::pack::File::at(path).with_context(|| "Could not open pack file")?;
pack.verify_checksum()?;
pack.verify_checksum().map(|id| (id, None))?
}
"idx" => {
let idx = git_odb::pack::index::File::at(path)
Expand All @@ -35,15 +37,15 @@ where
Err(e)
})
.ok();
idx.verify_checksum_of_index(pack.as_ref(), progress)?;
idx.verify_checksum_of_index(pack.as_ref(), progress)?
}
ext => {
return Err(anyhow!(
"Unknown extension {:?}, expecting 'idx' or 'pack'",
ext
))
}
}
};
writeln!(out, "OK")?;
Ok(())
Ok(res)
}
2 changes: 2 additions & 0 deletions src/plumbing/lean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,11 @@ pub fn main() -> Result<()> {
core::verify_pack_or_pack_index(
path,
progress::Log::new("verify-pack").into(),
statistics,
stdout(),
stderr(),
)
.map(|_| ())
}
}
}

0 comments on commit 5b4979c

Please sign in to comment.