Repository navigation
Eng 1510 get rid of client data #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
996b3bc
c50445c
3406f9a
a8c89c6
9263be9
a4a7396
6efae74
6f27898
e13ecfa
8cc00df
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,3 +1,3 @@ | ||
| #!/usr/bin/env bash | ||
| #MISE description="Run tests" | ||
| cargo test -p sqlite-compress | ||
| cargo test -p sqlite-compress -- --test-threads=1 |
This file was deleted.
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,5 +1,5 @@ | ||
| mod connection; | ||
| mod lru; | ||
| mod pool; | ||
| mod prepared; | ||
|
|
||
| pub(crate) use connection::DictCache; | ||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,92 @@ | ||
| use std::{ | ||
| collections::{HashMap, hash_map::Entry}, | ||
| sync::{Arc, LazyLock}, | ||
| }; | ||
|
|
||
| use parking_lot::Mutex; | ||
| use zstd::dict::{DecoderDictionary, EncoderDictionary}; | ||
|
|
||
| use crate::{dict::DictKey, functions::Level}; | ||
|
|
||
| const MAX_PREPARED_DICTS: usize = 64; | ||
|
|
||
| type PreparedMap<T> = HashMap<DictKey, Arc<T>>; | ||
|
|
||
| static ENCODERS: LazyLock<Mutex<PreparedMap<EncoderDictionary<'static>>>> = | ||
| LazyLock::new(|| Mutex::new(HashMap::with_capacity(MAX_PREPARED_DICTS))); | ||
|
|
||
| static DECODERS: LazyLock<Mutex<PreparedMap<DecoderDictionary<'static>>>> = | ||
| LazyLock::new(|| Mutex::new(HashMap::with_capacity(MAX_PREPARED_DICTS))); | ||
|
|
||
| pub(crate) fn get_encoder( | ||
| key: &DictKey, | ||
| level: Level, | ||
| load: impl FnOnce() -> Result<Vec<u8>, crate::DictError>, | ||
| ) -> Result<Arc<EncoderDictionary<'static>>, crate::DictError> { | ||
| if let Some(dict) = ENCODERS.lock().get(key).cloned() { | ||
| return Ok(dict); | ||
| } | ||
|
|
||
| let raw = load()?; | ||
|
|
||
| let mut cache = ENCODERS.lock(); | ||
| if let Some(dict) = cache.get(key).cloned() { | ||
| return Ok(dict); | ||
| } | ||
|
|
||
| let encoder = Arc::new(EncoderDictionary::copy(&raw, level.get())); | ||
| insert_capped(&mut cache, key.clone(), encoder.clone()); | ||
| Ok(encoder) | ||
| } | ||
|
|
||
| pub(crate) fn get_decoder( | ||
| key: &DictKey, | ||
| load: impl FnOnce() -> Result<Vec<u8>, crate::DictError>, | ||
| ) -> Result<Arc<DecoderDictionary<'static>>, crate::DictError> { | ||
| if let Some(dict) = DECODERS.lock().get(key).cloned() { | ||
| return Ok(dict); | ||
| } | ||
|
|
||
| let raw = load()?; | ||
|
|
||
| let mut cache = DECODERS.lock(); | ||
| if let Some(dict) = cache.get(key).cloned() { | ||
| return Ok(dict); | ||
| } | ||
|
|
||
| let decoder = Arc::new(DecoderDictionary::copy(&raw)); | ||
| insert_capped(&mut cache, key.clone(), decoder.clone()); | ||
| Ok(decoder) | ||
| } | ||
|
|
||
| fn insert_capped<T>(map: &mut PreparedMap<T>, key: DictKey, value: Arc<T>) { | ||
| let at_cap = map.len() >= MAX_PREPARED_DICTS; | ||
|
|
||
| match map.entry(key) { | ||
| Entry::Occupied(mut e) => { | ||
| e.insert(value); | ||
| } | ||
| Entry::Vacant(e) => { | ||
| if at_cap { | ||
| let key = e.into_key(); | ||
| map.clear(); | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This clears the whole map at cap, you sure it is what we want?
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yep the downside of this is that you need to fill it again with a lot of cache misses but i don't think we'll be doing clear that much. i mean the cost of having an LRU instead is acceptable |
||
| map.insert(key, value); | ||
| } else { | ||
| e.insert(value); | ||
| } | ||
| } | ||
| } | ||
| } | ||
|
|
||
| pub(crate) fn insert_prepared(key: DictKey, dictionary: &[u8], level: Level) { | ||
| insert_capped( | ||
| &mut ENCODERS.lock(), | ||
| key.clone(), | ||
| Arc::new(EncoderDictionary::copy(dictionary, level.get())), | ||
| ); | ||
| insert_capped( | ||
| &mut DECODERS.lock(), | ||
| key, | ||
| Arc::new(DecoderDictionary::copy(dictionary)), | ||
| ); | ||
| } | ||
Uh oh!
There was an error while loading. Please reload this page.