database_overlay_remove_tree.rs 2.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980
  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 perform writes to verify overlay's cache
  20. //! functionality.
  21. use kvdb_overlay::{Database, DatabaseOverlay, Result};
  22. const TREE_1: &str = "_tree1";
  23. const TREE_2: &str = "_tree2";
  24. #[test]
  25. fn database_overlay_remove_tree() -> Result<()> {
  26. // Initialize database
  27. let (db, _folder) = Database::open_temp()?;
  28. // Create tree in the database and insert some values
  29. let tree_1 = db.open_tree_default(TREE_1)?;
  30. tree_1.insert(b"key_a", b"val_a")?;
  31. tree_1.insert(b"key_b", b"val_b")?;
  32. tree_1.insert(b"key_c", b"val_c")?;
  33. // Don't forget to flush
  34. db.flush_default_mode()?;
  35. // Initialize overlay
  36. let mut overlay = DatabaseOverlay::new(&db, vec![])?;
  37. // Open tree in the overlay
  38. overlay.open_tree_default(TREE_1, false)?;
  39. // Verify values are in the overlay
  40. assert_eq!(overlay.get(TREE_1, b"key_a")?, Some(b"val_a".into()));
  41. assert_eq!(overlay.get(TREE_1, b"key_b")?, Some(b"val_b".into()));
  42. assert_eq!(overlay.get(TREE_1, b"key_c")?, Some(b"val_c".into()));
  43. // Drop tree
  44. overlay.drop_tree(TREE_1)?;
  45. // Try to drop the tree again
  46. assert!(overlay.drop_tree(TREE_1).is_err());
  47. // Try to drop a non existing tree
  48. assert!(overlay.drop_tree(TREE_2).is_err());
  49. // Open the new tree
  50. overlay.open_tree_default(TREE_2, false)?;
  51. // Drop the new tree
  52. overlay.drop_tree(TREE_2)?;
  53. // Now execute all tree batches in the overlay
  54. overlay.apply()?;
  55. // Don't forget to flush
  56. db.flush_default_mode()?;
  57. // Verify the database doesn't contain the trees
  58. let db_tree_names = db.tree_names()?;
  59. assert!(!db_tree_names.contains(&TREE_1.into()));
  60. assert!(!db_tree_names.contains(&TREE_2.into()));
  61. Ok(())
  62. }