-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Showing
7 changed files
with
106 additions
and
27 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
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 was deleted.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,3 @@ | ||
pub mod authentication; | ||
pub mod cluster_router; | ||
pub mod data_store; | ||
pub mod state; |
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,51 @@ | ||
//! Datastore to record clusters' infomation | ||
use std::collections::HashMap; | ||
|
||
use crate::cluster_router::{Cluster, ClusterId}; | ||
use rucat_common::error::Result; | ||
|
||
/// Store the metadata of Cluster | ||
/// The lifetime here reprensent that of the endpoint of the SurrealDB. | ||
// DO I need to add a rwlock for it? Or the mutable reference can make sure the r-w policy? | ||
// the problem here is that `Self` is shared between threads. | ||
// Achive the Component in Spring by using Axum::state? | ||
#[derive(Clone)] | ||
pub enum DataStore<'a> { | ||
InMemoryDataStore { | ||
store: HashMap<ClusterId, Cluster>, | ||
}, | ||
/// I want to find a distributed database for storing. | ||
SurrealDB { | ||
endpoint: &'a str, | ||
}, | ||
} | ||
|
||
impl<'a> DataStore<'a> { | ||
pub fn new_in_memory() -> Self { | ||
Self::InMemoryDataStore { | ||
store: HashMap::new(), | ||
} | ||
} | ||
|
||
fn add_cluster(&mut self, cluster: Cluster) -> Result<()> { | ||
let id = cluster.get_id(); | ||
todo!() | ||
} | ||
|
||
fn get_cluster(&self, id: ClusterId) -> Option<&Cluster> { | ||
todo!() | ||
} | ||
|
||
fn delete_cluster(&mut self, id: ClusterId) -> Result<()> { | ||
todo!() | ||
} | ||
|
||
// the returned reference in Box has the same lifetime as self | ||
fn get_all_clusters(&self) -> Box<dyn Iterator<Item = &Cluster> + '_> { | ||
match self { | ||
DataStore::InMemoryDataStore { store } => Box::new(store.values()), | ||
DataStore::SurrealDB { endpoint: _ } => todo!(), | ||
} | ||
} | ||
} |
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,18 @@ | ||
use self::data_store::DataStore; | ||
|
||
pub mod data_store; | ||
|
||
#[derive(Clone)] | ||
pub struct AppState<'a> { | ||
data_store: DataStore<'a>, | ||
} | ||
|
||
impl<'a> AppState<'a> { | ||
pub fn new(data_store: DataStore<'a>) -> Self { | ||
Self { data_store } | ||
} | ||
|
||
pub fn get_data_store(&self) -> &DataStore { | ||
&self.data_store | ||
} | ||
} |