@anysphere/file-service 0.0.0-930d43f → 0.0.0-a0aa43e6
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/index.d.ts +6 -1
- package/package.json +7 -7
- package/src/lib.rs +148 -55
- package/src/merkle_tree/local_construction.rs +21 -16
- package/src/merkle_tree/mod.rs +299 -123
- package/src/proto/aiserver.v1.rs +0 -12041
package/index.d.ts
CHANGED
|
@@ -6,10 +6,15 @@
|
|
|
6
6
|
export class MerkleClient {
|
|
7
7
|
constructor(rootDirectory: string)
|
|
8
8
|
init(): Promise<void>
|
|
9
|
+
computeMerkleTree(): Promise<void>
|
|
9
10
|
updateFile(filePath: string): Promise<void>
|
|
10
11
|
deleteFile(filePath: string): Promise<void>
|
|
11
|
-
getSubtreeHash(
|
|
12
|
+
getSubtreeHash(relativePath: string): Promise<string>
|
|
12
13
|
getNumEmbeddableFiles(): Promise<number>
|
|
13
14
|
getAllFiles(): Promise<Array<string>>
|
|
15
|
+
getAllDirFilesToEmbed(absoluteFilePath: string): Promise<Array<string>>
|
|
16
|
+
getNextFileToEmbed(): Promise<Array<string>>
|
|
17
|
+
getSpline(absoluteFilePath: string): Promise<Array<string>>
|
|
14
18
|
getHashesForFiles(files: Array<string>): Promise<Array<string>>
|
|
19
|
+
updateRootDirectory(rootDirectory: string): void
|
|
15
20
|
}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@anysphere/file-service",
|
|
3
|
-
"version": "0.0.0-
|
|
3
|
+
"version": "0.0.0-a0aa43e6",
|
|
4
4
|
"main": "index.js",
|
|
5
5
|
"types": "index.d.ts",
|
|
6
6
|
"napi": {
|
|
@@ -35,11 +35,11 @@
|
|
|
35
35
|
"version": "napi version"
|
|
36
36
|
},
|
|
37
37
|
"optionalDependencies": {
|
|
38
|
-
"@anysphere/file-service-win32-x64-msvc": "0.0.0-
|
|
39
|
-
"@anysphere/file-service-darwin-x64": "0.0.0-
|
|
40
|
-
"@anysphere/file-service-linux-x64-gnu": "0.0.0-
|
|
41
|
-
"@anysphere/file-service-darwin-arm64": "0.0.0-
|
|
42
|
-
"@anysphere/file-service-win32-arm64-msvc": "0.0.0-
|
|
43
|
-
"@anysphere/file-service-darwin-universal": "0.0.0-
|
|
38
|
+
"@anysphere/file-service-win32-x64-msvc": "0.0.0-a0aa43e6",
|
|
39
|
+
"@anysphere/file-service-darwin-x64": "0.0.0-a0aa43e6",
|
|
40
|
+
"@anysphere/file-service-linux-x64-gnu": "0.0.0-a0aa43e6",
|
|
41
|
+
"@anysphere/file-service-darwin-arm64": "0.0.0-a0aa43e6",
|
|
42
|
+
"@anysphere/file-service-win32-arm64-msvc": "0.0.0-a0aa43e6",
|
|
43
|
+
"@anysphere/file-service-darwin-universal": "0.0.0-a0aa43e6"
|
|
44
44
|
}
|
|
45
45
|
}
|
package/src/lib.rs
CHANGED
|
@@ -3,6 +3,8 @@ pub mod file_utils;
|
|
|
3
3
|
pub mod git_utils;
|
|
4
4
|
pub mod merkle_tree;
|
|
5
5
|
|
|
6
|
+
use std::vec;
|
|
7
|
+
|
|
6
8
|
use merkle_tree::{LocalConstruction, MerkleTree};
|
|
7
9
|
|
|
8
10
|
#[macro_use]
|
|
@@ -31,14 +33,14 @@ impl MerkleClient {
|
|
|
31
33
|
// 3. sync with the remote
|
|
32
34
|
self.compute_merkle_tree().await?;
|
|
33
35
|
|
|
34
|
-
|
|
36
|
+
Ok(())
|
|
35
37
|
}
|
|
36
38
|
|
|
37
39
|
pub async unsafe fn interrupt(&mut self) -> Result<(), napi::Error> {
|
|
38
40
|
unimplemented!("Interrupt is not implemented yet");
|
|
39
41
|
}
|
|
40
42
|
|
|
41
|
-
|
|
43
|
+
#[napi]
|
|
42
44
|
pub async unsafe fn compute_merkle_tree(
|
|
43
45
|
&mut self,
|
|
44
46
|
) -> Result<(), napi::Error> {
|
|
@@ -67,59 +69,150 @@ impl MerkleClient {
|
|
|
67
69
|
let _r = self.tree.delete_file(file_path);
|
|
68
70
|
}
|
|
69
71
|
|
|
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
|
-
|
|
72
|
+
#[napi]
|
|
73
|
+
pub async fn get_subtree_hash(
|
|
74
|
+
&self,
|
|
75
|
+
relative_path: String,
|
|
76
|
+
) -> Result<String, napi::Error> {
|
|
77
|
+
let absolute_path =
|
|
78
|
+
std::path::Path::new(&self.root_directory).join(relative_path);
|
|
79
|
+
let hash = self.tree.get_subtree_hash(absolute_path).await;
|
|
80
|
+
|
|
81
|
+
match hash {
|
|
82
|
+
Ok(hash) => Ok(hash),
|
|
83
|
+
Err(e) => Err(napi::Error::new(
|
|
84
|
+
napi::Status::Unknown,
|
|
85
|
+
format!("Error in get_subtree_hash: {:?}", e),
|
|
86
|
+
)),
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
|
|
90
|
+
#[napi]
|
|
91
|
+
pub async fn get_num_embeddable_files(&self) -> Result<i32, napi::Error> {
|
|
92
|
+
let num = self.tree.get_num_embeddable_files().await;
|
|
93
|
+
|
|
94
|
+
match num {
|
|
95
|
+
Ok(num) => Ok(num),
|
|
96
|
+
Err(e) => Err(napi::Error::new(
|
|
97
|
+
napi::Status::Unknown,
|
|
98
|
+
format!("Error in get_num_embeddable_files: {:?}", e),
|
|
99
|
+
)),
|
|
100
|
+
}
|
|
101
|
+
}
|
|
102
|
+
|
|
103
|
+
pub async fn get_num_embeddable_files_in_subtree(
|
|
104
|
+
&self,
|
|
105
|
+
relative_path: String,
|
|
106
|
+
) -> Result<i32, napi::Error> {
|
|
107
|
+
let absolute_path =
|
|
108
|
+
std::path::Path::new(&self.root_directory).join(relative_path);
|
|
109
|
+
let num = self
|
|
110
|
+
.tree
|
|
111
|
+
.get_num_embeddable_files_in_subtree(absolute_path)
|
|
112
|
+
.await;
|
|
113
|
+
|
|
114
|
+
match num {
|
|
115
|
+
Ok(num) => Ok(num),
|
|
116
|
+
Err(e) => Err(napi::Error::new(
|
|
117
|
+
napi::Status::Unknown,
|
|
118
|
+
format!("Error in get_num_embeddable_files_in_subtree: {:?}", e),
|
|
119
|
+
)),
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
|
|
123
|
+
#[napi]
|
|
124
|
+
pub async fn get_all_files(&self) -> Result<Vec<String>, napi::Error> {
|
|
125
|
+
let files = self.tree.get_all_files().await;
|
|
126
|
+
|
|
127
|
+
match files {
|
|
128
|
+
Ok(files) => Ok(files),
|
|
129
|
+
Err(e) => Err(napi::Error::new(
|
|
130
|
+
napi::Status::Unknown,
|
|
131
|
+
format!("Error in get_all_files: {:?}", e),
|
|
132
|
+
)),
|
|
133
|
+
}
|
|
134
|
+
}
|
|
135
|
+
|
|
136
|
+
#[napi]
|
|
137
|
+
pub async fn get_all_dir_files_to_embed(
|
|
138
|
+
&self,
|
|
139
|
+
absolute_file_path: String,
|
|
140
|
+
) -> Result<Vec<String>, napi::Error> {
|
|
141
|
+
let absolute_path_str = absolute_file_path.as_str();
|
|
142
|
+
let files = self
|
|
143
|
+
.tree
|
|
144
|
+
.get_all_dir_files_to_embed(absolute_path_str)
|
|
145
|
+
.await;
|
|
146
|
+
|
|
147
|
+
match files {
|
|
148
|
+
Ok(files) => Ok(files),
|
|
149
|
+
Err(e) => Err(napi::Error::new(
|
|
150
|
+
napi::Status::Unknown,
|
|
151
|
+
format!("Error in get_all_dir_files_to_embed: {:?}", e),
|
|
152
|
+
)),
|
|
153
|
+
}
|
|
154
|
+
}
|
|
155
|
+
|
|
156
|
+
#[napi]
|
|
157
|
+
pub async unsafe fn get_next_file_to_embed(
|
|
158
|
+
&mut self,
|
|
159
|
+
) -> Result<Vec<String>, napi::Error> {
|
|
160
|
+
let n = self.tree.get_next_file_to_embed().await;
|
|
161
|
+
|
|
162
|
+
match n {
|
|
163
|
+
Ok((file, path)) => {
|
|
164
|
+
// now our job is to put the filename as the first element of the path.
|
|
165
|
+
|
|
166
|
+
// TODO(sualeh): we should assert that the path is ascending up to the path.
|
|
167
|
+
|
|
168
|
+
let ret = vec![file];
|
|
169
|
+
let ret = ret.into_iter().chain(path.into_iter()).collect::<Vec<_>>();
|
|
170
|
+
|
|
171
|
+
Ok(ret)
|
|
172
|
+
}
|
|
173
|
+
Err(e) => Err(napi::Error::new(
|
|
174
|
+
napi::Status::Unknown,
|
|
175
|
+
format!("Error in get_next_file_to_embed: {:?}", e),
|
|
176
|
+
)),
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
|
|
180
|
+
// FIXME(sualeh): get_spline
|
|
181
|
+
#[napi]
|
|
182
|
+
pub async fn get_spline(
|
|
183
|
+
&self,
|
|
184
|
+
absolute_file_path: String,
|
|
185
|
+
) -> Result<Vec<String>, napi::Error> {
|
|
186
|
+
// let spline = self.tree.get_spline(absolute_file_path).await;
|
|
187
|
+
|
|
188
|
+
return Ok(vec![]);
|
|
189
|
+
|
|
190
|
+
// match spline {
|
|
191
|
+
// Ok(spline) => Ok(spline),
|
|
192
|
+
// Err(e) => Err(napi::Error::new(
|
|
193
|
+
// napi::Status::Unknown,
|
|
194
|
+
// format!("Error in get_spline: {:?}", e),
|
|
195
|
+
// )),
|
|
196
|
+
// }
|
|
197
|
+
}
|
|
198
|
+
|
|
199
|
+
#[napi]
|
|
200
|
+
pub async fn get_hashes_for_files(
|
|
201
|
+
&self,
|
|
202
|
+
files: Vec<String>,
|
|
203
|
+
) -> Result<Vec<String>, napi::Error> {
|
|
204
|
+
let hashes = self.tree.get_hashes_for_files(files).await;
|
|
205
|
+
|
|
206
|
+
match hashes {
|
|
207
|
+
Ok(hashes) => Ok(hashes),
|
|
208
|
+
Err(e) => Err(napi::Error::new(
|
|
209
|
+
napi::Status::Unknown,
|
|
210
|
+
format!("Error in get_hashes_for_files: {:?}", e),
|
|
211
|
+
)),
|
|
212
|
+
}
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
#[napi]
|
|
123
216
|
pub fn update_root_directory(&mut self, root_directory: String) {
|
|
124
217
|
self.root_directory = root_directory;
|
|
125
218
|
}
|
|
@@ -3,13 +3,16 @@ use crate::merkle_tree::{
|
|
|
3
3
|
};
|
|
4
4
|
|
|
5
5
|
use super::{LocalConstruction, MerkleTree};
|
|
6
|
+
use std::collections::BTreeMap;
|
|
6
7
|
use std::path::PathBuf;
|
|
7
8
|
use std::{collections::HashMap, path::Path, sync::Arc};
|
|
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,7 +28,9 @@ 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(
|
|
31
|
+
async fn construct_merkle_tree(
|
|
32
|
+
root_directory: String,
|
|
33
|
+
) -> Result<MerkleTree, anyhow::Error> {
|
|
29
34
|
let path = PathBuf::from(root_directory.clone());
|
|
30
35
|
if !path.exists() {
|
|
31
36
|
// FIXME: we should report this via a good logger.
|
|
@@ -35,22 +40,23 @@ impl LocalConstruction for MerkleTree {
|
|
|
35
40
|
let root_node = MerkleNode::new(path, None).await;
|
|
36
41
|
let mut mt = MerkleTree {
|
|
37
42
|
root: root_node,
|
|
38
|
-
files:
|
|
43
|
+
files: BTreeMap::new(),
|
|
39
44
|
root_path: root_directory,
|
|
45
|
+
cursor: None,
|
|
40
46
|
};
|
|
41
47
|
|
|
42
48
|
// we now iterate over all the nodes and add them to the hashmap
|
|
43
49
|
// TODO(later): i can make this parallel.
|
|
44
50
|
fn add_nodes_to_hashmap<'a>(
|
|
45
51
|
node: &'a MerkleNodePtr,
|
|
46
|
-
files: &'a mut
|
|
52
|
+
files: &'a mut BTreeMap<String, File>,
|
|
47
53
|
) -> PinnedFuture<'a, ()> {
|
|
48
54
|
Box::pin(async move {
|
|
49
55
|
let node_reader = node.read().await;
|
|
50
56
|
match &node_reader.node_type {
|
|
51
57
|
NodeType::Branch(n) => {
|
|
52
|
-
|
|
53
|
-
|
|
58
|
+
let children = &n.1;
|
|
59
|
+
files.insert(n.0.clone(), File { node: node.clone() });
|
|
54
60
|
for child in children {
|
|
55
61
|
add_nodes_to_hashmap(child, files).await;
|
|
56
62
|
}
|
|
@@ -102,9 +108,9 @@ impl LocalConstruction for MerkleTree {
|
|
|
102
108
|
// File does not exist in the tree, let's add it.
|
|
103
109
|
let e = self.attach_new_node_to_tree(file_path.clone()).await;
|
|
104
110
|
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
111
|
+
if e.is_err() {
|
|
112
|
+
return Err(anyhow::anyhow!("Could not attach new node to tree!"));
|
|
113
|
+
}
|
|
108
114
|
}
|
|
109
115
|
|
|
110
116
|
Ok(())
|
|
@@ -139,18 +145,17 @@ impl LocalConstruction for MerkleTree {
|
|
|
139
145
|
let parent_node = parent_node.unwrap();
|
|
140
146
|
let mut mut_parent = parent_node.write().await;
|
|
141
147
|
|
|
148
|
+
// BUG(sualeh): need to actually drop everything that is a child here.
|
|
149
|
+
// idea: enumerate all nodes that are children of this through a starts with query on the hashtable.
|
|
150
|
+
// then drop all of them.
|
|
151
|
+
// in opposite order of length.
|
|
142
152
|
|
|
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
|
|
153
|
+
// then remove the node from the parent and you are done
|
|
149
154
|
|
|
150
155
|
// Remove the child from the parent node
|
|
151
156
|
match mut_parent.node_type {
|
|
152
157
|
NodeType::Branch(ref mut node) => {
|
|
153
|
-
|
|
158
|
+
let children = &mut node.1;
|
|
154
159
|
let mut found = false;
|
|
155
160
|
let mut index = 0;
|
|
156
161
|
|