feat(cache): use u64 for cache versionning

This commit is contained in:
Mayeul@Zama
2022-11-08 17:12:17 +01:00
committed by mayeul-zama
parent 3a80e932af
commit 4ebb372a20
4 changed files with 16 additions and 15 deletions

View File

@@ -11,6 +11,8 @@ use crate::utils::cache::ephemeral::{CacheHashMap, EphemeralCache};
use crate::utils::cache::persistent::PersistentCacheHashMap;
use crate::utils::square;
use super::common::VERSION;
#[derive(Clone, Copy, Debug, Serialize, Deserialize)]
pub struct CbComplexityNoise {
pub decomp: BrDecompositionParameters,
@@ -111,5 +113,5 @@ pub fn cache(
glwe_params,
)
};
PersistentCacheHashMap::new(&path, "v0", function)
PersistentCacheHashMap::new(&path, VERSION, function)
}

View File

@@ -2,4 +2,4 @@ use crate::parameters::GlweParameters;
pub type MacroParam = (GlweParameters, u64);
pub const VERSION: &str = "v1";
pub const VERSION: u64 = 1;

View File

@@ -15,7 +15,7 @@ use crate::utils::cache::persistent::PersistentCacheHashMap;
use crate::{config, security};
use super::blind_rotate;
use super::common::MacroParam;
use super::common::{MacroParam, VERSION};
#[derive(Clone, Copy, Debug, Serialize, Deserialize, PartialEq)]
pub struct PpSwitchComplexityNoise {
@@ -117,5 +117,5 @@ pub fn cache(
&br,
)
};
PersistentCacheHashMap::new(&path, "v0", function)
PersistentCacheHashMap::new(&path, VERSION, function)
}

View File

@@ -22,7 +22,7 @@ where
// path on disk
path: String,
// version to invalidate no longer valid cache
version: String,
version: u64,
// content: the HashMap is read-only, but it can be a new HashMap
content: RwLock<Arc<ROC>>, // the HashMap is read once, never modified and shared
content_changed: AtomicBool, // true if the content changed since loading from disk
@@ -47,7 +47,7 @@ where
{
pub fn new(
path: &str,
version: &str,
version: u64,
function: impl 'static + Send + Sync + Fn(ROC::K) -> ROC::V,
) -> Self {
let t0 = Instant::now();
@@ -61,7 +61,6 @@ where
);
}
let path = path.into();
let version = version.into();
let content = RwLock::new(Arc::new(content));
let content_changed = AtomicBool::new(false);
Self {
@@ -117,7 +116,7 @@ where
(lock, content)
}
fn read_from_disk(path: &str, version: &str) -> Option<ROC> {
fn read_from_disk(path: &str, version: u64) -> Option<ROC> {
if DISABLE_CACHE {
return None;
}
@@ -170,7 +169,7 @@ where
return;
}
};
let maybe_disk_content = Self::read_given_lock(&filelock, &self.path, &self.version);
let maybe_disk_content = Self::read_given_lock(&filelock, &self.path, self.version);
if let Some(disk_content) = maybe_disk_content {
self.update_with(|content| ROC::merge(content, disk_content));
}
@@ -184,7 +183,7 @@ where
self.content_changed.store(false, Ordering::Relaxed);
}
fn read_given_lock(filelock: &FileLock, path: &str, version: &str) -> Option<ROC> {
fn read_given_lock(filelock: &FileLock, path: &str, version: u64) -> Option<ROC> {
match filelock.file.metadata() {
Ok(metadata) => {
if metadata.size() == 0 {
@@ -201,11 +200,11 @@ where
};
let mut buf = BufReader::new(&filelock.file);
let disk_version: Result<String, _> = bincode::deserialize_from(buf.borrow_mut());
let disk_version: Result<u64, _> = bincode::deserialize_from(buf.borrow_mut());
match disk_version {
Ok(disk_version) => {
if disk_version != *version {
if disk_version != version {
println!("PersistentCache:: Invalid version {path}: cleaning");
Self::clear_file(path);
return None;
@@ -298,21 +297,21 @@ mod tests {
let f = move |_key| value1;
{
PCache::clear_file(path);
let disk_cache = PCache::new(path, "v0", f);
let disk_cache = PCache::new(path, 0, f);
let mut mem_cache = disk_cache.cache();
let res = mem_cache.get(key1);
assert_eq!(res, &value1);
disk_cache.backport(mem_cache);
}
{
let cache_later = PCache::new(path, "v0", f);
let cache_later = PCache::new(path, 0, f);
let mut mem_cache = cache_later.cache();
let res = mem_cache.get(key1);
assert_eq!(res, &value1);
assert!(CacheHashMap::own_new_entries(mem_cache).is_empty());
}
{
let cache_later = PCache::new(path, "v1", f);
let cache_later = PCache::new(path, 1, f);
let mut mem_cache = cache_later.cache();
let res = mem_cache.get(key1);
assert_eq!(res, &value1);