Skip to content

Commit

Permalink
Base implementation of del_data route
Browse files Browse the repository at this point in the history
  • Loading branch information
RobinP122 committed Jun 26, 2024
1 parent 2723c4f commit 712b184
Show file tree
Hide file tree
Showing 4 changed files with 139 additions and 8 deletions.
61 changes: 58 additions & 3 deletions src/api/handlers.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
use crate::api::utils::{retrieve_from_db, serialize_all_entries};
use crate::api::utils::{retrieve_from_db, delete_from_db, serialize_all_entries};
use crate::interfaces::{SetRequestData, SetSaveData};
use futures::lock::Mutex;
use serde_json::Value;
use std::collections::HashMap;
use std::sync::Arc;
use tracing::{error, info, warn};
use tracing::{error, info, warn, debug};
use valence_core::api::errors::ApiErrorType;
use valence_core::api::interfaces::CFilterConnection;
use valence_core::api::responses::{json_serialize_embed, CallResponse, JsonReply};
Expand All @@ -17,7 +17,8 @@ use valence_core::utils::serialize_data;
///
/// ### Arguments
///
/// * `payload` - Request payload
/// * `headers` - Request headers
/// * `value_id` - Value ID to retrieve
/// * `db` - Database connection
/// * `cache` - Cache connection
/// * `c_filter` - Cuckoo filter connection
Expand Down Expand Up @@ -105,6 +106,7 @@ pub async fn get_data_handler<
/// * `db` - Database connection
/// * `cache` - Cache connection
/// * `c_filter` - Cuckoo filter connection
/// * `cache_ttl` - Cache TTL
pub async fn set_data_handler<
D: KvStoreConnection + Clone + Send + 'static,
C: KvStoreConnection + CacheHandler + Clone + Send + 'static,
Expand Down Expand Up @@ -172,3 +174,56 @@ pub async fn set_data_handler<
Err(_) => r.into_err_internal(ApiErrorType::CuckooFilterInsertionFailed),
}
}

/// Route to del data from DB
///
/// /// ### Arguments
///
/// * `headers` - Request headers
/// * `value_id` - Value ID to retrieve
/// * `db` - Database connection
/// * `cache` - Cache connection
/// * `c_filter` - Cuckoo filter connection
pub async fn del_data_handler<
D: KvStoreConnection + Clone + Send + 'static,
C: KvStoreConnection + Clone + Send + 'static,
>(
headers: warp::hyper::HeaderMap,
value_id: Option<String>,
db: Arc<Mutex<D>>,
cache: Arc<Mutex<C>>,
c_filter: CFilterConnection,
) -> Result<JsonReply, JsonReply> {
let r = CallResponse::new("del_data");
info!("DEL_DATA requested with headers: {:?}", headers);

let address = headers
.get("address")
.and_then(|n| n.to_str().ok())
.unwrap_or_default();

// delete address in cuckoo filter if no value_id is provided
if value_id.is_none() && !c_filter.lock().await.delete(&address) {
error!("Address not found in cuckoo filter");
return r.into_err_internal(ApiErrorType::CuckooFilterLookupFailed);
}

debug!("delete on cuckoo filter successful");

// Check cache
let mut cache_lock_result = cache.lock().await;
let cache_result = cache_lock_result
.del_data(address, value_id.as_deref())
.await;

match cache_result {
Ok(_) => {
info!("Data deleted from cache");
return delete_from_db(db, address, value_id.as_deref()).await
}
Err(_) => {
warn!("Cache deletion failed for address: {}", address);
return r.into_err_internal(ApiErrorType::Generic("Cache deletion failed".to_string()));
}
}
}
38 changes: 36 additions & 2 deletions src/api/routes.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::api::handlers::{get_data_handler, set_data_handler};
use crate::api::handlers::{get_data_handler, set_data_handler, del_data_handler};
use futures::lock::Mutex;
use std::sync::Arc;
use tracing::debug;
Expand Down Expand Up @@ -40,7 +40,7 @@ pub fn get_data_with_id<
.and(with_node_component(cuckoo_filter))
.and_then(move |_, headers, value_id: String, cache, db, cf| {
// Add type annotation for headers parameter
debug!("GET_DATA requested");
debug!("GET_DATA requested with value_id({:?})", value_id);
map_api_res(get_data_handler(headers, Some(value_id), db, cache, cf))
})
.with(get_cors())
Expand Down Expand Up @@ -108,3 +108,37 @@ pub fn set_data<
})
.with(post_cors())
}

