liboxen 0.46.7

Oxen is a fast, unstructured data version control, to help version large machine learning datasets written in Rust.
Documentation
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
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
use std::collections::HashMap;
use std::collections::HashSet;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::path::Path;
use std::path::PathBuf;

use super::*;
use crate::core::db::merkle_node::MerkleNodeDB;
use crate::error::OxenError;
use crate::model::{LocalRepository, MerkleHash, MerkleTreeNodeType};

use serde::{Deserialize, Serialize};

use super::merkle_tree_node_cache;

#[derive(Clone, Eq, Deserialize, Serialize)]
pub struct MerkleTreeNode {
    pub hash: MerkleHash,
    pub node: EMerkleTreeNode,
    pub parent_id: Option<MerkleHash>,
    pub children: Vec<MerkleTreeNode>,
}

impl MerkleTreeNode {
    /// Create an empty root node with a hash
    pub fn from_hash(repo: &LocalRepository, hash: &MerkleHash) -> Result<Self, OxenError> {
        // Check cache first
        if let Some(cached_node) = merkle_tree_node_cache::get_cached_node(repo, hash) {
            return Ok((*cached_node).clone());
        }

        // If not in cache, load from disk
        let node = Self::from_hash_uncached(repo, hash)?;

        // Cache the result
        merkle_tree_node_cache::cache_node(repo, *hash, node.clone());

        Ok(node)
    }

    /// Private implementation that loads from disk without caching
    fn from_hash_uncached(repo: &LocalRepository, hash: &MerkleHash) -> Result<Self, OxenError> {
        let node_db = MerkleNodeDB::open_read_only(repo, hash)?;
        let parent_id = node_db.parent_id;
        Ok(MerkleTreeNode {
            hash: *hash,
            node: node_db.node()?,
            parent_id,
            children: Vec::new(),
        })
    }

    pub fn read_children_from_hash(
        repo: &LocalRepository,
        hash: &MerkleHash,
    ) -> Result<Vec<(MerkleHash, MerkleTreeNode)>, OxenError> {
        // Check cache first
        if let Some(cached_children) = merkle_tree_node_cache::get_cached_children(repo, hash) {
            return Ok((*cached_children).clone());
        }

        // If not in cache, load from disk
        let children = Self::read_children_from_hash_uncached(repo, hash)?;

        // Cache the result
        merkle_tree_node_cache::cache_children(repo, *hash, children.clone());

        Ok(children)
    }

    /// Private implementation that loads from disk without caching
    fn read_children_from_hash_uncached(
        repo: &LocalRepository,
        hash: &MerkleHash,
    ) -> Result<Vec<(MerkleHash, MerkleTreeNode)>, OxenError> {
        let Ok(mut node_db) = MerkleNodeDB::open_read_only(repo, hash) else {
            // We don't return an error here because there are some situations where we won't have all the node files.
            // For example, when working in a subtree clone.
            log::warn!("no child node db: {hash:?}");
            return Ok(Vec::new());
        };
        node_db.map()
    }

    /// Check if the node is a leaf node (i.e. it has no children)
    pub fn is_leaf(&self) -> bool {
        matches!(
            &self.node,
            EMerkleTreeNode::File(_) | EMerkleTreeNode::FileChunk(_)
        )
    }

    /// Check if the node has children
    pub fn has_children(&self) -> bool {
        !self.children.is_empty()
    }

    /// Check if it is a file
    pub fn is_file(&self) -> bool {
        matches!(self.node, EMerkleTreeNode::File(_))
    }

    /// Check if it is a directory
    pub fn is_dir(&self) -> bool {
        matches!(self.node, EMerkleTreeNode::Directory(_))
    }

    /// Recursively count the total number of vnodes in the tree
    pub fn total_vnodes(&self) -> usize {
        let mut count = 0;
        for child in &self.children {
            if let EMerkleTreeNode::VNode(_) = child.node {
                count += 1
            }
            count += child.total_vnodes();
        }
        count
    }

