-
-
Notifications
You must be signed in to change notification settings - Fork 488
gix,gix-odb,gix-pack: stream blob contents from the ODB #2526
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Malcom Gilbert (mjgil)
wants to merge
6
commits into
GitoxideLabs:main
Choose a base branch
from
mjgil:pr-1595
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
f918257
gix,gix-odb,gix-pack: stream blob contents from the ODB
mjgil 84931e7
Harden blob streaming validation and add memory proof
mjgil 36495db
Fix blob stream loose-store test imports
mjgil 20eaed2
Relax truncated loose-stream test error kind
mjgil 41b5fb7
Fix loose stream truncation test setup
mjgil 4b7bf16
Fix Unix permissions in loose stream test
mjgil File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -57,6 +57,7 @@ pub mod find; | |
| pub mod prefix; | ||
|
|
||
| mod header; | ||
| mod stream; | ||
|
|
||
| /// | ||
| pub mod iter; | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,230 @@ | ||
| use std::{io::Seek, ops::Deref}; | ||
|
|
||
| use gix_features::zlib; | ||
| use gix_hash::oid; | ||
| use gix_pack::cache::DecodeEntry; | ||
|
|
||
| use super::find::Error; | ||
| use crate::store::{find::error::DeltaBaseRecursion, handle, load_index}; | ||
|
|
||
| impl<S> super::Handle<S> | ||
| where | ||
| S: Deref<Target = super::Store> + Clone, | ||
| { | ||
| pub(crate) fn try_find_stream_inner<'b>( | ||
| &'b self, | ||
| mut id: &'b gix_hash::oid, | ||
| inflate: &mut zlib::Inflate, | ||
| pack_cache: &mut dyn DecodeEntry, | ||
| snapshot: &mut load_index::Snapshot, | ||
| recursion: Option<DeltaBaseRecursion<'_>>, | ||
| ) -> Result<Option<(crate::find::Stream, Option<gix_pack::data::entry::Location>)>, Error> { | ||
| if let Some(r) = recursion { | ||
| if r.depth >= self.max_recursion_depth { | ||
| return Err(Error::DeltaBaseRecursionLimit { | ||
| max_depth: self.max_recursion_depth, | ||
| id: r.original_id.to_owned(), | ||
| }); | ||
| } | ||
| } else if !self.ignore_replacements { | ||
| if let Ok(pos) = self | ||
| .store | ||
| .replacements | ||
| .binary_search_by(|(map_this, _)| map_this.as_ref().cmp(id)) | ||
| { | ||
| id = self.store.replacements[pos].1.as_ref(); | ||
| } | ||
| } | ||
|
|
||
| 'outer: loop { | ||
| { | ||
| let marker = snapshot.marker; | ||
| for (idx, index) in snapshot.indices.iter_mut().enumerate() { | ||
| if let Some(handle::index_lookup::Outcome { | ||
| object_index: handle::IndexForObjectInPack { pack_id, pack_offset }, | ||
| index_file, | ||
| pack: possibly_pack, | ||
| }) = index.lookup(id) | ||
| { | ||
| let pack = match possibly_pack { | ||
| Some(pack) => pack, | ||
| None => match self.store.load_pack(pack_id, marker)? { | ||
| Some(pack) => { | ||
| *possibly_pack = Some(pack); | ||
| possibly_pack.as_deref().expect("just put it in") | ||
| } | ||
| None => match self.store.load_one_index(self.refresh, snapshot.marker)? { | ||
| Some(new_snapshot) => { | ||
| *snapshot = new_snapshot; | ||
| self.clear_cache(); | ||
| continue 'outer; | ||
| } | ||
| None => return Ok(None), | ||
| }, | ||
| }, | ||
| }; | ||
| let resolved_pack_id = pack.id; | ||
| let entry = pack.entry(pack_offset)?; | ||
| let header_size = entry.header_size(); | ||
| let result = { | ||
| let mut scratch = Vec::new(); | ||
| let mut temp = tempfile::tempfile()?; | ||
| let result = match pack.decode_entry_to_write( | ||
| entry, | ||
| &mut scratch, | ||
| inflate, | ||
| &mut temp, | ||
| &|id, _out| { | ||
| index_file.pack_offset_by_id(id).and_then(|pack_offset| { | ||
| pack.entry(pack_offset) | ||
| .ok() | ||
| .map(gix_pack::data::decode::entry::ResolvedBase::InPack) | ||
| }) | ||
| }, | ||
| pack_cache, | ||
| ) { | ||
| Ok(outcome) => Ok((outcome, temp)), | ||
| Err(gix_pack::data::decode::Error::DeltaBaseUnresolved(base_id)) => { | ||
| let mut buf = Vec::new(); | ||
| let obj_kind = self | ||
| .try_find_cached_inner( | ||
| &base_id, | ||
| &mut buf, | ||
| inflate, | ||
| pack_cache, | ||
| snapshot, | ||
| recursion | ||
| .map(DeltaBaseRecursion::inc_depth) | ||
| .or_else(|| DeltaBaseRecursion::new(id).into()), | ||
| ) | ||
| .map_err(|err| Error::DeltaBaseLookup { | ||
| err: Box::new(err), | ||
| base_id, | ||
| id: id.to_owned(), | ||
| })? | ||
| .ok_or_else(|| Error::DeltaBaseMissing { | ||
| base_id, | ||
| id: id.to_owned(), | ||
| })? | ||
| .0 | ||
| .kind; | ||
| let handle::index_lookup::Outcome { | ||
| object_index: | ||
| handle::IndexForObjectInPack { | ||
| pack_id: _, | ||
| pack_offset, | ||
| }, | ||
| index_file, | ||
| pack: possibly_pack, | ||
| } = match snapshot.indices[idx].lookup(id) { | ||
| Some(res) => res, | ||
| None => { | ||
| let mut out = None; | ||
| for index in &mut snapshot.indices { | ||
| out = index.lookup(id); | ||
| if out.is_some() { | ||
| break; | ||
| } | ||
| } | ||
|
|
||
| out.unwrap_or_else(|| { | ||
| panic!( | ||
| "could not find object {id} in any index after looking up one of its base objects {base_id}" | ||
| ) | ||
| }) | ||
| } | ||
| }; | ||
| let pack = possibly_pack | ||
| .as_ref() | ||
| .expect("pack to still be available like just now"); | ||
| let entry = pack.entry(pack_offset)?; | ||
| let mut scratch = Vec::new(); | ||
| let mut temp = tempfile::tempfile()?; | ||
| pack.decode_entry_to_write( | ||
| entry, | ||
| &mut scratch, | ||
| inflate, | ||
| &mut temp, | ||
| &|id, out| { | ||
| index_file | ||
| .pack_offset_by_id(id) | ||
| .and_then(|pack_offset| { | ||
| pack.entry(pack_offset) | ||
| .ok() | ||
| .map(gix_pack::data::decode::entry::ResolvedBase::InPack) | ||
| }) | ||
| .or_else(|| { | ||
| (id == base_id).then(|| { | ||
| out.resize(buf.len(), 0); | ||
| out.copy_from_slice(buf.as_slice()); | ||
| gix_pack::data::decode::entry::ResolvedBase::OutOfPack { | ||
| kind: obj_kind, | ||
| end: out.len(), | ||
| } | ||
| }) | ||
| }) | ||
| }, | ||
| pack_cache, | ||
| ) | ||
| .map(|outcome| (outcome, temp)) | ||
| } | ||
| Err(err) => Err(err), | ||
| }?; | ||
| result | ||
| }; | ||
| let (outcome, mut temp) = result; | ||
| temp.rewind()?; | ||
| let res = ( | ||
| crate::find::Stream::from_file(outcome.kind, outcome.object_size, temp), | ||
| Some(gix_pack::data::entry::Location { | ||
| pack_id: resolved_pack_id, | ||
| pack_offset, | ||
| entry_size: outcome.compressed_size + header_size, | ||
| }), | ||
| ); | ||
|
|
||
| if idx != 0 { | ||
| snapshot.indices.swap(0, idx); | ||
| } | ||
| return Ok(Some(res)); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| for lodb in snapshot.loose_dbs.iter() { | ||
| if lodb.contains(id) { | ||
| return lodb | ||
| .try_find_stream(id) | ||
| .map(|obj| obj.map(|obj| (obj, None))) | ||
| .map_err(Into::into); | ||
| } | ||
| } | ||
|
|
||
| match self.store.load_one_index(self.refresh, snapshot.marker)? { | ||
| Some(new_snapshot) => { | ||
| *snapshot = new_snapshot; | ||
| self.clear_cache(); | ||
| } | ||
| None => return Ok(None), | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| impl<S> super::Handle<S> | ||
| where | ||
| S: Deref<Target = super::Store> + Clone, | ||
| { | ||
| /// Try to find the object identified by `id` in any backing store and return it as a readable stream, | ||
| /// along with its pack location if it came from a pack. | ||
| pub fn try_find_stream( | ||
| &self, | ||
| id: &oid, | ||
| pack_cache: &mut dyn DecodeEntry, | ||
| ) -> Result<Option<(crate::find::Stream, Option<gix_pack::data::entry::Location>)>, gix_object::find::Error> { | ||
| let mut snapshot = self.snapshot.borrow_mut(); | ||
| let mut inflate = self.inflate.borrow_mut(); | ||
| self.try_find_stream_inner(id, &mut inflate, pack_cache, &mut snapshot, None) | ||
| .map_err(|err| Box::new(err) as _) | ||
| } | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
When
decode_entry_to_write()falls into theDeltaBaseUnresolvedretry path, the object is looked up and decoded again after recursive base resolution, and that recursive lookup can reorder or refreshsnapshot.indices. In that case, the successful retry may come from a different pack entry than the initial lookup, but the returned location still uses the originally capturedpack_id/header_size, so callers can receive a mismatchedLocation(pack_id,pack_offset,entry_size) for the bytes they actually streamed.Useful? React with 👍 / 👎.