instance and item now use dynamic dispatch

This commit is contained in:
Sebastian Moser
2024-06-13 08:22:48 +02:00
parent 33e75d9c25
commit b0306d0e33
7 changed files with 145 additions and 72 deletions

View File

@@ -25,12 +25,11 @@ pub mod subscription;
static MSG_CHANNEL_SIZE: usize = 200;
/// The Instance type is the heart of the mize system
#[derive(Clone)]
pub struct Instance<S: Store> {
pub struct Instance {
// question vec of stores???
// would mean replication logic and such things is handeled by the instance
// i think it would be better thogh to then implement a ReplicatedStore
pub store: S,
pub store: Box<dyn Store>,
peers: Arc<Mutex<Vec<Box<dyn Connection>>>>,
subs: Arc<Mutex<HashMap<MizeId, Subscription>>>,
pub id_pool: VecStringPool,
@@ -45,16 +44,16 @@ pub enum RealmId {
}
*/
impl Instance<MemStore> {
pub fn empty() -> Instance<MemStore> {
impl Instance {
pub fn empty() -> Instance {
let store = MemStore::new();
let id_pool = VecStringPool::default();
let peers = Arc::new(Mutex::new(Vec::new()));
let subs = Arc::new(Mutex::new(HashMap::new()));
let mut instance = Instance {store, peers, subs, id_pool };
let mut instance = Instance { store: Box::new(store), peers, subs, id_pool };
return instance;
}
pub fn new() -> MizeResult<Instance<MemStore>> {
pub fn new() -> MizeResult<Instance> {
trace!("[ {} ] Instance::new()", "CALL".yellow());
let mut instance = Instance::empty();
@@ -78,7 +77,7 @@ impl Instance<MemStore> {
Ok(())
}
pub fn with_config(config: ItemData) -> MizeResult<Instance<MemStore>> {
pub fn with_config(config: ItemData) -> MizeResult<Instance> {
trace!("[ {} ] Instance::with_config()", "CALL".yellow());
trace!("config: {}", config);
let mut instance = Instance::empty();
@@ -94,20 +93,35 @@ impl Instance<MemStore> {
Ok(instance)
}
pub fn migrate_to_store(&mut self, new_store: Box<dyn Store>) -> MizeResult<()> {
let old_store = &self.store;
for id in old_store.id_iter()? {
let data = self.store.get_value_data_full(self.id_from_string(id?))?;
let id_of_new_store = new_store.new_id()?;
new_store.set(self.id_from_string(id_of_new_store), data.to_owned())?;
};
self.store = new_store;
Ok(())
}
}
impl<S: Store> Instance<S> {
pub fn new_item(&self) -> MizeResult<Item<S>> {
impl Instance {
pub fn new_item(&self) -> MizeResult<Item> {
let id = self.id_from_string(self.store.new_id()?);
return Ok(Item::new(id, &self));
}
pub fn get<I: IntoMizeId<S>>(&self, id: I) -> MizeResult<Item<S>> {
pub fn get<I: IntoMizeId>(&self, id: I) -> MizeResult<Item> {
let id = id.to_mize_id(self);
return Ok(Item::new(id, &self));
}
pub fn set<I: IntoMizeId<S>, V: Into<ItemData>>(&mut self, id: I, value: V) -> MizeResult<()> {
pub fn set<I: IntoMizeId, V: Into<ItemData>>(&mut self, id: I, value: V) -> MizeResult<()> {
let id = id.to_mize_id(self);
let item_data = value.into();
let mut item = self.get(id.clone())?;
@@ -115,7 +129,7 @@ impl<S: Store> Instance<S> {
Ok(())
}
pub fn new_id<T: IntoMizeId<S>>(&self, value: T) -> MizeId {
pub fn new_id<T: IntoMizeId>(&self, value: T) -> MizeId {
value.to_mize_id(self)
}
@@ -130,22 +144,9 @@ impl<S: Store> Instance<S> {
//fn from(value: T) -> Self {
//}
//}
pub fn migrate_to_store<New: Store>(&mut self, new_store: New) -> MizeResult<Instance<New>> {
let old_store = self.store;
for id in old_store.id_iter()? {
let data = self.store.get_value_data_full(self.id_from_string(id))?;
let id_of_new_store = new_store.new_id()?;
new_store.set(self.id_from_string(id_of_new_store), data.to_owned())?;
};
self.store = new_store as S;
Ok(Instance { store: new_store, subs: self.subs, peers: self.peers, id_pool: self.id_pool })
}
}
impl<S: Store> fmt::Debug for Instance<S> {
impl fmt::Debug for Instance {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Mize Instance with subs: {:?}", self.subs,)
}