| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980 |
- /* This file is part of DarkFi (https://dark.fi)
- *
- * Copyright (C) 2026-2026 Dyne.org foundation
- *
- * This program is free software: you can redistribute it and/or modify
- * it under the terms of the GNU Affero General Public License as
- * published by the Free Software Foundation, either version 3 of the
- * License, or (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- * GNU Affero General Public License for more details.
- *
- * You should have received a copy of the GNU Affero General Public License
- * along with this program. If not, see <https://www.gnu.org/licenses/>.
- */
- //! Simulate the creation of a [`DatabaseOverlay`] on top of an entire
- //! [`Database`] instance, and perform writes to verify overlay's cache
- //! functionality.
- use kvdb_overlay::{Database, DatabaseOverlay, Result};
- const TREE_1: &str = "_tree1";
- const TREE_2: &str = "_tree2";
- #[test]
- fn database_overlay_remove_tree() -> Result<()> {
- // Initialize database
- let (db, _folder) = Database::open_temp()?;
- // Create tree in the database and insert some values
- let tree_1 = db.open_tree_default(TREE_1)?;
- tree_1.insert(b"key_a", b"val_a")?;
- tree_1.insert(b"key_b", b"val_b")?;
- tree_1.insert(b"key_c", b"val_c")?;
- // Don't forget to flush
- db.flush_default_mode()?;
- // Initialize overlay
- let mut overlay = DatabaseOverlay::new(&db, vec![])?;
- // Open tree in the overlay
- overlay.open_tree_default(TREE_1, false)?;
- // Verify values are in the overlay
- assert_eq!(overlay.get(TREE_1, b"key_a")?.unwrap().as_ref(), b"val_a");
- assert_eq!(overlay.get(TREE_1, b"key_b")?.unwrap().as_ref(), b"val_b");
- assert_eq!(overlay.get(TREE_1, b"key_c")?.unwrap().as_ref(), b"val_c");
- // Drop tree
- overlay.drop_tree(TREE_1)?;
- // Try to drop the tree again
- assert!(overlay.drop_tree(TREE_1).is_err());
- // Try to drop a non existing tree
- assert!(overlay.drop_tree(TREE_2).is_err());
- // Open the new tree
- overlay.open_tree_default(TREE_2, false)?;
- // Drop the new tree
- overlay.drop_tree(TREE_2)?;
- // Now execute all tree batches in the overlay
- overlay.apply()?;
- // Don't forget to flush
- db.flush_default_mode()?;
- // Verify the database doesn't contain the trees
- let db_tree_names = db.tree_names()?;
- assert!(!db_tree_names.contains(&TREE_1.into()));
- assert!(!db_tree_names.contains(&TREE_2.into()));
- Ok(())
- }
|