Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

Commit

Permalink
chunking: Factor out helper to make Chunk directly
Browse files Browse the repository at this point in the history
The inital `Chunking` only holds one chunk, so make a helper
to do that.
  • Loading branch information
cgwalters committed Jun 19, 2024
1 parent dfcea4a commit e4bf8c7
Showing 1 changed file with 31 additions and 20 deletions.
51 changes: 31 additions & 20 deletions lib/src/chunking.rs
Original file line number Diff line number Diff line change
Expand Up @@ -215,25 +215,8 @@ impl Chunk {
}
}

fn move_obj(&mut self, dest: &mut Self, checksum: &str) -> bool {
// In most cases, we expect the object to exist in the source. However, it's
// conveneient here to simply ignore objects which were already moved into
// a chunk.
if let Some((name, (size, paths))) = self.content.remove_entry(checksum) {
let v = dest.content.insert(name, (size, paths));
debug_assert!(v.is_none());
self.size -= size;
dest.size += size;
true
} else {
false
}
}
}

impl Chunking {
/// Generate an initial single chunk.
pub fn new(repo: &ostree::Repo, rev: &str) -> Result<Self> {
/// Generate an initial single chunk, returning it and its metadata size.
pub fn new_from_commit_with_size(repo: &ostree::Repo, rev: &str) -> Result<(Self, u64)> {
// Find the target commit
let rev = repo.require_rev(rev)?;

Expand All @@ -260,8 +243,36 @@ impl Chunking {

generate_chunking_recurse(repo, &mut gen, &mut chunk, &contents_v)?;

Ok((chunk, gen.metadata_size))
}

/// Generate an initial single chunk.
pub fn new_from_commit(repo: &ostree::Repo, rev: &str) -> Result<Self> {
Self::new_from_commit_with_size(repo, rev).map(|r| r.0)
}

fn move_obj(&mut self, dest: &mut Self, checksum: &str) -> bool {
// In most cases, we expect the object to exist in the source. However, it's
// conveneient here to simply ignore objects which were already moved into
// a chunk.
if let Some((name, (size, paths))) = self.content.remove_entry(checksum) {
let v = dest.content.insert(name, (size, paths));
debug_assert!(v.is_none());
self.size -= size;
dest.size += size;
true
} else {
false
}
}
}

impl Chunking {
/// Generate an initial single chunk.
pub fn new(repo: &ostree::Repo, rev: &str) -> Result<Self> {
let (chunk, metadata_size) = Chunk::new_from_commit_with_size(repo, rev)?;
let chunking = Chunking {
metadata_size: gen.metadata_size,
metadata_size: metadata_size,
remainder: chunk,
..Default::default()
};
Expand Down

0 comments on commit e4bf8c7

Please sign in to comment.