mirror of
https://github.com/bitcoinresearchkit/brk.git
synced 2026-05-04 03:09:09 -07:00
vec: don't store mmap in struct anymore
This commit is contained in:
@@ -3,10 +3,8 @@ use std::{
|
||||
fs::{File, OpenOptions},
|
||||
io::{self, Seek, SeekFrom, Write},
|
||||
path::{Path, PathBuf},
|
||||
sync::Arc,
|
||||
};
|
||||
|
||||
use arc_swap::ArcSwap;
|
||||
use brk_core::Result;
|
||||
use memmap2::Mmap;
|
||||
|
||||
@@ -38,7 +36,7 @@ where
|
||||
}
|
||||
#[inline]
|
||||
fn get_or_read_(&self, index: usize, mmap: &Mmap) -> Result<Option<Cow<T>>> {
|
||||
let stored_len = self.stored_len_(mmap);
|
||||
let stored_len = self.stored_len();
|
||||
|
||||
if index >= stored_len {
|
||||
let pushed = self.pushed();
|
||||
@@ -61,10 +59,7 @@ where
|
||||
format!("{}_to_{}", I::to_string(), self.name())
|
||||
}
|
||||
|
||||
fn mmap(&self) -> &ArcSwap<Mmap>;
|
||||
|
||||
fn stored_len(&self) -> usize;
|
||||
fn stored_len_(&self, mmap: &Mmap) -> usize;
|
||||
|
||||
fn pushed(&self) -> &[T];
|
||||
#[inline]
|
||||
@@ -116,7 +111,7 @@ where
|
||||
|
||||
fn file_set_len(&mut self, file: &mut File, len: u64) -> Result<()> {
|
||||
Self::file_set_len_(file, len)?;
|
||||
self.update_mmap(file)
|
||||
Ok(())
|
||||
}
|
||||
fn file_set_len_(file: &mut File, len: u64) -> Result<()> {
|
||||
file.set_len(len)?;
|
||||
@@ -127,7 +122,7 @@ where
|
||||
fn file_write_all(&mut self, file: &mut File, buf: &[u8]) -> Result<()> {
|
||||
file.write_all(buf)?;
|
||||
file.flush()?;
|
||||
self.update_mmap(file)
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn file_truncate_and_write_all(&mut self, file: &mut File, len: u64, buf: &[u8]) -> Result<()> {
|
||||
@@ -143,14 +138,10 @@ where
|
||||
self.file_truncate_and_write_all(&mut file, HEADER_OFFSET as u64, &[])
|
||||
}
|
||||
|
||||
fn new_mmap(file: &File) -> Result<Arc<Mmap>> {
|
||||
Ok(Arc::new(unsafe { Mmap::map(file)? }))
|
||||
}
|
||||
|
||||
fn update_mmap(&mut self, file: &File) -> Result<()> {
|
||||
let mmap = Self::new_mmap(file)?;
|
||||
self.mmap().store(mmap);
|
||||
Ok(())
|
||||
#[inline]
|
||||
fn create_mmap(&self) -> Result<Mmap> {
|
||||
let file = self.open_file()?;
|
||||
unsafe { Mmap::map(&file).map_err(|e| e.into()) }
|
||||
}
|
||||
|
||||
#[inline]
|
||||
|
||||
Reference in New Issue
Block a user