    /// Count the number of vnodes a dir has (not recursive)
    pub fn num_vnodes(&self) -> u128 {
        let mut count = 0;
        for child in &self.children {
            if let EMerkleTreeNode::VNode(_) = child.node {
                count += 1
            }
        }
        count
    }

    /// Get the latest commit id for file or dir
    pub fn latest_commit_id(&self) -> Result<&MerkleHash, OxenError> {
        match &self.node {
            EMerkleTreeNode::File(file_node) => Ok(file_node.last_commit_id()),
            EMerkleTreeNode::Directory(dir_node) => Ok(dir_node.last_commit_id()),
            _ => Err(OxenError::basic_str(
                "MerkleTreeNode::latest_commit_id called on invalid node type",
            )),
        }
    }

    /// Create a default DirNode with none of the metadata fields set
    pub fn default_dir() -> MerkleTreeNode {
        MerkleTreeNode {
            hash: MerkleHash::new(0),
            node: EMerkleTreeNode::Directory(DirNode::default()),
            parent_id: None,
            children: Vec::new(),
        }
    }

    /// Create a default DirNode with the given path
    pub fn default_dir_from_path(path: impl AsRef<Path>) -> MerkleTreeNode {
        let mut dir_node = DirNode::default();
        let dir_str = path.as_ref().to_str().unwrap().to_string();
        dir_node.set_name(dir_str);
        MerkleTreeNode {
            hash: MerkleHash::new(0),
            node: EMerkleTreeNode::Directory(dir_node),
            parent_id: None,
            children: Vec::new(),
        }
    }

    /// Create a default FileNode with none of the metadata fields set
    pub fn default_file() -> MerkleTreeNode {
        MerkleTreeNode {
            hash: MerkleHash::new(0),
            node: EMerkleTreeNode::File(FileNode::default()),
            parent_id: None,
            children: Vec::new(),
        }
    }

    /// Create a MerkleTreeNode from a FileNode
    pub fn from_file(file_node: FileNode) -> MerkleTreeNode {
        MerkleTreeNode {
            hash: *file_node.hash(),
            node: EMerkleTreeNode::File(file_node),
            parent_id: None,
            children: Vec::new(),
        }
    }

    /// Create a MerkleTreeNode from a FileNode with the path relative to the repo
    pub fn from_file_relative_to_repo(file_node: FileNode) -> MerkleTreeNode {
        MerkleTreeNode {
            hash: *file_node.hash(),
            node: EMerkleTreeNode::File(file_node),
            parent_id: None,
            children: Vec::new(),
        }
    }

    /// Create a MerkleTreeNode from a DirNode
    pub fn from_dir(dir_node: DirNode) -> MerkleTreeNode {
        MerkleTreeNode {
            hash: *dir_node.hash(),
            node: EMerkleTreeNode::Directory(dir_node),
            parent_id: None,
            children: Vec::new(),
        }
    }

    pub fn maybe_path(&self) -> Result<PathBuf, OxenError> {
        if let EMerkleTreeNode::Directory(dir_node) = &self.node {
            return Ok(PathBuf::from(dir_node.name()));
        }
        // From DEF of file_node, file_name.name == file_path to this file
        // e.g., the file 'happy' in the folder 'sad' is called 'sad//happy'
        if let EMerkleTreeNode::File(file_node) = &self.node {
            return Ok(PathBuf::from(file_node.name()));
        }
        Err(OxenError::basic_str(format!(
            "MerkleTreeNode::maybe_path called on non-file or non-dir node: {self:?}"
        )))
    }

    /// List all paths in the tree
    pub fn list_paths(&self) -> Result<Vec<PathBuf>, OxenError> {
        let mut paths = Vec::new();
        let current_path = Path::new("");
        self.list_paths_helper(current_path, &mut paths)?;
        Ok(paths)
    }

