-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #36 from nociza/storage-macro
Storage Macro Redis Support
- Loading branch information
Showing
9 changed files
with
228 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
[env] | ||
RUST_TEST_THREADS = "1" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use async_recursion::async_recursion; | ||
use redis::Commands; | ||
|
||
type Error = Box<dyn std::error::Error + Send + Sync + 'static>; | ||
|
||
impl crate::application::CoLink { | ||
fn _get_con_from_address(&self, redis_address: &str) -> Result<redis::Connection, Error> { | ||
let client = redis::Client::open(redis_address)?; | ||
let con = client.get_connection()?; | ||
Ok(con) | ||
} | ||
|
||
async fn _get_con_from_stored_credentials( | ||
&self, | ||
key_path: &str, | ||
) -> Result<redis::Connection, Error> { | ||
let redis_url_key = format!("{}:redis_url", key_path); | ||
let redis_url = self.read_entry(redis_url_key.as_str()).await?; | ||
let redis_url_string = String::from_utf8(redis_url)?; | ||
self._get_con_from_address(redis_url_string.as_str()) | ||
} | ||
|
||
#[async_recursion] | ||
pub(crate) async fn _create_entry_redis( | ||
&self, | ||
address: &str, | ||
key_name: &str, | ||
payload: &[u8], | ||
) -> Result<String, Error> { | ||
let mut con = self._get_con_from_stored_credentials(address).await?; | ||
let response: i32 = con.set_nx(key_name, payload)?; | ||
if response == 0 { | ||
Err("key already exists.")? | ||
} | ||
Ok(response.to_string()) | ||
} | ||
|
||
#[async_recursion] | ||
pub(crate) async fn _read_entry_redis( | ||
&self, | ||
address: &str, | ||
key_name: &str, | ||
) -> Result<Vec<u8>, Error> { | ||
let mut con = self._get_con_from_stored_credentials(address).await?; | ||
let response: Option<Vec<u8>> = con.get(key_name)?; | ||
match response { | ||
Some(response) => Ok(response), | ||
None => Err("key does not exist.")?, | ||
} | ||
} | ||
|
||
#[async_recursion] | ||
pub(crate) async fn _update_entry_redis( | ||
&self, | ||
address: &str, | ||
key_name: &str, | ||
payload: &[u8], | ||
) -> Result<String, Error> { | ||
let mut con = self._get_con_from_stored_credentials(address).await?; | ||
let response = con.set(key_name, payload)?; | ||
Ok(response) | ||
} | ||
|
||
#[async_recursion] | ||
pub(crate) async fn _delete_entry_redis( | ||
&self, | ||
address: &str, | ||
key_name: &str, | ||
) -> Result<String, Error> { | ||
let mut con = self._get_con_from_stored_credentials(address).await?; | ||
let response: i32 = con.del(key_name)?; | ||
if response == 0 { | ||
Err("key does not exist.")? | ||
} | ||
Ok(response.to_string()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
use colink::{ | ||
extensions::instant_server::{InstantRegistry, InstantServer}, | ||
CoLink, | ||
}; | ||
use rand::Rng; | ||
|
||
#[tokio::test] | ||
async fn test_storage_macro_chunk() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> | ||
{ | ||
let _ir = InstantRegistry::new(); | ||
let is = InstantServer::new(); | ||
let cl = is.get_colink().switch_to_generated_user().await?; | ||
|
||
let key_name = "storage_macro_test_chunk:$chunk"; | ||
test_crud(&cl, key_name).await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[tokio::test] | ||
async fn test_storage_macro_redis() -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> | ||
{ | ||
let _ir = InstantRegistry::new(); | ||
let is = InstantServer::new(); | ||
let cl = is.get_colink().switch_to_generated_user().await?; | ||
|
||
cl.create_entry("storage_macro_test_redis:redis_url", b"redis://localhost") | ||
.await?; | ||
let key_name = "storage_macro_test_redis:$redis:redis_key"; | ||
test_crud(&cl, key_name).await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
#[ignore] | ||
#[tokio::test] | ||
async fn test_storage_macro_chunk_redis( | ||
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { | ||
let _ir = InstantRegistry::new(); | ||
let is = InstantServer::new(); | ||
let cl = is.get_colink().switch_to_generated_user().await?; | ||
|
||
cl.create_entry( | ||
"test_storage_macro_chunk_redis:redis_url", | ||
b"redis://localhost", | ||
) | ||
.await?; | ||
let key_name = "test_storage_macro_chunk_redis:$redis:redis_chunk:$chunk"; | ||
test_crud(&cl, key_name).await?; | ||
|
||
Ok(()) | ||
} | ||
|
||
async fn test_crud( | ||
cl: &CoLink, | ||
key_name: &str, | ||
) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> { | ||
let payload = rand::thread_rng() | ||
.sample_iter(&rand::distributions::Standard) | ||
.take(5e6 as usize) | ||
.collect::<Vec<u8>>(); | ||
cl.create_entry(key_name, &payload.clone()).await?; | ||
assert!(cl.create_entry(key_name, b"").await.is_err()); | ||
let data = cl.read_entry(key_name).await?; | ||
assert_eq!(data, payload); | ||
let new_payload = rand::thread_rng() | ||
.sample_iter(&rand::distributions::Standard) | ||
.take(3e6 as usize) | ||
.collect::<Vec<u8>>(); | ||
cl.update_entry(key_name, &new_payload.clone()).await?; | ||
let data = cl.read_entry(key_name).await?; | ||
assert_eq!(data, new_payload); | ||
cl.delete_entry(key_name).await?; | ||
assert!(cl.read_entry(key_name).await.is_err()); | ||
assert!(cl.delete_entry(key_name).await.is_err()); | ||
Ok(()) | ||
} |