added namespace
This commit is contained in:
@@ -1,29 +1,50 @@
|
||||
use core::fmt;
|
||||
use std::{collections::binary_heap::Iter, fmt::write, hash::Hash};
|
||||
use interner::{shared::SharedPool, Pooled, shared::SharedVecString};
|
||||
use interner::{shared::{SharedPool, SharedString, SharedVecString}, Pooled};
|
||||
use std::collections::hash_map::RandomState;
|
||||
use std::path::Path;
|
||||
|
||||
use crate::instance::{store::Store, Instance};
|
||||
|
||||
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
|
||||
pub struct MizeId {
|
||||
pub path: SharedVecString,
|
||||
pub namespace: Namespace,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
|
||||
pub struct Namespace ( pub SharedString );
|
||||
|
||||
impl Namespace {
|
||||
fn as_string(self) -> SharedString {
|
||||
self.0
|
||||
}
|
||||
}
|
||||
|
||||
pub trait IntoMizeId {
|
||||
fn to_mize_id(self, instance: &Instance) -> MizeId;
|
||||
}
|
||||
|
||||
|
||||
|
||||
|
||||
impl MizeId {
|
||||
pub fn store_part(&self) -> &str {
|
||||
self.path.iter().nth(0)
|
||||
.expect("an empty MizeId found, that should absolutely not be possible!!!!").as_str()
|
||||
}
|
||||
|
||||
pub fn path(&self) -> SharedVecString {
|
||||
self.path.clone()
|
||||
}
|
||||
//pub fn as_slice(&self) -> u8 {
|
||||
//self.path
|
||||
//}
|
||||
//pub fn as_iter(&self) -> u8 {
|
||||
//self.path.iter()
|
||||
//}
|
||||
|
||||
pub fn namespace(&self) -> Namespace {
|
||||
self.namespace.clone()
|
||||
}
|
||||
|
||||
pub fn namespace_str(&self) -> &str {
|
||||
&self.namespace.0
|
||||
}
|
||||
}
|
||||
|
||||
impl fmt::Display for MizeId {
|
||||
@@ -32,10 +53,6 @@ impl fmt::Display for MizeId {
|
||||
}
|
||||
}
|
||||
|
||||
pub trait IntoMizeId {
|
||||
fn to_mize_id(self, instance: &Instance) -> MizeId;
|
||||
}
|
||||
|
||||
impl IntoMizeId for &str {
|
||||
fn to_mize_id(self, instance: &Instance) -> MizeId {
|
||||
instance.id_from_string(self.to_owned())
|
||||
|
||||
@@ -7,13 +7,13 @@ use uuid::Uuid;
|
||||
use std::fs::File;
|
||||
use colored::Colorize;
|
||||
use std::path::Path;
|
||||
use interner::shared::VecStringPool;
|
||||
use interner::shared::{VecStringPool, StringPool};
|
||||
|
||||
use crate::error::{MizeError, MizeResult, IntoMizeResult, MizeResultTrait};
|
||||
use crate::instance;
|
||||
use crate::instance::store::Store;
|
||||
use crate::instance::connection::Connection;
|
||||
use crate::id::{IntoMizeId, MizeId};
|
||||
use crate::id::{IntoMizeId, MizeId, Namespace};
|
||||
use crate::instance::subscription::Subscription;
|
||||
use crate::item::{Item, ItemData};
|
||||
use crate::memstore::MemStore;
|
||||
@@ -33,26 +33,23 @@ pub struct Instance {
|
||||
peers: Arc<Mutex<Vec<Box<dyn Connection>>>>,
|
||||
subs: Arc<Mutex<HashMap<MizeId, Subscription>>>,
|
||||
pub id_pool: VecStringPool,
|
||||
pub namespace_pool: StringPool,
|
||||
pub namespace: Namespace,
|
||||
}
|
||||
|
||||
|
||||
/*
|
||||
pub enum RealmId {
|
||||
Uuid(Uuid),
|
||||
Local(Vec<String>),
|
||||
Tld(Vec<String>),
|
||||
}
|
||||
*/
|
||||
|
||||
impl Instance {
|
||||
pub fn empty() -> Instance {
|
||||
let store = MemStore::new();
|
||||
let id_pool = VecStringPool::default();
|
||||
let namespace_pool = StringPool::default();
|
||||
let peers = Arc::new(Mutex::new(Vec::new()));
|
||||
let subs = Arc::new(Mutex::new(HashMap::new()));
|
||||
let mut instance = Instance { store: Box::new(store), peers, subs, id_pool };
|
||||
let namespace = Namespace ( namespace_pool.get("mize.default.namespace") );
|
||||
let mut instance = Instance { store: Box::new(store), peers, subs, id_pool, namespace_pool, namespace };
|
||||
return instance;
|
||||
}
|
||||
|
||||
pub fn new() -> MizeResult<Instance> {
|
||||
trace!("[ {} ] Instance::new()", "CALL".yellow());
|
||||
|
||||
@@ -135,15 +132,16 @@ impl Instance {
|
||||
|
||||
pub fn id_from_string(&self, string: String) -> MizeId {
|
||||
let vec_string: Vec<String> = string.split("/").map(|v| v.to_owned()).collect();
|
||||
return MizeId { path: self.id_pool.get(vec_string) };
|
||||
MizeId { path: self.id_pool.get(vec_string), namespace: self.namespace.clone() }
|
||||
}
|
||||
|
||||
pub fn id_from_vec_string(&self, vec_string: Vec<String>) -> MizeId {
|
||||
return MizeId { path: self.id_pool.get(vec_string) };
|
||||
MizeId { path: self.id_pool.get(vec_string), namespace: self.namespace.clone() }
|
||||
}
|
||||
|
||||
pub fn namespace_from_string(&self, ns_str: String) -> Namespace {
|
||||
Namespace ( self.namespace_pool.get(ns_str) )
|
||||
}
|
||||
//impl<T: Into<String>> From<T> for MizeId {
|
||||
//fn from(value: T) -> Self {
|
||||
//}
|
||||
//}
|
||||
}
|
||||
|
||||
impl fmt::Debug for Instance {
|
||||
|
||||
@@ -59,7 +59,10 @@ impl Store for FileStore {
|
||||
}
|
||||
|
||||
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 path = self.path.join("store").join(id.namespace_str()).join(id.store_part());
|
||||
|
||||
let file = OpenOptions::new().write(true).create(true).open(path)?;
|
||||
|
||||
ciborium::into_writer(data.cbor(), file)?;
|
||||
Ok(())
|
||||
}
|
||||
@@ -73,7 +76,9 @@ impl Store for FileStore {
|
||||
}
|
||||
|
||||
fn get_value_raw(&self, id: MizeId) -> MizeResult<Vec<u8>> {
|
||||
let file = OpenOptions::new().read(true).create(true).open(self.path.join("store").join(id.store_part()))?;
|
||||
let path = self.path.join("store").join(id.namespace_str()).join(id.store_part());
|
||||
|
||||
let file = OpenOptions::new().read(true).create(true).open(path)?;
|
||||
|
||||
let cbor_val: CborValue = ciborium::from_reader(file)
|
||||
.mize_result_msg(format!("could not read file '{}' from FileStore", self.path.join("store").join(id.store_part()).display()))?;
|
||||
|
||||
@@ -59,15 +59,14 @@ pub fn os_instance_init(instance: &mut Instance) -> MizeResult<()> {
|
||||
};
|
||||
|
||||
////// if a config.store is set, upgrade to the filestore there
|
||||
let store_path = instance.get("0/conifg/store")?.value_string()?;
|
||||
let instance_path = instance.get("0/conifg/instance_path")?.value_string()?;
|
||||
|
||||
if store_path != "" {
|
||||
let file_store = FileStore::new(store_path)?;
|
||||
if instance_path != "" {
|
||||
let file_store = FileStore::new(instance_path)?;
|
||||
let new_instance = &mut instance.migrate_to_store(Box::new(file_store))?;
|
||||
return Ok(());
|
||||
}
|
||||
|
||||
return Ok(());
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn config_from_cli_args(matches: &ArgMatches) -> MizeResult<ItemData> {
|
||||
|
||||
Reference in New Issue
Block a user