    fn list_paths_helper(
        &self,
        current_path: &Path,
        paths: &mut Vec<PathBuf>,
    ) -> Result<(), OxenError> {
        if let EMerkleTreeNode::File(file_node) = &self.node {
            paths.push(current_path.join(file_node.name()).to_path_buf());
        }

        for child in &self.children {
            if let EMerkleTreeNode::Directory(dir) = &child.node {
                let new_path = current_path.join(dir.name());
                paths.push(new_path.clone());
                child.list_paths_helper(&new_path, paths)?;
            } else {
                child.list_paths_helper(current_path, paths)?;
            }
        }
        Ok(())
    }

    /// List all the directories in the tree
    pub fn list_dir_paths(&self) -> Result<Vec<PathBuf>, OxenError> {
        let mut dirs = Vec::new();
        let current_path = Path::new("");
        self.list_dir_paths_helper(current_path, &mut dirs)?;
        Ok(dirs)
    }

    fn list_dir_paths_helper(
        &self,
        current_path: &Path,
        dirs: &mut Vec<PathBuf>,
    ) -> Result<(), OxenError> {
        if let EMerkleTreeNode::Directory(_) = &self.node {
            dirs.push(current_path.to_path_buf());
        }
        for child in &self.children {
            if let EMerkleTreeNode::Directory(dir) = &child.node {
                let new_path = current_path.join(dir.name());
                child.list_dir_paths_helper(&new_path, dirs)?;
            } else {
                child.list_dir_paths_helper(current_path, dirs)?;
            }
        }
        Ok(())
    }

    /// List all file hashes in the tree
    pub fn list_file_hashes(&self) -> Result<HashSet<MerkleHash>, OxenError> {
        let mut files = HashSet::new();
        let current_path = Path::new("");
        self.list_file_hashes_helper(current_path, &mut files)?;
        Ok(files)
    }

    fn list_file_hashes_helper(
        &self,
        current_path: &Path,
        files: &mut HashSet<MerkleHash>,
    ) -> Result<(), OxenError> {
        if let EMerkleTreeNode::File(_) = &self.node {
            files.insert(self.hash);
        }
        for child in &self.children {
            if let EMerkleTreeNode::Directory(dir) = &child.node {
                let new_path = current_path.join(dir.name());
                child.list_file_hashes_helper(&new_path, files)?;
            } else {
                child.list_file_hashes_helper(current_path, files)?;
            }
        }
        Ok(())
    }

    /// List all file hashes in the tree
    pub fn list_file_paths(&self) -> Result<HashSet<PathBuf>, OxenError> {
        let mut files = HashSet::new();
        let current_path = Path::new("");
        self.list_file_paths_helper(current_path, &mut files)?;
        Ok(files)
    }

    fn list_file_paths_helper(
        &self,
        current_path: &Path,
        files: &mut HashSet<PathBuf>,
    ) -> Result<(), OxenError> {
        if let EMerkleTreeNode::File(file) = &self.node {
            let file_path = current_path.join(file.name());
            files.insert(file_path);
        }
        for child in &self.children {
            if let EMerkleTreeNode::Directory(dir) = &child.node {
                let new_path = current_path.join(dir.name());
                child.list_file_paths_helper(&new_path, files)?;
            } else {
                child.list_file_paths_helper(current_path, files)?;
            }
        }
        Ok(())
    }

    /// List all file hashes in the tree
    pub fn list_files(&self) -> Result<HashMap<PathBuf, MerkleTreeNode>, OxenError> {
        let mut files = HashMap::new();
        let current_path = Path::new("");
        self.list_files_helper(current_path, &mut files)?;
        Ok(files)
    }

    fn list_files_helper(
        &self,
        current_path: &Path,
        files: &mut HashMap<PathBuf, MerkleTreeNode>,
    ) -> Result<(), OxenError> {
        if let EMerkleTreeNode::File(file_node) = &self.node {
            files.insert(
                current_path.join(file_node.name()).to_path_buf(),
                self.clone(),
            );
        }
        for child in &self.children {
            if let EMerkleTreeNode::Directory(dir) = &child.node {
                let new_path = current_path.join(dir.name());
                child.list_files_helper(&new_path, files)?;
            } else {
                child.list_files_helper(current_path, files)?;
            }
        }
        Ok(())
    }

