summaryrefslogtreecommitdiff
path: root/src/memory/cached_block.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/memory/cached_block.rs')
-rw-r--r--src/memory/cached_block.rs85
1 files changed, 9 insertions, 76 deletions
diff --git a/src/memory/cached_block.rs b/src/memory/cached_block.rs
index eb59a4b..77cdf61 100644
--- a/src/memory/cached_block.rs
+++ b/src/memory/cached_block.rs
@@ -1,5 +1,6 @@
use crate::block_device::{BlockDevice, BLOCK_SIZE};
use crate::disk::block::Block;
+use crate::disk::inode::Inode;
use crate::AyaFS;
use and_then_some::BoolExt;
use log::debug;
@@ -70,17 +71,18 @@ impl<T: Block> BlockCache<T> {
/// 从 LRU cache 里获取一个 block 的引用, 如果没有在 cache 中会加载.
pub(crate) fn get_block<U: Block>(&mut self, index: usize) -> Option<&CachedBlock<U>> {
- self.load_block(index)
- .and_then(|| self.cache.get(&index).map(convert::<T, U>))
+ if !self.cache.contains(&index) {
+ self.load_block(index);
+ }
+ self.cache.get(&index).map(convert::<T,U>)
}
/// 从 LRU cache 里获取一个 block 的可变引用, 如果没有在 cache 中会加载.
pub(crate) fn get_block_mut<U: Block>(&mut self, index: usize) -> Option<&mut CachedBlock<U>> {
- debug!("Blockcache get block mut");
- self.load_block(index).and_then(|| {
- debug!("block loaded");
- self.cache.get_mut(&index).map(convert_mut::<T, U>)
- })
+ if !self.cache.contains(&index) {
+ self.load_block(index);
+ }
+ self.cache.get_mut(&index).map(convert_mut::<T, U>)
}
/// 从 LRU cache 中读取一个 block 的引用, *不会* 影响 LRU cache 的结构, 如果没有在 cache 中不会加载.
@@ -145,73 +147,4 @@ impl AyaFS {
pub(crate) fn update_block<T: Block>(&mut self, block: CachedBlock<T>) -> bool {
self.cached_blocks.update_block(block)
}
-
- // pub(crate) fn update_block<T: Block>(&mut self, block: CachedBlock<T>) -> bool {
- // if self.cached_blocks.contains(&block.index) {
- // let data_block = convert::<T, DataBlock>(&block).clone();
- // self.cached_blocks.push(block.index, data_block);
- // true
- // } else {
- // false
- // }
- // }
- //
- // /// 从 LRU cache 里获取一个 block 的引用, 如果没有在 cache 中会加载.
- // pub(crate) fn get_block<T: Block>(&mut self, index: usize) -> Option<&CachedBlock<T>> {
- // self.load_block(index)
- // .and_then(|| self.cached_blocks.get(&index).map(convert::<DataBlock, T>))
- // }
- //
- // /// 从 LRU cache 里获取一个 block 的可变引用, 如果没有在 cache 中会加载.
- // pub(crate) fn get_block_mut<T: Block>(&mut self, index: usize) -> Option<&mut CachedBlock<T>> {
- // self.load_block(index).and_then(|| {
- // self.cached_blocks
- // .get_mut(&index)
- // .map(convert_mut::<DataBlock, T>)
- // })
- // }
- //
- // /// 从 LRU cache 中读取一个 block 的引用, *不会* 影响 LRU cache 的结构, 如果没有在 cache 中不会加载.
- // pub(crate) fn peek_block<T: Block>(&self, index: usize) -> Option<&CachedBlock<T>> {
- // self.cached_blocks.peek(&index).map(convert::<DataBlock, T>)
- // }
- //
- // /// 从 LRU cache 中读取一个 block 的可变引用, *不会* 影响 LRU cache 的结构, 如果没有在 cache 中不会加载.
- // pub(crate) fn peek_block_mut<T: Block>(&mut self, index: usize) -> Option<&mut CachedBlock<T>> {
- // self.cached_blocks.peek_mut(&index).map(convert_mut::<DataBlock, T>)
- // }
- //
- // pub(crate) fn load_block(&mut self, index: usize) -> bool {
- // // 先看这个 block 是不是 valid, 不 valid 直接返回 false.
- // if !self.data_bitmap.query(index) {
- // self.cached_blocks.pop(&index);
- // // deallocate 时只更新 bitmap 没有动 cache, lazy 地驱逐 cache 中的无效 entry.
- // return false;
- // }
- //
- // // 接下来这个 block 一定 valid. 如果 cache 里没有这个 block 就把它 load 到 cache 里
- // if self.cached_blocks.contains(&index) == false {
- // let block = DataBlock::default();
- // let buffer = unsafe {
- // std::slice::from_raw_parts_mut(&block as *const DataBlock as *mut u8, BLOCK_SIZE)
- // };
- // self.device.read(index, buffer);
- // let cached_block = CachedBlock {
- // block,
- // index,
- // dirty: false,
- // };
- // if let Some((old_index, old_block)) = self.cached_blocks.push(index, cached_block) {
- // assert_ne!(old_index, index); // 只有 block 不在 cache 里的时候才会插入
- // if old_block.dirty {
- // let old_block_ptr = &old_block.block as *const DataBlock as *mut u8;
- // let old_block_buffer =
- // unsafe { std::slice::from_raw_parts(old_block_ptr, BLOCK_SIZE) };
- // self.device.write(old_index, old_block_buffer);
- // }
- // }
- // }
- //
- // true
- // }
}