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

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

            
38
3151895
            self.trees.push(Box::new(tree));
39
714842
        }
40
3866737
    }
41

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

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