Skip to main content

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
77impl From<&DurabilityLevel> for u8 {
78    fn from(value: &DurabilityLevel) -> u8 {
79        match value.0 {
80            InnerDurabilityLevel::Majority => 1,
81            InnerDurabilityLevel::MajorityAndPersistActive => 2,
82            InnerDurabilityLevel::PersistToMajority => 3,
83            InnerDurabilityLevel::Other(val) => val,
84        }
85    }
86}
87
88#[derive(Copy, Clone, Debug)]
89pub struct DurabilityLevelSettings {
90    pub durability_level: DurabilityLevel,
91    pub timeout: Option<Duration>,
92}
93
94impl DurabilityLevelSettings {
95    pub fn new(level: DurabilityLevel) -> Self {
96        Self {
97            durability_level: level,
98            timeout: None,
99        }
100    }
101
102    pub fn new_with_timeout(level: DurabilityLevel, timeout: Duration) -> Self {
103        Self {
104            durability_level: level,
105            timeout: Some(timeout),
106        }
107    }
108}