some refactoring
This commit is contained in:
parent
490e4bffbd
commit
62944783ca
@ -160,13 +160,10 @@ impl MathApp {
|
||||
assume(!cached_data.is_empty());
|
||||
}
|
||||
|
||||
if commit == build::SHORT_COMMIT {
|
||||
if commit == build::SHORT_COMMIT.chars().map(|c| c as u8).collect::<Vec<u8>>().as_slice() {
|
||||
tracing::info!("Reading decompression cache. Bytes: {}", cached_data.len());
|
||||
return Some(cached_data.to_vec());
|
||||
} else {
|
||||
tracing::info!("Decompression cache are invalid (build: {}, previous: {})", build::SHORT_COMMIT, commit);
|
||||
|
||||
// is invalid
|
||||
None
|
||||
}
|
||||
}
|
||||
@ -179,7 +176,8 @@ impl MathApp {
|
||||
}
|
||||
|
||||
tracing::info!("Setting decompression cache");
|
||||
let saved_data = &crate::misc::hashed_storage_create(&build::SHORT_COMMIT.as_bytes(), data);
|
||||
let commit: [u8; crate::misc::HASH_LENGTH] = unsafe { build::SHORT_COMMIT.as_bytes().try_into().unwrap_unchecked() };
|
||||
let saved_data = &crate::misc::hashed_storage_create(commit, data);
|
||||
tracing::info!("Bytes: {}", saved_data.len());
|
||||
get_localstorage().set_item(DATA_NAME, saved_data).expect("failed to set local storage cache");
|
||||
}
|
||||
@ -200,13 +198,11 @@ impl MathApp {
|
||||
assume(!func_data.is_empty());
|
||||
}
|
||||
|
||||
if commit == build::SHORT_COMMIT {
|
||||
if commit == build::SHORT_COMMIT.chars().map(|c| c as u8).collect::<Vec<u8>>().as_slice() {
|
||||
tracing::info!("Reading previous function data");
|
||||
let function_manager: FunctionManager = bincode::deserialize(&func_data).ok()?;
|
||||
return Some(function_manager);
|
||||
} else {
|
||||
tracing::info!("Previous functions are invalid due to differing commits (build: {}, previous: {})", build::SHORT_COMMIT, commit);
|
||||
// is invalid
|
||||
None
|
||||
}
|
||||
}
|
||||
@ -619,10 +615,15 @@ impl App for MathApp {
|
||||
{
|
||||
tracing::info!("Setting current functions");
|
||||
let saved_data = &crate::misc::hashed_storage_create(
|
||||
&build::SHORT_COMMIT
|
||||
.chars()
|
||||
.map(|c| c as u8)
|
||||
.collect::<Vec<u8>>(),
|
||||
unsafe {
|
||||
build::SHORT_COMMIT
|
||||
.chars()
|
||||
.map(|c| c as u8)
|
||||
.collect::<Vec<u8>>()
|
||||
.as_slice()
|
||||
.try_into()
|
||||
.unwrap_unchecked()
|
||||
},
|
||||
bincode::serialize(&self.functions).unwrap().as_slice(),
|
||||
);
|
||||
// tracing::info!("Bytes: {}", saved_data.len());
|
||||
|
||||
11
src/misc.rs
11
src/misc.rs
@ -311,18 +311,15 @@ pub fn almost_variable(x: f64) -> Option<char> {
|
||||
pub const HASH_LENGTH: usize = 8;
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn hashed_storage_create(hash: &[u8], data: &[u8]) -> String {
|
||||
debug_assert_eq!(hash.len(), HASH_LENGTH);
|
||||
pub fn hashed_storage_create(hash: [u8; HASH_LENGTH], data: &[u8]) -> String {
|
||||
debug_assert!(!data.is_empty());
|
||||
|
||||
unsafe {
|
||||
assume(!data.is_empty());
|
||||
assume(hash.len() == HASH_LENGTH);
|
||||
assume(!hash.is_empty());
|
||||
}
|
||||
|
||||
// cannot use `from_utf8` seems to break on wasm. no clue why
|
||||
[hash, data]
|
||||
[&hash, data]
|
||||
.concat()
|
||||
.iter()
|
||||
.map(|b| *b as char)
|
||||
@ -330,7 +327,7 @@ pub fn hashed_storage_create(hash: &[u8], data: &[u8]) -> String {
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
pub fn hashed_storage_read(data: String) -> (String, Vec<u8>) {
|
||||
pub fn hashed_storage_read(data: String) -> ([u8; HASH_LENGTH], Vec<u8>) {
|
||||
debug_assert!(data.len() > HASH_LENGTH);
|
||||
unsafe {
|
||||
assume(!data.is_empty());
|
||||
@ -351,7 +348,7 @@ pub fn hashed_storage_read(data: String) -> (String, Vec<u8>) {
|
||||
}
|
||||
|
||||
(
|
||||
hash.iter().map(|c| *c as char).collect::<String>(),
|
||||
unsafe { hash.try_into().unwrap_unchecked() },
|
||||
cached_data.to_vec(),
|
||||
)
|
||||
}
|
||||
|
||||
@ -97,9 +97,15 @@ fn hashed_storage() {
|
||||
.chars()
|
||||
.map(|c| c as u8)
|
||||
.collect::<Vec<u8>>();
|
||||
let storage = hashed_storage_create(commit.as_slice(), data.as_slice());
|
||||
let storage = hashed_storage_create(
|
||||
commit
|
||||
.as_slice()
|
||||
.try_into()
|
||||
.expect("cannot turn into [u8; 8]"),
|
||||
data.as_slice(),
|
||||
);
|
||||
|
||||
let read = hashed_storage_read(storage);
|
||||
assert_eq!(read.0.chars().map(|c| c as u8).collect::<Vec<u8>>(), commit);
|
||||
assert_eq!(read.0.to_vec(), commit);
|
||||
assert_eq!(read.1, data);
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user