migrating to fsstore works

This commit is contained in:
Sebastian Moser
2024-06-16 17:44:20 +02:00
parent 5213dd156e
commit 7340aaa37a
8 changed files with 64 additions and 18 deletions

View File

@@ -19,6 +19,10 @@ pub fn get(sub_matches: &ArgMatches) -> MizeResult<()> {
let item = instance.get(id)?;
let recurse_count = sub_matches.get_count("recurse");
println!("{}", item.as_data_full()?);
return Ok(());
if recurse_count > 0 {
println!("{}", item.id());
println!("{}", item.as_data_full()?);

View File

@@ -20,23 +20,28 @@ pub struct 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
fs::create_dir_all(Path::new(&path).join("store"))?;
// check for valid pid file
if let Some(pid) = valid_pid_file(&path)? {
if let Some(pid) = valid_pid_file(path)? {
// 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 {
// write our own pid file
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)?;
}
// init the store
fs::write(path.join("next_id"), "0");
Ok(FileStore { path: Path::new(&path).to_owned() })
}
}
@@ -59,7 +64,10 @@ impl Store for FileStore {
}
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());
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)?;
@@ -92,7 +100,9 @@ impl Store for FileStore {
}
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)
.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 !pid_file_path.exists() {

View File

@@ -59,11 +59,11 @@ pub fn os_instance_init(instance: &mut Instance) -> MizeResult<()> {
};
////// 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 != "" {
let file_store = FileStore::new(instance_path)?;
let new_instance = &mut instance.migrate_to_store(Box::new(file_store))?;
if store_path != "" {
let file_store = FileStore::new(store_path.as_str())?;
instance.migrate_to_store(Box::new(file_store))?;
}
Ok(())