LCOV - code coverage report
Current view: top level - safekeeper/tests/walproposer_sim - block_storage.rs (source / functions) Coverage Total Hit
Test: fabb29a6339542ee130cd1d32b534fafdc0be240.info Lines: 92.3 % 39 36
Test Date: 2024-06-25 13:20:00 Functions: 75.0 % 12 9

            Line data    Source code
       1              : use std::collections::HashMap;
       2              : 
       3              : const BLOCK_SIZE: usize = 8192;
       4              : 
       5              : /// A simple in-memory implementation of a block storage. Can be used to implement external
       6              : /// storage in tests.
       7              : pub struct BlockStorage {
       8              :     blocks: HashMap<u64, [u8; BLOCK_SIZE]>,
       9              : }
      10              : 
      11              : impl Default for BlockStorage {
      12            0 :     fn default() -> Self {
      13            0 :         Self::new()
      14            0 :     }
      15              : }
      16              : 
      17              : impl BlockStorage {
      18        86060 :     pub fn new() -> Self {
      19        86060 :         BlockStorage {
      20        86060 :             blocks: HashMap::new(),
      21        86060 :         }
      22        86060 :     }
      23              : 
      24        28424 :     pub fn read(&self, pos: u64, buf: &mut [u8]) {
      25        28424 :         let mut buf_offset = 0;
      26        28424 :         let mut storage_pos = pos;
      27        43728 :         while buf_offset < buf.len() {
      28        15304 :             let block_id = storage_pos / BLOCK_SIZE as u64;
      29        15304 :             let block = self.blocks.get(&block_id).unwrap_or(&[0; BLOCK_SIZE]);
      30        15304 :             let block_offset = storage_pos % BLOCK_SIZE as u64;
      31        15304 :             let block_len = BLOCK_SIZE as u64 - block_offset;
      32        15304 :             let buf_len = buf.len() - buf_offset;
      33        15304 :             let copy_len = std::cmp::min(block_len as usize, buf_len);
      34        15304 :             buf[buf_offset..buf_offset + copy_len]
      35        15304 :                 .copy_from_slice(&block[block_offset as usize..block_offset as usize + copy_len]);
      36        15304 :             buf_offset += copy_len;
      37        15304 :             storage_pos += copy_len as u64;
      38        15304 :         }
      39        28424 :     }
      40              : 
      41       135905 :     pub fn write(&mut self, pos: u64, buf: &[u8]) {
      42       135905 :         let mut buf_offset = 0;
      43       135905 :         let mut storage_pos = pos;
      44       272208 :         while buf_offset < buf.len() {
      45       136303 :             let block_id = storage_pos / BLOCK_SIZE as u64;
      46       136303 :             let block = self.blocks.entry(block_id).or_insert([0; BLOCK_SIZE]);
      47       136303 :             let block_offset = storage_pos % BLOCK_SIZE as u64;
      48       136303 :             let block_len = BLOCK_SIZE as u64 - block_offset;
      49       136303 :             let buf_len = buf.len() - buf_offset;
      50       136303 :             let copy_len = std::cmp::min(block_len as usize, buf_len);
      51       136303 :             block[block_offset as usize..block_offset as usize + copy_len]
      52       136303 :                 .copy_from_slice(&buf[buf_offset..buf_offset + copy_len]);
      53       136303 :             buf_offset += copy_len;
      54       136303 :             storage_pos += copy_len as u64
      55              :         }
      56       135905 :     }
      57              : }
        

Generated by: LCOV version 2.1-beta