/// DELETE /del_data
///
/// Deletes all data associated with a given address
///
/// ### Arguments
///
/// * `db` - The database connection to use
/// * `cache` - The cache connection to use
/// * `cuckoo_filter` - The cuckoo filter connection to use
pub fn del_data<
D: KvStoreConnection + Clone + Send + 'static,
C: KvStoreConnection + Clone + Send + 'static,
>(
db: Arc<Mutex<D>>,
cache: Arc<Mutex<C>>,
cuckoo_filter: CFilterConnection,
) -> impl Filter<Extract = (impl Reply,), Error = Rejection> + Clone {
debug!("Setting up del_data route");

warp::path("del_data")
.and(warp::delete())
.and(sig_verify_middleware())
.and(warp::header::headers_cloned())
.and(with_node_component(cache))
.and(with_node_component(db))
.and(with_node_component(cuckoo_filter))
.and_then(move |_, headers, cache, db, cf| {
// Add type annotation for headers parameter
debug!("DEL_DATA requested");
map_api_res(del_data_handler(headers, None, db, cache, cf))
})
.with(get_cors())
}
43 changes: 40 additions & 3 deletions src/api/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use futures::lock::Mutex;
use serde_json::{json, Value};
use std::collections::HashMap;
use std::sync::Arc;
use tracing::info;
use tracing::{info, debug};
use valence_core::api::errors::ApiErrorType;
use valence_core::api::responses::{json_serialize_embed, CallResponse, JsonReply};
use valence_core::db::handler::KvStoreConnection;
Expand All @@ -23,7 +23,18 @@ pub async fn retrieve_from_db<D: KvStoreConnection + Clone + Send + 'static>(

let db_result: Result<Option<HashMap<String, String>>, _> =
db.lock().await.get_data(&address, value_id).await;
let value_id = value_id.unwrap().to_string();
// let value_id = value_id.unwrap().to_string();

let value_id: String = match value_id {
Some(value) => value.to_string(),
None => {
info!("No value id provided");
return r.into_err_internal(ApiErrorType::Generic("No value id provided".to_string()));
}
};

debug!("Value ID: {:?}", value_id);
debug!("DB Result: {:?}", db_result);

match db_result {
Ok(data) => match data {
Expand All @@ -47,6 +58,32 @@ pub async fn retrieve_from_db<D: KvStoreConnection + Clone + Send + 'static>(
}
}

/// Deletes data from the database
///
/// ### Arguments
///
/// * `db` - Database connection
/// * `address` - Address to retrieve data from
/// * `value_id` - Value ID to delete
pub async fn delete_from_db<D: KvStoreConnection + Clone + Send + 'static>(
db: Arc<Mutex<D>>,
address: &str,
value_id: Option<&str>,
) -> Result<JsonReply, JsonReply> {
let r = CallResponse::new("del_data");
info!("DELETE_FROM_DB requested with address: {:?}", address);

let db_result = db.lock().await.del_data(&address, value_id).await;

debug!("DB Result: {:?}", db_result);
match db_result {
Ok(_) => r.into_ok("Data deleted successfully", json_serialize_embed(address)),
Err(_) => r.into_err_internal(ApiErrorType::Generic(
format!("Failed to delete DB entry for {:?}", address)
)),
}
}

pub fn serialize_all_entries(data: HashMap<String, String>) -> HashMap<String, Value> {
let mut output = HashMap::new();

Expand All @@ -55,7 +92,7 @@ pub fn serialize_all_entries(data: HashMap<String, String>) -> HashMap<String, V
Ok(json_value) => {
output.insert(key, json_value);
}
Err(e) => {
Err(_e) => {
output.insert(key, json!(value));
}
}
Expand Down
5 changes: 5 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,11 @@ async fn main() {
config.body_limit,
config.cache_ttl,
))
.or(del_data(
db_conn.clone(),
cache_conn.clone(),
cuckoo_filter.clone(),
))
.recover(handle_rejection);

print_welcome(&db_addr, &cache_addr);
Expand Down

0 comments on commit 712b184

Please sign in to comment.