simplify sub system init
This commit is contained in:
@@ -60,19 +60,11 @@ impl crdt_enc::key_cryptor::KeyCryptor for KeyHandler {
|
||||
.data
|
||||
.lock()
|
||||
.map_err(|err| Error::msg(err.to_string()))?;
|
||||
data.info = Some(core.info());
|
||||
data.core = Some(dyn_clone::clone_box(core));
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn set_info(&self, info: &Info) -> Result<()> {
|
||||
let mut data = self
|
||||
.data
|
||||
.lock()
|
||||
.map_err(|err| Error::msg(err.to_string()))?;
|
||||
data.info = Some(info.clone());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn set_remote_meta(
|
||||
&self,
|
||||
new_remote_meta: Option<MVReg<VersionBytes, Uuid>>,
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
utils::{VersionBytes, VersionBytesRef},
|
||||
CoreSubHandle, Info,
|
||||
CoreSubHandle,
|
||||
};
|
||||
use ::anyhow::Result;
|
||||
use ::async_trait::async_trait;
|
||||
@@ -17,10 +17,6 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn set_info(&self, _info: &Info) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn set_remote_meta(&self, _data: Option<MVReg<VersionBytes, Uuid>>) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -1,6 +1,6 @@
|
||||
use crate::{
|
||||
utils::{VersionBytes, VersionBytesRef},
|
||||
CoreSubHandle, Info,
|
||||
CoreSubHandle,
|
||||
};
|
||||
use ::anyhow::Result;
|
||||
use ::async_trait::async_trait;
|
||||
@@ -24,10 +24,6 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn set_info(&self, _info: &Info) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn set_remote_meta(&self, _data: Option<MVReg<VersionBytes, Uuid>>) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -40,6 +40,8 @@ pub trait CoreSubHandle
|
||||
where
|
||||
Self: 'static + Debug + Send + Sync + DynClone,
|
||||
{
|
||||
fn info(&self) -> Info;
|
||||
|
||||
async fn compact(&self) -> Result<()>;
|
||||
async fn read_remote(&self) -> Result<()>;
|
||||
async fn read_remote_meta(&self) -> Result<()>;
|
||||
@@ -72,6 +74,10 @@ where
|
||||
C: Cryptor,
|
||||
KC: KeyCryptor,
|
||||
{
|
||||
fn info(&self) -> Info {
|
||||
self.info()
|
||||
}
|
||||
|
||||
async fn compact(&self) -> Result<()> {
|
||||
self.compact().await
|
||||
}
|
||||
@@ -206,6 +212,7 @@ struct CoreMutData<S> {
|
||||
state: StateWrapper<S>,
|
||||
read_states: HashSet<String>,
|
||||
read_remote_metas: HashSet<String>,
|
||||
info: Option<Info>,
|
||||
}
|
||||
|
||||
impl<S, ST, C, KC> Core<S, ST, C, KC>
|
||||
@@ -225,7 +232,7 @@ where
|
||||
C: Cryptor,
|
||||
KC: KeyCryptor,
|
||||
{
|
||||
pub async fn open(options: OpenOptions<ST, C, KC>) -> Result<(Arc<Self>, Info)> {
|
||||
pub async fn open(options: OpenOptions<ST, C, KC>) -> Result<Arc<Self>> {
|
||||
let core_data = SyncMutex::new(CoreMutData {
|
||||
local_meta: None,
|
||||
remote_meta: RemoteMeta::default(),
|
||||
@@ -236,6 +243,7 @@ where
|
||||
},
|
||||
read_states: HashSet::new(),
|
||||
read_remote_metas: HashSet::new(),
|
||||
info: None,
|
||||
});
|
||||
|
||||
let mut supported_data_versions = options.supported_data_versions;
|
||||
@@ -251,12 +259,6 @@ where
|
||||
apply_ops_lock: AsyncMutex::new(()),
|
||||
});
|
||||
|
||||
futures::try_join![
|
||||
core.storage.init(&core),
|
||||
core.cryptor.init(&core),
|
||||
core.key_cryptor.init(&core),
|
||||
]?;
|
||||
|
||||
let local_meta = core
|
||||
.storage
|
||||
.load_local_meta()
|
||||
@@ -293,13 +295,14 @@ where
|
||||
|
||||
core.with_mut_data(|data| {
|
||||
data.local_meta = Some(local_meta);
|
||||
data.info = Some(info.clone());
|
||||
Ok(())
|
||||
})?;
|
||||
|
||||
futures::try_join![
|
||||
core.storage.set_info(&info),
|
||||
core.cryptor.set_info(&info),
|
||||
core.key_cryptor.set_info(&info),
|
||||
core.storage.init(&core),
|
||||
core.cryptor.init(&core),
|
||||
core.key_cryptor.init(&core),
|
||||
]?;
|
||||
|
||||
core.read_remote_meta_(true).await?;
|
||||
@@ -317,7 +320,19 @@ where
|
||||
core.key_cryptor.set_keys(keys).await?;
|
||||
}
|
||||
|
||||
Ok((core, info))
|
||||
Ok(core)
|
||||
}
|
||||
|
||||
pub fn info(self: &Arc<Self>) -> Info {
|
||||
self.with_mut_data(|data| {
|
||||
let info = data
|
||||
.info
|
||||
.as_ref()
|
||||
.expect("info not set, yet. Do not call this fn in the init phase")
|
||||
.clone();
|
||||
Ok(info)
|
||||
})
|
||||
.unwrap()
|
||||
}
|
||||
|
||||
fn with_mut_data<F, R>(self: &Arc<Self>, f: F) -> Result<R>
|
||||
|
||||
@@ -1,4 +1,4 @@
|
||||
use crate::{utils::VersionBytes, CoreSubHandle, Info};
|
||||
use crate::{utils::VersionBytes, CoreSubHandle};
|
||||
use ::anyhow::Result;
|
||||
use ::async_trait::async_trait;
|
||||
use ::crdts::MVReg;
|
||||
@@ -14,10 +14,6 @@ where
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn set_info(&self, _info: &Info) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
async fn set_remote_meta(&self, _data: Option<MVReg<VersionBytes, Uuid>>) -> Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
@@ -25,7 +25,8 @@ async fn main() -> Result<()> {
|
||||
supported_data_versions: SUPPORTED_DATA_VERSIONS.iter().cloned().collect(),
|
||||
current_data_version: CURRENT_DATA_VERSION,
|
||||
};
|
||||
let (repo, info) = crdt_enc::Core::open(open_options).await?;
|
||||
let repo = crdt_enc::Core::open(open_options).await?;
|
||||
let info = repo.info();
|
||||
|
||||
// let actor_id = repo.actor_id();
|
||||
|
||||
|
||||
Reference in New Issue
Block a user