Skip to content

Commit

Permalink
Fix for cuckoo filter not saving values
Browse files Browse the repository at this point in the history
  • Loading branch information
BHouwens committed Aug 20, 2024
1 parent 334715c commit a8ba77c
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 6 deletions.
23 changes: 18 additions & 5 deletions src/api/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use crate::api::utils::{delete_from_db, retrieve_from_db, serialize_all_entries};
use crate::db::handler::{CacheHandler, KvStoreConnection};
use crate::interfaces::{SetRequestData, SetSaveData};
use crate::utils::save_cuckoo_filter_to_disk;
use futures::lock::Mutex;
use serde_json::Value;
use std::collections::HashMap;
Expand Down Expand Up @@ -143,7 +144,10 @@ pub async fn set_data_handler<
.lock()
.await
.expire_entry(&payload.address, cache_ttl)
.await;
.await
.map_err(|err| {
error!("Failed to expire cache entry: {:?}", err);
});

db.lock()
.await
Expand All @@ -164,10 +168,19 @@ pub async fn set_data_handler<
};

match c_filter_result {
Ok(_) => r.into_ok(
"Data set successfully",
json_serialize_embed(payload.address),
),
Ok(_) => {
// Save latest result to disk
let cf_lock = c_filter.lock().await;
if let Err(err) = save_cuckoo_filter_to_disk(&cf_lock, db).await {
error!("Failed to save cuckoo filter to disk: {:?}", err);
}

// Return success
r.into_ok(
"Data set successfully",
json_serialize_embed(payload.address),
)
}
Err(_) => r.into_err_internal(ApiErrorType::CuckooFilterInsertionFailed),
}
}
Expand Down
5 changes: 4 additions & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -152,7 +152,10 @@ pub async fn init_cuckoo_filter<T: KvStoreConnection>(
db: Arc<Mutex<T>>,
) -> Result<CuckooFilter<DefaultHasher>, String> {
match load_cuckoo_filter_from_disk(db.clone()).await {
Ok(cf) => Ok(cf),
Ok(cf) => {
info!("Cuckoo filter loaded from DB");
Ok(cf)
}
Err(_) => {
info!("No cuckoo filter found in DB, initializing new one");
let cf = CuckooFilter::new();
Expand Down

0 comments on commit a8ba77c

Please sign in to comment.