-
-
Notifications
You must be signed in to change notification settings - Fork 671
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
fix issue in composite file #1456
Merged
Merged
Changes from all commits
Commits
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 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 | ||||
---|---|---|---|---|---|---|
|
@@ -38,7 +38,7 @@ impl BinarySerializable for FileAddr { | |||||
/// A `CompositeWrite` is used to write a `CompositeFile`. | ||||||
pub struct CompositeWrite<W = WritePtr> { | ||||||
write: CountingWriter<W>, | ||||||
offsets: HashMap<FileAddr, u64>, | ||||||
offsets: Vec<(FileAddr, u64)>, | ||||||
} | ||||||
|
||||||
impl<W: TerminatingWrite + Write> CompositeWrite<W> { | ||||||
|
@@ -47,7 +47,7 @@ impl<W: TerminatingWrite + Write> CompositeWrite<W> { | |||||
pub fn wrap(w: W) -> CompositeWrite<W> { | ||||||
CompositeWrite { | ||||||
write: CountingWriter::wrap(w), | ||||||
offsets: HashMap::new(), | ||||||
offsets: Vec::new(), | ||||||
} | ||||||
} | ||||||
|
||||||
|
@@ -60,8 +60,8 @@ impl<W: TerminatingWrite + Write> CompositeWrite<W> { | |||||
pub fn for_field_with_idx(&mut self, field: Field, idx: usize) -> &mut CountingWriter<W> { | ||||||
let offset = self.write.written_bytes(); | ||||||
let file_addr = FileAddr::new(field, idx); | ||||||
assert!(!self.offsets.contains_key(&file_addr)); | ||||||
self.offsets.insert(file_addr, offset); | ||||||
assert!(!self.offsets.iter().any(|el| el.0 == file_addr)); | ||||||
self.offsets.push((file_addr, offset)); | ||||||
&mut self.write | ||||||
} | ||||||
|
||||||
|
@@ -73,16 +73,8 @@ impl<W: TerminatingWrite + Write> CompositeWrite<W> { | |||||
let footer_offset = self.write.written_bytes(); | ||||||
VInt(self.offsets.len() as u64).serialize(&mut self.write)?; | ||||||
|
||||||
let mut offset_fields: Vec<_> = self | ||||||
.offsets | ||||||
.iter() | ||||||
.map(|(file_addr, offset)| (*offset, *file_addr)) | ||||||
.collect(); | ||||||
|
||||||
offset_fields.sort(); | ||||||
|
||||||
let mut prev_offset = 0; | ||||||
for (offset, file_addr) in offset_fields { | ||||||
for (file_addr, offset) in self.offsets { | ||||||
VInt((offset - prev_offset) as u64).serialize(&mut self.write)?; | ||||||
file_addr.serialize(&mut self.write)?; | ||||||
prev_offset = offset; | ||||||
|
@@ -106,6 +98,14 @@ pub struct CompositeFile { | |||||
offsets_index: HashMap<FileAddr, Range<usize>>, | ||||||
} | ||||||
|
||||||
impl std::fmt::Debug for CompositeFile { | ||||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
f.debug_struct("CompositeFile") | ||||||
.field("offsets_index", &self.offsets_index) | ||||||
.finish() | ||||||
} | ||||||
} | ||||||
|
||||||
impl CompositeFile { | ||||||
/// Opens a composite file stored in a given | ||||||
/// `FileSlice`. | ||||||
|
@@ -233,4 +233,56 @@ mod test { | |||||
} | ||||||
Ok(()) | ||||||
} | ||||||
|
||||||
#[test] | ||||||
fn test_composite_file_bug() -> crate::Result<()> { | ||||||
let path = Path::new("test_path"); | ||||||
let directory = RamDirectory::create(); | ||||||
{ | ||||||
let w = directory.open_write(path).unwrap(); | ||||||
let mut composite_write = CompositeWrite::wrap(w); | ||||||
let mut write = composite_write.for_field_with_idx(Field::from_field_id(1u32), 0); | ||||||
VInt(32431123u64).serialize(&mut write)?; | ||||||
write.flush()?; | ||||||
let write = composite_write.for_field_with_idx(Field::from_field_id(1u32), 1); | ||||||
write.flush()?; | ||||||
|
||||||
let mut write = composite_write.for_field_with_idx(Field::from_field_id(0u32), 0); | ||||||
VInt(1_000_000).serialize(&mut write)?; | ||||||
write.flush()?; | ||||||
|
||||||
composite_write.close()?; | ||||||
} | ||||||
{ | ||||||
let r = directory.open_read(path)?; | ||||||
let composite_file = CompositeFile::open(&r)?; | ||||||
{ | ||||||
let file = composite_file | ||||||
.open_read_with_idx(Field::from_field_id(1u32), 0) | ||||||
.unwrap() | ||||||
.read_bytes()?; | ||||||
let mut file0_buf = file.as_slice(); | ||||||
let payload_0 = VInt::deserialize(&mut file0_buf)?.0; | ||||||
assert_eq!(file0_buf.len(), 0); | ||||||
assert_eq!(payload_0, 32431123u64); | ||||||
} | ||||||
{ | ||||||
let file = composite_file | ||||||
.open_read_with_idx(Field::from_field_id(1u32), 1) | ||||||
.unwrap() | ||||||
.read_bytes()?; | ||||||
let file = file.as_slice(); | ||||||
assert_eq!(file.len(), 0); | ||||||
} | ||||||
{ | ||||||
let file = composite_file | ||||||
.open_read_with_idx(Field::from_field_id(0u32), 0) | ||||||
.unwrap() | ||||||
.read_bytes()?; | ||||||
let file = file.as_slice(); | ||||||
assert_eq!(file.len(), 3); | ||||||
} | ||||||
} | ||||||
Ok(()) | ||||||
} | ||||||
} |
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.
Should this really be an
assert
instead of adebug_assert
, i.e. is it necessary for memory safety? This does have linear instead of amortized constant runtime in the number of offsets now, so avoiding the check in release builds would be nice if possible IMHO.