1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
// MIT LICENSE
//
// Copyright (c) 2023 Dash Core Group
//
// Permission is hereby granted, free of charge, to any
// person obtaining a copy of this software and associated
// documentation files (the "Software"), to deal in the
// Software without restriction, including without
// limitation the rights to use, copy, modify, merge,
// publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following
// conditions:
//
// The above copyright notice and this permission notice
// shall be included in all copies or substantial portions
// of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF
// ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED
// TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
// PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
// SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
// CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

//! GroveDB subtree path manipulation library.

#![deny(missing_docs)]

mod subtree_path;
mod subtree_path_builder;
mod subtree_path_iter;
mod util;

pub use subtree_path::SubtreePath;
pub use subtree_path_builder::SubtreePathBuilder;
pub use subtree_path_iter::SubtreePathIter;

#[cfg(test)]
mod tests {
    use super::*;
    use crate::util::calculate_hash;

    fn assert_path_properties<B>(path: SubtreePath<'_, B>, reference: Vec<Vec<u8>>)
    where
        B: AsRef<[u8]> + std::fmt::Debug,
    {
        // Assert `to_vec`
        assert_eq!(path.to_vec(), reference);

        // Assert `into_reverse_iter`
        assert!(path.clone().into_reverse_iter().eq(reference.iter().rev()));

        // Assert equality
        assert_eq!(path, SubtreePath::from(reference.as_slice()));

        // Assert hashing done properly
        let subtree_path_ref = SubtreePath::from(reference.as_slice());
        let subtree_path_builder = subtree_path_ref.derive_owned();
        assert_eq!(calculate_hash(&path), calculate_hash(&subtree_path_ref));
        assert_eq!(calculate_hash(&path), calculate_hash(&subtree_path_builder));
        assert_eq!(path.len(), reference.len());
    }

    #[test]
    fn test_root_and_roots_child_derivation_slice() {
        // Go two levels down just to complicate our test a bit:
        let path_array = [b"one", b"two"];
        let path = SubtreePath::from(path_array.as_ref());

        let (root, child) = path.derive_parent().unwrap().0.derive_parent().unwrap();

        assert_eq!(child, b"one");
        assert_eq!(root.to_vec(), Vec::<&[u8]>::new());

        assert_eq!(root.derive_parent(), None);
    }

    #[test]
    fn test_root_and_roots_child_derivation_builder() {
        let mut builder = SubtreePathBuilder::new();
        builder.push_segment(b"one");
        builder.push_segment(b"two");
        let path: SubtreePath<[u8; 0]> = (&builder).into();

        let (root, child) = path.derive_parent().unwrap().0.derive_parent().unwrap();

        assert_eq!(child, b"one");
        assert_eq!(root.to_vec(), Vec::<&[u8]>::new());

        assert_eq!(root.derive_parent(), None);
    }

    #[test]
    fn test_hashes_are_equal() {
        let path_array = [
            b"one".to_vec(),
            b"two".to_vec(),
            b"three".to_vec(),
            b"four".to_vec(),
            b"five".to_vec(),
        ];
        let path_array_refs = [
            b"one".as_ref(),
            b"two".as_ref(),
            b"three".as_ref(),
            b"four".as_ref(),
            b"five".as_ref(),
        ];
        let path_base_slice_slices = SubtreePath::from(path_array_refs.as_ref());

        let path_array_refs_six = [
            b"one".as_ref(),
            b"two".as_ref(),
            b"three".as_ref(),
            b"four".as_ref(),
            b"five".as_ref(),
            b"six".as_ref(),
        ];
        let path_base_slice_too_much = SubtreePath::from(path_array_refs_six.as_ref());
        let path_base_unfinished = SubtreePath::from([b"one", b"two"].as_ref());
        let path_empty = SubtreePathBuilder::new();

        let path_derived_11 = path_empty.derive_owned_with_child(b"one".as_ref());
        let path_derived_12 = path_derived_11.derive_owned_with_child(b"two".as_ref());
        let path_derived_13 = path_derived_12.derive_owned_with_child(b"three".as_ref());
        let path_derived_14 = path_derived_13.derive_owned_with_child(b"four".to_vec());
        let path_derived_1 = path_derived_14.derive_owned_with_child(b"five".as_ref());

        let (path_derived_2, _) = path_base_slice_too_much.derive_parent().unwrap();

        let path_derived_31 = path_base_unfinished.derive_owned_with_child(b"three".to_vec());
        let path_derived_32 = path_derived_31.derive_owned_with_child(b"four".as_ref());
        let path_derived_3 = path_derived_32.derive_owned_with_child(b"five".as_ref());

        assert_path_properties(path_base_slice_slices, path_array.to_vec());
        assert_path_properties(SubtreePath::from(&path_derived_1), path_array.to_vec());
        assert_path_properties(path_derived_2, path_array.to_vec());
        assert_path_properties(SubtreePath::from(&path_derived_3), path_array.to_vec());
    }

    #[test]
    fn test_is_root() {
        let path_empty = SubtreePathBuilder::new();
        assert!(path_empty.is_root());

        let path_derived = path_empty.derive_owned_with_child(b"two".as_ref());
        assert!(!path_derived.is_root());
        assert!(path_derived.derive_parent().unwrap().0.is_root());

        let path_not_empty = SubtreePath::from([b"one"].as_ref());
        assert!(!path_not_empty.is_root());
        assert!(path_not_empty.derive_parent().unwrap().0.is_root());
    }

    #[test]
    fn test_complex_derivation() {
        // Append only operations:
        let base = SubtreePath::from([b"one", b"two"].as_ref());
        let with_child_1 = base.derive_owned_with_child(b"three".to_vec());
        let mut with_child_inplace = with_child_1.derive_owned_with_child(b"four");
        with_child_inplace.push_segment(b"five");
        with_child_inplace.push_segment(b"six");
        with_child_inplace.push_segment(b"seven");
        with_child_inplace.push_segment(b"eight");

        // `with_child_inplace` should be like (substituted for digits for short):
        // [1, 2] -> 3 -> [4, 5, 6, 7, 8]
        assert_eq!(
            with_child_inplace.reverse_iter().fold(0, |acc, _| acc + 1),
            8
        );
        assert_path_properties(
            (&with_child_inplace).into(),
            vec![
                b"one".to_vec(),
                b"two".to_vec(),
                b"three".to_vec(),
                b"four".to_vec(),
                b"five".to_vec(),
                b"six".to_vec(),
                b"seven".to_vec(),
                b"eight".to_vec(),
            ],
        );

        // Now go to ancestors (note that intermediate derivations are dropped and
        // is still compiles, as [SubtreePath]s are intended for):
        let points_five = with_child_inplace
            .derive_parent()
            .unwrap()
            .0
            .derive_parent()
            .unwrap()
            .0
            .derive_parent()
            .unwrap()
            .0;

        assert_path_properties(
            points_five.clone(),
            vec![
                b"one".to_vec(),
                b"two".to_vec(),
                b"three".to_vec(),
                b"four".to_vec(),
                b"five".to_vec(),
            ],
        );

        // And add a couple of other derivations
        let after_five_1 = points_five.derive_owned_with_child(b"four");
        let after_five_2 = after_five_1.derive_owned_with_child(b"twenty");
        let mut after_five_3 = after_five_2.derive_owned();
        after_five_3.push_segment(b"thirteen");
        after_five_3.push_segment(b"thirtyseven");

        // `after_five_3` should be like this:
        // [1, 2] -> 3 -> [4, 5, 6, 7, 8]
        //                    ^-> 4 -> 20 -> [13, 37]
        assert_path_properties(
            (&after_five_3).into(),
            vec![
                b"one".to_vec(),
                b"two".to_vec(),
                b"three".to_vec(),
                b"four".to_vec(),
                b"five".to_vec(),
                b"four".to_vec(),
                b"twenty".to_vec(),
                b"thirteen".to_vec(),
                b"thirtyseven".to_vec(),
            ],
        );
    }
}