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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
use std::sync::Arc;
use tree::Tree;
use node::{Node, Content};
use errors::{Result, ErrorKind};
use containers::Container;

/// Deep clone a node and bump it's version
pub fn cow_node(node: &Arc<Node>) -> Arc<Node> {
    // Create a shallow copy of the node (inc ref count on Arc)
    let mut new_node = node.clone();

    // Force a deep copy on write copy and increment the version number
    Arc::make_mut(&mut new_node).version += 1;
    new_node
}

/// Directories contain a list of labels for each edge
/// Containers are an actual reference to the Container and it's data
#[derive(Eq, PartialEq)]
pub enum IterContent<'a> {
    Directory(Vec<&'a str>),
    Container(&'a Container)
}

#[derive(Eq, PartialEq)]
pub struct IterNode<'a> {
    pub path: &'a str,
    pub version: u64,
    pub content: IterContent<'a>
}

/// An iterator that performs a depth first walk of the entire tree.
pub struct Iter<'a> {
    stack: Vec<&'a Arc<Node>>
}

impl<'a> Iter<'a> {
    pub fn new(root: &'a Arc<Node>) -> Iter<'a> {
        Iter { stack: vec![root] }
    }
}

impl<'a> Iterator for Iter<'a> {
    type Item = IterNode<'a>;
    fn next(&mut self) -> Option<IterNode<'a>> {
        if self.stack.is_empty() {
            return None;
        }
        let node = self.stack.pop().unwrap();
        let content = match node.content {
            Content::Directory(ref edges) => {
                self.stack.extend(edges.iter().rev().map(|edge| &edge.node));
                IterContent::Directory(edges.iter().map(|edge| &edge.label as &str).collect())
            }
            Content::Container(ref container) => IterContent::Container(container),
        };
        Some(IterNode {
                 path: &node.path,
                 version: node.version,
                 content: content
             })
    }
}

/// Iterate over a tree in order, taking the shrotest path required to return each node in `paths`.
/// Along the way copy any paths necessary to build up a new tree that allows modifications to each
/// node in paths.
pub struct CowPathIter<'a> {
    tree: Tree,
    paths: Vec<&'a str>,
    stack: Vec<*mut Node>
}

impl<'a> CowPathIter<'a> {
    pub fn new(root: &'a Arc<Node>, mut paths: Vec<&'a str>, max_depth: u32) -> CowPathIter<'a> {
        paths.sort();
        paths.dedup();
        paths.reverse();
        let mut stack = Vec::with_capacity(max_depth as usize);
        let root = cow_node(root);
        let ptr: *mut Node = &*root as *const Node as *mut Node;
        stack.push(ptr);
        CowPathIter {
            tree: Tree {
                root: root,
                depth: max_depth
            },
            paths: paths,
            stack: stack
        }
    }

    /// Walk the tree to find the node living at `path`
    ///
    /// Copy the path to the node and return a mutable reference to the copied node.
    /// This implementation only performs copies while walking down the tree. Successive walks are
    /// performed from the last node, back up the tree and down other paths as necessary. Therefore,
    /// this implementation performs the minimum number of copies necessary for a COW tree.
    fn walk(&mut self) -> Result<&'a mut Node> {
        let path = self.paths.pop().unwrap();
        loop {
            unsafe {
                let mut node = match self.stack.last() {
                    Some(node) => *node,
                    None => return Err(ErrorKind::DoesNotExist(path.to_string()).into()),
                };
                if path.starts_with(&(*node).path) {
                    let num_labels = (*node).path.split('/').skip_while(|&s| s == "").count();
                    let split = path.split('/').skip_while(|&s| s == "").skip(num_labels);
                    for label in split {
                        if label == "" {
                            // Skip Trailing slashes
                            continue;
                        }
                        node = cow_get_child(node, label)?;
                        let ptr: *mut Node = &mut (*node);
                        self.stack.push(ptr);
                    }
                    return Ok(&mut *node);
                }
                // No matching prefix, back up the tree
                self.stack.pop();
            }
        }
    }

    /// Once `next` returns `None` a complete tree has been assembled. It can be retrieved via a
    /// call to `get_tree`.
    pub fn get_tree(&self) -> Tree {
        self.tree.clone()
    }
}

// TODO: Would it actually make sense to *only* return the content here?
// The caller should never modify the path or version
impl<'a> Iterator for CowPathIter<'a> {
    type Item = Result<&'a mut Node>;
    fn next(&mut self) -> Option<Result<&'a mut Node>> {
        if self.paths.is_empty() {
            return None;
        }
        Some(self.walk())
    }
}


/// Iterate over a tree in order, taking the shortest path required to return each node in `paths`.
pub struct PathIter<'a> {
    paths: Vec<&'a str>,
    stack: Vec<&'a Node>
}

