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