store_api/
manifest.rs

1// Copyright 2023 Greptime Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! metadata service
16pub mod action;
17mod storage;
18
19use async_trait::async_trait;
20use common_error::ext::ErrorExt;
21use serde::de::DeserializeOwned;
22use serde::Serialize;
23
24use crate::manifest::action::{ProtocolAction, ProtocolVersion};
25pub use crate::manifest::storage::*;
26
27pub type ManifestVersion = u64;
28pub const MIN_VERSION: u64 = 0;
29pub const MAX_VERSION: u64 = u64::MAX;
30
31/// The action to alter metadata
32pub trait MetaAction: Serialize + DeserializeOwned + Send + Sync + Clone + std::fmt::Debug {
33    type Error: ErrorExt + Send + Sync;
34
35    /// Set a protocol action into meta action
36    fn set_protocol(&mut self, action: ProtocolAction);
37
38    /// Set previous valid manifest version.
39    fn set_prev_version(&mut self, version: ManifestVersion);
40
41    /// Encode this action into a byte vector
42    fn encode(&self) -> Result<Vec<u8>, Self::Error>;
43
44    /// Decode self from byte slice with reader protocol version,
45    /// return error when reader version is not supported.
46    fn decode(
47        bs: &[u8],
48        reader_version: ProtocolVersion,
49    ) -> Result<(Self, Option<ProtocolAction>), Self::Error>;
50}
51/// The checkpoint by checkpoint
52pub trait Checkpoint: Send + Sync + Clone + std::fmt::Debug {
53    type Error: ErrorExt + Send + Sync;
54
55    /// Set a protocol action into checkpoint
56    fn set_protocol(&mut self, action: ProtocolAction);
57
58    /// The last compacted action's version of checkpoint
59    fn last_version(&self) -> ManifestVersion;
60
61    /// Encode this checkpoint into a byte vector
62    fn encode(&self) -> Result<Vec<u8>, Self::Error>;
63
64    /// Decode self from byte slice with reader protocol version,
65    /// return error when reader version is not supported.
66    fn decode(bs: &[u8], reader_version: ProtocolVersion) -> Result<Self, Self::Error>;
67}
68
69#[async_trait]
70pub trait MetaActionIterator {
71    type MetaAction: MetaAction;
72    type Error: ErrorExt + Send + Sync;
73
74    async fn next_action(
75        &mut self,
76    ) -> Result<Option<(ManifestVersion, Self::MetaAction)>, Self::Error>;
77}
78
79/// Manifest service
80#[async_trait]
81pub trait Manifest: Send + Sync + Clone + 'static {
82    type Error: ErrorExt + Send + Sync;
83    type MetaAction: MetaAction;
84    type MetaActionIterator: MetaActionIterator<Error = Self::Error, MetaAction = Self::MetaAction>;
85    type Checkpoint: Checkpoint;
86
87    /// Update metadata by the action
88    async fn update(&self, action: Self::MetaAction) -> Result<ManifestVersion, Self::Error>;
89
90    /// Scan actions which version in range [start, end)
91    async fn scan(
92        &self,
93        start: ManifestVersion,
94        end: ManifestVersion,
95    ) -> Result<Self::MetaActionIterator, Self::Error>;
96
97    /// Do a checkpoint, it will create a checkpoint and compact actions.
98    async fn do_checkpoint(&self) -> Result<Option<Self::Checkpoint>, Self::Error>;
99
100    /// Returns the last success checkpoint
101    async fn last_checkpoint(&self) -> Result<Option<Self::Checkpoint>, Self::Error>;
102
103    /// Returns the last(or latest) manifest version.
104    fn last_version(&self) -> ManifestVersion;
105
106    /// Start the service
107    async fn start(&self) -> Result<(), Self::Error> {
108        Ok(())
109    }
110    /// Stop the service
111    async fn stop(&self) -> Result<(), Self::Error> {
112        Ok(())
113    }
114}