impl<'a> PathIter<'a> {
    /// Create a new iterator for a set of given paths
    ///
    /// Allocate a stack to the max depth of the tree, so we don't need to resize it.
    pub fn new(root: &'a Arc<Node>, mut paths: Vec<&'a str>, max_depth: u32) -> PathIter<'a> {
        paths.sort();
        paths.dedup();
        paths.reverse();
        let mut stack = Vec::with_capacity(max_depth as usize);
        let node_ref: &Node = &*root;
        stack.push(node_ref);
        PathIter {
            paths: paths,
            stack: stack
        }
    }

    /// Walk the tree to find the node living at `path`
    fn walk(&mut self) -> Result<&'a Node> {
        let path = self.paths.pop().unwrap();
        loop {
            let mut node = match self.stack.last() {
                Some(node) => *node,
                None => return Err(ErrorKind::DoesNotExist(path.to_string()).into()),
            };
            if path.starts_with(&node.path) {
                let num_labels = node.path.split('/').skip_while(|&s| s == "").count();
                let split = path.split('/').skip_while(|&s| s == "").skip(num_labels);
                let mut depth_from_current_node = 0;
                for label in split {
                    depth_from_current_node += 1;
                    if label == "" {
                        // Skip Trailing slashes
                        continue;
                    }
                    node = get_child(node, label)?;
                    // Push the child on the stack
                    self.stack.push(node);
                }
                if depth_from_current_node > 0 {
                    // This path we are trying to find is a child of this node
                    return Ok(self.stack.last().unwrap());
                }
            }
            // No matching prefix, back up the tree
            self.stack.pop();
        }
    }
}

impl<'a> Iterator for PathIter<'a> {
    type Item = Result<&'a Node>;
    fn next(&mut self) -> Option<Result<&'a Node>> {
        if self.paths.is_empty() {
            return None;
        }
        Some(self.walk())
    }
}

/// Take a mutable parent directory node, and COW the child node given by it's label
///
/// Insert the COW'd child in the correct position and return a reference to it
unsafe fn cow_get_child(node: *mut Node, label: &str) -> Result<*mut Node> {
    if let Content::Directory(ref mut edges) = (*node).content {
        match edges.binary_search_by_key(&label, |e| &e.label) {
            Ok(index) => {
                let mut edge = edges.get_unchecked_mut(index);
                edge.node = cow_node(&edge.node);
                let ptr: *mut Node = &*edge.node as *const Node as *mut Node;
                return Ok(ptr);
            }
            Err(_) => {
                let mut path = (*node).path.clone();
                if &path != "/" {
                    path.push_str("/");
                }
                path.push_str(label);
                return Err(ErrorKind::DoesNotExist(path).into());
            }
        }
    }
    Err(ErrorKind::InvalidPathContent((*node).path.clone()).into())
}

/// Return a reference to a child node of a directory given the child's label
fn get_child<'a>(node: &'a Node, label: &'a str) -> Result<&'a Node> {
    if let Content::Directory(ref edges) = node.content {
        match edges.binary_search_by_key(&label, |e| &e.label) {
            Ok(index) => unsafe {
                return Ok(&*edges.get_unchecked(index).node);
            },
            Err(_) => {
                let mut path = node.path.clone();
                if &path != "/" {
                    path.push_str("/");
                }
                path.push_str(label);
                return Err(ErrorKind::DoesNotExist(path).into());
            }
        }
    }
    Err(ErrorKind::InvalidPathContent(node.path.clone()).into())
}

#[cfg(test)]
mod tests {
    use super::*;
    use tree::Tree;
    use node::*;
    use containers::*;
    use arbitrary::Path;

    quickcheck! {
        // Walking any existing paths always succeeds.
        fn prop_walk_existing_paths(node_specs: Vec<(Path, NodeType)>) -> bool {
            // Get the new tree and any node_specs where create succeeded
            let (tree, node_specs) =
                node_specs.iter().fold((Tree::new(),
                                        Vec::new()),
                                        |(tree, mut specs), &(ref path, node_type)|
                {
                    match tree.create(&path.clone().0, node_type) {
                        Ok(new_tree) => {
                            specs.push((path.clone(), node_type));
                            (new_tree, specs)
                        },
                        Err(_) => (tree, specs)
                    }
                });
            if node_specs.len() as u64 != tree.root.version {
                return false;
            }

            // Ensure we can find nodes at all inserted paths
            if node_specs.iter().any(|&(ref path, ty)| tree.find(&path.0, ty).is_err()) {
                return false;
            }

            // Ensure we can walk all inserted paths using a PathIter
            let paths = node_specs.iter().map(|&(ref path, _)| &path.0 as &str).collect();
            tree.path_iter(paths).count() == node_specs.len()
        }
    }

