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,)
}

View File

@@ -15,14 +15,14 @@ pub trait Store {
// is implemented, so that there can be a multithreaded implementation
//fn get(self, id: MizeId) -> MizeResult<Item<Self>> where Self: Sized;
fn set<T: Into<ItemData>>(&self, id: MizeId, data: T) -> MizeResult<()>;
fn set(&self, id: MizeId, data: ItemData) -> MizeResult<()>;
// in the future should implement transactions, ....
// funcs to do with links, backlinks
fn get_links(&self, item: Item<Self>) -> MizeResult<Vec<MizeId>> where Self: Sized;
fn get_links(&self, item: Item) -> MizeResult<Vec<MizeId>>;
fn get_backlinks(&self, item: Item<Self>) -> MizeResult<Vec<MizeId>> where Self: Sized;
fn get_backlinks(&self, item: Item) -> MizeResult<Vec<MizeId>>;
fn new_id(&self) -> MizeResult<String>;
@@ -30,6 +30,40 @@ pub trait Store {
fn get_value_data_full(&self, id: MizeId) -> MizeResult<ItemData>;
fn id_iter(&self) -> MizeResult<impl Iterator<Item=String> + '_>;
fn id_iter(&self) -> MizeResult<IdIter>;
fn next_id(&self, prev_id: &str) -> MizeResult<Option<String>>;
fn first_id(&self) -> MizeResult<String>;
}
pub struct IdIter {
cur_id: String,
store: Box<dyn Store>,
}
impl IdIter {
pub fn new(store: Box<dyn Store>) -> MizeResult<IdIter> {
Ok(IdIter { cur_id: store.first_id()?, store })
}
}
impl Iterator for IdIter {
type Item = MizeResult<String>;
fn next(&mut self) -> Option<Self::Item> {
match self.store.next_id(&self.cur_id) {
Err(err) => {
Some(Err(err))
},
Ok(Some(new_id)) => {
self.cur_id = new_id.clone();
Some(Ok(new_id))
},
Ok(None) => {
None
},
}
}
}