Skip to content

Commit

Permalink
Simplify StorageEntryReader+Writer
Browse files Browse the repository at this point in the history
  • Loading branch information
voltrevo committed Nov 1, 2023
1 parent e578385 commit 4868b59
Show file tree
Hide file tree
Showing 4 changed files with 91 additions and 99 deletions.
8 changes: 4 additions & 4 deletions storage/src/demo_val.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,15 +64,15 @@ impl DemoVal {
_tx: &mut Tx,
reader: &mut StorageEntryReader,
) -> Result<DemoVal, StorageError<SB>> {
let tag = reader.read_u8().map_err(StorageError::from)?;
let tag = reader.read_u8()?;

Ok(match tag {
NUMBER_TAG => {
let n = reader.read_u64().map_err(StorageError::from)?;
let n = reader.read_u64()?;
DemoVal::Number(n)
}
ARRAY_TAG => {
let len = reader.read_u64().map_err(StorageError::from)?;
let len = reader.read_u64()?;
let mut items = Vec::new();

for _ in 0..len {
Expand All @@ -82,7 +82,7 @@ impl DemoVal {
DemoVal::Array(Rc::new(items))
}
PTR_TAG => {
let ptr = reader.read_ref().map_err(StorageError::from)?;
let ptr = reader.read_ref()?;
DemoVal::Ptr(ptr)
}
_ => panic!("Invalid tag"),
Expand Down
70 changes: 41 additions & 29 deletions storage/src/storage_entry.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::io::Read;
use std::error::Error;

use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -30,48 +30,75 @@ impl<'a> StorageEntryReader<'a> {
self.refs_i == self.entry.refs.len() && self.data_i == self.entry.data.len()
}

pub fn read_ref(&mut self) -> std::io::Result<StorageEntryPtr> {
pub fn read_u8_array<const L: usize>(&mut self) -> Result<[u8; L], Box<dyn Error>> {
if self.data_i + L > self.entry.data.len() {
return Err(eof().into());
}

let mut bytes = [0; L];
bytes.copy_from_slice(&self.entry.data[self.data_i..self.data_i + L]);

self.data_i += L;

Ok(bytes)
}

pub fn read_buf(&mut self, len: usize) -> Result<Vec<u8>, Box<dyn Error>> {
if self.data_i + len > self.entry.data.len() {
return Err(eof().into());
}

let buf = self.entry.data[self.data_i..self.data_i + len].to_vec();
self.data_i += len;

Ok(buf)
}

pub fn read_vlq_buf(&mut self) -> Result<Vec<u8>, Box<dyn Error>> {
let len = self.read_vlq()?;
self.read_buf(len)
}

pub fn read_ref(&mut self) -> Result<StorageEntryPtr, Box<dyn Error>> {
if self.refs_i >= self.entry.refs.len() {
return Err(eof());
return Err(eof().into());
}

let ptr = self.entry.refs[self.refs_i];
self.refs_i += 1;
Ok(ptr)
}

pub fn read_u8(&mut self) -> std::io::Result<u8> {
pub fn read_u8(&mut self) -> Result<u8, Box<dyn Error>> {
if self.data_i >= self.entry.data.len() {
return Err(eof());
return Err(eof().into());
}

let byte = self.entry.data[self.data_i];
self.data_i += 1;
Ok(byte)
}

pub fn peek_u8(&self) -> std::io::Result<u8> {
pub fn peek_u8(&self) -> Result<u8, Box<dyn Error>> {
if self.data_i >= self.entry.data.len() {
return Err(eof());
return Err(eof().into());
}

Ok(self.entry.data[self.data_i])
}

pub fn read_u64(&mut self) -> std::io::Result<u64> {
let mut bytes = [0; 8];
self.read_exact(&mut bytes)?;
Ok(u64::from_le_bytes(bytes))
pub fn read_u64(&mut self) -> Result<u64, Box<dyn Error>> {
self.read_u8_array().map(u64::from_le_bytes)
}

pub fn read_vlq(&mut self) -> std::io::Result<u64> {
pub fn read_vlq(&mut self) -> Result<usize, Box<dyn Error>> {
let mut result = 0;
let mut shift = 0;

loop {
let byte = self.read_u8()?;

result |= ((byte & 0x7f) as u64) << shift;
result |= ((byte & 0x7f) as usize) << shift;

if byte & 0x80 == 0 {
break;
Expand All @@ -84,21 +111,6 @@ impl<'a> StorageEntryReader<'a> {
}
}

impl Read for StorageEntryReader<'_> {
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
let bytes = self
.entry
.data
.get(self.data_i..self.data_i + buf.len())
.ok_or(eof())?;

buf.copy_from_slice(bytes);
self.data_i += buf.len();

Ok(buf.len())
}
}

fn eof() -> std::io::Error {
std::io::Error::new(std::io::ErrorKind::UnexpectedEof, "not enough bytes")
}
Expand All @@ -120,7 +132,7 @@ impl<'a> StorageEntryWriter<'a> {
self.entry.data.extend_from_slice(bytes);
}

pub fn write_vlq(&mut self, mut num: u64) {
pub fn write_vlq(&mut self, mut num: usize) {
loop {
let mut byte = (num & 0x7f) as u8;

Expand Down
Loading

0 comments on commit 4868b59

Please sign in to comment.