/* 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 . */ //! Simulate the creation of a [`DatabaseOverlay`] on top of an entire //! [`Database`] instance, and perform checkpoints and writes to verify //! overlay's cache checkpoint functionality. use kvdb_overlay::{Database, DatabaseOverlay, Result}; const TREE: &str = "_tree"; const NEW_TREE: &str = "_new_tree"; #[test] fn database_overlay_checkpoint() -> Result<()> { // Initialize database let (db, _folder) = Database::open_temp()?; // Initialize overlay let mut overlay = DatabaseOverlay::new(&db, vec![])?; // Open tree in the overlay overlay.open_tree_default(TREE, false)?; // We keep seperate trees for validation let tree = db.open_tree_default(TREE)?; // Insert some values to the overlay overlay.insert(TREE, b"key_a", b"val_a")?; overlay.insert(TREE, b"key_b", b"val_b")?; overlay.insert(TREE, b"key_c", b"val_c")?; // Verify they are in the overlay assert_eq!(overlay.get(TREE, b"key_a")?.unwrap().as_ref(), b"val_a"); assert_eq!(overlay.get(TREE, b"key_b")?.unwrap().as_ref(), b"val_b"); assert_eq!(overlay.get(TREE, b"key_c")?.unwrap().as_ref(), b"val_c"); // Verify they are not in the database assert!(tree.get(b"key_a")?.is_none()); assert!(tree.get(b"key_b")?.is_none()); assert!(tree.get(b"key_c")?.is_none()); // Now we create an overlay checkpoint overlay.checkpoint(); // We add some more values to the overlay overlay.insert(TREE, b"key_d", b"val_d")?; overlay.insert(TREE, b"key_e", b"val_e")?; overlay.insert(TREE, b"key_f", b"val_f")?; // Verify they are in the overlay assert_eq!(overlay.get(TREE, b"key_d")?.unwrap().as_ref(), b"val_d"); assert_eq!(overlay.get(TREE, b"key_e")?.unwrap().as_ref(), b"val_e"); assert_eq!(overlay.get(TREE, b"key_f")?.unwrap().as_ref(), b"val_f"); // Verify they are not in the database assert!(tree.get(b"key_d")?.is_none()); assert!(tree.get(b"key_e")?.is_none()); assert!(tree.get(b"key_f")?.is_none()); // We also create a new tree overlay.open_tree_default(NEW_TREE, false)?; // We assume something went wrong, so we revert to last checkpoint overlay.revert_to_checkpoint(); // And drop the new tree we created db.drop_tree(NEW_TREE)?; // Now execute all tree batches in the overlay overlay.apply()?; // Don't forget to flush db.flush_default_mode()?; // Verify the database contains pre-checkpoint keys assert_eq!(tree.get(b"key_a")?.unwrap().as_ref(), b"val_a"); assert_eq!(tree.get(b"key_b")?.unwrap().as_ref(), b"val_b"); assert_eq!(tree.get(b"key_c")?.unwrap().as_ref(), b"val_c"); // Verify the database doesn't contains keys after checkpoint assert!(tree.get(b"key_d")?.is_none()); assert!(tree.get(b"key_e")?.is_none()); assert!(tree.get(b"key_f")?.is_none()); // Verify the database doesn't contain the new tree we created // after checkpoint assert!(!db.tree_names()?.contains(&NEW_TREE.into())); Ok(()) }