Skip to content

Commit

Permalink
Add chunk for oids (#279)
Browse files Browse the repository at this point in the history
  • Loading branch information
Byron committed Dec 31, 2021
1 parent 6a68ed7 commit 565a7ae
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 1 deletion.
16 changes: 16 additions & 0 deletions git-pack/src/multi_index/chunk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,11 +144,27 @@ pub mod fanout {

/// Information about the oid lookup table.
pub mod lookup {
use crate::multi_index;
use std::ops::Range;

/// The id uniquely identifying the oid lookup table.
pub const ID: git_chunk::Id = *b"OIDL";

/// Return the amount of bytes needed to store the data on disk for the given amount of `entries`
pub fn storage_size(entries: usize, object_hash: git_hash::Kind) -> u64 {
(entries * object_hash.len_in_bytes()) as u64
}

pub(crate) fn write(
sorted_entries: &[multi_index::write::Entry],
mut out: impl std::io::Write,
) -> std::io::Result<()> {
for entry in sorted_entries {
out.write_all(entry.id.as_slice())?;
}
Ok(())
}

/// Return true if the size of the `offset` range seems to match for a `hash` of the given kind and the amount of objects.
pub fn is_valid(offset: &Range<usize>, hash: git_hash::Kind, num_objects: u32) -> bool {
(offset.end - offset.start) / hash.len_in_bytes() == num_objects as usize
Expand Down
5 changes: 5 additions & 0 deletions git-pack/src/multi_index/write.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,10 @@ impl multi_index::File {
multi_index::chunk::index_names::storage_size(&index_filenames_sorted),
);
cf.plan_chunk(multi_index::chunk::fanout::ID, multi_index::chunk::fanout::SIZE as u64);
cf.plan_chunk(
multi_index::chunk::lookup::ID,
multi_index::chunk::lookup::storage_size(entries.len(), object_hash),
);

let bytes_written = Self::write_header(
&mut out,
Expand All @@ -130,6 +134,7 @@ impl multi_index::File {
multi_index::chunk::index_names::write(&index_filenames_sorted, &mut chunk_write)?
}
multi_index::chunk::fanout::ID => multi_index::chunk::fanout::write(&entries, &mut chunk_write)?,
multi_index::chunk::lookup::ID => multi_index::chunk::lookup::write(&entries, &mut chunk_write)?,
unknown => unreachable!("BUG: forgot to implement chunk {:?}", std::str::from_utf8(&unknown)),
}
}
Expand Down
1 change: 0 additions & 1 deletion git-pack/tests/pack/multi_index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,6 @@ mod write {
use std::sync::atomic::AtomicBool;

#[test]
#[ignore]
fn from_paths() {
let dir = tempfile::TempDir::new().unwrap();
let input_indices = std::fs::read_dir(fixture_path("objects/pack"))
Expand Down

0 comments on commit 565a7ae

Please sign in to comment.