    /// List all file and dir nodes in the tree
    pub fn list_files_and_dirs(&self) -> Result<HashMap<PathBuf, MerkleTreeNode>, OxenError> {
        let mut nodes = HashMap::new();
        let mut current_path = PathBuf::new();
        if let EMerkleTreeNode::Directory(dir) = &self.node {
            current_path = current_path.join(dir.name());
            nodes.insert(current_path.clone(), self.clone());
        }

        self.list_files_and_dirs_helper(&current_path, &mut nodes)?;
        Ok(nodes)
    }

    fn list_files_and_dirs_helper(
        &self,
        current_path: &Path,
        nodes: &mut HashMap<PathBuf, MerkleTreeNode>,
    ) -> Result<(), OxenError> {
        if let EMerkleTreeNode::File(file_node) = &self.node {
            nodes.insert(
                current_path.join(file_node.name()).to_path_buf(),
                self.clone(),
            );
        }
        for child in &self.children {
            if let EMerkleTreeNode::Directory(dir) = &child.node {
                let new_path = current_path.join(dir.name());
                nodes.insert(new_path.clone(), child.clone());

                child.list_files_and_dirs_helper(&new_path, nodes)?;
            } else {
                child.list_files_and_dirs_helper(current_path, nodes)?;
            }
        }
        Ok(())
    }

    /// List all the directory and vnode hashes in the tree
    pub fn list_dir_and_vnode_hashes(&self) -> Result<HashSet<MerkleHash>, OxenError> {
        let mut hashes = HashSet::new();
        let current_path = Path::new("");
        self.list_dir_and_vnode_hashes_helper(current_path, &mut hashes)?;
        Ok(hashes)
    }

    fn list_dir_and_vnode_hashes_helper(
        &self,
        current_path: &Path,
        hashes: &mut HashSet<MerkleHash>,
    ) -> Result<(), OxenError> {
        match &self.node {
            EMerkleTreeNode::Directory(_) | EMerkleTreeNode::VNode(_) => {
                hashes.insert(self.hash);
            }
            _ => {}
        };

        for child in &self.children {
            if let EMerkleTreeNode::Directory(dir) = &child.node {
                let new_path = current_path.join(dir.name());
                child.list_dir_and_vnode_hashes_helper(&new_path, hashes)?;
            } else {
                child.list_dir_and_vnode_hashes_helper(current_path, hashes)?;
            }
        }
        Ok(())
    }

    /// List all the directory and vnode hashes in the tree that aren't in old_hashes
    pub fn list_shared_dir_and_vnode_hashes(
        &self,
        old_hashes: &HashSet<MerkleHash>,
    ) -> Result<HashSet<MerkleHash>, OxenError> {
        let mut new_hashes = HashSet::new();
        let current_path = Path::new("");
        self.list_shared_dir_and_vnode_hashes_helper(current_path, &mut new_hashes, old_hashes)?;
        Ok(new_hashes)
    }

    fn list_shared_dir_and_vnode_hashes_helper(
        &self,
        current_path: &Path,
        new_hashes: &mut HashSet<MerkleHash>,
        old_hashes: &HashSet<MerkleHash>,
    ) -> Result<(), OxenError> {
        match &self.node {
            EMerkleTreeNode::Directory(_) | EMerkleTreeNode::VNode(_) => {
                // If the dir is in old_hashes, no need to search further
                if old_hashes.contains(&self.hash) {
                    new_hashes.insert(self.hash);
                    return Ok(());
                }
            }
            _ => {}
        };

        for child in &self.children {
            if let EMerkleTreeNode::Directory(dir) = &child.node {
                let new_path = current_path.join(dir.name());
                child.list_shared_dir_and_vnode_hashes_helper(&new_path, new_hashes, old_hashes)?;
            } else {
                child.list_shared_dir_and_vnode_hashes_helper(
                    current_path,
                    new_hashes,
                    old_hashes,
                )?;
            }
        }

        Ok(())
    }

