|
| 1 | +// Copyright 2021 Datafuse Labs |
| 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 | +use map_api::Marked; |
| 16 | +use map_api::SeqMarked; |
| 17 | + |
| 18 | +/// The value of an expiration index is the record key. |
| 19 | +#[derive(Default, Debug, Clone, PartialEq, Eq, serde::Serialize, serde::Deserialize)] |
| 20 | +pub struct ExpireValue { |
| 21 | + #[serde(skip_serializing_if = "is_zero")] |
| 22 | + #[serde(default)] |
| 23 | + pub seq: u64, |
| 24 | + pub key: String, |
| 25 | +} |
| 26 | + |
| 27 | +fn is_zero(v: &u64) -> bool { |
| 28 | + *v == 0 |
| 29 | +} |
| 30 | + |
| 31 | +impl ExpireValue { |
| 32 | + pub fn new(key: impl ToString, seq: u64) -> Self { |
| 33 | + Self { |
| 34 | + key: key.to_string(), |
| 35 | + seq, |
| 36 | + } |
| 37 | + } |
| 38 | + /// Convert internally used expire-index value `Marked<String>` to externally used type `ExpireValue`. |
| 39 | + /// |
| 40 | + /// `Marked<String>` is the value of an expire-index in the state machine. |
| 41 | + /// `ExpireValue.seq` equals to the seq of the str-map record, |
| 42 | + /// i.e., when an expire-index is inserted, the seq does not increase. |
| 43 | + pub fn from_marked(seq_marked: SeqMarked<String>) -> Option<Self> { |
| 44 | + let (seq, mm) = seq_marked.into_parts(); |
| 45 | + |
| 46 | + match mm { |
| 47 | + Marked::TombStone => None, |
| 48 | + Marked::Normal(s) => Some(ExpireValue::new(s, seq)), |
| 49 | + } |
| 50 | + } |
| 51 | +} |
| 52 | + |
| 53 | +impl From<ExpireValue> for SeqMarked<String> { |
| 54 | + fn from(value: ExpireValue) -> Self { |
| 55 | + SeqMarked::new_normal(value.seq, value.key) |
| 56 | + } |
| 57 | +} |
| 58 | + |
| 59 | +#[cfg(test)] |
| 60 | +mod tests { |
| 61 | + use super::*; |
| 62 | + |
| 63 | + #[test] |
| 64 | + fn test_expire_value_serde() -> anyhow::Result<()> { |
| 65 | + { |
| 66 | + let v = ExpireValue { |
| 67 | + seq: 0, |
| 68 | + key: "a".to_string(), |
| 69 | + }; |
| 70 | + let s = serde_json::to_string(&v)?; |
| 71 | + let want = r#"{"key":"a"}"#; |
| 72 | + assert_eq!(want, s); |
| 73 | + |
| 74 | + let got = serde_json::from_str::<ExpireValue>(want)?; |
| 75 | + assert_eq!(v, got); |
| 76 | + } |
| 77 | + |
| 78 | + { |
| 79 | + let v = ExpireValue { |
| 80 | + seq: 5, |
| 81 | + key: "a".to_string(), |
| 82 | + }; |
| 83 | + let s = serde_json::to_string(&v)?; |
| 84 | + let want = r#"{"seq":5,"key":"a"}"#; |
| 85 | + assert_eq!(want, s); |
| 86 | + |
| 87 | + let got = serde_json::from_str::<ExpireValue>(want)?; |
| 88 | + assert_eq!(v, got); |
| 89 | + } |
| 90 | + |
| 91 | + Ok(()) |
| 92 | + } |
| 93 | + |
| 94 | + #[test] |
| 95 | + fn test_from_seq_marked() { |
| 96 | + // Test normal case - should return Some(ExpireValue) |
| 97 | + { |
| 98 | + let seq = 42; |
| 99 | + let key = "test_key".to_string(); |
| 100 | + let marked = Marked::Normal(key.clone()); |
| 101 | + let seq_marked = SeqMarked::new(seq, marked); |
| 102 | + |
| 103 | + let result = ExpireValue::from_marked(seq_marked); |
| 104 | + |
| 105 | + assert!(result.is_some()); |
| 106 | + let expire_value = result.unwrap(); |
| 107 | + assert_eq!(expire_value.seq, seq); |
| 108 | + assert_eq!(expire_value.key, key); |
| 109 | + } |
| 110 | + |
| 111 | + // Test tombstone case - should return None |
| 112 | + { |
| 113 | + let seq = 100; |
| 114 | + let marked = Marked::TombStone; |
| 115 | + let seq_marked = SeqMarked::new(seq, marked); |
| 116 | + |
| 117 | + let result = ExpireValue::from_marked(seq_marked); |
| 118 | + |
| 119 | + assert!(result.is_none()); |
| 120 | + } |
| 121 | + |
| 122 | + // Test with zero seq |
| 123 | + { |
| 124 | + let seq = 0; |
| 125 | + let key = "zero_seq_key".to_string(); |
| 126 | + let marked = Marked::Normal(key.clone()); |
| 127 | + let seq_marked = SeqMarked::new(seq, marked); |
| 128 | + |
| 129 | + let result = ExpireValue::from_marked(seq_marked); |
| 130 | + |
| 131 | + assert!(result.is_some()); |
| 132 | + let expire_value = result.unwrap(); |
| 133 | + assert_eq!(expire_value.seq, seq); |
| 134 | + assert_eq!(expire_value.key, key); |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + // Test From<ExpireValue> for Marked<String> |
| 139 | + #[test] |
| 140 | + fn test_from_expire_value_for_marked() -> anyhow::Result<()> { |
| 141 | + let m = SeqMarked::new_normal(1, "2".to_string()); |
| 142 | + let s = ExpireValue::new("2", 1); |
| 143 | + assert_eq!(m, s.into()); |
| 144 | + |
| 145 | + Ok(()) |
| 146 | + } |
| 147 | + |
| 148 | + // Test From<Marked<String>> for Option<ExpireValue> |
| 149 | + #[test] |
| 150 | + fn test_from_marked_for_option_expire_value() -> anyhow::Result<()> { |
| 151 | + let m = SeqMarked::new_normal(1, "2".to_string()); |
| 152 | + let s: Option<ExpireValue> = Some(ExpireValue::new("2".to_string(), 1)); |
| 153 | + assert_eq!(s, ExpireValue::from_marked(m)); |
| 154 | + |
| 155 | + let m = SeqMarked::new_tombstone(1); |
| 156 | + let s: Option<ExpireValue> = None; |
| 157 | + assert_eq!(s, ExpireValue::from_marked(m)); |
| 158 | + |
| 159 | + Ok(()) |
| 160 | + } |
| 161 | +} |
0 commit comments