tree_overlay_checkpoint.rs 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. /* This file is part of DarkFi (https://dark.fi)
  2. *
  3. * Copyright (C) 2026-2026 Dyne.org foundation
  4. *
  5. * This program is free software: you can redistribute it and/or modify
  6. * it under the terms of the GNU Affero General Public License as
  7. * published by the Free Software Foundation, either version 3 of the
  8. * License, or (at your option) any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful,
  11. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. * GNU Affero General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU Affero General Public License
  16. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  17. */
  18. //! Simulate the creation of a [`TreeOverlay`] on top of a [`Tree`]
  19. //! instance, and perform checkpoints and writes to verify overlay's
  20. //! cache checkpoint functionality.
  21. use kvdb_overlay::{Database, Result, TreeOverlay};
  22. const TREE: &str = "_tree";
  23. #[test]
  24. fn tree_overlay_checkpoint() -> Result<()> {
  25. // Initialize database
  26. let (db, _folder) = Database::open_temp()?;
  27. // Initialize tree and its overlay
  28. let tree = db.open_tree_default(TREE)?;
  29. let mut overlay = TreeOverlay::new(&tree);
  30. // Insert some values to the overlay
  31. overlay.insert(b"key_a", b"val_a")?;
  32. overlay.insert(b"key_b", b"val_b")?;
  33. overlay.insert(b"key_c", b"val_c")?;
  34. // Verify they are in the overlay
  35. assert_eq!(overlay.get(b"key_a")?, Some(b"val_a".into()));
  36. assert_eq!(overlay.get(b"key_b")?, Some(b"val_b".into()));
  37. assert_eq!(overlay.get(b"key_c")?, Some(b"val_c".into()));
  38. // Verify they are not in the database
  39. assert_eq!(tree.get(b"key_a")?, None);
  40. assert_eq!(tree.get(b"key_b")?, None);
  41. assert_eq!(tree.get(b"key_c")?, None);
  42. // Now we create an overlay checkpoint
  43. overlay.checkpoint();
  44. // We add some more values to the overlay
  45. overlay.insert(b"key_d", b"val_d")?;
  46. overlay.insert(b"key_e", b"val_e")?;
  47. overlay.insert(b"key_f", b"val_f")?;
  48. // Verify they are in the overlay
  49. assert_eq!(overlay.get(b"key_d")?, Some(b"val_d".into()));
  50. assert_eq!(overlay.get(b"key_e")?, Some(b"val_e".into()));
  51. assert_eq!(overlay.get(b"key_f")?, Some(b"val_f".into()));
  52. // Verify they are not in the database
  53. assert_eq!(tree.get(b"key_d")?, None);
  54. assert_eq!(tree.get(b"key_e")?, None);
  55. assert_eq!(tree.get(b"key_f")?, None);
  56. // We assume something went wrong, so we revert to last checkpoint
  57. overlay.revert_to_checkpoint();
  58. // Now we write it to the database
  59. db.write_tree_overlays_changes(&[&overlay])?;
  60. db.flush_default_mode()?;
  61. // Verify database contains pre-checkpoint keys
  62. assert_eq!(tree.get(b"key_a")?, Some(b"val_a".into()));
  63. assert_eq!(tree.get(b"key_b")?, Some(b"val_b".into()));
  64. assert_eq!(tree.get(b"key_c")?, Some(b"val_c".into()));
  65. // Verify database doesn't contains keys after checkpoint
  66. assert_eq!(tree.get(b"key_d")?, None);
  67. assert_eq!(tree.get(b"key_e")?, None);
  68. assert_eq!(tree.get(b"key_f")?, None);
  69. Ok(())
  70. }