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
677313
#[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
3836048
    pub fn open_tree<R: Root>(
24
3836048
        &mut self,
25
3836048
        name: &str,
26
3836048
        #[cfg(any(feature = "encryption", feature = "compression"))] vault: Option<TreeVault>,
27
3836048
    ) {
28
3836048
        if !self.trees_index_by_name.contains_key(name) {
29
3062904
            self.trees_index_by_name
30
3062904
                .insert(name.to_string(), self.trees.len());
31
3062904
            let mut tree = R::tree(name.to_string());
32

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

            
38
3062904
            self.trees.push(Box::new(tree));
39
773144
        }
40
3836048
    }
41

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

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