instance and item now use dynamic dispatch
This commit is contained in:
@@ -32,33 +32,33 @@ impl fmt::Display for MizeId {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
pub trait IntoMizeId<S: Store> {
|
pub trait IntoMizeId {
|
||||||
fn to_mize_id(self, instance: &Instance<S>) -> MizeId;
|
fn to_mize_id(self, instance: &Instance) -> MizeId;
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: Store> IntoMizeId<S> for &str {
|
impl IntoMizeId for &str {
|
||||||
fn to_mize_id(self, instance: &Instance<S>) -> MizeId {
|
fn to_mize_id(self, instance: &Instance) -> MizeId {
|
||||||
instance.id_from_string(self.to_owned())
|
instance.id_from_string(self.to_owned())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl<S: Store> IntoMizeId<S> for String {
|
impl IntoMizeId for String {
|
||||||
fn to_mize_id(self, instance: &Instance<S>) -> MizeId {
|
fn to_mize_id(self, instance: &Instance) -> MizeId {
|
||||||
instance.id_from_string(self)
|
instance.id_from_string(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<S: Store> IntoMizeId<S> for &String {
|
impl IntoMizeId for &String {
|
||||||
fn to_mize_id(self, instance: &Instance<S>) -> MizeId {
|
fn to_mize_id(self, instance: &Instance) -> MizeId {
|
||||||
instance.id_from_string(self.to_owned())
|
instance.id_from_string(self.to_owned())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<S: Store> IntoMizeId<S> for Vec<String> {
|
impl IntoMizeId for Vec<String> {
|
||||||
fn to_mize_id(self, instance: &Instance<S>) -> MizeId {
|
fn to_mize_id(self, instance: &Instance) -> MizeId {
|
||||||
instance.id_from_vec_string(self)
|
instance.id_from_vec_string(self)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<S: Store> IntoMizeId<S> for &[&str] {
|
impl IntoMizeId for &[&str] {
|
||||||
fn to_mize_id(self, instance: &Instance<S>) -> MizeId {
|
fn to_mize_id(self, instance: &Instance) -> MizeId {
|
||||||
let mut vec: Vec<String> = Vec::new();
|
let mut vec: Vec<String> = Vec::new();
|
||||||
for i in self {
|
for i in self {
|
||||||
vec.push((*i).to_owned())
|
vec.push((*i).to_owned())
|
||||||
@@ -66,8 +66,8 @@ impl<S: Store> IntoMizeId<S> for &[&str] {
|
|||||||
instance.id_from_vec_string(vec)
|
instance.id_from_vec_string(vec)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<S: Store> IntoMizeId<S> for Vec<&str> {
|
impl IntoMizeId for Vec<&str> {
|
||||||
fn to_mize_id(self, instance: &Instance<S>) -> MizeId {
|
fn to_mize_id(self, instance: &Instance) -> MizeId {
|
||||||
let mut vec: Vec<String> = Vec::new();
|
let mut vec: Vec<String> = Vec::new();
|
||||||
for i in self {
|
for i in self {
|
||||||
vec.push((*i).to_owned())
|
vec.push((*i).to_owned())
|
||||||
@@ -75,8 +75,8 @@ impl<S: Store> IntoMizeId<S> for Vec<&str> {
|
|||||||
instance.id_from_vec_string(vec)
|
instance.id_from_vec_string(vec)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
impl<S: Store> IntoMizeId<S> for MizeId {
|
impl IntoMizeId for MizeId {
|
||||||
fn to_mize_id(self, instance: &Instance<S>) -> MizeId {
|
fn to_mize_id(self, instance: &Instance) -> MizeId {
|
||||||
self
|
self
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -25,12 +25,11 @@ pub mod subscription;
|
|||||||
static MSG_CHANNEL_SIZE: usize = 200;
|
static MSG_CHANNEL_SIZE: usize = 200;
|
||||||
|
|
||||||
/// The Instance type is the heart of the mize system
|
/// The Instance type is the heart of the mize system
|
||||||
#[derive(Clone)]
|
pub struct Instance {
|
||||||
pub struct Instance<S: Store> {
|
|
||||||
// question vec of stores???
|
// question vec of stores???
|
||||||
// would mean replication logic and such things is handeled by the instance
|
// would mean replication logic and such things is handeled by the instance
|
||||||
// i think it would be better thogh to then implement a ReplicatedStore
|
// 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>>>>,
|
peers: Arc<Mutex<Vec<Box<dyn Connection>>>>,
|
||||||
subs: Arc<Mutex<HashMap<MizeId, Subscription>>>,
|
subs: Arc<Mutex<HashMap<MizeId, Subscription>>>,
|
||||||
pub id_pool: VecStringPool,
|
pub id_pool: VecStringPool,
|
||||||
@@ -45,16 +44,16 @@ pub enum RealmId {
|
|||||||
}
|
}
|
||||||
*/
|
*/
|
||||||
|
|
||||||
impl Instance<MemStore> {
|
impl Instance {
|
||||||
pub fn empty() -> Instance<MemStore> {
|
pub fn empty() -> Instance {
|
||||||
let store = MemStore::new();
|
let store = MemStore::new();
|
||||||
let id_pool = VecStringPool::default();
|
let id_pool = VecStringPool::default();
|
||||||
let peers = Arc::new(Mutex::new(Vec::new()));
|
let peers = Arc::new(Mutex::new(Vec::new()));
|
||||||
let subs = Arc::new(Mutex::new(HashMap::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;
|
return instance;
|
||||||
}
|
}
|
||||||
pub fn new() -> MizeResult<Instance<MemStore>> {
|
pub fn new() -> MizeResult<Instance> {
|
||||||
trace!("[ {} ] Instance::new()", "CALL".yellow());
|
trace!("[ {} ] Instance::new()", "CALL".yellow());
|
||||||
|
|
||||||
let mut instance = Instance::empty();
|
let mut instance = Instance::empty();
|
||||||
@@ -78,7 +77,7 @@ impl Instance<MemStore> {
|
|||||||
Ok(())
|
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!("[ {} ] Instance::with_config()", "CALL".yellow());
|
||||||
trace!("config: {}", config);
|
trace!("config: {}", config);
|
||||||
let mut instance = Instance::empty();
|
let mut instance = Instance::empty();
|
||||||
@@ -94,20 +93,35 @@ impl Instance<MemStore> {
|
|||||||
|
|
||||||
Ok(instance)
|
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> {
|
impl Instance {
|
||||||
pub fn new_item(&self) -> MizeResult<Item<S>> {
|
pub fn new_item(&self) -> MizeResult<Item> {
|
||||||
let id = self.id_from_string(self.store.new_id()?);
|
let id = self.id_from_string(self.store.new_id()?);
|
||||||
return Ok(Item::new(id, &self));
|
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);
|
let id = id.to_mize_id(self);
|
||||||
return Ok(Item::new(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 id = id.to_mize_id(self);
|
||||||
let item_data = value.into();
|
let item_data = value.into();
|
||||||
let mut item = self.get(id.clone())?;
|
let mut item = self.get(id.clone())?;
|
||||||
@@ -115,7 +129,7 @@ impl<S: Store> Instance<S> {
|
|||||||
Ok(())
|
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)
|
value.to_mize_id(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -130,22 +144,9 @@ impl<S: Store> Instance<S> {
|
|||||||
//fn from(value: T) -> Self {
|
//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 {
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
||||||
write!(f, "Mize Instance with subs: {:?}", self.subs,)
|
write!(f, "Mize Instance with subs: {:?}", self.subs,)
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -15,14 +15,14 @@ pub trait Store {
|
|||||||
// is implemented, so that there can be a multithreaded implementation
|
// is implemented, so that there can be a multithreaded implementation
|
||||||
//fn get(self, id: MizeId) -> MizeResult<Item<Self>> where Self: Sized;
|
//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, ....
|
// in the future should implement transactions, ....
|
||||||
|
|
||||||
// funcs to do with links, backlinks
|
// 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>;
|
fn new_id(&self) -> MizeResult<String>;
|
||||||
|
|
||||||
@@ -30,6 +30,40 @@ pub trait Store {
|
|||||||
|
|
||||||
fn get_value_data_full(&self, id: MizeId) -> MizeResult<ItemData>;
|
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
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -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
|
// a item always has to do with a Instance, which takes care of how it is updated
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Item<'a, S: Store + Sized> {
|
pub struct Item<'a> {
|
||||||
id: MizeId,
|
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"
|
// 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)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct ItemData ( pub CborValue );
|
pub struct ItemData ( pub CborValue );
|
||||||
|
|
||||||
impl<S: Store> Item<'_, S> {
|
impl Item<'_> {
|
||||||
pub fn id(&self) -> MizeId {
|
pub fn id(&self) -> MizeId {
|
||||||
self.id.clone()
|
self.id.clone()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn new(id: MizeId, instance: &Instance<S>) -> Item<S> {
|
pub fn new(id: MizeId, instance: &Instance) -> Item {
|
||||||
Item { id, instance }
|
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
|
// this will call from the instance which gets the value from the store
|
||||||
self.instance.store.get_value_raw(self.id())
|
self.instance.store.get_value_raw(self.id())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn value_string(&self) -> MizeResult<String> {
|
pub fn value_string(&self) -> MizeResult<String> {
|
||||||
let raw = self.value_raw()?;
|
let raw = self.value_raw()?;
|
||||||
String::from_utf8(raw.clone())
|
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))
|
.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> {
|
pub fn as_data_full(&self) -> MizeResult<ItemData> {
|
||||||
self.instance.store.get_value_data_full(self.id())
|
self.instance.store.get_value_data_full(self.id())
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn merge<V: Into<ItemData>>(&mut self, mut value: V) -> MizeResult<()> {
|
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())?;
|
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 {
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
let item_data = self.as_data_full().map_err(|_| std::fmt::Error)?;
|
let item_data = self.as_data_full().map_err(|_| std::fmt::Error)?;
|
||||||
write!(f, "{}", item_data);
|
write!(f, "{}", item_data);
|
||||||
|
|||||||
@@ -7,7 +7,7 @@ use log::trace;
|
|||||||
use colored::Colorize;
|
use colored::Colorize;
|
||||||
|
|
||||||
use crate::id::MizeId;
|
use crate::id::MizeId;
|
||||||
use crate::instance::store::Store;
|
use crate::instance::store::{Store, IdIter};
|
||||||
use crate::error::{MizeError, MizeResult, IntoMizeResult};
|
use crate::error::{MizeError, MizeResult, IntoMizeResult};
|
||||||
use crate::item::{Item, ItemData};
|
use crate::item::{Item, ItemData};
|
||||||
use crate::instance::Instance;
|
use crate::instance::Instance;
|
||||||
@@ -26,18 +26,18 @@ struct MemStoreInner {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Store for MemStore {
|
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()?;
|
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(())
|
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()?;
|
let inner = self.inner.lock()?;
|
||||||
|
|
||||||
Ok(Vec::new())
|
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()?;
|
let inner = self.inner.lock()?;
|
||||||
|
|
||||||
Ok(Vec::new())
|
Ok(Vec::new())
|
||||||
@@ -80,11 +80,36 @@ impl Store for MemStore {
|
|||||||
|
|
||||||
return Ok(ret_data);
|
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 inner = self.inner.lock()?;
|
||||||
|
|
||||||
let keys: Vec<String> = inner.map.keys().map(|v| format!("{}", v)).collect();
|
let mut id = str_to_u64(prev_id_str)?;
|
||||||
return Ok(keys.into_iter());
|
|
||||||
|
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> {
|
fn id_to_u64(id: MizeId) -> MizeResult<u64> {
|
||||||
id.store_part()
|
str_to_u64(id.store_part())
|
||||||
.parse()
|
}
|
||||||
.mize_result_msg(format!("Could not parse the store_part of mizeid {} into a u64 for the MemStore", id))
|
|
||||||
|
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]> {
|
pub fn get_raw_from_cbor<'a>(value: &'a CborValue, path: Vec<&String>) -> MizeResult<&'a [u8]> {
|
||||||
|
|||||||
@@ -5,6 +5,7 @@ use sysinfo::{System, Pid, ProcessRefreshKind, RefreshKind};
|
|||||||
use std::io::Write;
|
use std::io::Write;
|
||||||
use ciborium::Value as CborValue;
|
use ciborium::Value as CborValue;
|
||||||
|
|
||||||
|
use crate::instance::store::IdIter;
|
||||||
use crate::item::IntoItemData;
|
use crate::item::IntoItemData;
|
||||||
use crate::memstore::MemStore;
|
use crate::memstore::MemStore;
|
||||||
use crate::{core::instance::store::Store, mize_err};
|
use crate::{core::instance::store::Store, mize_err};
|
||||||
@@ -57,17 +58,17 @@ impl Store for FileStore {
|
|||||||
return Ok(id_string);
|
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()))?;
|
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(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_links(&self, item: Item<Self>) -> MizeResult<Vec<MizeId>> {
|
fn get_links(&self, item: Item) -> MizeResult<Vec<MizeId>> {
|
||||||
Ok(Vec::new())
|
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())
|
Ok(Vec::new())
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -100,8 +101,14 @@ impl Store for FileStore {
|
|||||||
|
|
||||||
return Ok(ret_data);
|
return Ok(ret_data);
|
||||||
}
|
}
|
||||||
fn id_iter(&self) -> MizeResult<impl Iterator<Item=String> + '_> {
|
fn id_iter(&self) -> MizeResult<IdIter> {
|
||||||
Ok(vec!["1".to_owned(), "2".to_owned()].into_iter())
|
todo!()
|
||||||
|
}
|
||||||
|
fn next_id(&self, prev_id: &str) -> MizeResult<Option<String>> {
|
||||||
|
todo!()
|
||||||
|
}
|
||||||
|
fn first_id(&self) -> MizeResult<String> {
|
||||||
|
Ok("0".to_owned())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,7 +19,7 @@ mod fsstore;
|
|||||||
//mod unix-socket;
|
//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.
|
// 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
|
////// 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 != "" {
|
if store_path != "" {
|
||||||
let file_store = FileStore::new(store_path)?;
|
let file_store = FileStore::new(store_path)?;
|
||||||
let new_instance = &mut instance.migrate_to_store(file_store)?;
|
let new_instance = &mut instance.migrate_to_store(Box::new(file_store))?;
|
||||||
return Ok(new_instance);
|
return Ok(());
|
||||||
}
|
}
|
||||||
|
|
||||||
return Ok(());
|
return Ok(());
|
||||||
|
|||||||
Reference in New Issue
Block a user