Skip to content
This repository has been archived by the owner on Jun 11, 2022. It is now read-only.

Extracted file-offset functions #704

Merged
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
27 changes: 20 additions & 7 deletions storage-units/src/indexfile.rs
Expand Up @@ -198,11 +198,8 @@ impl Index {
tmpfile.write_all(&hash[..])?;
}

for &(_, ofs) in sorted.iter() {
let mut buf = [0u8; OFF_SIZE];
write_offset(&mut buf, ofs);
tmpfile.write_all(&buf[..])?;
}
write_offsets_to_file(tmpfile, sorted.iter().map(|(_, b)| b))?;

Ok(Lookup {
params: params,
fanout: fanout,
Expand Down Expand Up @@ -238,12 +235,29 @@ impl Lookup {
}
}

pub fn write_offsets_to_file<'a, I: Iterator<Item = &'a Offset>>(
tmpfile: &mut TmpFile,
offsets: I,
) -> Result<()> {
for ofs in offsets {
let mut buf = [0u8; OFF_SIZE];
write_offset(&mut buf, *ofs);
tmpfile.write_all(&buf[..])?;
}
Ok(())
}

fn file_read_offset(mut file: &fs::File) -> Offset {
let mut buf = [0u8; OFF_SIZE];
file.read_exact(&mut buf).unwrap();
read_offset(&buf)
}

pub fn file_read_offset_at(mut file: &fs::File, ofs: u64) -> Offset {
file.seek(SeekFrom::Start(ofs)).unwrap();
file_read_offset(file)
}

fn file_read_hash(mut file: &fs::File) -> BlockHash {
let mut buf = [0u8; HASH_SIZE];
file.read_exact(&mut buf).unwrap();
Expand Down Expand Up @@ -279,8 +293,7 @@ impl ReaderNoLookup<fs::File> {
let FanoutTotal(total) = lookup.fanout.get_total();
let ofs_base = offset_offsets(lookup.params.bloom_size, total);
let ofs = ofs_base + OFF_SIZE as u64 * index_offset as u64;
self.handle.seek(SeekFrom::Start(ofs)).unwrap();
file_read_offset(&mut self.handle)
file_read_offset_at(&mut self.handle, ofs)
}
}

Expand Down