@anysphere/file-service 0.0.0-b4c871b2 → 0.0.0-bf8eca2b
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/Cargo.toml +3 -0
- package/index.d.ts +6 -1
- package/package.json +7 -7
- package/src/file_utils.rs +4 -5
- package/src/git_utils.rs +158 -19
- package/src/lib.rs +194 -56
- package/src/merkle_tree/local_construction.rs +44 -21
- package/src/merkle_tree/mod.rs +445 -148
- package/src/test.rs +5 -0
- package/src/proto/aiserver.v1.rs +0 -12041
|
@@ -1,15 +1,18 @@
|
|
|
1
|
+
use crate::git_utils;
|
|
1
2
|
use crate::merkle_tree::{
|
|
2
3
|
File, MerkleNode, MerkleNodePtr, NodeType, PinnedFuture,
|
|
3
4
|
};
|
|
4
5
|
|
|
5
6
|
use super::{LocalConstruction, MerkleTree};
|
|
6
|
-
use std::
|
|
7
|
-
use std::
|
|
7
|
+
use std::collections::{BTreeMap, HashSet};
|
|
8
|
+
use std::path::{Path, PathBuf};
|
|
8
9
|
use tonic::async_trait;
|
|
9
10
|
|
|
10
11
|
#[async_trait]
|
|
11
12
|
impl LocalConstruction for MerkleTree {
|
|
12
|
-
async fn new(
|
|
13
|
+
async fn new(
|
|
14
|
+
root_directory: Option<String>,
|
|
15
|
+
) -> Result<MerkleTree, anyhow::Error> {
|
|
13
16
|
if let Some(root_directory) = root_directory {
|
|
14
17
|
let n = MerkleTree::construct_merkle_tree(root_directory).await;
|
|
15
18
|
return n;
|
|
@@ -25,32 +28,53 @@ impl LocalConstruction for MerkleTree {
|
|
|
25
28
|
/// 2. compute hash for each file
|
|
26
29
|
/// 3. construct merkle tree
|
|
27
30
|
/// 4. return merkle tree
|
|
28
|
-
async fn construct_merkle_tree(
|
|
29
|
-
|
|
31
|
+
async fn construct_merkle_tree(
|
|
32
|
+
absolute_path_to_root_directory: String,
|
|
33
|
+
) -> Result<MerkleTree, anyhow::Error> {
|
|
34
|
+
let path = PathBuf::from(absolute_path_to_root_directory.clone());
|
|
30
35
|
if !path.exists() {
|
|
31
36
|
// FIXME: we should report this via a good logger.
|
|
32
37
|
panic!("Root directory does not exist!");
|
|
33
38
|
}
|
|
34
39
|
|
|
35
|
-
|
|
40
|
+
// 1. get all the gitignored files
|
|
41
|
+
let git_ignored_files = match git_utils::list_ignored_files(
|
|
42
|
+
absolute_path_to_root_directory.as_str(),
|
|
43
|
+
true,
|
|
44
|
+
) {
|
|
45
|
+
Ok(git_ignored) => git_ignored,
|
|
46
|
+
Err(_e) => HashSet::new(),
|
|
47
|
+
};
|
|
48
|
+
|
|
49
|
+
tracing::info!("git_ignored_files: {:?}", git_ignored_files);
|
|
50
|
+
|
|
51
|
+
let root_node = MerkleNode::new(
|
|
52
|
+
path,
|
|
53
|
+
None,
|
|
54
|
+
&git_ignored_files,
|
|
55
|
+
absolute_path_to_root_directory.as_str(),
|
|
56
|
+
)
|
|
57
|
+
.await;
|
|
36
58
|
let mut mt = MerkleTree {
|
|
37
59
|
root: root_node,
|
|
38
|
-
files:
|
|
39
|
-
root_path:
|
|
60
|
+
files: BTreeMap::new(),
|
|
61
|
+
root_path: absolute_path_to_root_directory,
|
|
62
|
+
cursor: None,
|
|
63
|
+
git_ignored_files,
|
|
40
64
|
};
|
|
41
65
|
|
|
42
66
|
// we now iterate over all the nodes and add them to the hashmap
|
|
43
67
|
// TODO(later): i can make this parallel.
|
|
44
68
|
fn add_nodes_to_hashmap<'a>(
|
|
45
69
|
node: &'a MerkleNodePtr,
|
|
46
|
-
files: &'a mut
|
|
70
|
+
files: &'a mut BTreeMap<String, File>,
|
|
47
71
|
) -> PinnedFuture<'a, ()> {
|
|
48
72
|
Box::pin(async move {
|
|
49
73
|
let node_reader = node.read().await;
|
|
50
74
|
match &node_reader.node_type {
|
|
51
75
|
NodeType::Branch(n) => {
|
|
52
|
-
|
|
53
|
-
|
|
76
|
+
let children = &n.1;
|
|
77
|
+
files.insert(n.0.clone(), File { node: node.clone() });
|
|
54
78
|
for child in children {
|
|
55
79
|
add_nodes_to_hashmap(child, files).await;
|
|
56
80
|
}
|
|
@@ -102,9 +126,9 @@ impl LocalConstruction for MerkleTree {
|
|
|
102
126
|
// File does not exist in the tree, let's add it.
|
|
103
127
|
let e = self.attach_new_node_to_tree(file_path.clone()).await;
|
|
104
128
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
129
|
+
if e.is_err() {
|
|
130
|
+
return Err(anyhow::anyhow!("Could not attach new node to tree!"));
|
|
131
|
+
}
|
|
108
132
|
}
|
|
109
133
|
|
|
110
134
|
Ok(())
|
|
@@ -139,18 +163,17 @@ impl LocalConstruction for MerkleTree {
|
|
|
139
163
|
let parent_node = parent_node.unwrap();
|
|
140
164
|
let mut mut_parent = parent_node.write().await;
|
|
141
165
|
|
|
166
|
+
// BUG(sualeh): need to actually drop everything that is a child here.
|
|
167
|
+
// idea: enumerate all nodes that are children of this through a starts with query on the hashtable.
|
|
168
|
+
// then drop all of them.
|
|
169
|
+
// in opposite order of length.
|
|
142
170
|
|
|
143
|
-
|
|
144
|
-
// idea: enumerate all nodes that are children of this through a starts with query on the hashtable.
|
|
145
|
-
// then drop all of them.
|
|
146
|
-
// in opposite order of length.
|
|
147
|
-
|
|
148
|
-
// then remove the node from the parent and you are done
|
|
171
|
+
// then remove the node from the parent and you are done
|
|
149
172
|
|
|
150
173
|
// Remove the child from the parent node
|
|
151
174
|
match mut_parent.node_type {
|
|
152
175
|
NodeType::Branch(ref mut node) => {
|
|
153
|
-
|
|
176
|
+
let children = &mut node.1;
|
|
154
177
|
let mut found = false;
|
|
155
178
|
let mut index = 0;
|
|
156
179
|
|