reth_db/
utils.rs

1//! Utils crate for `db`.
2
3use std::path::Path;
4
5/// Returns the default page size that can be used in this OS.
6pub(crate) fn default_page_size() -> usize {
7    let os_page_size = page_size::get();
8
9    // source: https://gitflic.ru/project/erthink/libmdbx/blob?file=mdbx.h#line-num-821
10    let libmdbx_max_page_size = 0x10000;
11
12    // May lead to errors if it's reduced further because of the potential size of the
13    // data.
14    let min_page_size = 4096;
15
16    os_page_size.clamp(min_page_size, libmdbx_max_page_size)
17}
18
19/// Check if a db is empty. It does not provide any information on the
20/// validity of the data in it. We consider a database as non empty when it's a non empty directory.
21pub fn is_database_empty<P: AsRef<Path>>(path: P) -> bool {
22    let path = path.as_ref();
23
24    if !path.exists() {
25        true
26    } else if path.is_file() {
27        false
28    } else if let Ok(mut dir) = path.read_dir() {
29        // Check if directory has any entries without counting all of them
30        dir.next().is_none()
31    } else {
32        true
33    }
34}
35
36#[cfg(test)]
37mod tests {
38    use super::*;
39
40    #[test]
41    fn is_database_empty_false_if_db_path_is_a_file() {
42        let db_file = tempfile::NamedTempFile::new().unwrap();
43
44        let result = is_database_empty(&db_file);
45
46        assert!(!result);
47    }
48}