aboutsummaryrefslogblamecommitdiff
path: root/src/wal.rs
blob: dd41045d05f23ac9bab426f4b8f451122fa2911b (plain) (tree)























                                 
                                                  




                      




                                                    


























                                                                            




                                                                                                 
                                                                      
                              
                                                                                  


                    

                                               
                                                                                             
                                                                                         
                                         
                                                                

                                                                                               




                                                                                                  




























                                                                                                 
                                                                                       


                                              
                                                                                           


                                                  
                                                






                                                             
                                                                            



                                                                   




                                                                                                       

                                                 

                                                                                  






                                                         
                                                                          
                                            
                                                                                              
         
              

     
                                                           


















                                                                













                                                                                              
                                                                                              





































































                                                                                             

                                      



                                                                                                   
                                                                                 


















                                                                                  
                                             

                                        
              




                                   
                              




                                                                                    

                                                                         

     
                                                          

                                                              
                                                            
                               
                                                                 




                                                    
                                                         
                            
                                                                       
                                                                                  






                                                                                         
                                                                              
                                            
                                                                     


                                                  
                                                                                     


                                            
                                                                                               


                                            
                                                                                               









                                                                                          
                                                                                        



                                               

                                             

                               
                                    


                                                
                           

     
use std::collections::BinaryHeap;

#[repr(u8)]
enum WALRingType {
    #[allow(dead_code)]
    Null = 0x0,
    Full,
    First,
    Middle,
    Last
}

#[repr(packed)]
struct WALRingBlob {
    crc32: u32,
    rsize: u32,
    rtype: WALRingType,
    // payload follows
}

pub type WALBytes = Box<[u8]>;
pub type WALFileId = u64;
pub type WALPos = u64;

#[derive(Eq, PartialEq, Copy, Clone, Debug, Hash)]
pub struct WALRingId {
    start: WALPos,
    end: WALPos
}

impl WALRingId {
    pub fn get_start(&self) -> WALPos { self.start }
    pub fn get_end(&self) -> WALPos { self.end }
}

impl Ord for WALRingId {
    fn cmp(&self, other: &WALRingId) -> std::cmp::Ordering {
        other.start.cmp(&self.start).then_with(|| other.end.cmp(&self.end))
    }
}

impl PartialOrd for WALRingId {
    fn partial_cmp(&self, other: &WALRingId) -> Option<std::cmp::Ordering> {
        Some(self.cmp(other))
    }
}

/// the state for a WAL writer
struct WALState {
    /// the first file id of WAL
    first_fid: WALFileId,
    /// the next position for a record, addressed in the entire WAL space
    next: WALPos,
    /// number of bits for a file
    file_nbit: u64,
}

pub trait WALFile {
    /// Initialize the file space in [offset, offse