1
use std::collections::HashMap;
2

            
3
use bonsaidb_core::schema::{CollectionName, Schematic};
4
use nebari::io::any::AnyFile;
5
use nebari::tree::{AnyTreeRoot, Root, Unversioned, Versioned};
6

            
7
use crate::database::document_tree_name;
8
#[cfg(any(feature = "encryption", feature = "compression"))]
9
use crate::storage::TreeVault;
10
use crate::views::{
11
    view_document_map_tree_name, view_entries_tree_name, view_invalidated_docs_tree_name,
12
};
13

            
14
672852
#[derive(Default)]
15
pub(crate) struct OpenTrees {
16
    pub trees: Vec<Box<dyn AnyTreeRoot<AnyFile>>>,
17
    pub trees_index_by_name: HashMap<String, usize>,
18
}
19

            
20
impl OpenTrees {
21
    #[cfg_attr(not(feature = "encryption"), allow(unused_mut))]
22
    #[cfg_attr(feature = "encryption", allow(clippy::unnecessary_wraps))]
23
3825542
    pub fn open_tree<R: Root>(
24
3825542
        &mut self,
25
3825542
        name: &str,
26
3825542
        #[cfg(any(feature = "encryption", feature = "compression"))] vault: Option<TreeVault>,
27
3825542
    ) {
28
3825542
        if !self.trees_index_by_name.contains_key(name) {
29
3057582
            self.trees_index_by_name
30
3057582
                .insert(name.to_string(), self.trees.len());
31
3057582
            let mut tree = R::tree(name.to_string());
32

            
33
            #[cfg(any(feature = "encryption", feature = "compression"))]
34
3057582
            if let Some(vault) = vault {
35
1378511
                tree = tree.with_vault(vault);
36
1713851
            }
37

            
38
3057582
            self.trees.push(Box::new(tree));
39
767960
        }
40
3825542
    }
41

            
42
    #[allow(clippy::needless_pass_by_value)]
43
1046878
    pub fn open_trees_for_document_change(
44
1046878
        &mut self,
45
1046878
        collection: &CollectionName,
46
1046878
        schema: &Schematic,
47
1046878
        #[cfg(any(feature = "encryption", feature = "compression"))] vault: Option<TreeVault>,
48
1046878
    ) {
49
1046878
        self.open_tree::<Versioned>(
50
1046878
            &document_tree_name(collection),
51
1046878
            #[cfg(any(feature = "encryption", feature = "compression"))]
52
1046878
            vault.clone(),
53
1046878
        );
54

            
55
2317140
        for view in schema.views_in_collection(collection) {
56
2317140
            let view_name = view.view_name();
57
2317140
            if view.update_policy().is_eager() {
58
461524
                self.open_tree::<Unversioned>(
59
461524
                    &view_document_map_tree_name(&view_name),
60
461524
                    #[cfg(any(feature = "encryption", feature = "compression"))]
61
461524
                    vault.clone(),
62
461524
                );
63
461524
                self.open_tree::<Unversioned>(
64
461524
                    &view_entries_tree_name(&view_name),
65
461524
                    #[cfg(any(feature = "encryption", feature = "compression"))]
66
461524
                    vault.clone(),
67
461524
                );
68
1855616
            } else {
69
1855616
                self.open_tree::<Unversioned>(
70
1855616
                    &view_invalidated_docs_tree_name(&view_name),
71
1855616
                    #[cfg(any(feature = "encryption", feature = "compression"))]
72
1855616
                    vault.clone(),
73
1855616
                );
74
1855616
            }
75
        }
76
1046878
    }
77
}