    /// List missing file hashes
    pub async fn list_missing_file_hashes(
        &self,
        repo: &LocalRepository,
    ) -> Result<HashSet<MerkleHash>, OxenError> {
        let mut missing_hashes = HashSet::new();
        let version_store = repo.version_store()?;
        // Todo: parallelize for S3
        for child in &self.children {
            if let EMerkleTreeNode::File(_) = &child.node {
                // Check if the file exists in the version store
                if !version_store
                    .version_exists(&child.hash.to_string())
                    .await?
                {
                    missing_hashes.insert(child.hash);
                }
            }
        }
        Ok(missing_hashes)
    }

    /// Get all files and dirs in a directory
    pub fn get_all_children(&self) -> Result<Vec<MerkleTreeNode>, OxenError> {
        let mut children = Vec::new();
        for child in &self.children {
            children.push(child.clone());
            if let EMerkleTreeNode::Directory(_) = &child.node {
                children.extend(child.get_all_children()?);
            }
        }
        Ok(children)
    }

    /// Get all the vnodes for a given directory
    pub fn get_vnodes_for_dir(
        &self,
        path: impl AsRef<Path>,
    ) -> Result<Vec<MerkleTreeNode>, OxenError> {
        let path = path.as_ref();
        let Some(node) = self.get_by_path(path)? else {
            return Err(OxenError::basic_str(format!(
                "Merkle tree directory not found: '{path:?}'"
            )));
        };

        if MerkleTreeNodeType::Dir != node.node.node_type() {
            return Err(OxenError::basic_str(format!(
                "get_vnodes_for_dir Merkle tree node is not a directory: '{path:?}'"
            )));
        }

        let mut vnodes = Vec::new();
        for child in &node.children {
            if let EMerkleTreeNode::VNode(_) = &child.node {
                vnodes.push(child.clone());
            }
        }
        Ok(vnodes)
    }

    /// Search for a file node by path
    pub fn get_by_path(&self, path: impl AsRef<Path>) -> Result<Option<MerkleTreeNode>, OxenError> {
        let path = path.as_ref();
        let traversed_path = Path::new("");
        self.get_by_path_helper(traversed_path, path)
    }

