From e32463af312cccd08084fcc793c6f105478eb8bb Mon Sep 17 00:00:00 2001 From: Tim Bruijnzeels Date: Sat, 1 May 2021 21:22:39 +0200 Subject: [PATCH] Move directories rather than keys for Publication Server migration. --- src/commons/eventsourcing/kv.rs | 21 ++++++++++++++++++- src/upgrades/v0_9_0/pubd_objects_migration.rs | 4 +--- 2 files changed, 21 insertions(+), 4 deletions(-) diff --git a/src/commons/eventsourcing/kv.rs b/src/commons/eventsourcing/kv.rs index 2ae2feec..10b203da 100644 --- a/src/commons/eventsourcing/kv.rs +++ b/src/commons/eventsourcing/kv.rs @@ -176,6 +176,13 @@ impl KeyValueStore { } } + /// Archives the content of a scope to sub-scope in that scope + pub fn scope_archive(&self, scope: &str, sub_scope: &str) -> Result<(), KeyValueError> { + match self { + KeyValueStore::Disk(disk_store) => disk_store.scope_archive(scope, sub_scope), + } + } + /// Returns whether a scope exists pub fn has_scope(&self, scope: String) -> Result { match self { @@ -237,7 +244,7 @@ impl KeyValueStoreDiskImpl { path } - fn scope_path(&self, scope: Option<&String>) -> PathBuf { + fn scope_path>(&self, scope: Option

) -> PathBuf { let mut path = self.base.clone(); if let Some(scope) = scope { path.push(scope); @@ -331,6 +338,18 @@ impl KeyValueStoreDiskImpl { Self::read_dir(&self.base, false, true) } + fn scope_archive(&self, scope: &str, sub_scope: &str) -> Result<(), KeyValueError> { + let scope_path = self.scope_path(Some(scope)); + let tmp_path = self.scope_path(Some(format!(".{}", scope))); + let end_path = self.scope_path(Some(format!("{}/{}", scope, sub_scope))); + + fs::rename(&scope_path, &tmp_path)?; + fs::create_dir_all(&scope_path)?; + fs::rename(&tmp_path, &end_path)?; + + Ok(()) + } + fn keys(&self, scope: Option, matching: &str) -> Result, KeyValueError> { let path = self.scope_path(scope.as_ref()); diff --git a/src/upgrades/v0_9_0/pubd_objects_migration.rs b/src/upgrades/v0_9_0/pubd_objects_migration.rs index baab9b9e..b162f418 100644 --- a/src/upgrades/v0_9_0/pubd_objects_migration.rs +++ b/src/upgrades/v0_9_0/pubd_objects_migration.rs @@ -113,9 +113,7 @@ impl UpgradeStore for PubdStoreMigration { // Archive all keys in the scope, then we can write new keys as needed without // overwriting anything when we renumber. - for key in self.store.keys(Some(scope.to_string()), "")? { - self.archive_to_migration_scope(&key)?; - } + self.store.scope_archive(scope, MIGRATION_SCOPE)?; let migration_scope = format!("{}/{}", scope, MIGRATION_SCOPE);