From 32deecc4a170461892522d374aa2b2940c23bf0d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BC=A0=E7=82=8E=E6=B3=BC?= Date: Sun, 8 Oct 2023 12:41:52 +0800 Subject: [PATCH] chore: rename StaticLeveledMap to StaticLevels; refactor Marked::new_xxx() (#13114) --- .../src/sm_v002/leveled_store/level.rs | 2 +- .../src/sm_v002/leveled_store/leveled_map.rs | 15 +- .../sm_v002/leveled_store/leveled_map_test.rs | 198 ++++++++---------- .../src/sm_v002/leveled_store/mod.rs | 2 +- .../src/sm_v002/leveled_store/ref_.rs | 6 +- .../src/sm_v002/leveled_store/ref_mut.rs | 6 +- ...static_leveled_map.rs => static_levels.rs} | 6 +- .../src/sm_v002/marked/marked_test.rs | 24 +-- src/meta/raft-store/src/sm_v002/marked/mod.rs | 88 +++++++- src/meta/raft-store/src/sm_v002/sm_v002.rs | 2 +- .../raft-store/src/sm_v002/sm_v002_test.rs | 10 +- .../src/sm_v002/snapshot_view_v002.rs | 14 +- .../src/sm_v002/snapshot_view_v002_test.rs | 22 +- 13 files changed, 228 insertions(+), 167 deletions(-) rename src/meta/raft-store/src/sm_v002/leveled_store/{static_leveled_map.rs => static_levels.rs} (96%) diff --git a/src/meta/raft-store/src/sm_v002/leveled_store/level.rs b/src/meta/raft-store/src/sm_v002/leveled_store/level.rs index 737d0c2491d05..a5ab1904afcbe 100644 --- a/src/meta/raft-store/src/sm_v002/leveled_store/level.rs +++ b/src/meta/raft-store/src/sm_v002/leveled_store/level.rs @@ -120,7 +120,7 @@ impl MapApi for Level { let marked = if let Some((v, meta)) = value { let seq = self.sys_data_mut().next_seq(); - Marked::new_normal(seq, v, meta) + Marked::new_with_meta(seq, v, meta) } else { // Do not increase the sequence number, just use the max seq for all tombstone. let seq = self.curr_seq(); diff --git a/src/meta/raft-store/src/sm_v002/leveled_store/leveled_map.rs b/src/meta/raft-store/src/sm_v002/leveled_store/leveled_map.rs index a502ca697383b..0df7081de96a2 100644 --- a/src/meta/raft-store/src/sm_v002/leveled_store/leveled_map.rs +++ b/src/meta/raft-store/src/sm_v002/leveled_store/leveled_map.rs @@ -28,7 +28,7 @@ use crate::sm_v002::leveled_store::map_api::MapApiRO; use crate::sm_v002::leveled_store::map_api::MapKey; use crate::sm_v002::leveled_store::ref_::Ref; use crate::sm_v002::leveled_store::ref_mut::RefMut; -use crate::sm_v002::leveled_store::static_leveled_map::StaticLeveledMap; +use crate::sm_v002::leveled_store::static_levels::StaticLevels; use crate::sm_v002::marked::Marked; /// State machine data organized in multiple levels. @@ -42,9 +42,8 @@ pub struct LeveledMap { /// The top level is the newest and writable. writable: Level, - /// The immutable levels, from the oldest to the newest. - /// levels[0] is the bottom and oldest level. - frozen: StaticLeveledMap, + /// The immutable levels. + frozen: StaticLevels, } impl LeveledMap { @@ -62,8 +61,8 @@ impl LeveledMap { .chain(self.frozen.iter_levels()) } - /// Freeze the current writable level and create a new writable level. - pub fn freeze_writable(&mut self) -> &StaticLeveledMap { + /// Freeze the current writable level and create a new empty writable level. + pub fn freeze_writable(&mut self) -> &StaticLevels { let new_writable = self.writable.new_level(); let frozen = std::mem::replace(&mut self.writable, new_writable); @@ -83,12 +82,12 @@ impl LeveledMap { } /// Return a reference to the immutable levels. - pub fn frozen_ref(&self) -> &StaticLeveledMap { + pub fn frozen_ref(&self) -> &StaticLevels { &self.frozen } /// Replace all immutable levels with the given one. - pub(crate) fn replace_frozen_levels(&mut self, b: StaticLeveledMap) { + pub(crate) fn replace_frozen(&mut self, b: StaticLevels) { self.frozen = b; } diff --git a/src/meta/raft-store/src/sm_v002/leveled_store/leveled_map_test.rs b/src/meta/raft-store/src/sm_v002/leveled_store/leveled_map_test.rs index 920adffe2ea4f..a8ed6eb114914 100644 --- a/src/meta/raft-store/src/sm_v002/leveled_store/leveled_map_test.rs +++ b/src/meta/raft-store/src/sm_v002/leveled_store/leveled_map_test.rs @@ -29,20 +29,20 @@ async fn test_freeze() -> anyhow::Result<()> { // Insert an entry at level 0 let (prev, result) = l.str_map_mut().set(s("a1"), Some((b("b0"), None))).await; assert_eq!(prev, Marked::new_tomb_stone(0)); - assert_eq!(result, Marked::new_normal(1, b("b0"), None)); + assert_eq!(result, Marked::new_with_meta(1, b("b0"), None)); // Insert the same entry at level 1 l.freeze_writable(); let (prev, result) = l.str_map_mut().set(s("a1"), Some((b("b1"), None))).await; - assert_eq!(prev, Marked::new_normal(1, b("b0"), None)); - assert_eq!(result, Marked::new_normal(2, b("b1"), None)); + assert_eq!(prev, Marked::new_with_meta(1, b("b0"), None)); + assert_eq!(result, Marked::new_with_meta(2, b("b1"), None)); // Listing entries from all levels see the latest let got = l.str_map().range(s("")..).await.collect::>().await; assert_eq!(got, vec![ // - (s("a1"), Marked::new_normal(2, b("b1"), None)), + (s("a1"), Marked::new_with_meta(2, b("b1"), None)), ]); // Listing from the base level sees the old value. @@ -56,7 +56,7 @@ async fn test_freeze() -> anyhow::Result<()> { .await; assert_eq!(got, vec![ // - (s("a1"), Marked::new_normal(1, b("b0"), None)), + (s("a1"), Marked::new_with_meta(1, b("b0"), None)), ]); Ok(()) @@ -69,29 +69,29 @@ async fn test_single_level() -> anyhow::Result<()> { // Write a1 let (prev, result) = l.str_map_mut().set(s("a1"), Some((b("b1"), None))).await; assert_eq!(prev, Marked::new_tomb_stone(0)); - assert_eq!(result, Marked::new_normal(1, b("b1"), None)); + assert_eq!(result, Marked::new_with_meta(1, b("b1"), None)); // Write more let (_prev, result) = l.str_map_mut().set(s("a2"), Some((b("b2"), None))).await; - assert_eq!(result, Marked::new_normal(2, b("b2"), None)); + assert_eq!(result, Marked::new_with_meta(2, b("b2"), None)); let (_prev, result) = l.str_map_mut().set(s("a3"), Some((b("b3"), None))).await; - assert_eq!(result, Marked::new_normal(3, b("b3"), None)); + assert_eq!(result, Marked::new_with_meta(3, b("b3"), None)); let (_prev, result) = l.str_map_mut().set(s("x1"), Some((b("y1"), None))).await; - assert_eq!(result, Marked::new_normal(4, b("y1"), None)); + assert_eq!(result, Marked::new_with_meta(4, b("y1"), None)); let (_prev, result) = l.str_map_mut().set(s("x2"), Some((b("y2"), None))).await; - assert_eq!(result, Marked::new_normal(5, b("y2"), None)); + assert_eq!(result, Marked::new_with_meta(5, b("y2"), None)); // Override a1 let (prev, result) = l.str_map_mut().set(s("a1"), Some((b("b1"), None))).await; - assert_eq!(prev, Marked::new_normal(1, b("b1"), None)); - assert_eq!(result, Marked::new_normal(6, b("b1"), None)); + assert_eq!(prev, Marked::new_with_meta(1, b("b1"), None)); + assert_eq!(result, Marked::new_with_meta(6, b("b1"), None)); // Delete a3 let (prev, result) = l.str_map_mut().set(s("a3"), None).await; - assert_eq!(prev, Marked::new_normal(3, b("b3"), None)); + assert_eq!(prev, Marked::new_with_meta(3, b("b3"), None)); assert_eq!( result, Marked::new_tomb_stone(6), @@ -103,16 +103,16 @@ async fn test_single_level() -> anyhow::Result<()> { let got = it.collect::>().await; assert_eq!(got, vec![ // - (s("a1"), Marked::new_normal(6, b("b1"), None)), - (s("a2"), Marked::new_normal(2, b("b2"), None)), + (s("a1"), Marked::new_with_meta(6, b("b1"), None)), + (s("a2"), Marked::new_with_meta(2, b("b2"), None)), (s("a3"), Marked::new_tomb_stone(6)), - (s("x1"), Marked::new_normal(4, b("y1"), None)), - (s("x2"), Marked::new_normal(5, b("y2"), None)), + (s("x1"), Marked::new_with_meta(4, b("y1"), None)), + (s("x2"), Marked::new_with_meta(5, b("y2"), None)), ]); // Get let got = l.str_map().get("a2").await; - assert_eq!(got, Marked::new_normal(2, b("b2"), None)); + assert_eq!(got, Marked::new_with_meta(2, b("b2"), None)); let got = l.str_map().get("a3").await; assert_eq!(got, Marked::new_tomb_stone(6)); @@ -134,10 +134,10 @@ async fn test_two_levels() -> anyhow::Result<()> { let got = it.collect::>().await; assert_eq!(got, vec![ // - (s("a1"), Marked::new_normal(1, b("b1"), None)), - (s("a2"), Marked::new_normal(2, b("b2"), None)), - (s("x1"), Marked::new_normal(3, b("y1"), None)), - (s("x2"), Marked::new_normal(4, b("y2"), None)), + (s("a1"), Marked::new_with_meta(1, b("b1"), None)), + (s("a2"), Marked::new_with_meta(2, b("b2"), None)), + (s("x1"), Marked::new_with_meta(3, b("y1"), None)), + (s("x2"), Marked::new_with_meta(4, b("y2"), None)), ]); // Create a new level @@ -146,42 +146,42 @@ async fn test_two_levels() -> anyhow::Result<()> { // Override let (prev, result) = l.str_map_mut().set(s("a2"), Some((b("b3"), None))).await; - assert_eq!(prev, Marked::new_normal(2, b("b2"), None)); - assert_eq!(result, Marked::new_normal(5, b("b3"), None)); + assert_eq!(prev, Marked::new_with_meta(2, b("b2"), None)); + assert_eq!(result, Marked::new_with_meta(5, b("b3"), None)); // Override again let (prev, result) = l.str_map_mut().set(s("a2"), Some((b("b4"), None))).await; - assert_eq!(prev, Marked::new_normal(5, b("b3"), None)); - assert_eq!(result, Marked::new_normal(6, b("b4"), None)); + assert_eq!(prev, Marked::new_with_meta(5, b("b3"), None)); + assert_eq!(result, Marked::new_with_meta(6, b("b4"), None)); // Delete by adding a tombstone let (prev, result) = l.str_map_mut().set(s("a1"), None).await; - assert_eq!(prev, Marked::new_normal(1, b("b1"), None)); + assert_eq!(prev, Marked::new_with_meta(1, b("b1"), None)); assert_eq!(result, Marked::new_tomb_stone(6)); // Override tombstone let (prev, result) = l.str_map_mut().set(s("a1"), Some((b("b5"), None))).await; assert_eq!(prev, Marked::new_tomb_stone(6)); - assert_eq!(result, Marked::new_normal(7, b("b5"), None)); + assert_eq!(result, Marked::new_with_meta(7, b("b5"), None)); // Range let it = l.str_map().range(s("")..).await; let got = it.collect::>().await; assert_eq!(got, vec![ // - (s("a1"), Marked::new_normal(7, b("b5"), None)), - (s("a2"), Marked::new_normal(6, b("b4"), None)), - (s("x1"), Marked::new_normal(3, b("y1"), None)), - (s("x2"), Marked::new_normal(4, b("y2"), None)), + (s("a1"), Marked::new_with_meta(7, b("b5"), None)), + (s("a2"), Marked::new_with_meta(6, b("b4"), None)), + (s("x1"), Marked::new_with_meta(3, b("y1"), None)), + (s("x2"), Marked::new_with_meta(4, b("y2"), None)), ]); // Get let got = l.str_map().get("a1").await; - assert_eq!(got, Marked::new_normal(7, b("b5"), None)); + assert_eq!(got, Marked::new_with_meta(7, b("b5"), None)); let got = l.str_map().get("a2").await; - assert_eq!(got, Marked::new_normal(6, b("b4"), None)); + assert_eq!(got, Marked::new_with_meta(6, b("b4"), None)); let got = l.str_map().get("w1").await; assert_eq!(got, Marked::new_tomb_stone(0)); @@ -194,10 +194,10 @@ async fn test_two_levels() -> anyhow::Result<()> { let got = it.collect::>().await; assert_eq!(got, vec![ // - (s("a1"), Marked::new_normal(1, b("b1"), None)), - (s("a2"), Marked::new_normal(2, b("b2"), None)), - (s("x1"), Marked::new_normal(3, b("y1"), None)), - (s("x2"), Marked::new_normal(4, b("y2"), None)), + (s("a1"), Marked::new_with_meta(1, b("b1"), None)), + (s("a2"), Marked::new_with_meta(2, b("b2"), None)), + (s("x1"), Marked::new_with_meta(3, b("y1"), None)), + (s("x2"), Marked::new_with_meta(4, b("y2"), None)), ]); Ok(()) @@ -237,7 +237,7 @@ async fn test_three_levels_get_range() -> anyhow::Result<()> { let l = build_3_levels().await; let got = l.str_map().get("a").await; - assert_eq!(got, Marked::new_normal(1, b("a0"), None)); + assert_eq!(got, Marked::new_with_meta(1, b("a0"), None)); let got = l.str_map().get("b").await; assert_eq!(got, Marked::new_tomb_stone(4)); @@ -246,10 +246,10 @@ async fn test_three_levels_get_range() -> anyhow::Result<()> { assert_eq!(got, Marked::new_tomb_stone(6)); let got = l.str_map().get("d").await; - assert_eq!(got, Marked::new_normal(7, b("d2"), None)); + assert_eq!(got, Marked::new_with_meta(7, b("d2"), None)); let got = l.str_map().get("e").await; - assert_eq!(got, Marked::new_normal(6, b("e1"), None)); + assert_eq!(got, Marked::new_with_meta(6, b("e1"), None)); let got = l.str_map().get("f").await; assert_eq!(got, Marked::new_tomb_stone(0)); @@ -257,11 +257,11 @@ async fn test_three_levels_get_range() -> anyhow::Result<()> { let got = l.str_map().range(s("")..).await.collect::>().await; assert_eq!(got, vec![ // - (s("a"), Marked::new_normal(1, b("a0"), None)), + (s("a"), Marked::new_with_meta(1, b("a0"), None)), (s("b"), Marked::new_tomb_stone(4)), (s("c"), Marked::new_tomb_stone(6)), - (s("d"), Marked::new_normal(7, b("d2"), None)), - (s("e"), Marked::new_normal(6, b("e1"), None)), + (s("d"), Marked::new_with_meta(7, b("d2"), None)), + (s("e"), Marked::new_with_meta(6, b("e1"), None)), ]); Ok(()) @@ -272,38 +272,38 @@ async fn test_three_levels_override() -> anyhow::Result<()> { let mut l = build_3_levels().await; let (prev, result) = l.str_map_mut().set(s("a"), Some((b("x"), None))).await; - assert_eq!(prev, Marked::new_normal(1, b("a0"), None)); - assert_eq!(result, Marked::new_normal(8, b("x"), None)); + assert_eq!(prev, Marked::new_with_meta(1, b("a0"), None)); + assert_eq!(result, Marked::new_with_meta(8, b("x"), None)); let (prev, result) = l.str_map_mut().set(s("b"), Some((b("y"), None))).await; assert_eq!(prev, Marked::new_tomb_stone(4)); - assert_eq!(result, Marked::new_normal(9, b("y"), None)); + assert_eq!(result, Marked::new_with_meta(9, b("y"), None)); let (prev, result) = l.str_map_mut().set(s("c"), Some((b("z"), None))).await; assert_eq!(prev, Marked::new_tomb_stone(6)); - assert_eq!(result, Marked::new_normal(10, b("z"), None)); + assert_eq!(result, Marked::new_with_meta(10, b("z"), None)); let (prev, result) = l.str_map_mut().set(s("d"), Some((b("u"), None))).await; - assert_eq!(prev, Marked::new_normal(7, b("d2"), None)); - assert_eq!(result, Marked::new_normal(11, b("u"), None)); + assert_eq!(prev, Marked::new_with_meta(7, b("d2"), None)); + assert_eq!(result, Marked::new_with_meta(11, b("u"), None)); let (prev, result) = l.str_map_mut().set(s("e"), Some((b("v"), None))).await; - assert_eq!(prev, Marked::new_normal(6, b("e1"), None)); - assert_eq!(result, Marked::new_normal(12, b("v"), None)); + assert_eq!(prev, Marked::new_with_meta(6, b("e1"), None)); + assert_eq!(result, Marked::new_with_meta(12, b("v"), None)); let (prev, result) = l.str_map_mut().set(s("f"), Some((b("w"), None))).await; assert_eq!(prev, Marked::new_tomb_stone(0)); - assert_eq!(result, Marked::new_normal(13, b("w"), None)); + assert_eq!(result, Marked::new_with_meta(13, b("w"), None)); let got = l.str_map().range(s("")..).await.collect::>().await; assert_eq!(got, vec![ // - (s("a"), Marked::new_normal(8, b("x"), None)), - (s("b"), Marked::new_normal(9, b("y"), None)), - (s("c"), Marked::new_normal(10, b("z"), None)), - (s("d"), Marked::new_normal(11, b("u"), None)), - (s("e"), Marked::new_normal(12, b("v"), None)), - (s("f"), Marked::new_normal(13, b("w"), None)), + (s("a"), Marked::new_with_meta(8, b("x"), None)), + (s("b"), Marked::new_with_meta(9, b("y"), None)), + (s("c"), Marked::new_with_meta(10, b("z"), None)), + (s("d"), Marked::new_with_meta(11, b("u"), None)), + (s("e"), Marked::new_with_meta(12, b("v"), None)), + (s("f"), Marked::new_with_meta(13, b("w"), None)), ]); Ok(()) @@ -314,7 +314,7 @@ async fn test_three_levels_delete() -> anyhow::Result<()> { let mut l = build_3_levels().await; let (prev, result) = l.str_map_mut().set(s("a"), None).await; - assert_eq!(prev, Marked::new_normal(1, b("a0"), None)); + assert_eq!(prev, Marked::new_with_meta(1, b("a0"), None)); assert_eq!(result, Marked::new_tomb_stone(7)); let (prev, result) = l.str_map_mut().set(s("b"), None).await; @@ -326,11 +326,11 @@ async fn test_three_levels_delete() -> anyhow::Result<()> { assert_eq!(result, Marked::new_tomb_stone(7)); let (prev, result) = l.str_map_mut().set(s("d"), None).await; - assert_eq!(prev, Marked::new_normal(7, b("d2"), None)); + assert_eq!(prev, Marked::new_with_meta(7, b("d2"), None)); assert_eq!(result, Marked::new_tomb_stone(7)); let (prev, result) = l.str_map_mut().set(s("e"), None).await; - assert_eq!(prev, Marked::new_normal(6, b("e1"), None)); + assert_eq!(prev, Marked::new_with_meta(6, b("e1"), None)); assert_eq!(result, Marked::new_tomb_stone(7)); let (prev, result) = l.str_map_mut().set(s("f"), None).await; @@ -394,17 +394,17 @@ async fn test_two_level_update_value() -> anyhow::Result<()> { let (prev, result) = MapApiExt::upsert_value(&mut l, s("a"), b("a1")).await; assert_eq!( prev, - Marked::new_normal(1, b("a0"), Some(KVMeta { expire_at: Some(1) })) + Marked::new_with_meta(1, b("a0"), Some(KVMeta { expire_at: Some(1) })) ); assert_eq!( result, - Marked::new_normal(6, b("a1"), Some(KVMeta { expire_at: Some(1) })) + Marked::new_with_meta(6, b("a1"), Some(KVMeta { expire_at: Some(1) })) ); let got = l.str_map().get("a").await; assert_eq!( got, - Marked::new_normal(6, b("a1"), Some(KVMeta { expire_at: Some(1) })) + Marked::new_with_meta(6, b("a1"), Some(KVMeta { expire_at: Some(1) })) ); } @@ -415,35 +415,23 @@ async fn test_two_level_update_value() -> anyhow::Result<()> { let (prev, result) = MapApiExt::upsert_value(&mut l, s("b"), b("x1")).await; assert_eq!( prev, - Marked::new_normal( - 4, - b("b1"), - Some(KVMeta { - expire_at: Some(10) - }) - ) + Marked::new_normal(4, b("b1")).with_meta(Some(KVMeta { + expire_at: Some(10) + })) ); assert_eq!( result, - Marked::new_normal( - 6, - b("x1"), - Some(KVMeta { - expire_at: Some(10) - }) - ) + Marked::new_normal(6, b("x1")).with_meta(Some(KVMeta { + expire_at: Some(10) + })) ); let got = l.str_map().get("b").await; assert_eq!( got, - Marked::new_normal( - 6, - b("x1"), - Some(KVMeta { - expire_at: Some(10) - }) - ) + Marked::new_normal(6, b("x1")).with_meta(Some(KVMeta { + expire_at: Some(10) + })) ); } @@ -453,10 +441,10 @@ async fn test_two_level_update_value() -> anyhow::Result<()> { let (prev, result) = MapApiExt::upsert_value(&mut l, s("d"), b("d1")).await; assert_eq!(prev, Marked::new_tomb_stone(0)); - assert_eq!(result, Marked::new_normal(6, b("d1"), None)); + assert_eq!(result, Marked::new_with_meta(6, b("d1"), None)); let got = l.str_map().get("d").await; - assert_eq!(got, Marked::new_normal(6, b("d1"), None)); + assert_eq!(got, Marked::new_with_meta(6, b("d1"), None)); } Ok(()) @@ -472,17 +460,17 @@ async fn test_two_level_update_meta() -> anyhow::Result<()> { MapApiExt::update_meta(&mut l, s("a"), Some(KVMeta { expire_at: Some(2) })).await; assert_eq!( prev, - Marked::new_normal(1, b("a0"), Some(KVMeta { expire_at: Some(1) })) + Marked::new_with_meta(1, b("a0"), Some(KVMeta { expire_at: Some(1) })) ); assert_eq!( result, - Marked::new_normal(6, b("a0"), Some(KVMeta { expire_at: Some(2) })) + Marked::new_with_meta(6, b("a0"), Some(KVMeta { expire_at: Some(2) })) ); let got = l.str_map().get("a").await; assert_eq!( got, - Marked::new_normal(6, b("a0"), Some(KVMeta { expire_at: Some(2) })) + Marked::new_with_meta(6, b("a0"), Some(KVMeta { expire_at: Some(2) })) ); } @@ -493,7 +481,7 @@ async fn test_two_level_update_meta() -> anyhow::Result<()> { let (prev, result) = MapApiExt::update_meta(&mut l, s("b"), None).await; assert_eq!( prev, - Marked::new_normal( + Marked::new_with_meta( 4, b("b1"), Some(KVMeta { @@ -501,10 +489,10 @@ async fn test_two_level_update_meta() -> anyhow::Result<()> { }) ) ); - assert_eq!(result, Marked::new_normal(6, b("b1"), None)); + assert_eq!(result, Marked::new_with_meta(6, b("b1"), None)); let got = l.str_map().get("b").await; - assert_eq!(got, Marked::new_normal(6, b("b1"), None)); + assert_eq!(got, Marked::new_with_meta(6, b("b1"), None)); } // Update meta for c. @@ -519,28 +507,20 @@ async fn test_two_level_update_meta() -> anyhow::Result<()> { }), ) .await; - assert_eq!(prev, Marked::new_normal(5, b("c1"), None)); + assert_eq!(prev, Marked::new_with_meta(5, b("c1"), None)); assert_eq!( result, - Marked::new_normal( - 6, - b("c1"), - Some(KVMeta { - expire_at: Some(20) - }) - ) + Marked::new_normal(6, b("c1")).with_meta(Some(KVMeta { + expire_at: Some(20) + })) ); let got = l.str_map().get("c").await; assert_eq!( got, - Marked::new_normal( - 6, - b("c1"), - Some(KVMeta { - expire_at: Some(20) - }) - ) + Marked::new_normal(6, b("c1")).with_meta(Some(KVMeta { + expire_at: Some(20) + })) ); } diff --git a/src/meta/raft-store/src/sm_v002/leveled_store/mod.rs b/src/meta/raft-store/src/sm_v002/leveled_store/mod.rs index 21f81cb3410ef..3a4dedafd5e6b 100644 --- a/src/meta/raft-store/src/sm_v002/leveled_store/mod.rs +++ b/src/meta/raft-store/src/sm_v002/leveled_store/mod.rs @@ -17,7 +17,7 @@ pub mod leveled_map; pub mod map_api; pub mod ref_; pub mod ref_mut; -pub mod static_leveled_map; +pub mod static_levels; pub mod sys_data; pub mod sys_data_api; pub mod util; diff --git a/src/meta/raft-store/src/sm_v002/leveled_store/ref_.rs b/src/meta/raft-store/src/sm_v002/leveled_store/ref_.rs index b2d3ed9862127..601c65a97a705 100644 --- a/src/meta/raft-store/src/sm_v002/leveled_store/ref_.rs +++ b/src/meta/raft-store/src/sm_v002/leveled_store/ref_.rs @@ -23,7 +23,7 @@ use crate::sm_v002::leveled_store::map_api::compacted_get; use crate::sm_v002::leveled_store::map_api::compacted_range; use crate::sm_v002::leveled_store::map_api::MapApiRO; use crate::sm_v002::leveled_store::map_api::MapKey; -use crate::sm_v002::leveled_store::static_leveled_map::StaticLeveledMap; +use crate::sm_v002::leveled_store::static_levels::StaticLevels; use crate::sm_v002::marked::Marked; /// A readonly leveled map that does not not own the data. @@ -33,13 +33,13 @@ pub struct Ref<'d> { writable: Option<&'d Level>, /// The immutable levels. - frozen: &'d StaticLeveledMap, + frozen: &'d StaticLevels, } impl<'d> Ref<'d> { pub(in crate::sm_v002) fn new( writable: Option<&'d Level>, - frozen: &'d StaticLeveledMap, + frozen: &'d StaticLevels, ) -> Ref<'d> { Self { writable, frozen } } diff --git a/src/meta/raft-store/src/sm_v002/leveled_store/ref_mut.rs b/src/meta/raft-store/src/sm_v002/leveled_store/ref_mut.rs index 57a82edde654a..d0dd698a3f903 100644 --- a/src/meta/raft-store/src/sm_v002/leveled_store/ref_mut.rs +++ b/src/meta/raft-store/src/sm_v002/leveled_store/ref_mut.rs @@ -25,7 +25,7 @@ use crate::sm_v002::leveled_store::map_api::MapApi; use crate::sm_v002::leveled_store::map_api::MapApiRO; use crate::sm_v002::leveled_store::map_api::MapKey; use crate::sm_v002::leveled_store::ref_::Ref; -use crate::sm_v002::leveled_store::static_leveled_map::StaticLeveledMap; +use crate::sm_v002::leveled_store::static_levels::StaticLevels; use crate::sm_v002::marked::Marked; /// A writable leveled map that does not not own the data. @@ -35,11 +35,11 @@ pub struct RefMut<'d> { writable: &'d mut Level, /// The immutable levels. - frozen: &'d StaticLeveledMap, + frozen: &'d StaticLevels, } impl<'d> RefMut<'d> { - pub(in crate::sm_v002) fn new(writable: &'d mut Level, frozen: &'d StaticLeveledMap) -> Self { + pub(in crate::sm_v002) fn new(writable: &'d mut Level, frozen: &'d StaticLevels) -> Self { Self { writable, frozen } } diff --git a/src/meta/raft-store/src/sm_v002/leveled_store/static_leveled_map.rs b/src/meta/raft-store/src/sm_v002/leveled_store/static_levels.rs similarity index 96% rename from src/meta/raft-store/src/sm_v002/leveled_store/static_leveled_map.rs rename to src/meta/raft-store/src/sm_v002/leveled_store/static_levels.rs index 6e6ae6e3637cf..6c20a0eb3998d 100644 --- a/src/meta/raft-store/src/sm_v002/leveled_store/static_leveled_map.rs +++ b/src/meta/raft-store/src/sm_v002/leveled_store/static_levels.rs @@ -28,12 +28,12 @@ use crate::sm_v002::marked::Marked; /// A readonly leveled map that owns the data. #[derive(Debug, Default, Clone)] -pub struct StaticLeveledMap { +pub struct StaticLevels { /// From oldest to newest, i.e., levels[0] is the oldest levels: Vec>, } -impl StaticLeveledMap { +impl StaticLevels { pub(in crate::sm_v002) fn new(levels: impl IntoIterator>) -> Self { Self { levels: levels.into_iter().collect(), @@ -64,7 +64,7 @@ impl StaticLeveledMap { } #[async_trait::async_trait] -impl MapApiRO for StaticLeveledMap +impl MapApiRO for StaticLevels where K: MapKey, Level: MapApiRO, diff --git a/src/meta/raft-store/src/sm_v002/marked/marked_test.rs b/src/meta/raft-store/src/sm_v002/marked/marked_test.rs index 384b2632bde89..e0a57c125628e 100644 --- a/src/meta/raft-store/src/sm_v002/marked/marked_test.rs +++ b/src/meta/raft-store/src/sm_v002/marked/marked_test.rs @@ -26,7 +26,7 @@ fn test_from_tuple() -> anyhow::Result<()> { assert_eq!( m, - Marked::new_normal(1, 2, Some(KVMeta { expire_at: Some(3) })) + Marked::new_with_meta(1, 2, Some(KVMeta { expire_at: Some(3) })) ); Ok(()) @@ -34,12 +34,12 @@ fn test_from_tuple() -> anyhow::Result<()> { #[test] fn test_impl_trait_seq_value() -> anyhow::Result<()> { - let m = Marked::new_normal(1, 2, None); + let m = Marked::new_with_meta(1, 2, None); assert_eq!(m.seq(), 1); assert_eq!(m.value(), Some(&2)); assert_eq!(m.meta(), None); - let m = Marked::new_normal(1, 2, Some(KVMeta { expire_at: Some(3) })); + let m = Marked::new_with_meta(1, 2, Some(KVMeta { expire_at: Some(3) })); assert_eq!(m.seq(), 1); assert_eq!(m.value(), Some(&2)); assert_eq!(m.meta(), Some(&KVMeta { expire_at: Some(3) })); @@ -64,7 +64,7 @@ fn test_empty() -> anyhow::Result<()> { // Test Marked::internal_seq() #[test] fn test_internal_seq() -> anyhow::Result<()> { - let m = Marked::new_normal(1, 2, None); + let m = Marked::new_with_meta(1, 2, None); assert_eq!(m.internal_seq(), InternalSeq::normal(1)); let m: Marked = Marked::new_tomb_stone(1); @@ -76,10 +76,10 @@ fn test_internal_seq() -> anyhow::Result<()> { // Test Marked::unpack() #[test] fn test_unpack() -> anyhow::Result<()> { - let m = Marked::new_normal(1, 2, None); + let m = Marked::new_with_meta(1, 2, None); assert_eq!(m.unpack_ref(), Some((&2, None))); - let m = Marked::new_normal(1, 2, Some(KVMeta { expire_at: Some(3) })); + let m = Marked::new_with_meta(1, 2, Some(KVMeta { expire_at: Some(3) })); assert_eq!( m.unpack_ref(), Some((&2, Some(&KVMeta { expire_at: Some(3) }))) @@ -94,8 +94,8 @@ fn test_unpack() -> anyhow::Result<()> { // Test Marked::max() #[test] fn test_max() -> anyhow::Result<()> { - let m1 = Marked::new_normal(1, 2, None); - let m2 = Marked::new_normal(3, 2, None); + let m1 = Marked::new_with_meta(1, 2, None); + let m2 = Marked::new_with_meta(3, 2, None); let m3: Marked = Marked::new_tomb_stone(2); assert_eq!(Marked::max_ref(&m1, &m2), &m2); @@ -112,11 +112,11 @@ fn test_max() -> anyhow::Result<()> { // Test From> for Option> #[test] fn test_from_marked_for_option_seqv() -> anyhow::Result<()> { - let m = Marked::new_normal(1, 2, None); + let m = Marked::new_with_meta(1, 2, None); let s: Option> = Some(SeqV::new(1, 2)); assert_eq!(s, m.into()); - let m = Marked::new_normal(1, 2, Some(KVMeta { expire_at: Some(3) })); + let m = Marked::new_with_meta(1, 2, Some(KVMeta { expire_at: Some(3) })); let s: Option> = Some(SeqV::with_meta(1, Some(KVMeta { expire_at: Some(3) }), 2)); assert_eq!(s, m.into()); @@ -130,7 +130,7 @@ fn test_from_marked_for_option_seqv() -> anyhow::Result<()> { // Test From for Marked #[test] fn test_from_expire_value_for_marked() -> anyhow::Result<()> { - let m = Marked::new_normal(1, "2".to_string(), None); + let m = Marked::new_with_meta(1, "2".to_string(), None); let s = ExpireValue::new("2", 1); assert_eq!(m, s.into()); @@ -140,7 +140,7 @@ fn test_from_expire_value_for_marked() -> anyhow::Result<()> { // Test From> for Option #[test] fn test_from_marked_for_option_expire_value() -> anyhow::Result<()> { - let m = Marked::new_normal(1, "2".to_string(), None); + let m = Marked::new_with_meta(1, "2".to_string(), None); let s: Option = Some(ExpireValue::new("2".to_string(), 1)); assert_eq!(s, m.into()); diff --git a/src/meta/raft-store/src/sm_v002/marked/mod.rs b/src/meta/raft-store/src/sm_v002/marked/mod.rs index a165ad2aaec1a..a061449971dde 100644 --- a/src/meta/raft-store/src/sm_v002/marked/mod.rs +++ b/src/meta/raft-store/src/sm_v002/marked/mod.rs @@ -56,7 +56,7 @@ impl From<(u64, T, Option)> for Marked { impl From> for Marked { fn from(value: SeqV) -> Self { - Marked::new_normal(value.seq, value.data, value.meta) + Marked::new_with_meta(value.seq, value.data, value.meta) } } @@ -148,7 +148,15 @@ impl Marked { Marked::TombStone { internal_seq } } - pub fn new_normal(seq: u64, value: T, meta: Option) -> Self { + pub fn new_normal(seq: u64, value: T) -> Self { + Marked::Normal { + internal_seq: seq, + value, + meta: None, + } + } + + pub fn new_with_meta(seq: u64, value: T, meta: Option) -> Self { Marked::Normal { internal_seq: seq, value, @@ -156,6 +164,23 @@ impl Marked { } } + pub fn with_meta(self, meta: Option) -> Self { + match self { + Marked::TombStone { .. } => { + unreachable!("Tombstone has no meta") + } + Marked::Normal { + internal_seq, + value, + .. + } => Marked::Normal { + internal_seq, + value, + meta, + }, + } + } + /// Return if the entry is neither a normal entry nor a tombstone. pub fn not_found(&self) -> bool { matches!(self, Marked::TombStone { @@ -189,7 +214,7 @@ impl From> for Option> { impl From for Marked { fn from(value: ExpireValue) -> Self { - Marked::new_normal(value.seq, value.key, None) + Marked::new_with_meta(value.seq, value.key, None) } } @@ -205,3 +230,60 @@ impl From> for Option { } } } + +#[cfg(test)] +mod tests { + use common_meta_types::KVMeta; + + use crate::sm_v002::marked::Marked; + + #[test] + fn test_marked_new() { + let m = Marked::new_normal(1, "a"); + assert_eq!( + Marked::Normal { + internal_seq: 1, + value: "a", + meta: None + }, + m + ); + + let m = m.with_meta(Some(KVMeta { + expire_at: Some(20), + })); + + assert_eq!( + Marked::Normal { + internal_seq: 1, + value: "a", + meta: Some(KVMeta { + expire_at: Some(20) + }) + }, + m + ); + + let m = Marked::new_with_meta( + 2, + "b", + Some(KVMeta { + expire_at: Some(30), + }), + ); + + assert_eq!( + Marked::Normal { + internal_seq: 2, + value: "b", + meta: Some(KVMeta { + expire_at: Some(30) + }) + }, + m + ); + + let m: Marked = Marked::new_tomb_stone(3); + assert_eq!(Marked::TombStone { internal_seq: 3 }, m); + } +} diff --git a/src/meta/raft-store/src/sm_v002/sm_v002.rs b/src/meta/raft-store/src/sm_v002/sm_v002.rs index 9ffc4b7ec501a..577e4dfeb0b69 100644 --- a/src/meta/raft-store/src/sm_v002/sm_v002.rs +++ b/src/meta/raft-store/src/sm_v002/sm_v002.rs @@ -384,7 +384,7 @@ impl SMV002 { "the base must not be changed" ); - self.levels.replace_frozen_levels(snapshot.compacted()); + self.levels.replace_frozen(snapshot.compacted()); } /// It returns 2 entries: the previous one and the new one after upsert. diff --git a/src/meta/raft-store/src/sm_v002/sm_v002_test.rs b/src/meta/raft-store/src/sm_v002/sm_v002_test.rs index 14fe98d662167..8c5f1ebadf745 100644 --- a/src/meta/raft-store/src/sm_v002/sm_v002_test.rs +++ b/src/meta/raft-store/src/sm_v002/sm_v002_test.rs @@ -192,16 +192,16 @@ async fn test_internal_expire_index() -> anyhow::Result<()> { assert_eq!(got, vec![ ( ExpireKey::new(5_000, 2), - Marked::new_normal(2, s("b"), None) + Marked::new_with_meta(2, s("b"), None) ), (ExpireKey::new(10_000, 1), Marked::new_tomb_stone(4)), ( ExpireKey::new(15_000, 4), - Marked::new_normal(4, s("a"), None) + Marked::new_with_meta(4, s("a"), None) ), ( ExpireKey::new(20_000, 3), - Marked::new_normal(3, s("c"), None) + Marked::new_with_meta(3, s("c"), None) ), ]); @@ -261,13 +261,13 @@ async fn test_inserting_expired_becomes_deleting() -> anyhow::Result<()> { // ( ExpireKey::new(5_000, 2), - Marked::new_normal(2, s("b"), None) + Marked::new_with_meta(2, s("b"), None) ), (ExpireKey::new(10_000, 1), Marked::new_tomb_stone(4)), (ExpireKey::new(15_000, 4), Marked::new_tomb_stone(5),), ( ExpireKey::new(20_000, 3), - Marked::new_normal(3, s("c"), None) + Marked::new_with_meta(3, s("c"), None) ), ]); diff --git a/src/meta/raft-store/src/sm_v002/snapshot_view_v002.rs b/src/meta/raft-store/src/sm_v002/snapshot_view_v002.rs index 521c0127b6bd8..48a934659423e 100644 --- a/src/meta/raft-store/src/sm_v002/snapshot_view_v002.rs +++ b/src/meta/raft-store/src/sm_v002/snapshot_view_v002.rs @@ -25,7 +25,7 @@ use crate::ondisk::Header; use crate::ondisk::OnDisk; use crate::sm_v002::leveled_store::map_api::AsMap; use crate::sm_v002::leveled_store::map_api::MapApiRO; -use crate::sm_v002::leveled_store::static_leveled_map::StaticLeveledMap; +use crate::sm_v002::leveled_store::static_levels::StaticLevels; use crate::sm_v002::leveled_store::sys_data_api::SysDataApiRO; use crate::sm_v002::marked::Marked; use crate::state_machine::ExpireValue; @@ -36,16 +36,16 @@ use crate::state_machine::StateMachineMetaValue; /// A snapshot view of a state machine, which is static and not affected by further writing to the state machine. pub struct SnapshotViewV002 { /// The compacted snapshot data. - compacted: StaticLeveledMap, + compacted: StaticLevels, /// Original non compacted snapshot data. /// /// This is kept just for debug. - original: StaticLeveledMap, + original: StaticLevels, } impl SnapshotViewV002 { - pub fn new(top: StaticLeveledMap) -> Self { + pub fn new(top: StaticLevels) -> Self { Self { compacted: top.clone(), original: top, @@ -53,12 +53,12 @@ impl SnapshotViewV002 { } /// Return the data level of this snapshot - pub fn compacted(&self) -> StaticLeveledMap { + pub fn compacted(&self) -> StaticLevels { self.compacted.clone() } /// The original, non compacted snapshot data. - pub fn original_ref(&self) -> &StaticLeveledMap { + pub fn original_ref(&self) -> &StaticLevels { &self.original } @@ -115,7 +115,7 @@ impl SnapshotViewV002 { data.replace_expire(bt); - self.compacted = StaticLeveledMap::new([Arc::new(data)]); + self.compacted = StaticLevels::new([Arc::new(data)]); } /// Export all its data in RaftStoreEntry format. diff --git a/src/meta/raft-store/src/sm_v002/snapshot_view_v002_test.rs b/src/meta/raft-store/src/sm_v002/snapshot_view_v002_test.rs index f89b327cb16a4..5dabed3c29cc4 100644 --- a/src/meta/raft-store/src/sm_v002/snapshot_view_v002_test.rs +++ b/src/meta/raft-store/src/sm_v002/snapshot_view_v002_test.rs @@ -30,7 +30,7 @@ use crate::sm_v002::leveled_store::leveled_map::LeveledMap; use crate::sm_v002::leveled_store::map_api::AsMap; use crate::sm_v002::leveled_store::map_api::MapApi; use crate::sm_v002::leveled_store::map_api::MapApiRO; -use crate::sm_v002::leveled_store::static_leveled_map::StaticLeveledMap; +use crate::sm_v002::leveled_store::static_levels::StaticLevels; use crate::sm_v002::leveled_store::sys_data_api::SysDataApiRO; use crate::sm_v002::marked::Marked; use crate::sm_v002::sm_v002::SMV002; @@ -70,9 +70,9 @@ async fn test_compact_copied_value_and_kv() -> anyhow::Result<()> { .await; assert_eq!(got, vec![ // - (s("a"), Marked::new_normal(1, b("a0"), None)), - (s("d"), Marked::new_normal(7, b("d2"), None)), - (s("e"), Marked::new_normal(6, b("e1"), None)), + (s("a"), Marked::new_with_meta(1, b("a0"), None)), + (s("d"), Marked::new_with_meta(7, b("d2"), None)), + (s("e"), Marked::new_with_meta(6, b("e1"), None)), ]); let got = d.expire_map().range(..).await.collect::>().await; @@ -103,7 +103,7 @@ async fn test_compact_expire_index() -> anyhow::Result<()> { // ( s("a"), - Marked::new_normal( + Marked::new_with_meta( 4, b("a1"), Some(KVMeta { @@ -113,11 +113,11 @@ async fn test_compact_expire_index() -> anyhow::Result<()> { ), ( s("b"), - Marked::new_normal(2, b("b0"), Some(KVMeta { expire_at: Some(5) })) + Marked::new_with_meta(2, b("b0"), Some(KVMeta { expire_at: Some(5) })) ), ( s("c"), - Marked::new_normal( + Marked::new_with_meta( 3, b("c0"), Some(KVMeta { @@ -132,15 +132,15 @@ async fn test_compact_expire_index() -> anyhow::Result<()> { // ( ExpireKey::new(5_000, 2), - Marked::new_normal(2, s("b"), None) + Marked::new_with_meta(2, s("b"), None) ), ( ExpireKey::new(15_000, 4), - Marked::new_normal(4, s("a"), None) + Marked::new_with_meta(4, s("a"), None) ), ( ExpireKey::new(20_000, 3), - Marked::new_normal(3, s("c"), None) + Marked::new_with_meta(3, s("c"), None) ), ]); @@ -226,7 +226,7 @@ async fn test_import() -> anyhow::Result<()> { let d = SMV002::import(data)?; - let snapshot = SnapshotViewV002::new(StaticLeveledMap::new([Arc::new(d)])); + let snapshot = SnapshotViewV002::new(StaticLevels::new([Arc::new(d)])); let got = snapshot .export()