-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a0b2e8c
commit fe01697
Showing
4 changed files
with
68 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
use native_db::*; | ||
use native_model::{native_model, Model}; | ||
use serde::{Deserialize, Serialize}; | ||
use shortcut_assert_fs::TmpFs; | ||
|
||
#[derive(Serialize, Deserialize, Eq, PartialEq, Debug, Clone)] | ||
#[native_model(id = 1, version = 1)] | ||
#[native_db] | ||
struct Item { | ||
#[primary_key] | ||
id: u32, | ||
name: String, | ||
} | ||
|
||
#[test] | ||
fn test_compact() { | ||
let tf = TmpFs::new().unwrap(); | ||
let db_path = tf.path("test"); | ||
|
||
let mut models = Models::new(); | ||
models.define::<Item>().unwrap(); | ||
let mut db = Builder::new().create(&models, db_path.clone()).unwrap(); | ||
|
||
// Insert 1000 items | ||
let rw = db.rw_transaction().unwrap(); | ||
for i in 0..999 { | ||
rw.insert(Item { | ||
id: i, | ||
name: format!("test_{}", i), | ||
}) | ||
.unwrap(); | ||
} | ||
rw.commit().unwrap(); | ||
|
||
// Check the size of the database | ||
let metadata = std::fs::metadata(db_path.clone()).unwrap(); | ||
let file_size = metadata.len(); | ||
assert_eq!(file_size, 1589248); | ||
dbg!(file_size); | ||
|
||
let out = db.compact().unwrap(); | ||
assert!(out); | ||
|
||
// Check the size of the compacted database | ||
let metadata = std::fs::metadata(db_path.clone()).unwrap(); | ||
let file_size = metadata.len(); | ||
assert_eq!(file_size, 700416); | ||
} |