|
| 1 | +use std::{fs, vec}; |
| 2 | + |
| 3 | +use tracing::{error, info}; |
| 4 | + |
| 5 | +use crate::{ |
| 6 | + base::swappable::Swappable, |
| 7 | + log_file::mapped_file::{default_impl::DefaultMappedFile, MappedFile}, |
| 8 | +}; |
| 9 | + |
| 10 | +pub struct MappedFileQueue { |
| 11 | + pub(crate) store_path: String, |
| 12 | + |
| 13 | + pub(crate) mapped_file_size: u64, |
| 14 | + |
| 15 | + pub(crate) mapped_files: Vec<Box<dyn MappedFile>>, |
| 16 | + |
| 17 | + //AllocateMappedFileService -- todo |
| 18 | + pub(crate) flushed_where: u64, |
| 19 | + |
| 20 | + pub(crate) committed_where: u64, |
| 21 | + |
| 22 | + pub(crate) store_timestamp: u64, |
| 23 | +} |
| 24 | + |
| 25 | +impl Swappable for MappedFileQueue { |
| 26 | + fn swap_map( |
| 27 | + &self, |
| 28 | + _reserve_num: i32, |
| 29 | + _force_swap_interval_ms: i64, |
| 30 | + _normal_swap_interval_ms: i64, |
| 31 | + ) { |
| 32 | + todo!() |
| 33 | + } |
| 34 | + |
| 35 | + fn clean_swapped_map(&self, _force_clean_swap_interval_ms: i64) { |
| 36 | + todo!() |
| 37 | + } |
| 38 | +} |
| 39 | + |
| 40 | +impl MappedFileQueue { |
| 41 | + pub fn load(&mut self) -> bool { |
| 42 | + //list dir files |
| 43 | + match std::path::Path::new(&self.store_path).read_dir() { |
| 44 | + Ok(dir) => { |
| 45 | + let mut files = vec![]; |
| 46 | + for file in dir { |
| 47 | + files.push(file.unwrap()); |
| 48 | + } |
| 49 | + if files.is_empty() { |
| 50 | + return true; |
| 51 | + } |
| 52 | + self.do_load(files).unwrap() |
| 53 | + } |
| 54 | + Err(_) => false, |
| 55 | + } |
| 56 | + } |
| 57 | + |
| 58 | + fn do_load(&mut self, files: Vec<fs::DirEntry>) -> anyhow::Result<bool> { |
| 59 | + // Ascending order sorting |
| 60 | + let sorted_files: Vec<_> = files.into_iter().collect(); |
| 61 | + //sorted_files.sort_by(|a, b| a.file_name().cmp(&b.file_name())); |
| 62 | + |
| 63 | + for (i, file) in sorted_files.iter().enumerate() { |
| 64 | + if file.path().is_dir() { |
| 65 | + continue; |
| 66 | + } |
| 67 | + |
| 68 | + if file.metadata()?.len() == 0 && i == sorted_files.len() - 1 { |
| 69 | + fs::remove_file(file.path())?; |
| 70 | + error!("{} size is 0, auto delete.", file.path().display()); |
| 71 | + continue; |
| 72 | + } |
| 73 | + |
| 74 | + if file.metadata()?.len() != self.mapped_file_size { |
| 75 | + error!( |
| 76 | + "{} length not matched message store config value, please check it manually", |
| 77 | + file.path().display() |
| 78 | + ); |
| 79 | + return Ok(false); |
| 80 | + } |
| 81 | + |
| 82 | + let mapped_file = DefaultMappedFile::new( |
| 83 | + file.path().into_os_string().to_string_lossy().to_string(), |
| 84 | + self.mapped_file_size, |
| 85 | + ); |
| 86 | + // Set wrote, flushed, committed positions for mapped_file |
| 87 | + |
| 88 | + self.mapped_files.push(Box::new(mapped_file)); |
| 89 | + info!("load {} OK", file.path().display()); |
| 90 | + } |
| 91 | + |
| 92 | + Ok(true) |
| 93 | + } |
| 94 | +} |
0 commit comments