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
use std::path::Path;
use super::Migrate;
use crate::command::migrate::Direction;
use crate::config::RepositoryConfig;
use crate::core::db::merkle_node::MerkleNodeDB;
use crate::core::versions::MinOxenVersion;
use crate::error::OxenError;
use crate::model::merkle_tree::merkle_tree_node_cache;
use crate::model::merkle_tree::node::vnode::VNodeOpts;
use crate::model::merkle_tree::node::{
CommitNode, DirNode, EMerkleTreeNode, MerkleTreeNode, VNode,
};
use crate::model::{Commit, LocalRepository, MerkleHash};
use crate::util::hasher;
use crate::{repositories, util};
pub struct AddChildCountsToNodesMigration;
impl Migrate for AddChildCountsToNodesMigration {
fn name(&self) -> &'static str {
"add_child_counts_to_nodes"
}
fn description(&self) -> &'static str {
"Re-writes merkle tree with child counts for all directories and vnode nodes"
}
fn up(&self, repo: LocalRepository) -> Result<(), OxenError> {
let commits = repositories::commits::list_all(&repo)?;
merkle_tree_node_cache::with_cache_disabled(|| -> Result<(), OxenError> {
for commit in commits {
run_on_commit(&repo, &commit)?;
}
Ok(())
})
}
fn down(&self, _: LocalRepository) -> Result<(), OxenError> {
Err(OxenError::MigrationUnimplemented(Direction::Down))
}
fn is_needed(&self, repo: &LocalRepository) -> Result<bool, OxenError> {
let min_version = repo.min_version();
log::debug!(
"checking if migration is needed for repo: {:?}, min_version: {}",
repo.path,
min_version
);
Ok(min_version == MinOxenVersion::V0_19_0)
}
}
fn run_on_commit(repository: &LocalRepository, commit: &Commit) -> Result<(), OxenError> {
log::info!(
"Running add_child_counts_to_nodes on commit: {} for repo: {:?}",
commit,
repository.path
);
let old_repo = repository.clone();
// println!("old tree for commit {}", commit);
// repositories::tree::print_tree(&old_repo, commit)?;
// Iterate over the nodes, find the VNode and DirNode, and add the child counts
let mut new_repo = repository.clone();
new_repo.set_min_version(MinOxenVersion::LATEST);
// *******************************************************************
// We need to load all the children of all the VNodes for each DirNode
// Then re-split the children to new VNodes based on their path name
// this way we can look up files by path faster, which is what this
// migration is all about.
// *******************************************************************
let Some(root_node) = repositories::tree::get_root_with_children(&old_repo, commit)? else {
return Err(OxenError::basic_str("Root node not found"));
};
let root_dir_node = repositories::tree::get_root_dir(&root_node)?;
let EMerkleTreeNode::Directory(dir_node) = root_dir_node.node.clone() else {
return Err(OxenError::basic_str("Root node must be CommitNode"));
};
// ✍️ Do all the rewriting
let num_children = root_dir_node.children.len();
log::debug!("setting num children {num_children} for root dir on commit {commit}");
let mut dir_node_opts = dir_node.get_opts();
dir_node_opts.num_entries = num_children as u64;
let dir_node = DirNode::new(&new_repo, dir_node_opts)?;
// Write a new commit db
let commit_node = CommitNode::from_commit(commit.clone());
let mut root_commit_db =
MerkleNodeDB::open_read_write(&old_repo.path, &commit_node, root_node.parent_id)?;
root_commit_db.add_child(&dir_node)?;
let current_path = Path::new("");
rewrite_nodes(&old_repo, &new_repo, &root_node, current_path)?;
// println!("new tree for commit {}", commit);
// repositories::tree::print_tree(&new_repo, commit)?;
// Stamp the migrated repo with the current LATEST version string.
let mut config = RepositoryConfig::from_repo(&new_repo)?;
config.min_version = Some(MinOxenVersion::LATEST.as_str().to_string());
let path = util::fs::config_filepath(&new_repo.path);
config.save(&path)?;
Ok(())
}
// Forgive me if you are reading this for reference, we don't have great writers for the
// merkle tree yet - so there is a lot of duplicate logic with `commit_writer.rs`
fn rewrite_nodes(
old_repo: &LocalRepository,
new_repo: &LocalRepository,
node: &MerkleTreeNode,
current_dir: &Path,
) -> Result<(), OxenError> {
for child in node.children.iter() {
match &child.node {
EMerkleTreeNode::Directory(dir) => {
// Load all the children of children (files and folders)
// Then redistribute into buckets...
// and then just use the MerkleNodeDB to write the nodes
// to the new tree
let dir_children = repositories::tree::list_files_and_folders(child)?;
let current_dir = current_dir.join(dir.name());
// log::debug!(
// "rewrite_nodes {} children on current_dir {:?} DIRECTORY {} {}",
// dir_children.len(),
// current_dir,
// dir.hash(),
// dir
// );
let total_children = dir_children.len();
let vnode_size = old_repo.vnode_size();
let num_vnodes = (total_children as f32 / vnode_size as f32).ceil() as u128;
// Create our new DirNode
let mut dir_node_opts = dir.get_opts();
dir_node_opts.num_entries = total_children as u64;
let dir = DirNode::new(new_repo, dir_node_opts)?;
let mut dir_db =
MerkleNodeDB::open_read_write(&old_repo.path, &dir, node.parent_id)?;
// log::debug!(
// "rewrite_nodes {} VNodes for {} children in {} with vnode size {}",
// num_vnodes,
// total_children,
// dir,
// vnode_size
// );
// Compute buckets
let mut buckets: Vec<Vec<MerkleTreeNode>> = vec![vec![]; num_vnodes as usize];
for dir_child in dir_children {
let path = current_dir.join(dir_child.maybe_path().unwrap());
let hash = hasher::hash_buffer_128bit(path.to_str().unwrap().as_bytes());
let bucket_idx = hash % num_vnodes;
// log::debug!(
// "\trewrite_nodes dir_child {:?} bucket {} num_vnodes {} hash {} {}",
// path,
// bucket_idx,
// num_vnodes,
// hash,
// dir_child
// );
buckets[bucket_idx as usize].push(dir_child);
}
// Compute hashes and sort entries to get vnode buckets
let mut vnodes: Vec<(MerkleHash, Vec<MerkleTreeNode>)> = vec![];
for bucket in buckets.iter_mut() {
// Sort the entries in the vnode by path
// to make searching for entries faster
bucket.sort_by_key(|a| a.maybe_path().unwrap());
// Compute hash for the vnode
let mut vnode_hasher = xxhash_rust::xxh3::Xxh3::new();
vnode_hasher.update(b"vnode");
// add the dir name to the vnode hash
vnode_hasher.update(dir.name().as_bytes());
for entry in bucket.iter() {
if let EMerkleTreeNode::File(file_node) = &entry.node {
vnode_hasher.update(&file_node.combined_hash().to_le_bytes());
} else if let EMerkleTreeNode::Directory(dir_node) = &entry.node {
vnode_hasher.update(&dir_node.hash().to_le_bytes());
}
}
let vnode_id = MerkleHash::new(vnode_hasher.digest128());
vnodes.push((vnode_id, bucket.clone()));
}
// log::debug!("rewrite_nodes count vnodes: {}", vnodes.len());
for (hash, entries) in vnodes.iter() {
// create a new vnode obj and add the the db
let opts = VNodeOpts {
hash: *hash,
num_entries: entries.len() as u64,
};
let vnode_obj = VNode::new(new_repo, opts)?;
// log::debug!("rewrite_nodes adding VNode to DirNode! {:?}", vnode_obj);
dir_db.add_child(&vnode_obj)?;
let mut vnode_db = MerkleNodeDB::open_read_write(
&new_repo.path,
&vnode_obj,
Some(dir_db.node_id),
)?;
// log::debug!("rewrite_nodes count entries {}", entries.len());
for entry in entries {
match &entry.node {
EMerkleTreeNode::File(f_node) => {
// log::debug!("rewrite_nodes adding FileNode to VNode! {}", f_node);
vnode_db.add_child(f_node)?;
}
EMerkleTreeNode::Directory(d_node) => {
let mut d_node_opts = d_node.get_opts();
let d_children = repositories::tree::list_files_and_folders(entry)?;
d_node_opts.num_entries = d_children.len() as u64;
// log::debug!(
// "rewrite_nodes adding DirNode to VNode with {} num_entries {}",
// d_node_opts.num_entries,
// d_node
// );
let d_node = DirNode::new(new_repo, d_node_opts)?;
vnode_db.add_child(&d_node)?;
}
_ => {
panic!("Shouldn't reach here.")
}
}
}
}
rewrite_nodes(old_repo, new_repo, child, ¤t_dir)?;
}
EMerkleTreeNode::VNode(_) => {
// VNode just needs to traverse to the next dirnode
rewrite_nodes(old_repo, new_repo, child, current_dir)?;
}
_ => {
// pass, FileNode was not changed, so it is on the latest version
}
}
}
Ok(())
}
// Add tests
#[cfg(test)]
mod tests {
use super::*;
use crate::{model::merkle_tree::node::EMerkleTreeNode, test};
use std::path::PathBuf;
#[tokio::test]
async fn test_add_child_counts_to_nodes_migration() -> Result<(), OxenError> {
test::run_empty_dir_test_async(|dir| async move {
// Instantiate an older repository
let repo = repositories::init::init_with_version(dir, MinOxenVersion::V0_19_0)?;
// Populate the repo with some files
test::populate_dir_with_training_data(&repo.path)?;
// Make a variety of commits
test::make_many_commits(&repo).await?;
// Test that the root commit
let latest_commit = repositories::commits::latest_commit(&repo)?;
let commit_hash = latest_commit.id.parse()?;
let Some(old_root_node) =
repositories::tree::get_node_by_id_with_children(&repo, &commit_hash)?
else {
return Err(OxenError::basic_str("Root node not found"));
};
repositories::tree::print_tree(&repo, &latest_commit)?;
old_root_node.walk_tree(|node| {
println!("test_add_child_counts_to_nodes node: {node}");
match &node.node {
EMerkleTreeNode::Commit(commit) => {
assert_eq!(commit.version(), MinOxenVersion::V0_19_0);
}
EMerkleTreeNode::Directory(dir) => {
assert_eq!(dir.version(), MinOxenVersion::V0_19_0);
}
EMerkleTreeNode::VNode(vnode) => {
assert_eq!(vnode.version(), MinOxenVersion::V0_19_0);
}
_ => {
// pass, FileNode was not changed, so it is on the latest version
}
}
});
// Run the migration
let repo_path = repo.path.clone();
AddChildCountsToNodesMigration.up(repo)?;
// Clear the cache after the migration
merkle_tree_node_cache::remove_from_cache(&repo_path)?;
let repo = LocalRepository::from_dir(&repo_path)?;
let latest_commit = repositories::commits::latest_commit(&repo)?;
let commit_node_version =
repositories::tree::get_commit_node_version(&repo, &latest_commit)?;
let node_version_str = commit_node_version.to_string();
assert_eq!(node_version_str, "0.36.0");
let commit_hash = latest_commit.id.parse()?;
let Some(new_root_node) =
repositories::tree::get_node_by_id_with_children(&repo, &commit_hash)?
else {
return Err(OxenError::basic_str("Root node not found"));
};
new_root_node.walk_tree(|node| {
println!("test_add_child_counts_to_nodes node: {node}");
match &node.node {
EMerkleTreeNode::Commit(commit) => {
assert_eq!(
commit.version(),
MinOxenVersion::from_string("0.36.0").unwrap()
);
}
EMerkleTreeNode::Directory(dir) => {
assert_eq!(
dir.version(),
MinOxenVersion::from_string("0.36.0").unwrap()
);
}
EMerkleTreeNode::VNode(vnode) => {
assert_eq!(
vnode.version(),
MinOxenVersion::from_string("0.36.0").unwrap()
);
}
_ => {
// pass, FileNode was not changed, so it is on the latest version
}
}
});
// Make sure that the repo version is updated
let repo = LocalRepository::from_dir(&repo.path)?;
let version_str = repo.min_version().to_string();
assert_eq!(version_str, "0.36.0");
Ok(())
})
.await
}
#[tokio::test]
async fn test_add_child_counts_migration_with_many_vnodes() -> Result<(), OxenError> {
test::run_empty_dir_test_async(|dir| async move {
// Instantiate an older repository
let mut repo = repositories::init::init_with_version(dir, MinOxenVersion::V0_19_0)?;
// Set the vnode size to 3
repo.set_vnode_size(3);
// Populate the repo with some files
test::populate_dir_with_training_data(&repo.path)?;
// Make a variety of commits
test::make_many_commits(&repo).await?;
// Test that the root commit
let latest_commit = repositories::commits::latest_commit(&repo)?;
let commit_hash = latest_commit.id.parse()?;
let Some(old_root_node) =
repositories::tree::get_node_by_id_with_children(&repo, &commit_hash)?
else {
return Err(OxenError::basic_str("Root node not found"));
};
repositories::tree::print_tree(&repo, &latest_commit)?;
old_root_node.walk_tree(|node| {
println!("test_add_child_counts_to_nodes node: {node}");
match &node.node {
EMerkleTreeNode::Commit(commit) => {
assert_eq!(commit.version(), MinOxenVersion::V0_19_0);
}
EMerkleTreeNode::Directory(dir) => {
assert_eq!(dir.version(), MinOxenVersion::V0_19_0);
}
EMerkleTreeNode::VNode(vnode) => {
assert_eq!(vnode.version(), MinOxenVersion::V0_19_0);
}
_ => {
// pass, FileNode was not changed, so it is on the latest version
}
}
});
// Run the migration
let repo_path = repo.path.clone();
AddChildCountsToNodesMigration.up(repo)?;
// Clear the cache after the migration
merkle_tree_node_cache::remove_from_cache(&repo_path)?;
let mut repo = LocalRepository::from_dir(&repo_path)?;
repo.set_vnode_size(3);
let latest_commit = repositories::commits::latest_commit(&repo)?;
let commit_node_version =
repositories::tree::get_commit_node_version(&repo, &latest_commit)?;
let node_version_str = commit_node_version.to_string();
assert_eq!(node_version_str, "0.36.0");
let commit_hash = latest_commit.id.parse()?;
let Some(new_root_node) =
repositories::tree::get_node_by_id_with_children(&repo, &commit_hash)?
else {
return Err(OxenError::basic_str("Root node not found"));
};
new_root_node.walk_tree(|node| {
println!("test_add_child_counts_to_nodes node: {node}");
match &node.node {
EMerkleTreeNode::Commit(commit) => {
assert_eq!(
commit.version(),
MinOxenVersion::from_string("0.36.0").unwrap()
);
}
EMerkleTreeNode::Directory(dir) => {
assert_eq!(
dir.version(),
MinOxenVersion::from_string("0.36.0").unwrap()
);
}
EMerkleTreeNode::VNode(vnode) => {
assert_eq!(
vnode.version(),
MinOxenVersion::from_string("0.36.0").unwrap()
);
}
_ => {
// pass, FileNode was not changed, so it is on the latest version
}
}
});
println!("Checking files on latest_commit: {latest_commit}");
// Make sure we can get an individual file
let file_node = repositories::tree::get_node_by_path(
&repo,
&latest_commit,
PathBuf::from("README.md"),
)?;
assert!(file_node.is_some());
for i in 1..3 {
let path = PathBuf::from("train").join(format!("cat_{i}.jpg"));
log::debug!("LOOKING UP CAT: {path:?}");
let file_node = repositories::tree::get_node_by_path(&repo, &latest_commit, &path)?;
assert!(file_node.is_some());
}
for i in 1..4 {
let path = PathBuf::from("train").join(format!("dog_{i}.jpg"));
log::debug!("LOOKING UP DOG: {path:?}");
let file_node = repositories::tree::get_node_by_path(&repo, &latest_commit, &path)?;
assert!(file_node.is_some());
}
// Make sure that the repo version is updated
let repo = LocalRepository::from_dir(&repo.path)?;
let version_str = repo.min_version().to_string();
assert_eq!(version_str, "0.36.0");
Ok(())
})
.await
}
}