MizeIds are now stored in a StringPool for Vec<String>

This commit is contained in:
Sebastian Moser
2024-05-31 12:05:53 +02:00
parent 38f967db76
commit 47b187d012
11 changed files with 245 additions and 120 deletions

View File

@@ -1,20 +1,29 @@
use core::fmt;
use std::{fmt::write, hash::Hash};
use std::{collections::binary_heap::Iter, fmt::write, hash::Hash};
use interner::{shared::SharedPool, Pooled, shared::SharedVecString};
use std::collections::hash_map::RandomState;
use crate::instance::{store::Store, Instance};
#[derive(Debug, Clone, Hash, Eq, PartialEq)]
pub struct MizeId {
path: Vec<String>
pub path: SharedVecString,
}
impl MizeId {
pub fn store_part(&self) -> &str {
self.path.first()
self.path.iter().nth(0)
.expect("an empty MizeId found, that should absolutely not be possible!!!!").as_str()
}
pub fn as_vec(self) -> Vec<String> {
self.path
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()
//}
}
impl fmt::Display for MizeId {
@@ -23,12 +32,63 @@ impl fmt::Display for MizeId {
}
}
impl<T: Into<String>> From<T> for MizeId {
fn from(value: T) -> Self {
let string = value.into();
let path: Vec<String> = string.split("/").map(|v| v.to_owned()).collect();
return MizeId { path }
pub trait IntoMizeId<S: Store> {
fn to_mize_id(self, instance: &Instance<S>) -> MizeId;
}
impl<S: Store> IntoMizeId<S> for &str {
fn to_mize_id(self, instance: &Instance<S>) -> MizeId {
instance.id_from_string(self.to_owned())
}
}
impl<S: Store> IntoMizeId<S> for String {
fn to_mize_id(self, instance: &Instance<S>) -> MizeId {
instance.id_from_string(self)
}
}
impl<S: Store> IntoMizeId<S> for &String {
fn to_mize_id(self, instance: &Instance<S>) -> 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 {
instance.id_from_vec_string(self)
}
}
impl<S: Store> IntoMizeId<S> for &[&str] {
fn to_mize_id(self, instance: &Instance<S>) -> MizeId {
let mut vec: Vec<String> = Vec::new();
for i in self {
vec.push((*i).to_owned())
}
instance.id_from_vec_string(vec)
}
}
impl<S: Store> IntoMizeId<S> for Vec<&str> {
fn to_mize_id(self, instance: &Instance<S>) -> MizeId {
let mut vec: Vec<String> = Vec::new();
for i in self {
vec.push((*i).to_owned())
}
instance.id_from_vec_string(vec)
}
}
impl<S: Store> IntoMizeId<S> for MizeId {
fn to_mize_id(self, instance: &Instance<S>) -> MizeId {
self
}
}
// would only work with a global id_pool
// for now use Instance::new_id()
//impl<T: Into<String>> From<T> for MizeId {
//fn from(value: T) -> Self {
//let string = value.into();
//let path: Vec<String> = string.split("/").map(|v| v.to_owned()).collect();
//return MizeId { MIZE_ID_POOL.get(path) }
//}
//}

View File

@@ -7,11 +7,12 @@ use uuid::Uuid;
use std::fs::File;
use colored::Colorize;
use std::path::Path;
use interner::shared::VecStringPool;
use crate::error::{MizeError, MizeResult, IntoMizeResult, MizeResultTrait};
use crate::instance::store::Store;
use crate::instance::connection::Connection;
use crate::id::MizeId;
use crate::id::{IntoMizeId, MizeId};
use crate::instance::subscription::Subscription;
use crate::item::{Item, ItemData};
use crate::memstore::MemStore;
@@ -31,6 +32,7 @@ pub struct Instance<S: Store> {
pub store: S,
peers: Arc<Mutex<Vec<Box<dyn Connection>>>>,
subs: Arc<Mutex<HashMap<MizeId, Subscription>>>,
pub id_pool: VecStringPool,
}
@@ -46,36 +48,73 @@ impl Instance<MemStore> {
pub fn new() -> MizeResult<Instance<MemStore>> {
trace!("[ {} ] Instance::new()", "CALL".yellow());
let memstore = MemStore::new();
let instance = Instance {store: memstore, peers: Arc::new(Mutex::new(Vec::new())), subs: Arc::new(Mutex::new(HashMap::new())) };
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 };
instance.init();
return Ok(instance);
}
fn init(&mut self) -> MizeResult<()> {
// platform specific init code
if cfg!(feature = "os-target") { // on os platforms
crate::platform::os::os_instance_init(instance.clone())?
crate::platform::os::os_instance_init(self)?
}
// end of platform specific init code
Ok(())
}
return Ok(instance);
pub fn with_config(config: ItemData) -> MizeResult<Instance<MemStore>> {
let mut instance = Instance::new()?;
instance.set("0", config.clone());
instance.init()?;
// set it again, so that the passed config data has presidence over anything the init would set
instance.set("0", config);
Ok(instance)
}
}
impl<S: Store> Instance<S> {
pub fn new_item(self) -> MizeResult<Item<S>> {
let id = self.store.new_id()?;
pub fn new_item(&self) -> MizeResult<Item<S>> {
let id = self.id_from_string(self.store.new_id()?);
return Ok(Item {id, instance: self});
}
pub fn get<I: Into<MizeId>>(self, id: I) -> MizeResult<Item<S>> {
return Ok(Item {id: id.into(), instance: self});
pub fn get<I: IntoMizeId<S>>(&self, id: I) -> MizeResult<Item<S>> {
let id = id.to_mize_id(self);
return Ok(Item {id: id.to_mize_id(self), instance: self});
}
pub fn set<I: Into<MizeId>, V: Into<ItemData>>(self, id: I, value: V) -> MizeResult<()> {
self.store.set(&id.into(), value)
pub fn set<I: IntoMizeId<S>, V: Into<ItemData>>(&mut self, id: I, value: V) -> MizeResult<()> {
let id = id.to_mize_id(self);
self.store.set(id, value)
}
pub fn new_id<T: IntoMizeId<S>>(&self, value: T) -> MizeId {
value.to_mize_id(self)
}
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) };
}
pub fn id_from_vec_string(&self, vec_string: Vec<String>) -> MizeId {
return MizeId { path: self.id_pool.get(vec_string) };
}
//impl<T: Into<String>> From<T> for MizeId {
//fn from(value: T) -> Self {
//}
//}
}
impl<S: Store> fmt::Debug for Instance<S> {

View File

@@ -9,21 +9,17 @@ 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<T: Into<ItemData>>(&mut self, id: MizeId, data: T) -> 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<Self>) -> MizeResult<Vec<MizeId>> where Self: Sized;
fn get_backlinks(self, item: Item<Self>) -> MizeResult<Vec<MizeId>> where Self: Sized;
fn get_backlinks(&self, item: Item<Self>) -> MizeResult<Vec<MizeId>> where Self: Sized;
fn new_id(&self) -> MizeResult<MizeId>;
fn new_id(&self) -> MizeResult<String>;
fn get_value_raw(&self, id: &MizeId) -> MizeResult<Vec<u8>>;
fn get_value_raw(&self, id: MizeId) -> MizeResult<Vec<u8>>;
}
/// references an item that is stored in a store
pub trait StoreRef {
// do we need this or is this basically an item
}

View File

@@ -13,30 +13,30 @@ 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<S: Store + Sized> {
pub struct Item<'a, S: Store + Sized> {
pub id: MizeId,
pub instance: Instance<S>
pub instance: &'a Instance<S>
}
// without an Instance it is not an item, but only the "data of an item"
// and this type for now is just an alias to CborValue
pub type ItemData = CborValue;
impl<S: Store> Item<S> {
pub fn id(&self) -> &MizeId {
&self.id
impl<S: Store> Item<'_, S> {
pub fn id(&self) -> MizeId {
self.id.clone()
}
pub fn value_raw(&self) -> MizeResult<Vec<u8>> {
// 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())
}
}
impl<S: Store> Display for Item<S> {
impl<S: Store> Display for Item<'_, S> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self.value_raw() {
Ok(value) => match String::from_utf8(value) {
Ok(value) => match String::from_utf8(value.to_owned()) {
Ok(string) => write!(f, "{}", string),
Err(_) => write!(f, "{:?}", self.value_raw()),
}

View File

@@ -1,3 +1,4 @@
use std::collections::binary_heap::Iter;
use std::collections::HashMap;
use std::iter::Map;
use std::sync::{Arc, Mutex};
@@ -14,6 +15,8 @@ pub struct MemStore {
inner: Arc<Mutex<MemStoreInner>>,
}
pub type MemStoreId = u64;
#[derive(Debug)]
struct MemStoreInner {
map: HashMap<u64, CborValue>,
@@ -21,39 +24,43 @@ struct MemStoreInner {
}
impl Store for MemStore {
fn set<T: Into<ItemData>>(mut self, id: &MizeId, data: T) -> MizeResult<()> {
fn set<T: Into<ItemData>>(&mut self, id: MizeId, data: T) -> MizeResult<()> {
let mut inner = self.inner.lock()?;
inner.map.insert(id_to_u64(id)?, data.into());
return Ok(())
}
fn get_links(self, item: Item<Self>) -> MizeResult<Vec<MizeId>> {
fn get_links(&self, item: Item<Self>) -> 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<Self>) -> MizeResult<Vec<MizeId>> {
let inner = self.inner.lock()?;
Ok(Vec::new())
}
fn new_id(&self) -> MizeResult<MizeId> {
let inner = self.inner.lock()?;
fn new_id(&self) -> MizeResult<String> {
let mut inner = self.inner.lock()?;
let id_string = format!("{}", inner.next_id);
return Ok(id_string.into());
inner.next_id += 1;
return Ok(id_string);
}
fn get_value_raw(&self, id: &MizeId) -> MizeResult<Vec<u8>> {
fn get_value_raw(&self, id: MizeId) -> MizeResult<Vec<u8>> {
let inner = self.inner.lock()?;
let cbor_val = inner.map.get(&id_to_u64(id)?)
let cbor_val = inner.map.get(&id_to_u64(id.clone())?)
.ok_or(MizeError::new()
.msg(format!("Item with store_part: {}, id: {} does not exist in MemStore", id_to_u64(id)?, id)))?;
.msg(format!("Item with store_part: {}, id: {} does not exist in MemStore", id_to_u64(id.clone())?, id)))?;
let mut id_without_first = id.clone().as_vec();
id_without_first.remove(0);
get_raw_from_cbor(cbor_val.to_owned(), id_without_first)
let path = id.path();
let mut id_iter = path.iter();
id_iter.next();
let id_without_first = id_iter.collect();
Ok(get_raw_from_cbor(cbor_val, id_without_first)?.to_owned())
}
}
@@ -64,34 +71,39 @@ impl MemStore {
}
}
fn id_to_u64(id: &MizeId) -> MizeResult<u64> {
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))
}
fn get_raw_from_cbor(value: CborValue, mut path: Vec<String>) -> MizeResult<Vec<u8>> {
fn get_raw_from_cbor<'a>(value: &'a CborValue, path: Vec<&String>) -> MizeResult<&'a [u8]> {
match value {
CborValue::Bytes(vec) => Ok(vec),
CborValue::Text(string) => Ok(string.into_bytes()),
CborValue::Bytes(vec) => Ok(&vec[..]),
CborValue::Text(string) => Ok(string.as_bytes()),
CborValue::Map(map) => {
let mut inner_val: CborValue = CborValue::Null;
trace!("map: {:?}", map);
let mut inner_val: &CborValue = &CborValue::Null;
trace!("map: {:?}", map);
for (key, val) in map.into_iter() {
let first_path_string = path.first()
.ok_or(MizeError::new().msg("get_raw_from_cbor: path is empty"))?;
let first_path_string = *path.iter().nth(0)
.ok_or(MizeError::new()
.msg("get_raw_from_cbor: path is empty"))?;
if let CborValue::Text(key_text) = key {
if key_text == *first_path_string {
if key_text == first_path_string {
inner_val = val
}
}
}
if inner_val == CborValue::Null {
return Err(MizeError::new().msg(format!("get_raw_from_cbor: Path {} not Found", path.join("/"))))
if inner_val == &CborValue::Null {
return Err(MizeError::new()
// jesus christ, this is a pfusch...
.msg(format!("get_raw_from_cbor: Path {} not Found", path.clone().into_iter().map(|v| v.to_owned()).collect::<Vec<String>>().join("/"))))
}
path.remove(0);
get_raw_from_cbor(inner_val, path)
let mut path_iter = path.into_iter();
path_iter.next();
let inner_path = path_iter.collect();
get_raw_from_cbor(inner_val, inner_path)
},
_ => Err(MizeError::new().msg("get_raw_from_cbor: value is not Bytes, Text or Map")),
}

View File

@@ -12,7 +12,15 @@ use mize::instance::Instance;
pub fn run(sub_matches: &ArgMatches) -> MizeResult<()> {
let instance = Instance::new();
// TODO NEXT!!!
// - add --system arg
// - add --user arg
// - --config arg
// - to set a location, where there will be a /store and a /sock
// - listen for connections on that sock
// - msg handeling
let instance = Instance::with_config(todo!());
/*
let instance_path = match sub_matches.get_one::<String>("store") {
Some(a) => PathBuf::from(a),

View File

@@ -260,10 +260,10 @@ fn level_from_env(var_name: &str, log_level: LevelFilter) -> (Option<log::LevelF
fn cli_matches() -> clap::ArgMatches {
let store_arg = Arg::new("store")
.short('s')
.long("store")
.help("The folder used as the Itemstore");
let folder_arg = Arg::new("folder")
.short('f')
.long("folder")
.help("The folder the Instance stores all it's data and the socket for connections");
let main = Command::new(APPNAME)
.version(crate_version!())
@@ -284,6 +284,7 @@ fn cli_matches() -> clap::ArgMatches {
.action(ArgAction::SetTrue)
.help("set the log-level to OFF")
)
.arg(&folder_arg)
.subcommand(
Command::new("run")
.aliases(["r"])
@@ -295,30 +296,29 @@ fn cli_matches() -> clap::ArgMatches {
.subcommand(
Command::new("mount")
.aliases(["m"])
.arg(&store_arg)
)
.subcommand(
Command::new("get")
.aliases(["g"])
.arg(&store_arg)
.arg(&folder_arg)
.arg(Arg::new("id").help("The id or path to get"))
)
.subcommand(
Command::new("set")
.aliases(["s"])
.arg(&store_arg)
.arg(&folder_arg)
.arg(Arg::new("id").help("The id or path to set"))
)
.subcommand(
Command::new("show")
.aliases(["so"])
.arg(&store_arg)
.arg(&folder_arg)
.arg(Arg::new("id").help("The id or path to sub to and show"))
)
.subcommand(
Command::new("call")
.aliases(["c"])
.arg(&store_arg)
.arg(&folder_arg)
)
.arg_required_else_help(true);

View File

@@ -8,10 +8,10 @@ use crate::instance::Instance;
use crate::error::{IntoMizeResult, MizeError, MizeResult};
pub fn os_instance_init<S: Store>(instance: Instance<S>) -> MizeResult<()> {
pub fn os_instance_init<S: Store>(instance: &mut Instance<S>) -> 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
////// load MIZE_CONFIG_FILE as the instance, which is item 0
let config_file_path = std::env::var("MIZE_CONFIG_FILE")
.mize_result_msg("Could not get the MIZE_CONFIG_FILE Environment Variable")?;
@@ -24,6 +24,9 @@ pub fn os_instance_init<S: Store>(instance: Instance<S>) -> MizeResult<()> {
.mize_result_msg(format!("Could not deserialize the content of MIZE_CONFIG_FILE at {}", &config_file_path))?;
instance.set("0", config)?;
////// load config from MIZE_CONFIG env var
//TODO
return Ok(());
}