couchbase_core/memdx/
durability_level.rs

1/*
2 *
3 *  * Copyright (c) 2025 Couchbase, Inc.
4 *  *
5 *  * Licensed under the Apache License, Version 2.0 (the "License");
6 *  * you may not use this file except in compliance with the License.
7 *  * You may obtain a copy of the License at
8 *  *
9 *  *    http://www.apache.org/licenses/LICENSE-2.0
10 *  *
11 *  * Unless required by applicable law or agreed to in writing, software
12 *  * distributed under the License is distributed on an "AS IS" BASIS,
13 *  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 *  * See the License for the specific language governing permissions and
15 *  * limitations under the License.
16 *
17 */
18
19use std::fmt::Display;
20use std::time::Duration;
21
22#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
23#[non_exhaustive]
24pub struct DurabilityLevel(InnerDurabilityLevel);
25
26impl DurabilityLevel {
27    pub const MAJORITY: DurabilityLevel = DurabilityLevel(InnerDurabilityLevel::Majority);
28
29    pub const MAJORITY_AND_PERSIST_ACTIVE: DurabilityLevel =
30        DurabilityLevel(InnerDurabilityLevel::MajorityAndPersistActive);
31
32    pub const PERSIST_TO_MAJORITY: DurabilityLevel =
33        DurabilityLevel(InnerDurabilityLevel::PersistToMajority);
34}
35
36impl Display for DurabilityLevel {
37    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
38        match &self.0 {
39            InnerDurabilityLevel::Majority => write!(f, "majority"),
40            InnerDurabilityLevel::MajorityAndPersistActive => write!(f, "majorityAndPersistActive"),
41            InnerDurabilityLevel::PersistToMajority => write!(f, "persistToMajority"),
42            InnerDurabilityLevel::Other(val) => write!(f, "unknown({val})"),
43        }
44    }
45}
46
47#[derive(Copy, Clone, Debug, Ord, PartialOrd, Eq, PartialEq, Hash)]
48pub(crate) enum InnerDurabilityLevel {
49    Majority,
50    MajorityAndPersistActive,
51    PersistToMajority,
52    Other(u8),
53}
54
55impl From<DurabilityLevel> for u8 {
56    fn from(value: DurabilityLevel) -> u8 {
57        match value {
58            DurabilityLevel::MAJORITY => 1,
59            DurabilityLevel::MAJORITY_AND_PERSIST_ACTIVE => 2,
60            DurabilityLevel::PERSIST_TO_MAJORITY => 3,
61            _ => 0,
62        }
63    }
64}
65
66impl From<u8> for DurabilityLevel {
67    fn from(data: u8) -> Self {
68        match data {
69            1 => DurabilityLevel::MAJORITY,
70            2 => DurabilityLevel::MAJORITY_AND_PERSIST_ACTIVE,
71            3 => DurabilityLevel::PERSIST_TO_MAJORITY,
72            _ => DurabilityLevel(InnerDurabilityLevel::Other(data)),
73        }
74    }
75}
76
77#[derive(Copy, Clone, Debug)]
78pub struct DurabilityLevelSettings {
79    pub durability_level: DurabilityLevel,
80    pub timeout: Option<Duration>,
81}
82
83impl DurabilityLevelSettings {
84    pub fn new(level: DurabilityLevel) -> Self {
85        Self {
86            durability_level: level,
87            timeout: None,
88        }
89    }
90
91    pub fn new_with_timeout(level: DurabilityLevel, timeout: Duration) -> Self {
92        Self {
93            durability_level: level,
94            timeout: Some(timeout),
95        }
96    }
97}