    fn get_by_path_helper(
        &self,
        traversed_path: &Path,
        path: &Path,
    ) -> Result<Option<MerkleTreeNode>, OxenError> {
        // log::debug!(
        //     "get_by_path_helper {} traversed_path: {:?} path: {:?}",
        //     self,
        //     traversed_path,
        //     path
        // );
        if traversed_path.components().count() > path.components().count() {
            // log::debug!(
            //     "get_by_path_helper {} returning None traversed_path {:?} is longer than path {:?}",
            //     self,
            //     traversed_path,
            //     path
            // );
            return Ok(None);
        }

        if let EMerkleTreeNode::File(_) = &self.node {
            let file_node = self.file()?;
            let file_path = traversed_path.join(file_node.name());
            // log::debug!(
            //     "get_by_path_helper {} is file! [{:?}] {:?} {:?}",
            //     self,
            //     self.node.node_type(),
            //     file_path,
            //     path
            // );
            if file_path == path {
                return Ok(Some(self.clone()));
            }
        }

        if let EMerkleTreeNode::Directory(_) = &self.node {
            // log::debug!(
            //     "get_by_path_helper {} is dir! {:?} {:?}",
            //     self,
            //     traversed_path,
            //     path
            // );
            if traversed_path == path {
                return Ok(Some(self.clone()));
            }
        }

        if let EMerkleTreeNode::VNode(_) = &self.node {
            // Binary search implementation
            let target_name = path
                .file_name()
                .and_then(|s| s.to_str())
                .unwrap_or("")
                .to_string();
            match self.children.binary_search_by(|child| {
                // log::debug!(
                //     "get_by_path_helper {} binary_search_by child: {}",
                //     self,
                //     child
                // );
                let child_name = match &child.node {
                    EMerkleTreeNode::Directory(dir) => Some(dir.name()),
                    EMerkleTreeNode::File(file) => Some(file.name()),
                    _ => None,
                };
                // log::debug!(
                //     "get_by_path_helper {} binary_search_by child_name: {:?} target_name: {:?}",
                //     self,
                //     child_name,
                //     target_name
                // );
                child_name.unwrap_or("").cmp(&target_name)
            }) {
                Ok(index) => {
                    let child = &self.children[index];
                    // log::debug!(
                    //     "get_by_path_helper {} found index: {} child: {}",
                    //     self,
                    //     index,
                    //     child
                    // );
                    if let EMerkleTreeNode::Directory(dir_node) = &child.node {
                        // log::debug!(
                        //     "get_by_path_helper {} traversing dir child: {}",
                        //     self,
                        //     dir_node
                        // );
                        if let Some(node) =
                            child.get_by_path_helper(&traversed_path.join(dir_node.name()), path)?
                        {
                            return Ok(Some(node));
                        }
                    } else {
                        // log::debug!(
                        //     "get_by_path_helper {} traversing other child: {}",
                        //     self,
                        //     child
                        // );
                        if let Some(node) = child.get_by_path_helper(traversed_path, path)? {
                            return Ok(Some(node));
                        }
                    }
                }
                Err(_) => {
                    // If the value is not found then Result::Err is returned,
                    // containing the index where a matching element could be inserted while maintaining sorted order.
                    // log::debug!(
                    //     "get_by_path_helper {} could not find path: {:?} possible insert point: {:?}",
                    //     self,
                    //     target_name,
                    //     err
                    // );
                }
            }
        }

        // log::debug!(
        //     "get_by_path_helper {} traversing children {}",
        //     self,
        //     self.children.len()
        // );
        for child in &self.children {
            // log::debug!("get_by_path_helper {} traversing child: {}", self, child);
            if let EMerkleTreeNode::Directory(dir_node) = &child.node {
                if let Some(node) =
                    child.get_by_path_helper(&traversed_path.join(dir_node.name()), path)?
                {
                    return Ok(Some(node));
                }
            } else if let Some(node) = child.get_by_path_helper(traversed_path, path)? {
                return Ok(Some(node));
            }
        }
        // log::debug!(
        //     "get_by_path_helper {} returning None for path: {:?}",
        //     self,
        //     path
        // );
        Ok(None)
    }

    pub fn to_node(&self) -> EMerkleTreeNode {
        self.node.clone()
    }

    pub fn deserialize_id(data: &[u8], dtype: MerkleTreeNodeType) -> Result<MerkleHash, OxenError> {
        match dtype {
            MerkleTreeNodeType::Commit => {
                CommitNode::deserialize(data).map(|commit| *commit.hash())
            }
            MerkleTreeNodeType::VNode => VNode::deserialize(data).map(|vnode| *vnode.hash()),
            MerkleTreeNodeType::Dir => DirNode::deserialize(data).map(|dir| *dir.hash()),
            MerkleTreeNodeType::File => FileNode::deserialize(data).map(|file| *file.hash()),
            MerkleTreeNodeType::FileChunk => {
                FileChunkNode::deserialize(data).map(|file_chunk| file_chunk.hash)
            }
        }
    }

    pub fn commit(&self) -> Result<CommitNode, OxenError> {
        if let EMerkleTreeNode::Commit(commit_node) = &self.node {
            Ok(commit_node.clone())
        } else {
            Err(OxenError::basic_str(
                "MerkleTreeNode::commit called on non-commit node",
            ))
        }
    }

    pub fn vnode(&self) -> Result<VNode, OxenError> {
        if let EMerkleTreeNode::VNode(vnode) = &self.node {
            Ok(vnode.clone())
        } else {
            Err(OxenError::basic_str(
                "MerkleTreeNode::vnode called on non-vnode node",
            ))
        }
    }

    pub fn dir(&self) -> Result<DirNode, OxenError> {
        if let EMerkleTreeNode::Directory(dir_node) = &self.node {
            Ok(dir_node.clone())
        } else {
            Err(OxenError::basic_str(
                "MerkleTreeNode::dir called on non-dir node",
            ))
        }
    }