    #[test]
    fn create_nodes_iter_check() {
        let tree = Tree::new();
        let tree = tree.create("/somenode", NodeType::Directory).unwrap();
        let tree = tree.create("/somenode/somechildnode", NodeType::Set)
                       .unwrap();
        let tree = tree.create("/somedir1/somedir2/leaf", NodeType::Queue)
                       .unwrap();

        // Tuples are (path, num_edges, version)
        // Num edges is  None if not a directory
        // Note the sorted order
        let expected = [("/", Some(2), 3),
                        ("/somedir1", Some(1), 0),
                        ("/somedir1/somedir2", Some(1), 0),
                        ("/somedir1/somedir2/leaf", None, 0),
                        ("/somenode", Some(1), 1),
                        ("/somenode/somechildnode", None, 0)];

        // All the directories and leaves including "/"
        assert_eq!(tree.iter().count(), expected.len());
        for (i, node) in tree.iter().enumerate() {
            let (path, num_edges, version) = expected[i];
            assert_eq!(node.path, path);
            assert_eq!(node.version, version);
            if let Some(num_edges) = num_edges {
                if let IterContent::Directory(ref labels) =
                    node.content {
                    assert_eq!(labels.len(), num_edges);
                } else {
                    assert!(false);
                }
            }
        }
    }

    #[test]
    fn path_iter() {
        let tree = Tree::new();
        let tree = tree.create("/somenode", NodeType::Directory).unwrap();
        let tree = tree.create("/somenode/somechildnode", NodeType::Set)
                       .unwrap();
        let tree = tree.create("/somedir1/somedir2/leaf", NodeType::Queue)
                       .unwrap();

        let mut paths = vec!["/somenode/somechildnode",
                             "/somedir1/somedir2",
                             "/somedir1/somedir2/leaf",
                             "/somenode/"];


        let iter = PathIter::new(&tree.root, paths.clone(), tree.depth);
        let collected: Vec<&str> = iter.map(|node| &node.unwrap().path as &str).collect();
        paths.sort();
        let paths: Vec<&str> = paths.iter().map(|p| p.trim_right_matches('/')).collect();
        assert_eq!(paths, collected);
    }

    #[test]
    fn bad_path_iter() {
        let tree = Tree::new();
        let tree = tree.create("/somenode", NodeType::Directory).unwrap();
        let tree = tree.create("/somenode/somechildnode", NodeType::Set)
                       .unwrap();
        let tree = tree.create("/somedir1/somedir2/leaf", NodeType::Queue)
                       .unwrap();

        let paths = vec!["/somenode/somechildnode", "/zzz"];

        let mut iter = PathIter::new(&tree.root, paths, tree.depth);
        assert_matches!(iter.next(), Some(Ok(_)));
        assert_matches!(iter.next(), Some(Err(_)));
        assert_matches!(iter.next(), None);
    }

    #[test]
    fn cow_path_iter() {
        let tree = Tree::new();
        let tree = tree.create("/somenode", NodeType::Directory).unwrap();
        let tree = tree.create("/somenode/somechildnode", NodeType::Blob)
                       .unwrap();
        let tree = tree.create("/somedir1/somedir2/leaf", NodeType::Blob)
                       .unwrap();

        // 3 create calls were made
        assert_eq!(3, tree.root.version);

        let paths = vec!["/somenode/somechildnode", "/somedir1/somedir2/leaf"];
        let mut iter = CowPathIter::new(&tree.root, paths.clone(), tree.depth);

        for node in iter.by_ref() {
            let blob = "hello".to_string().into_bytes();
            node.unwrap().content = Content::Container(Container::Blob(blob));
        }

        let cow_tree = iter.get_tree();

        // Original tree still unchanged
        assert_eq!(3, tree.root.version);

        // Mutation is optimal for CowPathIter even when copying multiple paths. Therefore,
        // the root version count is only incremented once.
        assert_eq!(4, cow_tree.root.version);

        // Iterate the original tree and show the empty blobs
        let iter = PathIter::new(&tree.root, paths.clone(), tree.depth);
        for node in iter {
            let node = node.unwrap();
            let blob = node.content.get_blob().unwrap();
            assert_eq!(blob.len(), 0);
            assert_eq!(node.version, 0);
        }

        // Iterate the modified tree and show "hello"
        let iter = PathIter::new(&cow_tree.root, paths.clone(), tree.depth);
        for node in iter {
            let node = node.unwrap();
            let blob = node.content.get_blob().unwrap();
            assert_eq!(&blob[..], "hello".as_bytes());
            // Each node was only modified once
            assert_eq!(node.version, 1);
        }

    }
}