fix mutability for chunk store

This commit is contained in:
Dietmar Maurer 2018-12-21 12:15:26 +01:00
parent 2d9d143a8f
commit 03e4753d8e
4 changed files with 27 additions and 19 deletions

View File

@ -34,6 +34,17 @@ fn lookup_datastore(name: &str) -> Result<Arc<DataStore>, Error> {
bail!("store not found"); bail!("store not found");
} }
// this is just a test for mutability/mutex handling - will remove later
fn test_sync_api_handler2(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
println!("This is a test {}", param);
let datastore = lookup_datastore("store1")?;
datastore.garbage_collection();
Ok(json!(null))
}
fn test_sync_api_handler(param: Value, _info: &ApiMethod) -> Result<Value, Error> { fn test_sync_api_handler(param: Value, _info: &ApiMethod) -> Result<Value, Error> {
println!("This is a test {}", param); println!("This is a test {}", param);

View File

@ -16,7 +16,6 @@ pub struct ChunkStore {
name: String, // used for error reporting name: String, // used for error reporting
base: PathBuf, base: PathBuf,
chunk_dir: PathBuf, chunk_dir: PathBuf,
hasher: Sha512Trunc256,
mutex: Mutex<bool>, mutex: Mutex<bool>,
_lockfile: File, _lockfile: File,
} }
@ -109,13 +108,12 @@ impl ChunkStore {
name: name.to_owned(), name: name.to_owned(),
base, base,
chunk_dir, chunk_dir,
hasher: Sha512Trunc256::new(), _lockfile: lockfile,
_lockfile: lockfile,
mutex: Mutex::new(false) mutex: Mutex::new(false)
}) })
} }
pub fn touch_chunk(&mut self, digest:&[u8]) -> Result<(), Error> { pub fn touch_chunk(&self, digest:&[u8]) -> Result<(), Error> {
// fixme: nix::sys::stat::utimensat // fixme: nix::sys::stat::utimensat
let mut chunk_path = self.chunk_dir.clone(); let mut chunk_path = self.chunk_dir.clone();
@ -162,7 +160,7 @@ impl ChunkStore {
Ok(()) Ok(())
} }
pub fn sweep_used_chunks(&mut self) -> Result<(), Error> { pub fn sweep_used_chunks(&self) -> Result<(), Error> {
use nix::fcntl::OFlag; use nix::fcntl::OFlag;
use nix::sys::stat::Mode; use nix::sys::stat::Mode;
@ -215,13 +213,12 @@ impl ChunkStore {
Ok(()) Ok(())
} }
pub fn insert_chunk(&mut self, chunk: &[u8]) -> Result<(bool, [u8; 32]), Error> { pub fn insert_chunk(&self, chunk: &[u8]) -> Result<(bool, [u8; 32]), Error> {
let mut hasher = Sha512Trunc256::new();
self.hasher.reset(); hasher.input(chunk);
self.hasher.input(chunk);
let mut digest = [0u8; 32]; let mut digest = [0u8; 32];
self.hasher.result(&mut digest); hasher.result(&mut digest);
//println!("DIGEST {}", digest_to_hex(&digest)); //println!("DIGEST {}", digest_to_hex(&digest));
let mut chunk_path = self.chunk_dir.clone(); let mut chunk_path = self.chunk_dir.clone();
@ -284,7 +281,7 @@ fn test_chunk_store1() {
let chunk_store = ChunkStore::open("test", ".testdir"); let chunk_store = ChunkStore::open("test", ".testdir");
assert!(chunk_store.is_err()); assert!(chunk_store.is_err());
let mut chunk_store = ChunkStore::create("test", ".testdir").unwrap(); let chunk_store = ChunkStore::create("test", ".testdir").unwrap();
let (exists, _) = chunk_store.insert_chunk(&[0u8, 1u8]).unwrap(); let (exists, _) = chunk_store.insert_chunk(&[0u8, 1u8]).unwrap();
assert!(!exists); assert!(!exists);

View File

@ -34,9 +34,9 @@ impl DataStore {
Ok(index) Ok(index)
} }
pub fn open_image_reader<P: AsRef<Path>>(&mut self, filename: P) -> Result<ImageIndexReader, Error> { pub fn open_image_reader<P: AsRef<Path>>(&self, filename: P) -> Result<ImageIndexReader, Error> {
let index = ImageIndexReader::open(&mut self.chunk_store, filename.as_ref())?; let index = ImageIndexReader::open(&self.chunk_store, filename.as_ref())?;
Ok(index) Ok(index)
} }
@ -61,19 +61,19 @@ impl DataStore {
Ok(list) Ok(list)
} }
fn mark_used_chunks(&mut self) -> Result<(), Error> { fn mark_used_chunks(&self) -> Result<(), Error> {
let image_list = self.list_images()?; let image_list = self.list_images()?;
for path in image_list { for path in image_list {
let mut index = self.open_image_reader(path)?; let index = self.open_image_reader(path)?;
index.mark_used_chunks()?; index.mark_used_chunks()?;
} }
Ok(()) Ok(())
} }
pub fn garbage_collection(&mut self) -> Result<(), Error> { pub fn garbage_collection(&self) -> Result<(), Error> {
self.mark_used_chunks()?; self.mark_used_chunks()?;

View File

@ -22,7 +22,7 @@ pub struct ImageIndexHeader {
// split image into fixed size chunks // split image into fixed size chunks
pub struct ImageIndexReader<'a> { pub struct ImageIndexReader<'a> {
store: &'a mut ChunkStore, store: &'a ChunkStore,
filename: PathBuf, filename: PathBuf,
chunk_size: usize, chunk_size: usize,
size: usize, size: usize,
@ -42,7 +42,7 @@ impl <'a> Drop for ImageIndexReader<'a> {
impl <'a> ImageIndexReader<'a> { impl <'a> ImageIndexReader<'a> {
pub fn open(store: &'a mut ChunkStore, path: &Path) -> Result<Self, Error> { pub fn open(store: &'a ChunkStore, path: &Path) -> Result<Self, Error> {
let full_path = store.relative_path(path); let full_path = store.relative_path(path);
@ -100,7 +100,7 @@ impl <'a> ImageIndexReader<'a> {
Ok(()) Ok(())
} }
pub fn mark_used_chunks(&mut self) -> Result<(), Error> { pub fn mark_used_chunks(&self) -> Result<(), Error> {
if self.index == std::ptr::null_mut() { bail!("detected closed index file."); } if self.index == std::ptr::null_mut() { bail!("detected closed index file."); }