generated from PaulRBerg/hardhat-template
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Added DataStore struct to act as an injection point for persistence
- Loading branch information
Showing
9 changed files
with
80 additions
and
47 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,49 @@ | ||
use actix::{Addr, Message, Recipient}; | ||
use anyhow::Result; | ||
|
||
use crate::InMemDataStore; | ||
|
||
#[derive(Message, Clone, Debug, PartialEq, Eq, Hash)] | ||
#[rtype(result = "()")] | ||
pub struct Insert(pub Vec<u8>, pub Vec<u8>); | ||
impl Insert { | ||
pub fn key(&self) -> Vec<u8> { | ||
self.0.clone() | ||
} | ||
|
||
pub fn value(&self) -> Vec<u8> { | ||
self.1.clone() | ||
} | ||
} | ||
|
||
#[derive(Message, Clone, Debug, PartialEq, Eq, Hash)] | ||
#[rtype(result = "Option<Vec<u8>>")] | ||
pub struct Get(pub Vec<u8>); | ||
impl Get { | ||
pub fn key(&self) -> Vec<u8> { | ||
self.0.clone() | ||
} | ||
} | ||
|
||
#[derive(Clone)] | ||
pub struct DataStore(Recipient<Get>, Recipient<Insert>); | ||
impl DataStore { | ||
pub async fn read(&self, msg: Get) -> Result<Option<Vec<u8>>> { | ||
Ok(self.0.send(msg).await?) | ||
} | ||
|
||
pub fn write(&self, msg: Insert) { | ||
self.1.do_send(msg) | ||
} | ||
|
||
// use this for testing | ||
pub fn from_in_mem(addr: Addr<InMemDataStore>) -> Self { | ||
Self(addr.clone().recipient(), addr.clone().recipient()) | ||
} | ||
|
||
// // use this for production | ||
// pub fn from_sled(&data_addr: Addr<SledDb>) -> Self { | ||
// let d = data_addr.clone(); | ||
// Self(d.recipient(),d.recipient()) | ||
// } | ||
} |
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 |
---|---|---|
@@ -1,2 +1,4 @@ | ||
mod data; | ||
pub use data::*; | ||
mod in_mem; | ||
mod data_store; | ||
pub use in_mem::*; | ||
pub use data_store::*; |
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