migrating to fsstore works
This commit is contained in:
@@ -145,6 +145,13 @@ impl MizeError {
|
|||||||
panic!();
|
panic!();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub fn location(mut self, location: MizeCodeLocation) -> MizeError {
|
||||||
|
|
||||||
|
self.code_location = Some(location);
|
||||||
|
|
||||||
|
self
|
||||||
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
|||||||
@@ -57,7 +57,7 @@ impl Instance {
|
|||||||
|
|
||||||
instance.init();
|
instance.init();
|
||||||
|
|
||||||
debug!("instance inited with conifg: {}", instance.get("0/config")?.as_data_full()?);
|
debug!("instance inited with config: {}", instance.get("0/config")?.as_data_full()?);
|
||||||
|
|
||||||
return Ok(instance);
|
return Ok(instance);
|
||||||
}
|
}
|
||||||
@@ -85,15 +85,18 @@ impl Instance {
|
|||||||
debug!("overwriting instance config again with the one passed to Instance::with_config()");
|
debug!("overwriting instance config again with the one passed to Instance::with_config()");
|
||||||
instance.set("0", config);
|
instance.set("0", config);
|
||||||
|
|
||||||
//debug!("instance inited with conifg: {}", instance.get("0/config")?);
|
debug!("instance inited with conifg: {}", instance.get("0/config")?);
|
||||||
debug!("instance inited with conifg: no");
|
|
||||||
|
|
||||||
Ok(instance)
|
Ok(instance)
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn migrate_to_store(&mut self, new_store: Box<dyn Store>) -> MizeResult<()> {
|
pub fn migrate_to_store(&mut self, new_store: Box<dyn Store>) -> MizeResult<()> {
|
||||||
|
println!("MIGRATING");
|
||||||
let old_store = &self.store;
|
let old_store = &self.store;
|
||||||
|
|
||||||
|
let inst_data = self.store.get_value_data_full(self.id_from_string("0".to_owned()))?;
|
||||||
|
new_store.set(self.id_from_string("0".to_owned()), inst_data.to_owned())?;
|
||||||
|
|
||||||
for id in old_store.id_iter()? {
|
for id in old_store.id_iter()? {
|
||||||
let data = self.store.get_value_data_full(self.id_from_string(id?))?;
|
let data = self.store.get_value_data_full(self.id_from_string(id?))?;
|
||||||
|
|
||||||
|
|||||||
@@ -66,8 +66,8 @@ impl Item<'_> {
|
|||||||
|
|
||||||
impl<'a> Display for Item<'a> {
|
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 with id: {}", self.id());
|
||||||
return Ok(());
|
return Ok(());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -120,6 +120,9 @@ impl ItemData {
|
|||||||
|
|
||||||
pub fn item_data_get_path(data: &CborValue, path: Vec<String>) -> MizeResult<&CborValue> {
|
pub fn item_data_get_path(data: &CborValue, path: Vec<String>) -> MizeResult<&CborValue> {
|
||||||
let mut path_iter = path.clone().into_iter();
|
let mut path_iter = path.clone().into_iter();
|
||||||
|
//println!("data: {:?}", data);
|
||||||
|
//println!("path: {:?}", path);
|
||||||
|
|
||||||
let path_el = match path_iter.nth(0) {
|
let path_el = match path_iter.nth(0) {
|
||||||
Some(val) => val,
|
Some(val) => val,
|
||||||
None => return Ok(data), // our base case
|
None => return Ok(data), // our base case
|
||||||
@@ -129,8 +132,11 @@ pub fn item_data_get_path(data: &CborValue, path: Vec<String>) -> MizeResult<&Cb
|
|||||||
match data {
|
match data {
|
||||||
CborValue::Map(ref map) => {
|
CborValue::Map(ref map) => {
|
||||||
for (key, val) in map {
|
for (key, val) in map {
|
||||||
|
//println!("key: {:?}", key);
|
||||||
|
//println!("path_el: {}", path_el);
|
||||||
if let CborValue::Text(key_str) = key {
|
if let CborValue::Text(key_str) = key {
|
||||||
if key_str == &path_el {
|
if key_str == &path_el {
|
||||||
|
println!("setting sub_data to: {:?}", val);
|
||||||
sub_data = val;
|
sub_data = val;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -86,6 +86,7 @@ impl Store for MemStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn next_id(&self, prev_id_str: &str) -> MizeResult<Option<String>> {
|
fn next_id(&self, prev_id_str: &str) -> MizeResult<Option<String>> {
|
||||||
|
println!("next_id: prev_id_str: {}", prev_id_str);
|
||||||
let inner = self.inner.lock()?;
|
let inner = self.inner.lock()?;
|
||||||
|
|
||||||
let mut id = str_to_u64(prev_id_str)?;
|
let mut id = str_to_u64(prev_id_str)?;
|
||||||
@@ -99,6 +100,7 @@ impl Store for MemStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if inner.map.contains_key(&id) {
|
if inner.map.contains_key(&id) {
|
||||||
|
println!("returning: {}", id);
|
||||||
return Ok(Some(format!("{}", id)));
|
return Ok(Some(format!("{}", id)));
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
@@ -139,7 +141,20 @@ pub fn get_raw_from_cbor<'a>(value: &'a CborValue, path: Vec<&String>) -> MizeRe
|
|||||||
Some(val) => val,
|
Some(val) => val,
|
||||||
None => {
|
None => {
|
||||||
// our base case
|
// our base case
|
||||||
return Err(MizeError::new().msg("path is empty"));
|
let ret_val = match value {
|
||||||
|
CborValue::Bytes(vec) => {
|
||||||
|
trace!("[ {} ] ret value: {:?}", "RET".yellow(), &vec[..]);
|
||||||
|
return Ok(&vec[..]);
|
||||||
|
},
|
||||||
|
CborValue::Text(string) => {
|
||||||
|
trace!("[ {} ] ret value: {:?}", "RET".yellow(), string.as_bytes());
|
||||||
|
return Ok(string.as_bytes());
|
||||||
|
},
|
||||||
|
other => {
|
||||||
|
return Err(MizeError::new()
|
||||||
|
.msg("path is empty and the value is neither Bytes nor Text"));
|
||||||
|
},
|
||||||
|
};
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
trace!("hoooooooo: {:?}", path_el);
|
trace!("hoooooooo: {:?}", path_el);
|
||||||
@@ -181,7 +196,7 @@ pub fn get_raw_from_cbor<'a>(value: &'a CborValue, path: Vec<&String>) -> MizeRe
|
|||||||
trace!("[ {} ] ret value: {:?}", "RET".yellow(), ret_value);
|
trace!("[ {} ] ret value: {:?}", "RET".yellow(), ret_value);
|
||||||
return ret_value;
|
return ret_value;
|
||||||
},
|
},
|
||||||
_ => Err(MizeError::new().msg("get_raw_from_cbor: value is not Bytes, Text or Map")),
|
_ => Err(MizeError::new().msg("get_raw_from_cbor: value is not a map, text or bytes")),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -19,6 +19,10 @@ pub fn get(sub_matches: &ArgMatches) -> MizeResult<()> {
|
|||||||
let item = instance.get(id)?;
|
let item = instance.get(id)?;
|
||||||
|
|
||||||
let recurse_count = sub_matches.get_count("recurse");
|
let recurse_count = sub_matches.get_count("recurse");
|
||||||
|
|
||||||
|
println!("{}", item.as_data_full()?);
|
||||||
|
return Ok(());
|
||||||
|
|
||||||
if recurse_count > 0 {
|
if recurse_count > 0 {
|
||||||
println!("{}", item.id());
|
println!("{}", item.id());
|
||||||
println!("{}", item.as_data_full()?);
|
println!("{}", item.as_data_full()?);
|
||||||
|
|||||||
@@ -20,23 +20,28 @@ pub struct FileStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl FileStore {
|
impl FileStore {
|
||||||
pub fn new(path: String) -> MizeResult<FileStore> {
|
pub fn new(path_str: &str) -> MizeResult<FileStore> {
|
||||||
|
|
||||||
|
let path = Path::new(path_str);
|
||||||
|
|
||||||
// create that path if it does not exist
|
// create that path if it does not exist
|
||||||
fs::create_dir_all(Path::new(&path).join("store"))?;
|
fs::create_dir_all(Path::new(&path).join("store"))?;
|
||||||
|
|
||||||
// check for valid pid file
|
// check for valid pid file
|
||||||
if let Some(pid) = valid_pid_file(&path)? {
|
if let Some(pid) = valid_pid_file(path)? {
|
||||||
// store already opened
|
// store already opened
|
||||||
return Err(mize_err!("MizeStore at path {} is already opened by process with pid {}", &path, pid));
|
return Err(mize_err!("MizeStore at path {} is already opened by process with pid {}", path.display(), pid));
|
||||||
|
|
||||||
} else {
|
} else {
|
||||||
// write our own pid file
|
// write our own pid file
|
||||||
let pid = std::process::id();
|
let pid = std::process::id();
|
||||||
let mut file = OpenOptions::new().write(true).create(true).open(&path)?;
|
let mut file = OpenOptions::new().write(true).create(true).open(path.join("pid"))?;
|
||||||
write!(file, "{}", pid)?;
|
write!(file, "{}", pid)?;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// init the store
|
||||||
|
fs::write(path.join("next_id"), "0");
|
||||||
|
|
||||||
Ok(FileStore { path: Path::new(&path).to_owned() })
|
Ok(FileStore { path: Path::new(&path).to_owned() })
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -59,7 +64,10 @@ impl Store for FileStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn set(&self, id: MizeId, data: ItemData) -> MizeResult<()> {
|
fn set(&self, id: MizeId, data: ItemData) -> MizeResult<()> {
|
||||||
|
println!("setting: {:?}", id);
|
||||||
let path = self.path.join("store").join(id.namespace_str()).join(id.store_part());
|
let path = self.path.join("store").join(id.namespace_str()).join(id.store_part());
|
||||||
|
println!("path: {:?}", &path);
|
||||||
|
fs::create_dir_all(self.path.join("store").join(id.namespace_str()))?;
|
||||||
|
|
||||||
let file = OpenOptions::new().write(true).create(true).open(path)?;
|
let file = OpenOptions::new().write(true).create(true).open(path)?;
|
||||||
|
|
||||||
@@ -92,7 +100,9 @@ impl Store for FileStore {
|
|||||||
}
|
}
|
||||||
|
|
||||||
fn get_value_data_full(&self, id: MizeId) -> MizeResult<ItemData> {
|
fn get_value_data_full(&self, id: MizeId) -> MizeResult<ItemData> {
|
||||||
let file = OpenOptions::new().read(true).create(true).open(self.path.join("store").join(id.store_part()))?;
|
let file_path = self.path.join("store").join(id.namespace_str()).join(id.store_part());
|
||||||
|
println!("file_path: {:?}", file_path.display());
|
||||||
|
let file = OpenOptions::new().read(true).open(file_path)?;
|
||||||
|
|
||||||
let cbor_value: CborValue = ciborium::from_reader(file)
|
let cbor_value: CborValue = ciborium::from_reader(file)
|
||||||
.mize_result_msg(format!("could not read file '{}' from FileStore", self.path.join("store").join(id.store_part()).display()))?;
|
.mize_result_msg(format!("could not read file '{}' from FileStore", self.path.join("store").join(id.store_part()).display()))?;
|
||||||
@@ -117,9 +127,9 @@ impl Store for FileStore {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn valid_pid_file(path: &String) -> MizeResult<Option<u32>> {
|
fn valid_pid_file(path: &Path) -> MizeResult<Option<u32>> {
|
||||||
|
|
||||||
let pid_file_path = Path::new(path).join("pid");
|
let pid_file_path = path.join("pid");
|
||||||
|
|
||||||
// if the pid file does not exist, there is no pid...
|
// if the pid file does not exist, there is no pid...
|
||||||
if !pid_file_path.exists() {
|
if !pid_file_path.exists() {
|
||||||
|
|||||||
@@ -59,11 +59,11 @@ pub fn os_instance_init(instance: &mut Instance) -> MizeResult<()> {
|
|||||||
};
|
};
|
||||||
|
|
||||||
////// if a config.store is set, upgrade to the filestore there
|
////// if a config.store is set, upgrade to the filestore there
|
||||||
let instance_path = instance.get("0/conifg/instance_path")?.value_string()?;
|
let store_path = instance.get("0/config/store_path")?.value_string()?;
|
||||||
|
|
||||||
if instance_path != "" {
|
if store_path != "" {
|
||||||
let file_store = FileStore::new(instance_path)?;
|
let file_store = FileStore::new(store_path.as_str())?;
|
||||||
let new_instance = &mut instance.migrate_to_store(Box::new(file_store))?;
|
instance.migrate_to_store(Box::new(file_store))?;
|
||||||
}
|
}
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
|
|||||||
@@ -2,3 +2,4 @@
|
|||||||
[config]
|
[config]
|
||||||
namespace = "test.c2vi.dev"
|
namespace = "test.c2vi.dev"
|
||||||
test = "hello from test item"
|
test = "hello from test item"
|
||||||
|
store_path = "/home/me/.mize"
|
||||||
|
|||||||
Reference in New Issue
Block a user