reduct_base/msg/
bucket_api.rs

1// Copyright 2023 ReductSoftware UG
2// This Source Code Form is subject to the terms of the Mozilla Public
3//    License, v. 2.0. If a copy of the MPL was not distributed with this
4//    file, You can obtain one at https://mozilla.org/MPL/2.0/.
5use crate::msg::entry_api::EntryInfo;
6use serde::{Deserialize, Serialize};
7use std::fmt::Display;
8use std::str::FromStr;
9
10/// Quota type
11///
12/// NONE: No quota
13/// FIFO: When quota_size is reached, the oldest records are deleted
14/// HARD: When quota_size is reached, no more records can be added
15#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
16pub enum QuotaType {
17    #[default]
18    NONE = 0,
19    FIFO = 1,
20    HARD = 2,
21}
22
23impl From<i32> for QuotaType {
24    fn from(value: i32) -> Self {
25        match value {
26            0 => QuotaType::NONE,
27            1 => QuotaType::FIFO,
28            2 => QuotaType::HARD,
29            _ => QuotaType::NONE,
30        }
31    }
32}
33
34impl FromStr for QuotaType {
35    type Err = ();
36
37    fn from_str(s: &str) -> Result<Self, Self::Err> {
38        match s {
39            "NONE" => Ok(QuotaType::NONE),
40            "FIFO" => Ok(QuotaType::FIFO),
41            "HARD" => Ok(QuotaType::HARD),
42            _ => Err(()),
43        }
44    }
45}
46
47impl Display for QuotaType {
48    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
49        match self {
50            QuotaType::NONE => write!(f, "NONE"),
51            QuotaType::FIFO => write!(f, "FIFO"),
52            QuotaType::HARD => write!(f, "HARD"),
53        }
54    }
55}
56
57/// Bucket settings
58#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
59pub struct BucketSettings {
60    /// Quota type see QuotaType
61    pub quota_type: Option<QuotaType>,
62    /// Quota size in bytes
63    pub quota_size: Option<u64>,
64    /// Max size of a block in bytes to start a new one
65    pub max_block_size: Option<u64>,
66    /// Max records in a block to start a new block one
67    pub max_block_records: Option<u64>,
68}
69
70/// Bucket information
71#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
72pub struct BucketInfo {
73    /// Unique bucket name
74    pub name: String,
75    /// Number of records in bucket
76    pub entry_count: u64,
77    /// Total size of bucket in bytes
78    pub size: u64,
79    /// Oldest record in bucket
80    pub oldest_record: u64,
81    /// Latest record in bucket
82    pub latest_record: u64,
83    /// Provisioned
84    pub is_provisioned: bool,
85}
86
87/// Full bucket information
88#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
89pub struct FullBucketInfo {
90    /// Bucket information
91    pub info: BucketInfo,
92    /// Bucket settings
93    pub settings: BucketSettings,
94    /// Entries in bucket
95    pub entries: Vec<EntryInfo>,
96}
97
98#[cfg(test)]
99mod tests {
100    use super::*;
101    use rstest::rstest;
102
103    #[rstest]
104    #[case(QuotaType::NONE, "NONE")]
105    #[case(QuotaType::FIFO, "FIFO")]
106    #[case(QuotaType::HARD, "HARD")]
107    fn test_enum_as_string(#[case] quota_type: QuotaType, #[case] expected: &str) {
108        let settings = BucketSettings {
109            quota_type: Some(quota_type),
110            quota_size: Some(100),
111            max_block_size: Some(100),
112            max_block_records: Some(100),
113        };
114        let serialized = serde_json::to_string(&settings).unwrap();
115
116        assert_eq!(
117            serialized,
118            format!("{{\"quota_type\":\"{}\",\"quota_size\":100,\"max_block_size\":100,\"max_block_records\":100}}", expected)
119        );
120    }
121
122    #[rstest]
123    #[case(QuotaType::NONE, "NONE")]
124    #[case(QuotaType::FIFO, "FIFO")]
125    #[case(QuotaType::HARD, "HARD")]
126    fn test_quota_from_string(#[case] expected: QuotaType, #[case] as_string: &str) {
127        assert_eq!(QuotaType::from_str(as_string).unwrap(), expected);
128    }
129}
130
131#[derive(Serialize, Deserialize, Default, Clone, Debug, PartialEq)]
132pub struct RenameBucket {
133    /// New bucket name
134    pub new_name: String,
135}