LCOV - code coverage report
Current view: top level - safekeeper/tests/walproposer_sim - walproposer_disk.rs (source / functions) Coverage Total Hit
Test: e5024a5c05016c30dec7897aca22d1040a340f63.info Lines: 100.0 % 35 35
Test Date: 2024-11-20 11:45:54 Functions: 95.2 % 21 20

            Line data    Source code
       1              : use std::{ffi::CStr, sync::Arc};
       2              : 
       3              : use parking_lot::{Mutex, MutexGuard};
       4              : use postgres_ffi::v16::wal_generator::{LogicalMessageGenerator, WalGenerator};
       5              : use utils::lsn::Lsn;
       6              : 
       7              : use super::block_storage::BlockStorage;
       8              : 
       9              : /// Simulation implementation of walproposer WAL storage.
      10              : pub struct DiskWalProposer {
      11              :     state: Mutex<State>,
      12              : }
      13              : 
      14              : impl DiskWalProposer {
      15         9165 :     pub fn new() -> Arc<DiskWalProposer> {
      16         9165 :         Arc::new(DiskWalProposer {
      17         9165 :             state: Mutex::new(State {
      18         9165 :                 internal_available_lsn: Lsn(0),
      19         9165 :                 prev_lsn: Lsn(0),
      20         9165 :                 disk: BlockStorage::new(),
      21         9165 :                 wal_generator: WalGenerator::new(LogicalMessageGenerator::new(c"", &[])),
      22         9165 :             }),
      23         9165 :         })
      24         9165 :     }
      25              : 
      26        14395 :     pub fn lock(&self) -> MutexGuard<State> {
      27        14395 :         self.state.lock()
      28        14395 :     }
      29              : }
      30              : 
      31              : pub struct State {
      32              :     // flush_lsn
      33              :     internal_available_lsn: Lsn,
      34              :     // needed for WAL generation
      35              :     prev_lsn: Lsn,
      36              :     // actual WAL storage
      37              :     disk: BlockStorage,
      38              :     // WAL record generator
      39              :     wal_generator: WalGenerator<LogicalMessageGenerator>,
      40              : }
      41              : 
      42              : impl State {
      43          870 :     pub fn read(&self, pos: u64, buf: &mut [u8]) {
      44          870 :         self.disk.read(pos, buf);
      45          870 :         // TODO: fail on reading uninitialized data
      46          870 :     }
      47              : 
      48          211 :     pub fn write(&mut self, pos: u64, buf: &[u8]) {
      49          211 :         self.disk.write(pos, buf);
      50          211 :     }
      51              : 
      52              :     /// Update the internal available LSN to the given value.
      53          293 :     pub fn reset_to(&mut self, lsn: Lsn) {
      54          293 :         self.internal_available_lsn = lsn;
      55          293 :         self.prev_lsn = Lsn(0); // Safekeeper doesn't care if this is omitted
      56          293 :         self.wal_generator.lsn = self.internal_available_lsn;
      57          293 :         self.wal_generator.prev_lsn = self.prev_lsn;
      58          293 :     }
      59              : 
      60              :     /// Get current LSN.
      61         1671 :     pub fn flush_rec_ptr(&self) -> Lsn {
      62         1671 :         self.internal_available_lsn
      63         1671 :     }
      64              : 
      65              :     /// Inserts a logical record in the WAL at the current LSN.
      66        11350 :     pub fn insert_logical_message(&mut self, prefix: &CStr, msg: &[u8]) {
      67        11350 :         let (_, record) = self.wal_generator.append_logical_message(prefix, msg);
      68        11350 :         self.disk.write(self.internal_available_lsn.into(), &record);
      69        11350 :         self.prev_lsn = self.internal_available_lsn;
      70        11350 :         self.internal_available_lsn += record.len() as u64;
      71        11350 :     }
      72              : }
        

Generated by: LCOV version 2.1-beta