diff --git a/mercury/src/internal/pack/cache.rs b/mercury/src/internal/pack/cache.rs index 64c4cd409..244a12f31 100644 --- a/mercury/src/internal/pack/cache.rs +++ b/mercury/src/internal/pack/cache.rs @@ -159,7 +159,9 @@ impl _Cache for Caches { self.complete_signal.clone(), Some(self.pool.clone()), ); - a_obj.set_store_path(Caches::generate_temp_path(&self.tmp_path, hash)); + if self.mem_size.is_some() { + a_obj.set_store_path(Caches::generate_temp_path(&self.tmp_path, hash)); + } let _ = map.insert(hash, a_obj); } //order maters as for reading in 'get_by_offset()' diff --git a/mercury/src/internal/pack/decode.rs b/mercury/src/internal/pack/decode.rs index 2766e5960..7755a03e2 100644 --- a/mercury/src/internal/pack/decode.rs +++ b/mercury/src/internal/pack/decode.rs @@ -75,7 +75,7 @@ impl Pack { pool: Arc::new(ThreadPool::new(thread_num)), waitlist: Arc::new(Waitlist::new()), caches: Arc::new(Caches::new(cache_mem_size, temp_path, thread_num)), - mem_limit: mem_limit.unwrap_or(usize::MAX), + mem_limit, cache_objs_mem: Arc::new(AtomicUsize::default()), clean_tmp, } @@ -363,7 +363,8 @@ impl Pack { } // 3 parts: Waitlist + TheadPool + Caches // hardcode the limit of the tasks of threads_pool queue, to limit memory - while self.memory_used() > self.mem_limit || self.pool.queued_count() > 2000 { + while self.pool.queued_count() > 2000 + || self.mem_limit.map(|limit| self.memory_used() > limit).unwrap_or(false) { thread::yield_now(); } let r: Result = self.decode_pack_object(&mut reader, &mut offset); @@ -709,6 +710,19 @@ mod tests { p.decode(&mut buffered,|_,_|{}).unwrap(); } + #[test] + fn test_pack_decode_no_mem_limit() { + let mut source = PathBuf::from(env::current_dir().unwrap().parent().unwrap()); + source.push("tests/data/packs/pack-1d0e6c14760c956c173ede71cb28f33d921e232f.pack"); + + let tmp = PathBuf::from("/tmp/.cache_temp"); + + let f = fs::File::open(source).unwrap(); + let mut buffered = BufReader::new(f); + let mut p = Pack::new(None, None, Some(tmp), true); + p.decode(&mut buffered, |_,_|{}).unwrap(); + } + #[test] fn test_pack_decode_with_large_file_with_delta_without_ref() { init_logger(); diff --git a/mercury/src/internal/pack/mod.rs b/mercury/src/internal/pack/mod.rs index 3f942ca2a..360204ce2 100644 --- a/mercury/src/internal/pack/mod.rs +++ b/mercury/src/internal/pack/mod.rs @@ -29,7 +29,7 @@ pub struct Pack { pub pool: Arc, pub waitlist: Arc, pub caches: Arc, - pub mem_limit: usize, + pub mem_limit: Option, pub cache_objs_mem: Arc, // the memory size of CacheObjects in this Pack pub clean_tmp: bool, }