aboutsummaryrefslogtreecommitdiff
path: root/src/util/migrate.rs
diff options
context:
space:
mode:
authorAlex Auvolat <alex@adnab.me>2023-01-03 14:44:47 +0100
committerAlex Auvolat <alex@adnab.me>2023-01-03 14:44:47 +0100
commitcdb2a591e9d393d24ab5c49bb905b0589b193299 (patch)
tree10c95206d0bd7b30c1fcd14ccc188be374cb1066 /src/util/migrate.rs
parent582b0761790b7958a3ba10c4b549b466997d2dcd (diff)
downloadgarage-cdb2a591e9d393d24ab5c49bb905b0589b193299.tar.gz
garage-cdb2a591e9d393d24ab5c49bb905b0589b193299.zip
Refactor how things are migrated
Diffstat (limited to 'src/util/migrate.rs')
-rw-r--r--src/util/migrate.rs75
1 files changed, 75 insertions, 0 deletions
diff --git a/src/util/migrate.rs b/src/util/migrate.rs
new file mode 100644
index 00000000..199c68f6
--- /dev/null
+++ b/src/util/migrate.rs
@@ -0,0 +1,75 @@
+use serde::{Deserialize, Serialize};
+
+pub trait Migrate: Serialize + for<'de> Deserialize<'de> + 'static {
+ /// A sequence of bytes to add at the beginning of the serialized
+ /// string, to identify that the data is of this version.
+ const MARKER: &'static [u8] = b"";
+
+ /// The previous version of this data type, from which items of this version
+ /// can be migrated. Set `type Previous = NoPrevious` to indicate that this datatype
+ /// is the initial schema and cannot be migrated.
+ type Previous: Migrate;
+
+ /// This function must be filled in by implementors to migrate from a previons iteration
+ /// of the data format.
+ fn migrate(previous: Self::Previous) -> Self;
+
+ fn decode(bytes: &[u8]) -> Option<Self> {
+ if bytes.len() >= Self::MARKER.len() && &bytes[..Self::MARKER.len()] == Self::MARKER {
+ if let Ok(value) =
+ rmp_serde::decode::from_read_ref::<_, Self>(&bytes[Self::MARKER.len()..])
+ {
+ return Some(value);
+ }
+ }
+
+ Self::Previous::decode(bytes).map(Self::migrate)
+ }
+
+ fn encode(&self) -> Result<Vec<u8>, rmp_serde::encode::Error> {
+ let mut wr = Vec::with_capacity(128);
+ wr.extend_from_slice(Self::MARKER);
+ let mut se = rmp_serde::Serializer::new(&mut wr)
+ .with_struct_map()
+ .with_string_variants();
+ self.serialize(&mut se)?;
+ Ok(wr)
+ }
+}
+
+pub trait InitialFormat: Serialize + for<'de> Deserialize<'de> + 'static {
+ /// A sequence of bytes to add at the beginning of the serialized
+ /// string, to identify that the data is of this version.
+ const MARKER: &'static [u8] = b"";
+}
+
+// ----
+
+impl<T: InitialFormat> Migrate for T {
+ const MARKER: &'static [u8] = <T as InitialFormat>::MARKER;
+
+ type Previous = NoPrevious;
+
+ fn migrate(_previous: Self::Previous) -> Self {
+ unreachable!();
+ }
+}
+
+#[derive(Serialize, Deserialize)]
+pub struct NoPrevious;
+
+impl Migrate for NoPrevious {
+ type Previous = NoPrevious;
+
+ fn migrate(_previous: Self::Previous) -> Self {
+ unreachable!();
+ }
+
+ fn decode(_bytes: &[u8]) -> Option<Self> {
+ None
+ }
+
+ fn encode(&self) -> Result<Vec<u8>, rmp_serde::encode::Error> {
+ unreachable!()
+ }
+}