    pub fn file(&self) -> Result<FileNode, OxenError> {
        if let EMerkleTreeNode::File(file_node) = &self.node {
            Ok(file_node.clone())
        } else {
            Err(OxenError::basic_str(
                "MerkleTreeNode::file called on non-file node",
            ))
        }
    }

    pub fn file_chunk(&self) -> Result<FileChunkNode, OxenError> {
        if let EMerkleTreeNode::FileChunk(file_chunk_node) = &self.node {
            Ok(file_chunk_node.clone())
        } else {
            Err(OxenError::basic_str(
                "MerkleTreeNode::file_chunk called on non-file_chunk node",
            ))
        }
    }

    pub fn walk_tree(&self, mut f: impl FnMut(&MerkleTreeNode)) {
        let mut stack = vec![self];
        while let Some(node) = stack.pop() {
            f(node);
            for child in node.children.iter().rev() {
                stack.push(child);
            }
        }
    }

    pub fn walk_tree_mut(&mut self, mut f: impl FnMut(&mut MerkleTreeNode)) {
        let mut stack = vec![self];
        while let Some(node) = stack.pop() {
            f(node);
            for child in node.children.iter_mut().rev() {
                stack.push(child);
            }
        }
    }

    pub fn walk_tree_without_leaves(&self, mut f: impl FnMut(&MerkleTreeNode)) {
        let mut stack = vec![self];
        while let Some(node) = stack.pop() {
            f(node);
            for child in node.children.iter().rev() {
                if !child.is_leaf() {
                    stack.push(child);
                }
            }
        }
    }

    pub fn get_nodes_along_paths(
        &self,
        paths: Vec<PathBuf>,
    ) -> Result<(Option<MerkleTreeNode>, Vec<MerkleTreeNode>), OxenError> {
        let traversed_path = Path::new("");
        self.get_nodes_along_paths_helper(traversed_path, paths)
    }

    fn get_nodes_along_paths_helper(
        &self,
        traversed_path: &Path,
        path: Vec<PathBuf>,
    ) -> Result<(Option<MerkleTreeNode>, Vec<MerkleTreeNode>), OxenError> {
        let mut traversed_nodes = Vec::new(); // Vector to store traversed nodes
        let mut path_components = path.clone(); // Use the provided path directly

        if traversed_path.components().count() > path_components.len() {
            return Ok((None, traversed_nodes));
        }

        // Check if there are still components to check
        if path_components.is_empty() {
            return Ok((None, traversed_nodes));
        }

        if let EMerkleTreeNode::File(_) = &self.node {
            let file_node = self.file()?;
            let file_path = traversed_path.join(file_node.name());
            if &file_path == path_components.last().unwrap() {
                traversed_nodes.push(self.clone()); // Add the current node to the traversed nodes
                return Ok((Some(self.clone()), traversed_nodes));
            }
        }

        if let EMerkleTreeNode::Directory(_) = &self.node
            && traversed_path == path_components.last().unwrap()
        {
            traversed_nodes.push(self.clone());
            return Ok((Some(self.clone()), traversed_nodes));
        }

        if let EMerkleTreeNode::VNode(_) = &self.node {
            let target_name = path_components[0]
                .file_name()
                .and_then(|s| s.to_str())
                .unwrap_or("")
                .to_string();
            if let Ok(index) = self.children.binary_search_by(|child| {
                let child_name = match &child.node {
                    EMerkleTreeNode::Directory(dir) => Some(dir.name().to_owned()),
                    EMerkleTreeNode::File(file) => Some(file.name().to_owned()),
                    _ => None,
                };
                child_name.unwrap_or("".to_string()).cmp(&target_name)
            }) {
                let child = &self.children[index];
                if let EMerkleTreeNode::Directory(dir_node) = &child.node {
                    // Check if the directory name matches the next component of the path
                    if dir_node.name() == target_name {
                        path_components.remove(0); // Remove the matched component
                        if let (Some(node), mut child_traversed_nodes) = child
                            .get_nodes_along_paths_helper(
                                &traversed_path.join(dir_node.name()),
                                path_components.clone(),
                            )?
                        {
                            traversed_nodes.push(self.clone());
                            traversed_nodes.append(&mut child_traversed_nodes);
                            return Ok((Some(node), traversed_nodes));
                        }
                    }
                } else if let (Some(node), mut child_traversed_nodes) = child
                    .get_nodes_along_paths_helper(traversed_path, path_components.clone())
                    .unwrap_or((None, Vec::new()))
                {
                    traversed_nodes.push(self.clone());
                    traversed_nodes.append(&mut child_traversed_nodes);
                    return Ok((Some(node), traversed_nodes));
                }
            }
        }

        for child in &self.children {
            if let EMerkleTreeNode::Directory(dir_node) = &child.node {
                if let (Some(node), mut child_traversed_nodes) = child
                    .get_nodes_along_paths_helper(
                        &traversed_path.join(dir_node.name()),
                        path_components.clone(),
                    )?
                {
                    traversed_nodes.push(self.clone());
                    traversed_nodes.append(&mut child_traversed_nodes);
                    return Ok((Some(node), traversed_nodes));
                }
            } else if let (Some(node), mut child_traversed_nodes) = child
                .get_nodes_along_paths_helper(traversed_path, path_components.clone())
                .unwrap_or((None, Vec::new()))
            {
                traversed_nodes.push(self.clone());
                traversed_nodes.append(&mut child_traversed_nodes);
                return Ok((Some(node), traversed_nodes));
            }
        }

        Ok((None, traversed_nodes))
    }
}

