Skip to content

Commit

Permalink
renamings
Browse files Browse the repository at this point in the history
  • Loading branch information
cheme committed Apr 15, 2024
1 parent 51ee857 commit 0ab21e9
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 20 deletions.
34 changes: 17 additions & 17 deletions src/file.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ const GROW_SIZE_BYTES: u64 = 256 * 1024;

#[derive(Debug)]
pub struct TableFile {
pub map: RwLock<Vec<(memmap2::MmapMut, std::fs::File)>>,
pub maps: RwLock<Vec<(memmap2::MmapMut, std::fs::File)>>,
pub path_base: std::path::PathBuf,
pub capacity: AtomicU64,
pub id: TableId,
Expand Down Expand Up @@ -148,7 +148,7 @@ impl TableFile {
};
Ok(TableFile {
path_base,
map: RwLock::new(maps),
maps: RwLock::new(maps),
capacity: AtomicU64::new(capacity),
id,
max_size,
Expand All @@ -172,7 +172,7 @@ impl TableFile {
pub fn read_at(&self, buf: &mut [u8], offset: u64) -> Result<()> {
let (offset, file_index) = offset_to_file_index(offset, self.max_size);
let offset = offset as usize;
let map = self.map.read();
let map = self.maps.read();
let (map, _) = map.get(file_index).unwrap();
buf.copy_from_slice(&map[offset..offset + buf.len()]);
Ok(())
Expand All @@ -182,7 +182,7 @@ impl TableFile {
pub fn slice_at(&self, offset: u64, len: usize) -> MappedBytesGuard {
let (offset, file_index) = offset_to_file_index(offset, self.max_size);
let offset = offset as usize;
let map = self.map.read();
let map = self.maps.read();
parking_lot::RwLockReadGuard::map(map, |map| {
let (map, _) = map.get(file_index).unwrap();
&map[offset..offset + len]
Expand All @@ -199,7 +199,7 @@ impl TableFile {
}

pub fn write_at(&self, buf: &[u8], offset: u64) -> Result<()> {
let map = self.map.read();
let map = self.maps.read();
let (offset, file_index) = offset_to_file_index(offset, self.max_size);
let (map, _) = map.get(file_index).unwrap();
let offset = offset as usize;
Expand All @@ -216,9 +216,9 @@ impl TableFile {
}

pub fn grow(&self, entry_size: u16) -> Result<()> {
let mut map_and_file = self.map.write();
let map_and_file_len = map_and_file.len();
let (current_len, push) = match map_and_file.last() {
let mut maps = self.maps.write();
let num_maps = maps.len();
let (current_len, push) = match maps.last() {
None => (0, true),
Some((_, file)) => {
let len = try_io!(file.metadata()).len();
Expand All @@ -229,20 +229,20 @@ impl TableFile {
}
},
};
let per_page_capacity = self.max_size.map(|m| m / entry_size as usize).unwrap_or(0);
let per_file_capacity = self.max_size.map(|m| m / entry_size as usize).unwrap_or(0);
let (new_len, prev_page_capacity) = if push {
let file =
self.create_file(self.max_size.is_some().then(|| map_and_file_len as u32))?;
self.create_file(self.max_size.is_some().then(|| num_maps as u32))?;
let new_len = self
.max_size
.map(|m| std::cmp::min(m as u64, GROW_SIZE_BYTES))
.unwrap_or(GROW_SIZE_BYTES);
try_io!(file.set_len(new_len));
let map = mmap(&file, new_len as usize)?;
map_and_file.push((map, file));
(new_len, map_and_file_len * per_page_capacity)
maps.push((map, file));
(new_len, num_maps * per_file_capacity)
} else {
let (map, file) = map_and_file.last_mut().unwrap();
let (map, file) = maps.last_mut().unwrap();
let new_len = current_len + GROW_SIZE_BYTES as u64;
let new_len =
self.max_size.map(|m| std::cmp::min(m as u64, new_len)).unwrap_or(new_len);
Expand All @@ -252,8 +252,8 @@ impl TableFile {
let old_map = std::mem::replace(map, new_map);
try_io!(old_map.flush());
}
if map_and_file_len > 0 {
(new_len, (map_and_file_len - 1) * per_page_capacity)
if num_maps > 0 {
(new_len, (num_maps - 1) * per_file_capacity)
} else {
(new_len, 0)
}
Expand All @@ -264,15 +264,15 @@ impl TableFile {
}

pub fn flush(&self) -> Result<()> {
let maps = self.map.read();
let maps = self.maps.read();
for (map, _) in maps.iter() {
try_io!(map.flush());
}
Ok(())
}

pub fn remove(&self) -> Result<()> {
let mut maps_lock = self.map.write();
let mut maps_lock = self.maps.write();
let mut maps = std::mem::take(&mut *maps_lock);
let maps_len = maps.len();
maps.reverse();
Expand Down
6 changes: 3 additions & 3 deletions src/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ impl ValueTable {
let file = crate::file::TableFile::open(filepath, entry_size, id, max_file_size)?;
let mut filled = 1;
let mut last_removed = 0;
if !file.map.read().is_empty() {
if !file.maps.read().is_empty() {
let mut header = Header::default();
file.read_at(&mut header.0, 0)?;
last_removed = header.last_removed();
Expand Down Expand Up @@ -1110,7 +1110,7 @@ impl ValueTable {
}

pub fn refresh_metadata(&self) -> Result<()> {
if self.file.map.read().is_empty() {
if self.file.maps.read().is_empty() {
return Ok(())
}
let _free_entries_guard = if let Some(free_entries) = &self.free_entries {
Expand Down Expand Up @@ -1195,7 +1195,7 @@ impl ValueTable {
}

pub fn is_init(&self) -> bool {
!self.file.map.read().is_empty()
!self.file.maps.read().is_empty()
}

pub fn init_with_entry(&self, entry: &[u8]) -> Result<()> {
Expand Down

0 comments on commit 0ab21e9

Please sign in to comment.