Skip to content

Commit

Permalink
pass threadlimit down from CLIs
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Jul 13, 2020
1 parent 7c5d8b8 commit f98c5b1
Show file tree
Hide file tree
Showing 3 changed files with 54 additions and 19 deletions.
57 changes: 38 additions & 19 deletions gitoxide-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,26 +39,64 @@ impl FromStr for OutputFormat {
}
}

/// A general purpose context for many operations provided here
pub struct Context<W1: io::Write, W2: io::Write> {
/// If set, provide statistics to `out` in the given format
pub output_statistics: Option<OutputFormat>,
/// A stream to which to output operation results
pub out: W1,
/// A stream to which to errors
pub err: W2,
/// If set, don't use more than this amount of threads.
/// Otherwise, usually use as many threads as there are logical cores.
/// A value of 0 is interpreted as no-limit
pub thread_limit: Option<usize>,
}

impl Default for Context<Vec<u8>, Vec<u8>> {
fn default() -> Self {
Context {
output_statistics: None,
thread_limit: None,
out: Vec::new(),
err: Vec::new(),
}
}
}

pub fn init() -> Result<()> {
git_repository::init::repository().with_context(|| "Repository initialization failed")
}

enum EitherCache {
Left(pack::cache::DecodeEntryNoop),
Right(pack::cache::DecodeEntryLRU),
}

impl pack::cache::DecodeEntry for EitherCache {
fn put(&mut self, offset: u64, data: &[u8], kind: Kind, compressed_size: usize) {
match self {
EitherCache::Left(v) => v.put(offset, data, kind, compressed_size),
EitherCache::Right(v) => v.put(offset, data, kind, compressed_size),
}
}

fn get(&mut self, offset: u64, out: &mut Vec<u8>) -> Option<(Kind, usize)> {
match self {
EitherCache::Left(v) => v.get(offset, out),
EitherCache::Right(v) => v.get(offset, out),
}
}
}

pub fn verify_pack_or_pack_index<P, W1, W2>(
path: impl AsRef<Path>,
progress: Option<P>,
Context {
mut out,
mut err,
output_statistics,
thread_limit,
}: Context<W1, W2>,
) -> Result<(git_object::Id, Option<index::PackFileChecksumResult>)>
where
Expand Down Expand Up @@ -94,25 +132,6 @@ where
Err(e)
})
.ok();
enum EitherCache {
Left(pack::cache::DecodeEntryNoop),
Right(pack::cache::DecodeEntryLRU),
};
impl pack::cache::DecodeEntry for EitherCache {
fn put(&mut self, offset: u64, data: &[u8], kind: Kind, compressed_size: usize) {
match self {
EitherCache::Left(v) => v.put(offset, data, kind, compressed_size),
EitherCache::Right(v) => v.put(offset, data, kind, compressed_size),
}
}

fn get(&mut self, offset: u64, out: &mut Vec<u8>) -> Option<(Kind, usize)> {
match self {
EitherCache::Left(v) => v.get(offset, out),
EitherCache::Right(v) => v.get(offset, out),
}
}
}
idx.verify_checksum_of_index(pack.as_ref(), progress, || -> EitherCache {
if output_statistics.is_some() {
// turn off acceleration as we need to see entire chains all the time
Expand Down
8 changes: 8 additions & 0 deletions src/plumbing/lean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ mod options {
/// print the program version.
pub version: bool,

#[argh(option, short = 't')]
/// the amount of threads to use for some operations.
///
/// If unset, or the value is 0, there is no limit and all logical cores can be used.
pub threads: Option<usize>,

#[argh(subcommand)]
pub subcommand: SubCommands,
}
Expand Down Expand Up @@ -69,6 +75,7 @@ fn prepare(verbose: bool, name: &str) -> (Option<prodash::line::JoinHandle>, Opt
pub fn main() -> Result<()> {
pub use options::*;
let cli: Args = crate::shared::from_env();
let thread_limit = cli.threads;
match cli.subcommand {
SubCommands::VerifyPack(VerifyPack {
path,
Expand All @@ -85,6 +92,7 @@ pub fn main() -> Result<()> {
} else {
None
},
thread_limit,
out: stdout(),
err: stderr(),
},
Expand Down
8 changes: 8 additions & 0 deletions src/plumbing/pretty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,12 @@ mod options {
#[structopt(name = "gio-plumbing", about = "The git underworld")]
#[structopt(settings = &[AppSettings::SubcommandRequired, AppSettings::ColoredHelp])]
pub struct Args {
#[structopt(long, short = "t")]
/// The amount of threads to use for some operations.
///
/// If unset, or the value is 0, there is no limit and all logical cores can be used.
pub threads: Option<usize>,

#[structopt(subcommand)]
pub cmd: Subcommands,
}
Expand Down Expand Up @@ -147,6 +153,7 @@ fn prepare_and_run<T: Send + 'static>(

pub fn main() -> Result<()> {
let args = Args::from_args();
let thread_limit = args.threads;
match args.cmd {
Subcommands::VerifyPack {
path,
Expand All @@ -165,6 +172,7 @@ pub fn main() -> Result<()> {
path,
progress,
core::Context {
thread_limit,
output_statistics: if statistics { Some(format) } else { None },
out,
err,
Expand Down

0 comments on commit f98c5b1

Please sign in to comment.