database_overlay_clone.rs 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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 [`DatabaseOverlay`] on top of an entire
  19. //! [`Database`] instance, and clone it to verify writes on the cloned
  20. //! overlay's cache do not affect the original one.
  21. use kvdb_overlay::{Database, DatabaseOverlay, Result};
  22. const TREE: &str = "_tree";
  23. #[test]
  24. fn database_overlay_clone() -> Result<()> {
  25. // Initialize database
  26. let (db, _folder) = Database::open_temp()?;
  27. // Initialize overlay
  28. let mut overlay = DatabaseOverlay::new(&db, vec![])?;
  29. // Open tree in the overlay
  30. overlay.open_tree_default(TREE, false)?;
  31. // Check overlay tree is empty
  32. assert!(overlay.is_empty(TREE)?);
  33. // Check last value is `None`
  34. assert_eq!(overlay.last(TREE)?, None);
  35. // We keep seperate tree for validation
  36. let tree = db.open_tree_default(TREE)?;
  37. // Insert some values to the overlay
  38. overlay.insert(TREE, b"key_a", b"val_a")?;
  39. overlay.insert(TREE, b"key_b", b"val_b")?;
  40. overlay.insert(TREE, b"key_c", b"val_c")?;
  41. // Verify they are in the overlay
  42. assert_eq!(overlay.get(TREE, b"key_a")?, Some(b"val_a".into()));
  43. assert_eq!(overlay.get(TREE, b"key_b")?, Some(b"val_b".into()));
  44. assert_eq!(overlay.get(TREE, b"key_c")?, Some(b"val_c".into()));
  45. // Check overlay tree is not empty
  46. assert!(!overlay.is_empty(TREE)?);
  47. // Check its last value
  48. assert_eq!(
  49. overlay.last(TREE)?,
  50. Some((b"key_c".into(), b"val_c".into()))
  51. );
  52. // Verify they are not in the database
  53. assert_eq!(tree.get(b"key_a")?, None);
  54. assert_eq!(tree.get(b"key_b")?, None);
  55. assert_eq!(tree.get(b"key_c")?, None);
  56. // Clone the overlay
  57. let mut overlay_clone = overlay.clone();
  58. // Check cloned overlay tree is not empty
  59. assert!(!overlay_clone.is_empty(TREE)?);
  60. // Check its last value
  61. assert_eq!(
  62. overlay_clone.last(TREE)?,
  63. Some((b"key_c".into(), b"val_c".into()))
  64. );
  65. // Insert some values to the cloned overlay
  66. overlay_clone.insert(TREE, b"key_d", b"val_d")?;
  67. overlay_clone.insert(TREE, b"key_e", b"val_e")?;
  68. overlay_clone.insert(TREE, b"key_f", b"val_f")?;
  69. // Verify all records are in the cloned overlay
  70. assert_eq!(overlay_clone.get(TREE, b"key_a")?, Some(b"val_a".into()));
  71. assert_eq!(overlay_clone.get(TREE, b"key_b")?, Some(b"val_b".into()));
  72. assert_eq!(overlay_clone.get(TREE, b"key_c")?, Some(b"val_c".into()));
  73. assert_eq!(overlay_clone.get(TREE, b"key_d")?, Some(b"val_d".into()));
  74. assert_eq!(overlay_clone.get(TREE, b"key_e")?, Some(b"val_e".into()));
  75. assert_eq!(overlay_clone.get(TREE, b"key_f")?, Some(b"val_f".into()));
  76. // Check its last values
  77. assert_eq!(
  78. overlay_clone.last(TREE)?,
  79. Some((b"key_f".into(), b"val_f".into()))
  80. );
  81. // Verify they are not in original overlay or the database
  82. assert_eq!(tree.get(b"key_d")?, None);
  83. assert_eq!(tree.get(b"key_e")?, None);
  84. assert_eq!(tree.get(b"key_f")?, None);
  85. assert_eq!(overlay.get(TREE, b"key_d")?, None);
  86. assert_eq!(overlay.get(TREE, b"key_e")?, None);
  87. assert_eq!(overlay.get(TREE, b"key_f")?, None);
  88. // We finished processing the cloned overlay, so we can
  89. // discard it and apply all tree baches of the original
  90. // overlay.
  91. overlay.apply()?;
  92. // Don't forget to flush
  93. db.flush_default_mode()?;
  94. // Verify the database contains keys
  95. assert_eq!(tree.get(b"key_a")?, Some(b"val_a".into()));
  96. assert_eq!(tree.get(b"key_b")?, Some(b"val_b".into()));
  97. assert_eq!(tree.get(b"key_c")?, Some(b"val_c".into()));
  98. Ok(())
  99. }