/// Debug is used for verbose multi-line output with println!("{:?}", node)
impl fmt::Debug for MerkleTreeNode {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        writeln!(f, "{self}\n=============")?;
        writeln!(f, "hash: {}", self.hash)?;
        writeln!(f, "node: {:?}", self.node)?;
        writeln!(
            f,
            "parent_id: {}",
            self.parent_id
                .map_or("None".to_string(), |id| id.to_string())
        )?;
        writeln!(f, "children.len(): {}", self.children.len())?;
        writeln!(f, "=============")?;

        for child in &self.children {
            writeln!(f, "{child}")?;
        }
        Ok(())
    }
}

impl Default for MerkleTreeNode {
    fn default() -> Self {
        Self::default_dir()
    }
}

/// Display is used for single line output with println!("{}", node)
impl fmt::Display for MerkleTreeNode {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match &self.node {
            EMerkleTreeNode::Commit(commit) => {
                write!(f, "[{:?}] {} {}", self.node.node_type(), self.hash, commit)
            }
            EMerkleTreeNode::VNode(vnode) => {
                write!(
                    f,
                    "[{:?}] {} {} ({} entries)",
                    self.node.node_type(),
                    self.hash.to_short_str(),
                    vnode,
                    vnode.num_entries()
                )
            }
            EMerkleTreeNode::Directory(dir) => {
                write!(
                    f,
                    "[{:?}] {} {} ({} entries)",
                    self.node.node_type(),
                    self.hash.to_short_str(),
                    dir,
                    dir.num_entries()
                )
            }
            EMerkleTreeNode::File(file) => {
                write!(
                    f,
                    "[{:?}] {} {}",
                    self.node.node_type(),
                    self.hash.to_short_str(),
                    file
                )
            }
            EMerkleTreeNode::FileChunk(file_chunk) => {
                write!(
                    f,
                    "[{:?}] {} {}",
                    self.node.node_type(),
                    self.hash.to_short_str(),
                    file_chunk
                )
            }
        }
    }
}

impl PartialEq for MerkleTreeNode {
    fn eq(&self, other: &Self) -> bool {
        self.hash == other.hash
    }
}

impl Hash for MerkleTreeNode {
    fn hash<H: Hasher>(&self, state: &mut H) {
        self.hash.to_u128().hash(state);
        if let Ok(path) = self.maybe_path() {
            path.hash(state);
        }
    }
}