aboutsummaryrefslogblamecommitdiff
path: root/examples/demo1.rs
blob: 47d1423375768787e3744fb060f86a0652d44278 (plain) (tree)
1
2
3
4
5
6
7
8
9
10
                             
                







                                                                    
 


                                                                         
 





                     
                                                               


                     
                                           


                                          
                                                


                                            
     

 





                                
                     
                              














                                                                               
     





                                                                  







                                                                             

                                                






                                         
     
 




                                       

                                  

                                                      






                                                 



                     
                  
                     



                                                   
                                           



                                                                             





                                                                   
         




                                               
                         
                                                           
          
                                         





                                    


     
                     
                                

                                                   




                                       
                                                                      
                                            

                                                              
     
 
                                                                     
                                                       





                                       
     
 
                                                                 
                                      



                                                                            
                                
     
 









                                                     
              


     










                                                              
                                               
                          
     
       


           
                                     
                                                       
                                                                     
                   






                                          

                   



                                                                    
     
 
                                                        
                                                                     
                   








                                
     
 
                                                        
                                                                     













                                                              
                                                              

         
 
use async_trait::async_trait;
use libc::off_t;
use nix::fcntl::{fallocate, open, openat, FallocateFlags, OFlag};
use nix::sys::{
    stat::Mode,
    uio::{pread, pwrite},
};
use nix::unistd::{close, ftruncate, mkdir, unlinkat, UnlinkatFlags};
use rand::{seq::SliceRandom, Rng};
use std::os::unix::io::RawFd;

use growthring::wal::{
    WALBytes, WALFile, WALLoader, WALPos, WALRingId, WALStore, WALWriter,
};

struct WALFileTest {
    filename: String,
    fd: RawFd,
}

impl WALFileTest {
    fn new(rootfd: RawFd, filename: &str) -> Result<Self, ()> {
        openat(
            rootfd,
            filename,
            OFlag::O_CREAT | OFlag::O_RDWR,
            Mode::S_IRUSR | Mode::S_IWUSR,
        )
        .and_then(|fd| {
            let filename = filename.to_string();
            Ok(WALFileTest { filename, fd })
        })
        .or_else(|_| Err(()))
    }
}

impl Drop for WALFileTest {
    fn drop(&mut self) {
        close(self.fd).unwrap();
    }
}

#[async_trait(?Send)]
impl WALFile for WALFileTest {
    async fn allocate(&self, offset: WALPos, length: usize) -> Result<(), ()> {
        println!(
            "{}.allocate(offset=0x{:x}, end=0x{:x})",
            self.filename,
            offset,
            offset + length as u64
        );
        fallocate(
            self.fd,