append(): break out pub fn get_bytes_used()

This commit is contained in:
Astro 2020-12-11 18:53:17 +01:00
parent e5f8b6b56e
commit 9153240b9b
1 changed files with 12 additions and 9 deletions

View File

@ -63,6 +63,17 @@ impl<B: StoreBackend> Store<B> {
.transpose()) .transpose())
} }
/// how many bytes are currently used
///
/// equally, offset of free space
pub fn get_bytes_used(&self) -> Result<usize, ReadError> {
let mut iter = Iter::new(self.backend.data().as_ref());
while let Some(result) = iter.next() {
let _ = result?;
}
Ok(iter.offset)
}
unsafe fn append_at(&mut self, mut offset: usize, unsafe fn append_at(&mut self, mut offset: usize,
key: &[u8], value: &[u8]) -> Result<usize, WriteError<B::Error>> { key: &[u8], value: &[u8]) -> Result<usize, WriteError<B::Error>> {
let record_size = 4 + key.len() + 1 + value.len(); let record_size = 4 + key.len() + 1 + value.len();
@ -122,16 +133,8 @@ impl<B: StoreBackend> Store<B> {
} }
fn append(&mut self, key: &str, value: &[u8]) -> Result<(), Error<B::Error>> { fn append(&mut self, key: &str, value: &[u8]) -> Result<(), Error<B::Error>> {
let free_offset = { let free_offset = self.get_bytes_used()?;
let mut iter = Iter::new(self.backend.data().as_ref());
while let Some(result) = iter.next() {
let _ = result?;
}
iter.offset
};
unsafe { self.append_at(free_offset, key.as_bytes(), value)? }; unsafe { self.append_at(free_offset, key.as_bytes(), value)? };
Ok(()) Ok(())
} }