Skip to content

Commit

Permalink
Only create index subdirs when necessary
Browse files Browse the repository at this point in the history
  • Loading branch information
sourcefrog committed Feb 7, 2020
1 parent 6fcab6e commit 2f00734
Showing 1 changed file with 7 additions and 5 deletions.
12 changes: 7 additions & 5 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ use super::*;

pub const MAX_ENTRIES_PER_HUNK: usize = 1000;

pub const HUNKS_PER_SUBDIR: u32 = 10_000;

/// Description of one archived file.
///
/// This struct is directly encoded/decoded to the json index file, and also can be constructed by
Expand Down Expand Up @@ -172,11 +174,11 @@ impl IndexBuilder {
return Ok(());
}

// TODO: Only make the directory on the first file in that dir.
let path = &path_for_hunk(&self.dir, self.sequence);

ensure_dir_exists(&subdir_for_hunk(&self.dir, self.sequence))
.context(errors::WriteIndex { path })?;
if (self.sequence % HUNKS_PER_SUBDIR) == 0 {
ensure_dir_exists(&subdir_for_hunk(&self.dir, self.sequence))
.context(errors::WriteIndex { path })?;
}

let json = serde_json::to_vec(&self.entries).context(errors::SerializeJson { path })?;
let uncompressed_len = json.len() as u64;
Expand Down Expand Up @@ -204,7 +206,7 @@ impl IndexBuilder {
/// Return the subdirectory for a hunk numbered `hunk_number`.
fn subdir_for_hunk(dir: &Path, hunk_number: u32) -> PathBuf {
let mut buf = dir.to_path_buf();
buf.push(format!("{:05}", hunk_number / 10000));
buf.push(format!("{:05}", hunk_number / HUNKS_PER_SUBDIR));
buf
}

Expand Down

0 comments on commit 2f00734

Please sign in to comment.