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

@@ -32,33 +32,33 @@ impl fmt::Display for MizeId {
}
}
pub trait IntoMizeId<S: Store> {
fn to_mize_id(self, instance: &Instance<S>) -> MizeId;
pub trait IntoMizeId {
fn to_mize_id(self, instance: &Instance) -> MizeId;
}
impl<S: Store> IntoMizeId<S> for &str {
fn to_mize_id(self, instance: &Instance<S>) -> MizeId {
impl IntoMizeId for &str {
fn to_mize_id(self, instance: &Instance) -> MizeId {
instance.id_from_string(self.to_owned())
}
}
impl<S: Store> IntoMizeId<S> for String {
fn to_mize_id(self, instance: &Instance<S>) -> MizeId {
impl IntoMizeId for String {
fn to_mize_id(self, instance: &Instance) -> MizeId {
instance.id_from_string(self)
}
}
impl<S: Store> IntoMizeId<S> for &String {
fn to_mize_id(self, instance: &Instance<S>) -> MizeId {
impl IntoMizeId for &String {
fn to_mize_id(self, instance: &Instance) -> MizeId {
instance.id_from_string(self.to_owned())
}
}
impl<S: Store> IntoMizeId<S> for Vec<String> {
fn to_mize_id(self, instance: &Instance<S>) -> MizeId {
impl IntoMizeId for Vec<String> {
fn to_mize_id(self, instance: &Instance) -> MizeId {
instance.id_from_vec_string(self)
}
}
impl<S: Store> IntoMizeId<S> for &[&str] {
fn to_mize_id(self, instance: &Instance<S>) -> MizeId {
impl IntoMizeId for &[&str] {
fn to_mize_id(self, instance: &Instance) -> MizeId {
let mut vec: Vec<String> = Vec::new();
for i in self {
vec.push((*i).to_owned())
@@ -66,8 +66,8 @@ impl<S: Store> IntoMizeId<S> for &[&str] {
instance.id_from_vec_string(vec)
}
}
impl<S: Store> IntoMizeId<S> for Vec<&str> {
fn to_mize_id(self, instance: &Instance<S>) -> MizeId {
impl IntoMizeId for Vec<&str> {
fn to_mize_id(self, instance: &Instance) -> MizeId {
let mut vec: Vec<String> = Vec::new();
for i in self {
vec.push((*i).to_owned())
@@ -75,8 +75,8 @@ impl<S: Store> IntoMizeId<S> for Vec<&str> {
instance.id_from_vec_string(vec)
}
}
impl<S: Store> IntoMizeId<S> for MizeId {
fn to_mize_id(self, instance: &Instance<S>) -> MizeId {
impl IntoMizeId for MizeId {
fn to_mize_id(self, instance: &Instance) -> MizeId {
self
}
}

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

View File

@@ -18,9 +18,9 @@ use ciborium::Value as CborValue;
// a item always has to do with a Instance, which takes care of how it is updated
#[derive(Debug)]
pub struct Item<'a, S: Store + Sized> {
pub struct Item<'a> {
id: MizeId,
pub instance: &'a Instance<S>
pub instance: &'a Instance
}
// without an Instance it is not an item, but only the "data of an item"
@@ -28,12 +28,12 @@ pub struct Item<'a, S: Store + Sized> {
#[derive(Debug, Clone)]
pub struct ItemData ( pub CborValue );
impl<S: Store> Item<'_, S> {
impl Item<'_> {
pub fn id(&self) -> MizeId {
self.id.clone()
}
pub fn new(id: MizeId, instance: &Instance<S>) -> Item<S> {
pub fn new(id: MizeId, instance: &Instance) -> Item {
Item { id, instance }
}
@@ -41,14 +41,17 @@ impl<S: Store> Item<'_, S> {
// this will call from the instance which gets the value from the store
self.instance.store.get_value_raw(self.id())
}
pub fn value_string(&self) -> MizeResult<String> {
let raw = self.value_raw()?;
String::from_utf8(raw.clone())
.mize_result_msg(format!("failed to convert raw value from item '{}' into a utf-8 string. raw value was: '{:02X?}'", self.id(), raw))
}
pub fn as_data_full(&self) -> MizeResult<ItemData> {
self.instance.store.get_value_data_full(self.id())
}
pub fn merge<V: Into<ItemData>>(&mut self, mut value: V) -> MizeResult<()> {
let mut old_data = self.instance.store.get_value_data_full(self.id())?;
@@ -61,7 +64,7 @@ impl<S: Store> Item<'_, S> {
}
}
impl<S: Store> Display for Item<'_, S> {
impl<'a> Display for Item<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let item_data = self.as_data_full().map_err(|_| std::fmt::Error)?;
write!(f, "{}", item_data);

View File

@@ -7,7 +7,7 @@ use log::trace;
use colored::Colorize;
use crate::id::MizeId;
use crate::instance::store::Store;
use crate::instance::store::{Store, IdIter};
use crate::error::{MizeError, MizeResult, IntoMizeResult};
use crate::item::{Item, ItemData};
use crate::instance::Instance;
@@ -26,18 +26,18 @@ struct MemStoreInner {
}
impl Store for MemStore {
fn set<T: Into<ItemData>>(&self, id: MizeId, data: T) -> MizeResult<()> {
fn set(&self, id: MizeId, data: ItemData) -> MizeResult<()> {
let mut inner = self.inner.lock()?;
inner.map.insert(id_to_u64(id)?, data.into());
inner.map.insert(id_to_u64(id)?, data);
return Ok(())
}
fn get_links(&self, item: Item<Self>) -> MizeResult<Vec<MizeId>> {
fn get_links(&self, item: Item) -> MizeResult<Vec<MizeId>> {
let inner = self.inner.lock()?;
Ok(Vec::new())
}
fn get_backlinks(&self, item: Item<Self>) -> MizeResult<Vec<MizeId>> {
fn get_backlinks(&self, item: Item) -> MizeResult<Vec<MizeId>> {
let inner = self.inner.lock()?;
Ok(Vec::new())
@@ -80,11 +80,36 @@ impl Store for MemStore {
return Ok(ret_data);
}
fn id_iter(&self) -> MizeResult<impl Iterator<Item=String> + '_> {
fn id_iter(&self) -> MizeResult<IdIter> {
IdIter::new(Box::new(self.to_owned()))
}
fn next_id(&self, prev_id_str: &str) -> MizeResult<Option<String>> {
let inner = self.inner.lock()?;
let keys: Vec<String> = inner.map.keys().map(|v| format!("{}", v)).collect();
return Ok(keys.into_iter());
let mut id = str_to_u64(prev_id_str)?;
loop {
id += 1;
if id > inner.next_id {
return Ok(None);
}
if inner.map.contains_key(&id) {
return Ok(Some(format!("{}", id)));
} else {
// try id +1
continue;
}
}
}
fn first_id(&self) -> MizeResult<String> {
Ok("0".to_owned())
}
}
@@ -96,9 +121,12 @@ impl MemStore {
}
fn id_to_u64(id: MizeId) -> MizeResult<u64> {
id.store_part()
.parse()
.mize_result_msg(format!("Could not parse the store_part of mizeid {} into a u64 for the MemStore", id))
str_to_u64(id.store_part())
}
fn str_to_u64(id_str: &str) -> MizeResult<u64> {
id_str.parse()
.mize_result_msg(format!("Could not parse the store_part of mizeid {} into a u64 for the MemStore", id_str))
}
pub fn get_raw_from_cbor<'a>(value: &'a CborValue, path: Vec<&String>) -> MizeResult<&'a [u8]> {

View File

@@ -5,6 +5,7 @@ use sysinfo::{System, Pid, ProcessRefreshKind, RefreshKind};
use std::io::Write;
use ciborium::Value as CborValue;
use crate::instance::store::IdIter;
use crate::item::IntoItemData;
use crate::memstore::MemStore;
use crate::{core::instance::store::Store, mize_err};
@@ -57,17 +58,17 @@ impl Store for FileStore {
return Ok(id_string);
}
fn set<T: Into<ItemData>>(&self, id: MizeId, data: T) -> MizeResult<()> {
fn set(&self, id: MizeId, data: ItemData) -> MizeResult<()> {
let file = OpenOptions::new().write(true).create(true).open(self.path.join("store").join(id.store_part()))?;
ciborium::into_writer(data.into().cbor(), file)?;
ciborium::into_writer(data.cbor(), file)?;
Ok(())
}
fn get_links(&self, item: Item<Self>) -> MizeResult<Vec<MizeId>> {
fn get_links(&self, item: Item) -> MizeResult<Vec<MizeId>> {
Ok(Vec::new())
}
fn get_backlinks(&self, item: Item<Self>) -> MizeResult<Vec<MizeId>> {
fn get_backlinks(&self, item: Item) -> MizeResult<Vec<MizeId>> {
Ok(Vec::new())
}
@@ -100,8 +101,14 @@ impl Store for FileStore {
return Ok(ret_data);
}
fn id_iter(&self) -> MizeResult<impl Iterator<Item=String> + '_> {
Ok(vec!["1".to_owned(), "2".to_owned()].into_iter())
fn id_iter(&self) -> MizeResult<IdIter> {
todo!()
}
fn next_id(&self, prev_id: &str) -> MizeResult<Option<String>> {
todo!()
}
fn first_id(&self) -> MizeResult<String> {
Ok("0".to_owned())
}
}

View File

@@ -19,7 +19,7 @@ mod fsstore;
//mod unix-socket;
pub fn os_instance_init<S: Store>(instance: Instance<MemStore>) -> MizeResult<Instance<FileStore>> {
pub fn os_instance_init(instance: &mut Instance) -> MizeResult<()> {
// this is the code, that runs to initialize an Instance on a system with an os present.
////// load MIZE_CONFIG_FILE as the instance, which is item 0
@@ -63,8 +63,8 @@ pub fn os_instance_init<S: Store>(instance: Instance<MemStore>) -> MizeResult<In
if store_path != "" {
let file_store = FileStore::new(store_path)?;
let new_instance = &mut instance.migrate_to_store(file_store)?;
return Ok(new_instance);
let new_instance = &mut instance.migrate_to_store(Box::new(file_store))?;
return Ok(());
}
return Ok(());