reifydb_core/key/
migration.rs1use crate::{
5 encoded::key::{EncodedKey, EncodedKeyRange},
6 interface::catalog::id::MigrationId,
7 key::{EncodableKey, KeyKind},
8 util::encoding::keycode::{deserializer::KeyDeserializer, serializer::KeySerializer},
9};
10
11#[derive(Debug, Clone, PartialEq)]
12pub struct MigrationKey {
13 pub migration: MigrationId,
14}
15
16impl MigrationKey {
17 pub fn new(migration: MigrationId) -> Self {
18 Self {
19 migration,
20 }
21 }
22
23 pub fn encoded(migration: impl Into<MigrationId>) -> EncodedKey {
24 Self::new(migration.into()).encode()
25 }
26
27 pub fn full_scan() -> EncodedKeyRange {
28 EncodedKeyRange::start_end(Some(Self::start()), Some(Self::end()))
29 }
30
31 fn start() -> EncodedKey {
32 let mut serializer = KeySerializer::with_capacity(1);
33 serializer.extend_u8(Self::KIND as u8);
34 serializer.to_encoded_key()
35 }
36
37 fn end() -> EncodedKey {
38 let mut serializer = KeySerializer::with_capacity(1);
39 serializer.extend_u8(Self::KIND as u8 - 1);
40 serializer.to_encoded_key()
41 }
42}
43
44impl EncodableKey for MigrationKey {
45 const KIND: KeyKind = KeyKind::Migration;
46
47 fn encode(&self) -> EncodedKey {
48 let mut serializer = KeySerializer::with_capacity(9);
49 serializer.extend_u8(Self::KIND as u8).extend_u64(self.migration);
50 serializer.to_encoded_key()
51 }
52
53 fn decode(key: &EncodedKey) -> Option<Self> {
54 let mut de = KeyDeserializer::from_bytes(key.as_slice());
55
56 let kind: KeyKind = de.read_u8().ok()?.try_into().ok()?;
57 if kind != Self::KIND {
58 return None;
59 }
60
61 let migration = de.read_u64().ok()?;
62
63 Some(Self {
64 migration: MigrationId(migration),
65 })
66 }
67}