Skip to content

Commit

Permalink
refactor: also return the size prefix size in read_header()
Browse files Browse the repository at this point in the history
`read_header()` also returns the number of bytes read. Make that number
contain the total amount of bytes read by this function, which includes
the size prefix as well as the byte size of the header itself.
  • Loading branch information
vmx committed Dec 8, 2020
1 parent 1fe10ec commit 824c2bf
Show file tree
Hide file tree
Showing 4 changed files with 18 additions and 12 deletions.
4 changes: 2 additions & 2 deletions examples/indexinfo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ use std::env;
use std::fs::File;
use std::io::BufReader;

use storethehash::index::{self, IndexIter, SIZE_PREFIX_SIZE};
use storethehash::index::{self, IndexIter};
use storethehash::recordlist::{RecordList, BUCKET_PREFIX_SIZE};

fn index_info(index_path: &str) {
Expand All @@ -13,7 +13,7 @@ fn index_info(index_path: &str) {
let (_header, bytes_read) = index::read_header(&mut index_file).unwrap();

let mut buffered = BufReader::new(index_file);
for entry in IndexIter::new(&mut buffered, SIZE_PREFIX_SIZE + bytes_read) {
for entry in IndexIter::new(&mut buffered, bytes_read) {
match entry {
Ok((data, _pos)) => {
let bucket = u32::from_le_bytes(data[..BUCKET_PREFIX_SIZE].try_into().unwrap());
Expand Down
4 changes: 2 additions & 2 deletions examples/indexstats.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::env;
use std::fs::File;
use std::io::BufReader;

use storethehash::index::{self, IndexIter, SIZE_PREFIX_SIZE};
use storethehash::index::{self, IndexIter};
use storethehash::recordlist::{RecordList, BUCKET_PREFIX_SIZE};

fn index_stats(index_path: &str) -> BTreeMap<u32, Vec<usize>> {
Expand All @@ -16,7 +16,7 @@ fn index_stats(index_path: &str) -> BTreeMap<u32, Vec<usize>> {
let (_header, bytes_read) = index::read_header(&mut index_file).unwrap();

let mut buffered = BufReader::new(index_file);
for entry in IndexIter::new(&mut buffered, SIZE_PREFIX_SIZE + bytes_read) {
for entry in IndexIter::new(&mut buffered, bytes_read) {
match entry {
Ok((data, _pos)) => {
let bucket = u32::from_le_bytes(data[..BUCKET_PREFIX_SIZE].try_into().unwrap());
Expand Down
10 changes: 8 additions & 2 deletions src/index.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl<P: PrimaryStorage, const N: u8> Index<P, N> {
// TODO vmx 2020-11-30: Find if there's a better way than cloning the file. Perhaps
// a BufReader should be used instead of File for this whole module?
let mut buffered = BufReader::new(file.try_clone()?);
for entry in IndexIter::new(&mut buffered, SIZE_PREFIX_SIZE + bytes_read) {
for entry in IndexIter::new(&mut buffered, bytes_read) {
match entry {
Ok((data, pos)) => {
let bucket_prefix = u32::from_le_bytes(
Expand Down Expand Up @@ -398,14 +398,20 @@ pub fn read_size_prefix<R: Read>(reader: &mut R) -> Result<usize, io::Error> {
}

/// Returns the headet together with the bytes read.
///
/// The bytes read include all the bytes that were read by this function. Hence it also includes
/// the 4-byte size prefix of the header besides the size of the header data itself.
pub fn read_header(file: &mut File) -> Result<(Header, usize), io::Error> {
let mut header_size_buffer = [0; SIZE_PREFIX_SIZE];
file.read_exact(&mut header_size_buffer)?;
let header_size =
usize::try_from(u32::from_le_bytes(header_size_buffer)).expect(">=32-bit platform needed");
let mut header_bytes = vec![0u8; header_size];
file.read_exact(&mut header_bytes)?;
Ok((Header::from(&header_bytes[..]), header_size))
Ok((
Header::from(&header_bytes[..]),
SIZE_PREFIX_SIZE + header_size,
))
}

/// Returns the position of the first character that both given slices have not in common.
Expand Down
12 changes: 6 additions & 6 deletions tests/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use std::convert::TryInto;
use std::fs::{self, File};
use std::path::Path;

use storethehash::index::{self, Header, Index, IndexIter, INDEX_VERSION, SIZE_PREFIX_SIZE};
use storethehash::index::{self, Header, Index, IndexIter, INDEX_VERSION};
use storethehash::recordlist::RecordList;
use storethehash_primary_inmemory::InMemory;

Expand Down Expand Up @@ -35,7 +35,7 @@ fn assert_common_prefix_trimmed(key1: Vec<u8>, key2: Vec<u8>, expected_key_lengt

// The record list is append only, hence the first record list only contains the first insert
{
let (data, _pos) = IndexIter::new(&mut file, SIZE_PREFIX_SIZE + bytes_read)
let (data, _pos) = IndexIter::new(&mut file, bytes_read)
.next()
.unwrap()
.unwrap();
Expand All @@ -49,7 +49,7 @@ fn assert_common_prefix_trimmed(key1: Vec<u8>, key2: Vec<u8>, expected_key_lengt

// The second block contains both keys
{
let (data, _pos) = IndexIter::new(&mut file, SIZE_PREFIX_SIZE + bytes_read)
let (data, _pos) = IndexIter::new(&mut file, bytes_read)
.next()
.unwrap()
.unwrap();
Expand Down Expand Up @@ -81,7 +81,7 @@ fn index_put_single_key() {
let mut file = File::open(index_path).unwrap();
let (_header, bytes_read) = index::read_header(&mut file).unwrap();

let (data, _pos) = IndexIter::new(&mut file, SIZE_PREFIX_SIZE + bytes_read)
let (data, _pos) = IndexIter::new(&mut file, bytes_read)
.next()
.unwrap()
.unwrap();
Expand Down Expand Up @@ -110,7 +110,7 @@ fn index_put_distinct_key() {
let mut file = File::open(index_path).unwrap();
let (_header, bytes_read) = index::read_header(&mut file).unwrap();

let (data, _pos) = IndexIter::new(&mut file, SIZE_PREFIX_SIZE + bytes_read)
let (data, _pos) = IndexIter::new(&mut file, bytes_read)
.last()
.unwrap()
.unwrap();
Expand Down Expand Up @@ -165,7 +165,7 @@ fn index_put_prev_and_next_key_common_prefix() {
let mut file = File::open(index_path).unwrap();
let (_header, bytes_read) = index::read_header(&mut file).unwrap();

let (data, _pos) = IndexIter::new(&mut file, SIZE_PREFIX_SIZE + bytes_read)
let (data, _pos) = IndexIter::new(&mut file, bytes_read)
.last()
.unwrap()
.unwrap();
Expand Down

0 comments on commit 824c2bf

Please sign in to comment.