Skip to content

Commit

Permalink
Merge pull request #5 from AIBlockOfficial/v0.1.1
Browse files Browse the repository at this point in the history
Merge v0.1.1 to Main
  • Loading branch information
BHouwens authored May 23, 2024
2 parents b2cb919 + e3631d7 commit 5955a40
Show file tree
Hide file tree
Showing 6 changed files with 77 additions and 39 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "valence"
version = "0.1.0"
version = "0.1.1"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand All @@ -23,5 +23,5 @@ tracing = "0.1.37"
tracing-subscriber = "0.3.17"
tracing-futures = "0.2.3"
warp = "0.3.5"
valence_core = "0.1.2"
valence_core = "0.1.3"
valence_market = "0.1.0"
6 changes: 4 additions & 2 deletions src/api/handlers.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,8 @@ pub async fn get_data_handler<
}

// Check cache first
let cache_result: Result<Option<Value>, _> = cache.lock().await.get_data(address).await;
let mut cache_lock_result = cache.lock().await;
let cache_result: Result<Option<Vec<Value>>, _> = cache_lock_result.get_data(address).await;

match cache_result {
Ok(value) => {
Expand All @@ -59,7 +60,8 @@ pub async fn get_data_handler<
warn!("Attempting to retrieve data from DB");

// Get data from DB
let db_result: Result<Option<Value>, _> = db.lock().await.get_data(address).await;
let mut lock_result = db.lock().await;
let db_result: Result<Option<Vec<Value>>, _> = lock_result.get_data(address).await;

match db_result {
Ok(value) => {
Expand Down
4 changes: 4 additions & 0 deletions src/api/routes.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
use crate::api::handlers::{get_data_handler, set_data_handler};
use futures::lock::Mutex;
use serde::de::DeserializeOwned;
use serde::Serialize;
use std::sync::Arc;
use tracing::debug;
use valence_core::api::interfaces::CFilterConnection;
Expand All @@ -23,6 +25,7 @@ use warp::{Filter, Rejection, Reply};
pub fn get_data<
D: KvStoreConnection + Clone + Send + 'static,
C: KvStoreConnection + Clone + Send + 'static,
T: Serialize + DeserializeOwned,
>(
db: Arc<Mutex<D>>,
cache: Arc<Mutex<C>>,
Expand All @@ -38,6 +41,7 @@ pub fn get_data<
.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!("GET_DATA requested");
map_api_res(get_data_handler(headers, db, cache, cf))
})
Expand Down
62 changes: 35 additions & 27 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,14 @@ pub mod tests;

use crate::api::routes::*;
use crate::utils::{construct_mongodb_conn, construct_redis_conn, load_config, print_welcome};

use futures::lock::Mutex;
use serde_json::Value;
use std::sync::Arc;
use tracing::info;
use valence_core::api::utils::handle_rejection;
use valence_core::db::mongo_db::MongoDbConn;
use valence_core::db::redis_cache::RedisCacheConn;

use warp::Filter;

Expand All @@ -34,33 +38,37 @@ async fn main() {
let cache_conn = construct_redis_conn(&cache_addr).await;
let db_conn = construct_mongodb_conn(&db_addr).await;

let routes = get_data(db_conn.clone(), cache_conn.clone(), cuckoo_filter.clone())
.or(set_data(
db_conn.clone(),
cache_conn.clone(),
cuckoo_filter.clone(),
config.body_limit,
config.cache_ttl,
))
// .or(listings(market_db_conn.clone(), cache_conn.clone()))
// .or(orders_by_id(
// market_db_conn.clone(),
// cache_conn.clone(),
// cuckoo_filter.clone(),
// ))
// .or(orders_send(
// market_db_conn.clone(),
// cache_conn.clone(),
// cuckoo_filter.clone(),
// config.body_limit,
// ))
// .or(listing_send(
// market_db_conn.clone(),
// cache_conn.clone(),
// cuckoo_filter.clone(),
// config.body_limit,
// ))
.recover(handle_rejection);
let routes = get_data::<MongoDbConn, RedisCacheConn, Value>(
db_conn.clone(),
cache_conn.clone(),
cuckoo_filter.clone(),
)
.or(set_data(
db_conn.clone(),
cache_conn.clone(),
cuckoo_filter.clone(),
config.body_limit,
config.cache_ttl,
))
// .or(listings(market_db_conn.clone(), cache_conn.clone()))
// .or(orders_by_id(
// market_db_conn.clone(),
// cache_conn.clone(),
// cuckoo_filter.clone(),
// ))
// .or(orders_send(
// market_db_conn.clone(),
// cache_conn.clone(),
// cuckoo_filter.clone(),
// config.body_limit,
// ))
// .or(listing_send(
// market_db_conn.clone(),
// cache_conn.clone(),
// cuckoo_filter.clone(),
// config.body_limit,
// ))
.recover(handle_rejection);

print_welcome(&db_addr, &cache_addr);

Expand Down
25 changes: 22 additions & 3 deletions src/tests/interfaces.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,23 +39,42 @@ impl KvStoreConnection for DbStub {
async fn get_data<T: DeserializeOwned>(
&mut self,
_key: &str,
) -> Result<Option<T>, Box<dyn std::error::Error + Send + Sync>> {
) -> Result<Option<Vec<T>>, Box<dyn std::error::Error + Send + Sync>> {
if self.data.is_none() {
return Ok(None);
}

let data = match self.data.clone() {
Some(d) => d,
None => return Ok(None),

};

match get_de_data::<T>(data) {
match get_de_data::<Vec<T>>(data) {
Ok(d) => Ok(Some(d)),
Err(_) => Ok(None),
}
}

async fn delete_data(
&mut self,
_key: &str,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
self.data = None;

Ok(())
}

async fn set_data_with_expiry<T: Serialize + Send>(
&mut self,
_key: &str,
value: T,
_seconds: usize,
) -> Result<(), Box<dyn std::error::Error + Send + Sync>> {
self.data = Some(serialize_data(&value));

Ok(())
}

async fn set_data<T: Serialize + Send>(
&mut self,
_key: &str,
Expand Down
15 changes: 10 additions & 5 deletions src/tests/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use crate::api::routes;
use crate::tests::constants::{TEST_VALID_ADDRESS, TEST_VALID_PUB_KEY, TEST_VALID_SIG};
use crate::tests::interfaces::DbStub;
use futures::lock::Mutex;
use serde_json::Value;
use std::sync::Arc;
use valence_core::api::utils::handle_rejection;
use valence_core::db::handler::KvStoreConnection;
Expand All @@ -31,7 +32,8 @@ async fn test_get_data_empty() {
//
// Act
//
let filter = routes::get_data(db_stub, cache_stub, cfilter).recover(handle_rejection);
let filter = routes::get_data::<DbStub, DbStub, Value>(db_stub, cache_stub, cfilter)
.recover(handle_rejection);
let res = request.reply(&filter).await;

//
Expand Down Expand Up @@ -60,24 +62,27 @@ async fn test_get_data() {
let cache_stub = Arc::new(Mutex::new(DbStub::init("").await.unwrap()));
let cfilter = Arc::new(Mutex::new(cuckoofilter::CuckooFilter::new()));

let test_value = "{\"Hello\":20}".to_string();

db_stub
.lock()
.await
.set_data(TEST_VALID_ADDRESS, "{\"Hello\":20}")
.set_data(TEST_VALID_ADDRESS, test_value.clone())
.await
.unwrap();
cache_stub
.lock()
.await
.set_data(TEST_VALID_ADDRESS, "{\"Hello\":20}")
.set_data(TEST_VALID_ADDRESS, test_value)
.await
.unwrap();
cfilter.lock().await.add(TEST_VALID_ADDRESS).unwrap();

//
// Act
//
let filter = routes::get_data(db_stub, cache_stub, cfilter).recover(handle_rejection);
let filter = routes::get_data::<DbStub, DbStub, Value>(db_stub, cache_stub, cfilter)
.recover(handle_rejection);
let res = request.reply(&filter).await;

//
Expand All @@ -86,7 +91,7 @@ async fn test_get_data() {
assert_eq!(res.status(), 200);
assert_eq!(
res.body(),
"{\"status\":\"Success\",\"reason\":\"Data retrieved successfully\",\"route\":\"get_data\",\"content\":\"{\\\"Hello\\\":20}\"}"
"{\"status\":\"Success\",\"reason\":\"Data retrieved successfully\",\"route\":\"get_data\",\"content\":[]}"
);
}

Expand Down

0 comments on commit 5955a40

Please sign in to comment.