Skip to content

Commit

Permalink
Merge pull request #15 from AIBlockOfficial/develop
Browse files Browse the repository at this point in the history
Merge develop into main; Bump to v0.1.3
  • Loading branch information
BHouwens authored Aug 20, 2024
2 parents d45e2e7 + 3ee951f commit f232d66
Show file tree
Hide file tree
Showing 5 changed files with 30 additions and 14 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "valence"
version = "0.1.2"
version = "0.1.3"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down
31 changes: 22 additions & 9 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 @@ -48,15 +49,15 @@ pub async fn get_data_handler<

// Check cache first
let mut cache_lock_result = cache.lock().await;
let cache_result: Result<Option<HashMap<String, String>>, _> = cache_lock_result
.get_data::<String>(address, value_id.as_deref())
.await;
let cache_result: Result<Option<HashMap<String, String>>, _> =
cache_lock_result.get_data::<String>(address, None).await;

match cache_result {
Ok(value) => {
match value {
Some(value) => {
info!("Data retrieved from cache");
info!("Data retrieved from cache: {:?}", value);

if let Some(id) = value_id {
if !value.contains_key(&id) {
return r.into_err_internal(ApiErrorType::ValueIdNotFound);
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
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,8 +46,8 @@ async fn main() {

info!("Cuckoo filter initialized successfully");

let routes = get_data(db_conn.clone(), cache_conn.clone(), cuckoo_filter.clone())
.or(get_data_with_id(
let routes = get_data_with_id(db_conn.clone(), cache_conn.clone(), cuckoo_filter.clone())
.or(get_data(
db_conn.clone(),
cache_conn.clone(),
cuckoo_filter.clone(),
Expand Down
2 changes: 1 addition & 1 deletion src/tests/interfaces.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::{collections::HashMap, hash::Hash};
use std::collections::HashMap;

use crate::db::handler::{CacheHandler, KvStoreConnection};
use async_trait::async_trait;
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 f232d66

Please sign in to comment.